【发布时间】:2015-08-14 10:48:14
【问题描述】:
这是我的mysql查询:
SELECT DISTINCT a.lineid
FROM (SELECT DISTINCT tmd.lineid, a.linename
FROM tagmodeldata tmd
INNER JOIN
tagline a
ON a.documentid = tmd.documentid AND tmd.tagvalue = 3
WHERE tmd.documentid = 926980) a
INNER JOIN
(SELECT DISTINCT tmd.lineid, b.linename
FROM tagmodeldata tmd
INNER JOIN
tagline b
ON b.documentid = tmd.documentid AND tmd.tagvalue IN (0 , 1)
WHERE tmd.documentid = 926980) b
ON b.linename = a.linename;
运行大约需要 160 秒,这对我来说太慢了。基本思想是检索那些 lineid 与 tagvalue 为 3 的 lineid,与 tagvalue 0 或 1 匹配的 linename。
+--+----+-------------+------------+------+---------------------------+----------------+---------+------+-------+--------------------------------+
| | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+--+----+-------------+------------+------+---------------------------+----------------+---------+------+-------+--------------------------------+
| | 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 14760 | Using temporary |
| | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 72160 | Using where; Using join buffer |
| | 3 | DERIVED | b | ref | documentid | documentid | 5 | | 593 | Using where; Using temporary |
| | 3 | DERIVED | tmd | ref | documentid,document_index | document_index | 4 | | 66784 | Using where |
| | 2 | DERIVED | a | ref | documentid | documentid | 5 | | 593 | Using where; Using temporary |
| | 2 | DERIVED | tmd | ref | documentid,document_index | document_index | 4 | | 66784 | Using where |
+--+----+-------------+------------+------+---------------------------+----------------+---------+------+-------+--------------------------------+
【问题讨论】:
-
为您的查询提供示例数据。 SQL Fiddle 会有所帮助。您可能可以简化它,但在不知道数据是什么样子的情况下,真的很难提供帮助。
-
更好的是,运行 EXPLAIN PLAN 并查找表扫描。您可能缺少索引。
标签: mysql inner-join explain