即使没有更多的行,mysql也会执行完整的索引扫描

时间:2017-10-16 19:51:48

标签: mysql

我正在尝试找到一条恰好2 ^ 1024长度的链,这是一个简化版本:

fmt.Print(t.ExtendedTweet)

问题是很多行在CREATE VIEW full2 AS SELECT a.id1,b.id2 FROM gt a JOIN gt b ON a.id2=b.id1; CREATE VIEW full3 AS SELECT a.id1,b.id2 FROM full2 a JOIN full2 b ON a.id2=b.id1; CREATE VIEW full4 AS SELECT a.id1,b.id2 FROM full3 a JOIN full3 b ON a.id2=b.id1; 之后消失了,但不知何故mysql的执行计划是

f2

enter image description here

我应该解释一下:

+----+-------------+-------+------------+-------+---------------+------+---------+--------------+------+----------+--------------------------+
| id | select_type | table | partitions | type  | possible_keys | key  | key_len | ref          | rows | filtered | Extra                    |
+----+-------------+-------+------------+-------+---------------+------+---------+--------------+------+----------+--------------------------+
|  1 | SIMPLE      | gt1   | NULL       | index | id1,id2       | id2  | 4       | NULL         |   33 |    81.00 | Using where; Using index |
|  1 | SIMPLE      | gt2   | NULL       | ref   | id1,id2       | id2  | 4       | picr.gt1.id1 |    1 |    90.00 | Using where; Using index |
|  1 | SIMPLE      | gt2   | NULL       | ref   | id1,id2       | id1  | 4       | picr.gt1.id2 |    1 |    90.00 | Using where; Using index |
|  1 | SIMPLE      | gt1   | NULL       | ref   | id1,id2       | id2  | 4       | picr.gt2.id1 |    1 |    90.00 | Using where; Using index |
|  1 | SIMPLE      | gt1   | NULL       | ref   | id1,id2       | id1  | 4       | picr.gt2.id2 |    1 |    90.00 | Using where; Using index |
|  1 | SIMPLE      | gt2   | NULL       | ref   | id1,id2       | id2  | 4       | picr.gt1.id1 |    1 |    90.00 | Using where; Using index |
|  1 | SIMPLE      | gt2   | NULL       | ref   | id1           | id1  | 4       | picr.gt1.id2 |    1 |   100.00 | Using index              |
|  1 | SIMPLE      | gt1   | NULL       | ref   | id2           | id2  | 4       | picr.gt2.id1 |    1 |   100.00 | Using index              |
+----+-------------+-------+------------+-------+---------------+------+---------+--------------+------+----------+--------------------------+

这是查询时间的巨大消耗。这是因为我实际上在f2中没有任何行(当然还没有),如果f2中没有任何内容,那么加入f2(无)实际上任何其他查询应立即停止sql计算(但它可能会开始正确离开,但同样的问题,右侧也将是空的)。计算时间在每个新视图上呈指数上升。

我该怎么办?

for each row in comparison1 that id2 MIGHT exist in comparison.id1
 for each row in comparison2 that id2 MIGHT exist in comparison.id1 and id1 MIGHT exist in comparison.id2
  for each row in comparison3 that id2 MIGHT exist in comparison.id1 and id1 MIGHT exist in comparison.id2
   ...
         does comparison1.id2=comparison2.id1 and comparison2.id2=comparison3.id1 comparison3.id2=comparison4.id1 ... 
   ...
  }
 }
}  

不可能吗?是的!这是一个更奇怪的查询,不会进入细节(它应该加入从长度1到1024没有任何联合的所有内容并保存id1,id2和之前的id2并且具有巨大的where条件)但实际上它做得更多更快!

for each row in comparison1 
 for each row in comparison2 that id1 exists in comparison1 .id2
 if fail then skip
  for each row in comparison3 that id1 exists in comparison2 .id2
  if fail then skip
   ...
         does comparison1.id2=comparison2.id1 and comparison2.id2=comparison3.id1 comparison3.id2=comparison4.id1 ... 
   ...
  }
 }
}  

enter image description here

0 个答案:

没有答案
相关问题