mysql - SQL Match Against query for Multiple tables 的性能问题

标签 mysql sql full-text-search database-performance

我在运行使用 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/

相关文章:

php - 如何显示以下场景的分组结果?

php - 使用 PHP 为 SQL 数据库准备 HTML 表单输入

sql - SQL 中按列排序的自定义搜索

algorithm - Google 使用什么搜索算法/概念?

postgresql - 了解全文搜索查询中子句顺序的影响

Mysql优化避免表扫描

mysql - 从另一个表中选择一个表 + 我自己的数据插入到一个表中

mysql - 带有聚合函数的子查询 SQL

sql - Spark 计算分组依据中的单词数

sql - 这个SQL Server查询除法计算有什么问题?