栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Oracle报错】ORA-01795: 列表中的最大表达式数为 1000 问题解决(使用JDK8的 stream 实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Oracle报错】ORA-01795: 列表中的最大表达式数为 1000 问题解决(使用JDK8的 stream 实现)

1. 问题说明
-- 类似这种 SQL
SELECt * FROM tableName WHERe tableField IN ('','')

姑且不说这种 SQL 的效率和可优化和替代性,就当前问题在 MySQL、Greenplum 数据库没有 1000 的限制。

2. 解决方法

解决的方法较多,这里使用的是 JDK8 的 stream 方法,代码如下:

    private String getInStrByList(List> list) {
        int listSize = list.size();
        List> tempRecordList;
        // 分段大小(这个数值可以写成参数传递过来)
        int lengthControl = 1000;
        List inStrSegmentList = new ArrayList<>();
        if (listSize <= lengthControl) {
            return list.stream().map(oneMap-> MapUtils.getString(oneMap, "fieldName")).collect(Collectors.joining("','", " table_field in ( '", "' ) "));
        } else {
        	// 进行分段
            double number = listSize * 1.0 / lengthControl;
            int n = ((Double) Math.ceil(number)).intValue();
            for (int i = 0; i < n; i++) {
                int iLength = i * lengthControl;
                int end = lengthControl * (i + 1);
                if (end > listSize) {
                    end = listSize;
                }
                tempRecordList = list.subList(iLength, end);
                String inStrSegment = tempRecordList.stream().map(oneMap -> MapUtils.getString(oneMap, "fieldName")).collect(Collectors.joining("','", "'", "'"));
                inStrSegmentList.add(inStrSegment);
            }
        }
        return inStrSegmentList.stream().collect(Collectors.joining(" ) or table_field in ( "," ( table_field in ( "," ) )"));
    }

字符串的使用举例:

 
        SELECT * FROM table_name
        
            
                AND ${inStr}
            
        
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号