使用OR与MATCH AGAINST的MySQL大大减慢了查询速度

时间:2015-02-12 21:29:19

标签: mysql performance full-text-search sql-like full-text-indexing

列是索引和全文。为什么" OR"加5-8秒?

SELECT * FROM exampleTable WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE) 
// returns in .05 seconds
SELECT * FROM exampleTable WHERE someColumn LIKE 'testing123%' 
// returns in .003 seconds
SELECT * FROM exampleTable WHERE (MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE) OR MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)) 
// returns in 5-8 seconds; yes, they are same MATCH statement...
SELECT * FROM exampleTable WHERE (MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)OR someOtherIndexedColumn LIKE 'testing123%' 
// returns in 5-8 seconds; ***** different columns this time*******

我觉得奇怪的是,如果MySQL的声明相同,那么它就太慢了。这就像使声明" SELECT * FROM tableName WHERE 1 OR 1" 为5秒。无论列是相同还是不同,速度都没有区别。

1 个答案:

答案 0 :(得分:3)

虽然MySQL 5.0+现在支持Index Merge,但目前存在这种限制:

  

索引合并不适用于全文索引。我们计划延长   它将在未来的MySQL版本中涵盖这些内容。

通常,您可以将这些重写为UNION个查询:

SELECT * FROM exampleTable
WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)
UNION
SELECT * FROM exampleTable
WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)