所以-我有两张桌子:entries
表格:
id - Primary key
meta_keys VARCHAR 20
meta_desc VARCHAR 20
..some other unimportant columns...
headwords
表格:id - Primary key
fk_entries_id - foreign key refers to entries(id)
headword_text VARCHAR(200)
..some other unimportant columns...
所以条目可能有多个标题词-对。所以我只是想把标题中有特定单词的条目分页。我提出的唯一一个查询需要左连接,然后喜欢并有点慢。
SELECT entries.*, GROUP_CONCAT(DISTINCT headwords.headword_text SEPARATOR ' ') AS hw
FROM entries
LEFT JOIN headwords ON (entries.id = headwords.fk_entries_id)
GROUP BY entries.id
HAVING hw LIKE '%dog%'
LIMIT 20,20;
寻求加快这一方法的建议。
编辑>等待-这是一个错误的查询吗?:
SELECT entries.id, GROUP_CONCAT(DISTINCT headwords.headword_text SEPARATOR ' ') AS hw
FROM entries
LEFT JOIN headwords
ON (entries.id = headwords.fk_entries_id)
WHERE headwords.headword_text LIKE 'dog%'
GROUP BY entries.id;
最佳答案
您应该使用IN比较来避免所有字符串连接和搜索:
select e.*
from entries e
where e.id IN (
select distinct fk_entries_id
from headwords
where headword_text like '%dog%'
)
order by e.id
limit 20,20;
此外,如果只匹配单词的开头,则可以将“%dog%”替换为“dog%”,从而进一步提高它的性能。这将进一步提高性能,因为您不再需要考虑在每个标题词前面添加的文本。
关于mysql - mysql分页与左联接和喜欢-> super 慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19868903/