[英]How to optimize mysql query with multiple fulltext matches
Here is my query:这是我的查询:
SELECT heading FROM table_a, table_b as m1, table_b as m2 WHERE (m1.join_id = '69' AND MATCH(m1.content) AGAINST('Harry' IN BOOLEAN MODE)) AND (m2.join_id = '71' AND MATCH(m2.content) AGAINST('+Highway +Design' IN BOOLEAN MODE)) AND m1.webid = table_a.id AND m2.webid = table_a.id
Right now it takes about 3 seconds.现在大约需要 3 秒。 If i take out one of the conditions like this:如果我拿出这样的条件之一:
SELECT heading FROM table_a, table_b as m2 WHERE (m2.join_id = '71' AND MATCH(m2.content) AGAINST('+Highway +Design' IN BOOLEAN MODE)) AND m2.webid = table_a.id
It takes around 0.05 seconds.大约需要 0.05 秒。
I have a fulltext index on the 'content' column.我在“内容”列上有一个全文索引。
Also, in the first query, if I were to search for 'Highway Design' (no operators like + or "") it takes about 30 seconds.此外,在第一个查询中,如果我要搜索“Highway Design”(没有 + 或“”之类的运算符),大约需要 30 秒。
Here is my explain query:这是我的解释查询:
d select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE m1 fulltext id_index,content_ft content_ft 0 1 Using where
1 SIMPLE table_a eq_ref PRIMARY PRIMARY 4 user.m1.id 1
1 SIMPLE m2 fulltext id_index,content_ft content_ft 0 1 Using where
Is there anything else I can do to speed it up?我还能做些什么来加快速度吗?
To explain my tables, table_a is the main table that has a heading, and a content field and table_b is my attribute table for the rows in table_a, so that rows in table_a can have additional attributes.为了解释我的表,table_a 是具有标题的主表,并且 table_b 是我的 table_a 中行的属性表,因此 table_a 中的行可以具有附加属性。
Let me know if I need to explain this better.如果我需要更好地解释这一点,请告诉我。
Thanks!谢谢!
UPDATE here is the explanation of the fast query: UPDATE 这里是快速查询的解释:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE m2 fulltext id_index,content_ft content_ft 0 1 Using where
1 SIMPLE table_a eq_ref PRIMARY PRIMARY 4 user.m2.webid 1
ANOTHER UPDATE - TABLE definitions: Table_b另一个更新 - 表定义:Table_b
id int(11) No None AUTO_INCREMENT
join_id int(11) No None
webid int(11) No None
content text utf8_general_ci No None
Indexes for content table_b内容 table_b 的索引
PRIMARY BTREE Yes No id 2723702 A
content BTREE No No content (333) 226975 A
id_index BTREE No No webid 151316 A
content_ft FULLTEXT No No content 118421
Table_a表_a
id int(11) No None AUTO_INCREMENT
heading text utf8_general_ci Yes NULL
Table a indexes表一索引
PRIMARY BTREE Yes No id 179154 A
heading BTREE No No heading (300) 89577 A YES
Ok, my first opinion would be to change one of fulltext searches to LIKE
clause.好的,我的第一个意见是将全文搜索更改为LIKE
子句。 For this purpose it would be good to have an index on webid , or even better a composite index on (webid, join_id).为此,最好在webid上有一个索引,或者在 (webid, join_id) 上有一个复合索引更好。 This should help for more consistent table joining.这应该有助于更一致的表连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.