我在运行使用 MyISAM 数据库的 SQL 查询时遇到性能问题。
为了简单起见,我有 3 个表:
表:A(引擎MyISAM。总记录:1847)
表:B(引擎MyISAM。总记录数:1110)
表:C(引擎MyISAM。总记录:57867)
现在我正在运行的查询需要 623 秒才能执行,有时会发生来自服务器的连接(与本地主机的情况相同)被中止。
下面是我正在执行的查询:
SELECT MATCH(A.title, A.description) AGAINST('Computer Graphic Artist') AS 'Score',
A.code AS 'Code',
B.cluster AS 'Cluster',
B.pathway AS 'Pathway',
A.title AS 'Role',
A.description AS 'Description'
FROM B
INNER JOIN A ON B.code = A.code
INNER JOIN C ON B.code = C.code
WHERE MATCH(A.title, A.description) AGAINST('Computer Graphic Artist')
OR MATCH(B.cluster, B.pathway, B.descripton) AGAINST('Computer Graphic Artist')
OR MATCH(C.title) AGAINST('Computer Graphic Artist')
ORDER BY Score DESC, B.cluster ASC
也可以引用Pastie (如果您觉得看到此 SQL 有困难)。我在适用的地方添加了 FULLTEXT 属性。
注意:表A、B、C也很少有重复记录。
请告诉我如何优化此 SQL 以实现快速输出。
最佳答案
首先要做的是确保在从每个表中查询的列的确切集合上有一个 FULLTEXT 索引:
alter table A add fulltext index a_fti (title,description);
alter table B add fulltext index b_fti (cluster, pathway, descripton);
alter table C add fulltext index c_fti (title);
然后我会建议重写您的查询以使用 UNION 而不是 OR。使用这种方法,我从 FULLTEXT 搜索中获得了更好的性能,尤其是在 MySQL 中。
这是一个使用您的查询的示例:
select Score, Code, Cluster, Pathway, Role, Description
from
(
SELECT MATCH(A.title, A.description) AGAINST('Computer Graphic Artist') AS 'Score',
A.code AS 'Code',
B.cluster AS 'Cluster',
B.pathway AS 'Pathway',
A.title AS 'Role',
A.description AS 'Description'
FROM B
INNER JOIN A ON B.code = A.code
INNER JOIN C ON B.code = C.code
WHERE MATCH(A.title, A.description) AGAINST('Computer Graphic Artist')
UNION
SELECT MATCH(A.title, A.description) AGAINST('Computer Graphic Artist') AS 'Score',
A.code AS 'Code',
B.cluster AS 'Cluster',
B.pathway AS 'Pathway',
A.title AS 'Role',
A.description AS 'Description'
FROM B
INNER JOIN A ON B.code = A.code
INNER JOIN C ON B.code = C.code
WHERE MATCH(B.cluster, B.pathway, B.descripton) AGAINST('Computer Graphic Artist')
UNION
SELECT MATCH(A.title, A.description) AGAINST('Computer Graphic Artist') AS 'Score',
A.code AS 'Code',
B.cluster AS 'Cluster',
B.pathway AS 'Pathway',
A.title AS 'Role',
A.description AS 'Description'
FROM B
INNER JOIN A ON B.code = A.code
INNER JOIN C ON B.code = C.code
WHERE MATCH(C.title) AGAINST('Computer Graphic Artist')
) as sub_query
ORDER BY Score DESC, Cluster ASC
关于mysql - SQL Match Against query for Multiple tables 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11579811/