同事遇到一个有意思的语句,说一条SQL在MySQL8.0.25版本运行出的结果明显与给定的where条件不符,而在8.0.26版本上是正常的,语句上加了一个无关的用户变量后在8.0.25版本上结果才是正确的,想不通这是怎么回事,这么有意思的事情自然引起了我的兴趣,借此机会深入了解了一下MySQL关于derived table的优化。为了方便演示效果,让小伙伴们关注到现象的本质,我将语句进行了简化处理。下面是模拟的表结构与数据。
MySQL 8.0.22 and later supports derived condition pushdown for eligible subqueries. For a query such as SELECT * FROM (SELECT i, j FROM t1) AS dt WHERE i > constant, it is possible in many cases to push the outer WHERE condition down to the derived table, in this case resulting in SELECT * FROM (SELECT i, j FROM t1 WHERE i > constant) AS dt.在8.0.26版本中修复的bug中发现一个与此问题相关的bug。描述如下:
When a condition is pushed down to a materialized derived table, a clone of the derived table expression replaces the column (from the outer query block) in the condition. When the cloned item included a FULLTEXT function, it was added to the outer query block instead of the derived table query block, which led to problems. To fix this, we now use the derived query block to clone such items. (Bug #32820437)看到这里我们可以确定,就是8.0.22版本时这个新特性的引入,导致了此问题的产生,庆幸的是这个问题在8.0.26版本中已得到解决。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |