SQL IN 参数超过2100的解决方案

在SQL Server或者MySQL中,当你在IN子句中需要处理的结果集可能超过2100个元素时,由于SQL Server对IN子句中的参数数量存在大约2100个左右的限制,直接使用IN会导致错误。为了解决这个问题,可以采取以下策略:

1、将参数拆分,分批次查询出结果然后合并

2、建立临时表批量插入IN参数 join 或者 exists 查询后删除掉

      我一般都是将参数表一直留着,插入时候带入时间戳参数,查询时候按时间戳查,

      后面跑任务定时批量清除

     参考代码

QueryWrapper<EVVipset> wrapper = new QueryWrapper();
        List<String> includeList=term.getIncludeList();
        if(!isListNotEmpty(includeList)) return new ArrayList<>();
        int includeSize=includeList.size();
        if(includeSize>2000){
            String guid= UUID.randomUUID().toString();
            List<TempForQuery> forQueryList=new ArrayList<>();
            for(String dm:includeList){
                forQueryList.add(new TempForQuery(guid,"vipset",dm));
            }
            tempForQueryMapper.insertBatch(forQueryList);
            wrapper.exists("select 1 from TempForQuery WITH(NOLOCK) where guid='"+guid+
                    "' and type='vipset' and code=V_VIPSET.dm");
        }else{
            wrapper.in("dm",includeList);
        }
        List<EVVipset> list = vipsetMapper.getCustList(wrapper);
        return list;

相关推荐

  1. SQL IN 参数超过2100解决方案

    2024-03-20 14:42:06       43 阅读
  2. 开发中遇到SQL IN传入参数个数超过2100bug

    2024-03-20 14:42:06       30 阅读
  3. Elasticsearch 查询超过10000 解决方案 - Python

    2024-03-20 14:42:06       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 14:42:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 14:42:06       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 14:42:06       82 阅读
  4. Python语言-面向对象

    2024-03-20 14:42:06       91 阅读

热门阅读

  1. Python从入门到精通秘籍八

    2024-03-20 14:42:06       51 阅读
  2. React 应该如何学习?

    2024-03-20 14:42:06       42 阅读
  3. OSI参考模型各层作用

    2024-03-20 14:42:06       38 阅读
  4. ubuntu 开发软件安装

    2024-03-20 14:42:06       40 阅读
  5. git是什么git能做什么

    2024-03-20 14:42:06       42 阅读