简体   繁体   中英

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. 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.

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.

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.

Let me know if I need to explain this better.

Thanks!

UPDATE here is the explanation of the fast query:

    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

    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

    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

    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. For this purpose it would be good to have an index on webid , or even better a composite index on (webid, join_id). This should help for more consistent table joining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM