MySQL 将 OR 与 MATCH AGAINST 结合使用会大大降低查询速度
MySQL using OR with MATCH AGAINST slows query drastically
列已编入索引且为全文。为什么 "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 秒。列是否相同对速度没有影响。
虽然MySQL5.0+现在支持Index Merge,但目前有这个限制:
Index Merge is not applicable to full-text indexes. We plan to extend
it to cover these in a future MySQL release.
通常,您可以将这些重写为 UNION
查询:
SELECT * FROM exampleTable
WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)
UNION
SELECT * FROM exampleTable
WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)
列已编入索引且为全文。为什么 "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 秒。列是否相同对速度没有影响。
虽然MySQL5.0+现在支持Index Merge,但目前有这个限制:
Index Merge is not applicable to full-text indexes. We plan to extend it to cover these in a future MySQL release.
通常,您可以将这些重写为 UNION
查询:
SELECT * FROM exampleTable
WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)
UNION
SELECT * FROM exampleTable
WHERE MATCH(someColumn) AGAINST('+testing123*' IN BOOLEAN MODE)