闽公网安备 35020302035485号
ORA-01795: maximum number of expressions in a list is 1000MySQL 虽然没有明确的限制不能超过 1000,但是也会受系统参数的影响。今天来聊一聊 MySQL in 语句为什么要限制参数数量。
net_buffer_length:网络缓冲区的大小。如果 in 语句返回的结果超过网络缓冲区大小,可能导致传输问题。
select * from table1 where id in(select id from table2)如果查询涉及到排序,并且排序的数据量很大,导致 sort buffer 不够用,就需要利用磁盘临时文件辅助排序,性能下降。即使 in 语句没有影响到 Server 端内存,in 语句中参数数量太多的话,也会增加比较次数,增加单个语句的执行时间,降低性能。
// 堆代码 duidaima.com
List<Long> allIds = table2Dao.selectAllIds();
List<Long> splitIds;
int start = 0;
while(true){
splitIds = start + 1000 > allIds.size() ? allIds.subList(start, allIds.size()) : allIds.subList(start, start + 1000);
List<ResultObject> batchResults = table1Dao.query(splitIds);
if(start + 1000 > allIds.size()){
break;
}
start += 1000;
}
2.2 使用临时表--创建临时表
CREATE TEMPORARY TABLE temp_table2_ids (
id BIGINT PRIMARY KEY
);
--把 table2 的 id 插入临时表
insert into temp_table2_ids select id from table2;
--使用 EXISTS 语句代替 in
SELECT * FROM table1 t1 WHERE EXISTS (SELECT * FROM temp_table2_ids t2 WHERE t1.id = t2.id);