简体   繁体   English

MySQL 慢速查询连接两个表,每个表都有全文条件

[英]MySQL slow query joining two tables with fulltext condition on each

I need to join two tables (1M rows and 10M rows respectively)我需要加入两个表(分别为 1M 行和 10M 行)

Each table is filtered with a fulltext match condition:每个表都使用全文匹配条件进行过滤:

SELECT SQL_NO_CACHE c.company_index
    
FROM dw.companies c INNER JOIN dw.people p 
ON c.company_index = p.company_index
    
WHERE MATCH ( c.tag ) AGAINST ( 'ecommerce' IN BOOLEAN MODE )
AND MATCH ( p.title ) AGAINST ( 'director' IN BOOLEAN MODE )
    
ORDER BY c.company_index DESC ;

Both tables have fulltext indexes (on "tag" and "title")两个表都有全文索引(在“标签”和“标题”上)

The query time is more than 1 mn with both conditions.两种情况下查询时间均超过 1 百万。

With only one of the two conditions, the query time is below 1 sec.只有这两个条件之一,查询时间低于 1 秒。

How could I optimize this query?我怎样才能优化这个查询?

I think the problem is that FULLTEXT is very fast if it can be performed first , but very slow if not.我认为问题在于如果可以执行 FULLTEXT 非常快,但如果不能执行则非常慢。 With both of the tests in the original query, one MATCH can be first, but the other cannot be.对于原始查询中的两个测试,一个 MATCH 可以是第一个,但另一个不能。

Here's a messy idea on how to work around the problem.这是关于如何解决该问题的混乱想法。

SELECT c.company_index
    FROM ( SELECT company_index FROM companies WHERE MATCH... ) AS c
    JOIN ( SELECT company_index FROM people    WHERE MATCH... ) AS p
       ON c.company_index
        = p.company_index
    ORDER BY ...

What version of MySQL are you using?您使用的是什么版本的 MySQL? Newer versions will automatically create an index on one of the 'derived' tables, thereby making the JOIN quite efficient.较新的版本将自动在“派生”表之一上创建索引,从而使JOIN非常有效。

Here's another approach:这是另一种方法:

SELECT c.company_index
    FROM ( SELECT company_index FROM companies WHERE MATCH... ) AS c
    WHERE EXISTS ( SELECT 1 FROM people WHERE MATCH...
                      AND company_index = c.company_index )
    ORDER BY ...

In both cases (I hope) one SELECT will use one FT index;在这两种情况下(我希望)一个SELECT将使用一个 FT 索引; the other will use the other, thereby getting the FT performance benefit.另一个将使用另一个,从而获得 FT 性能优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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