这是我的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秒,这对我来说太慢了。基本思路是检索那些lineval with tagvalue为3的lineids,将linename与tagvalue 0或1匹配。
+--+----+-------------+------------+------+---------------------------+----------------+---------+------+-------+--------------------------------+
| | 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 |
+--+----+-------------+------------+------+---------------------------+----------------+---------+------+-------+--------------------------------+
答案 0 :(得分:0)
您似乎想要具有3和0或1的特定文档的行。如果是这样,您可以只使用条件聚合。生成的查询是这样的:
SELECT tmd.lineid
FROM tagmodeldata tmd INNER JOIN
tagline a
ON a.documentid = tmd.documentid AND tmd.tagvalue IN (0, 1, 3)
WHERE tmd.documentid = 926980
GROUP BY tmd.lineid
HAVING SUM(tmd.tagvalue = 3) > 0 AND
SUM(tmd.tagvalue IN (0, 1)) > 0;
目前尚不清楚tagline.linename
和tagline.lineid
之间的关系。以上假设它们是相同的。