3

这是 MySQL 的后续- 查找与连接表中的所有行匹配的行

感谢这个网站,查询运行完美。

但现在我不得不扩展查询以搜索艺术家和曲目。这导致我进行以下查询:

SELECT DISTINCT`t`.`id` 
FROM `trackwords` AS `tw`  
INNER JOIN `wordlist` AS `wl` ON wl.id=tw.wordid  
INNER JOIN `track` AS `t` ON tw.trackid=t.id  
WHERE (wl.trackusecount>0) AND  
(wl.word IN ('please','dont','leave','me')) AND  
t.artist IN ( 
 SELECT a.id  
 FROM artist as a 
 INNER JOIN `artistalias` AS `aa` ON aa.ref=a.id  
 WHERE a.name LIKE 'pink%' OR  aa.name LIKE 'pink%' 
)  
GROUP BY tw.trackid 
HAVING (COUNT(*) = 4);

我认为这个查询的解释看起来相当不错:

+----+--------------------+-------+--------+----------------------------+---------+---------+-----------------+------+----------------------------------------------+
| id | select_type        | table | type   | possible_keys              | key     | key_len | ref             | rows | Extra                                        |
+----+--------------------+-------+--------+----------------------------+---------+---------+-----------------+------+----------------------------------------------+
|  1 | PRIMARY            | wl    | range  | PRIMARY,word,trackusecount | word    | 767     | NULL            |    4 | Using where; Using temporary; Using filesort | 
|  1 | PRIMARY            | tw    | ref    | wordid,trackid             | wordid  | 4       | mbdb.wl.id      |   31 |                                              | 
|  1 | PRIMARY            | t     | eq_ref | PRIMARY                    | PRIMARY | 4       | mbdb.tw.trackid |    1 | Using where                                  | 
|  2 | DEPENDENT SUBQUERY | aa    | ref    | ref,name                   | ref     | 4       | func            |    2 |                                              | 
|  2 | DEPENDENT SUBQUERY | a     | eq_ref | PRIMARY,name,namefull      | PRIMARY | 4       | func            |    1 | Using where                                  | 
+----+--------------------+-------+--------+----------------------------+---------+---------+-----------------+------+----------------------------------------------+

你看到优化空间了吗?Query 的运行时间大约为 7 秒,不幸的是。欢迎任何建议。

TIA

4

2 回答 2

1

您在这里有两个可能的选择条件:artists's nameword list.

假设这些词比艺术家更有选择性:

SELECT  tw.trackid 
FROM    (
        SELECT  tw.trackid
        FROM    wordlist AS wl
        JOIN    trackwords AS tw  
        ON      tw.wordid = wl.id
        WHERE   wl.trackusecount > 0
                AND wl.word IN ('please','dont','leave','me')
        GROUP BY
                tw.trackid
        HAVING  COUNT(*) = 4
        ) tw
INNER JOIN
        track AS t
ON      t.id = tw.trackid
        AND EXISTS
        (
        SELECT  NULL
        FROM    artist a
        WHERE   a.name LIKE 'pink%'
                AND a.id = t.artist
        UNION ALL
        SELECT  NULL
        FROM    artist a
        JOIN    artistalias aa
        ON      aa.ref = a.id
                AND aa.name LIKE 'pink%'
        WHERE   a.id = t.artist
        )                

您需要具有以下索引才能有效:

wordlist (word, trackusecount)
trackwords (wordid, trackid)
artistalias (ref, name)
于 2009-08-07T15:23:14.087 回答
0

您是否已经为名称列编制了索引?那应该加快速度。

您还可以尝试将全文搜索与匹配和反对一起使用。

于 2009-08-07T15:09:17.107 回答