在这个查询https://www.db-fiddle.com/f/snzSLd8y1oRNRqdsFpNdHa/0中
SELECT test0.id,
(SELECT GROUP_CONCAT(test2.one_info) FROM test2 WHERE test2.one_id = test1.id) AS GC
FROM test0
INNER JOIN test1 ON test1.zero_id = test0.id
WHERE test0.id = 1 AND test1.id = 1 AND test1.rand = 5;test1.rand=5存在,所以执行计划是
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
| --- | ------------------ | ----- | ---------- | ----- | ------------- | ------- | ------- | ----- | ---- | -------- | ----------- |
| 1 | PRIMARY | test0 | | const | PRIMARY | PRIMARY | 4 | const | 1 | 100 | Using index |
| 1 | PRIMARY | test1 | | const | PRIMARY | PRIMARY | 4 | const | 1 | 100 | |
| 2 | DEPENDENT SUBQUERY | test2 | | ref | IND | IND | 5 | const | 4 | 100 | Using index |但是,当我把它改为不存在的东西,比如test1.rand=10,它就给出了这个计划
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
| --- | ------------------ | ----- | ---------- | ---- | ------------- | --- | ------- | ----- | ---- | -------- | --------------------------------------------------- |
| 1 | PRIMARY | | | | | | | | | | Impossible WHERE noticed after reading const tables |
| 2 | DEPENDENT SUBQUERY | test2 | | ref | IND | IND | 5 | const | 4 | 100 | Using index |尽管Extra: Impossible WHERE noticed after reading const tables没有返回任何结果,它仍然使用select_type: DEPENDANT SUBQUERY并获得了rows: 4
如何解决此问题,并使其根本不返回任何东西,以节省性能?
发布于 2020-04-30 04:11:59
查看Handler计数,看看它是否实际接触到任何行:http://mysql.rjweb.org/doc.php/index_烹饪书_mysql#handler_数数
并使用EXPLAIN FORMAT=JSON SELECT ...获取更多细节。
无论如何,由于它是一个依赖的子查询,因此将要求它根据需要经常运行。但那是零次。
而且,EXPLAIN也不是完美的。子查询没有得到4行;EXPLAIN估计每次调用子查询时它都会碰到4行。同样,0*4 = 0。
https://dba.stackexchange.com/questions/265810
复制相似问题