您的位置:宽带测速网 > mysql教程 > MySQL关于In的优化是怎么样的

MySQL关于In的优化是怎么样的

2025-06-22 17:06来源:互联网 [ ]

MySQL版本:5.6.14

files表记录的是dfs系统中的文件信息.

有一批数据上传出现错误,需要重新上传.
错误文件的范围已经记录在了test.files_20170206表中。

运行如下查询,竟然很长时间没有结果.

    select * from files t1 where (oldpath,flen) in (

    select oldpath,max(flen) from files f where oldpath in

    (select oldpath from test.files_20170206 )

    group by oldpath

    )


使用explain extended 查看执行计划





原来的SQL,使用了Exists方式.

改写SQL如下,实际上就是加了一层嵌套.

    select * from files t1 where (oldpath,flen) in (

    select * from (

    select oldpath,max(flen) from files f where oldpath in

    (select oldpath from test.files_20170206 )

    group by oldpath

    ) a

    )




经过改写之后,就符合了原来的预期,先将结果保存为一个临时表.然后通过临时表再查数据.