我有一个wordpress插件,它本质上创建了一个mysql查询并将结果返回给wordpress。
它是用户驱动的,因此最终可能会出现多个NOT LIKE的大型查询,从而导致查询速度非常慢。
我可以用来改进的任何建议:
SELECT field1,field2,field3,field4
from datatable
WHERE (title NOT LIKE '%word%' AND title NOT LIKE '%word2%'
AND title NOT LIKE '%word3%' AND title NOT LIKE '%word4%'
AND title NOT LIKE '%word5%' AND title NOT LIKE '%word6%'
AND title NOT LIKE '%word7%' AND title NOT LIKE '%word8%'
AND title NOT LIKE '%word9%')
AND MATCH (title) AGAINST ("\"brandname\" " IN BOOLEAN MODE)
ORDER BY total ASC LIMIT 0,60
客户在wordpress插件中添加了大量否定关键字,导致查询次数大于上述查询。
答案 0 :(得分:5)
使用REGEXP
最容易做到这一点。对于多个字词,请使用(one|two|three)
SELECT
field1,
field2,
field3,
field4
from datatable
WHERE
title NOT REGEXP '(word1|word2|word3|word4|word5...|word9)'
AND MATCH (title) AGAINST ("\"brandname\" " IN BOOLEAN MODE)
ORDER BY total ASC
LIMIT 0,60
答案 1 :(得分:1)
您可以使用REGEXP
操作一次比较所有模式。
您的查询将类似于:
SELECT field1,field2,field3,field4
FROM data table
WHERE title NOT REGEXP '^word[0-9]?$'
AND MATCH(title) ("\"brandname\" " IN BOOLEAN MODE)
ORDER BY total ASC LIMIT 0,60