处理 Oracle SQL in 超过1000 的解决方案

    处理oracle sql 语句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超过1000项就会报错。这主要是oracle考虑性能问题做的限制。如果要解决次问题,可以用 where id (1, 2, ..., 1000) or id (1001, ...)

package windy.learn;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;

public class OracleSqlUtils {
    /**
     * x,y相除,向上取整
     * @param x
     * @param y
     * @return x,y相除,向上取整
     */
    private static int ceilDiv(int x, int y) {
        int r = x / y;
        return r * y == x ? r : r + 1;
    }

    public static String getOracleSQLIn(Collection<String> ids, String field) {
        return getOracleSQLIn(ids, 1000, field);
    }

    /**
     * oracle sql 语句in子句中如果子句中超过1000项就会报错。这主要是oracle考虑性能问题做的限制。 如果要解决次问题,可以用
     * where id (1, 2, ..., 1000) or id (1001, ...)
     *
     * @param ids
     *            in语句中的集合对象
     * @param count
     *            in语句中出现的条件个数
     * @param field
     *            in语句对应的数据库查询字段
     * @return 返回 field in (...) or field in (...) 字符串
     */
    public static String getOracleSQLIn(Collection<String> ids, int count,
            String field) {
        String[] idsArr = ids.toArray(new String[0]);
        count = Math.min(count, 1000);
        int len = idsArr.length;
        int size = ceilDiv(len, count);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < size; i++) {
            int fromIndex = i * count;
            int toIndex = Math.min(fromIndex + count, len);
            String productId = StringUtils.defaultIfEmpty(
                    StringUtils.join(idsArr, "‘,‘", fromIndex, toIndex), "");
            if (i != 0) {
                builder.append(" or ");
            }
            builder.append(field).append(" in (‘").append(productId)
                    .append("‘)");
        }

        return StringUtils.defaultIfEmpty(builder.toString(), field
                + " in (‘‘)");
    }
}

参考:http://www.cnblogs.com/hoojo/archive/2012/08/31/2665396.html

时间: 2025-01-06 14:10:50

处理 Oracle SQL in 超过1000 的解决方案的相关文章

oracle select in超过1000条报错解决方法

本博客介绍oracle select in超过1000条数据的解决方法,java框架是采用mybatis的,这可以说是一种比较常见的错误:select * from A where id in(...),oracle官方函数做了限定,in里的参数只能1000个,所以超过1000个参数就会报错,解决方法是将集合分为每个集合1000的小集合,然后用or拼起来select * from A where id in(1,2,...,1000) or id in (1001,1002,2000)...,好

Oracle数据库查询用 where in 查询的项超过1000条的解决方案

众所周知,如果我们的用SQL查询语句时,如果用where in带的参数超过1000条的话,oracle是会报错的. 因为项目中遇到这样的问题,所以找到了接下来我要说的这个办法. 因为用的地方很多,所以我把这个封装成了一个方法. /// <summary> /// 获取查询条件超过1000个列表数据的SQL /// columnName In :list /// </summary> /// <param name="list">参数列表</pa

oracle的in里面的 参数不能超过1000个,解决方法,拆成 in () or in()

Stacktracesorg.hibernate.exception.SQLGrammarException: ORA-01795: ??????????? 1000 org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122) org.hibernate.exception.internal.StandardSQLExceptionConverte

Oracle SQL性能优化

转载自:http://www.cnblogs.com/rootq/archive/2008/11/17/1334727.html (1)      选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理,在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.如果有3个以上的表连接查询, 那就需要选择交叉表(intersection ta

&lt;转&gt;Oracle SQL性能优化

原文链接:http://www.cnblogs.com/rootq/archive/2008/11/17/1334727.html (1)      选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理,在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.如果有3个以上的表连接查询, 那就需要选择交叉表(intersection t

OracleSpatial 处理超过1000个点的几何对象时,报错Ora-00939: 函数的参数过多解决方法

oracle空间字段mdsys.sdo_geometry存储多边形,报错ora-00939:to many arguments for function sql语句  update d_area set coordinate=mdsys.sdo_geometry(2003,8307,null,mdsys.sdo_elem_info_array(1,1003,1), mdsys.sdo_ordinate_array( 119.65415,29.14556,119.64824,....)) wher

oracle sql语句

##########基本操作##########启动数据库:su - oraclesqlplus / as sysdbaSQL> startup 对scott用户解锁:SQL> conn / as sysdbaSQL> alter user scott identified by tiger account unlock; 连接到指定的数据库用户:SQL> conn scott/tiger 实现操作系统开机数据库自动open:vi /etc/oratab--------------

Oracle sql develpoer

Oracle SQL Developer是针对Oracle数据库的交互式开发环境(IDE) Oracle SQL Developer简化了Oracle数据库的开发和管理. SQL Developer提供了PL/SQL程序的端到端开发,运行查询工作表的脚本,管理数据库的DBA控制台,报表接口,完整的数据建模的解决方案,并且能够支持将你的第三方数据库迁移至Oracle. SQL Developer可以连接到任何Oracle 10g及其后续版本的数据库,并且能在Windows,Linux和Mac OS

oracle sql优化

第一掌 避免对列的操作 任何对列的操作都可能导致全表扫描,这里所谓的操作包括数据库函数.计算表达式等等,查询时要尽可能将操作移至等式的右边,甚至去掉函数. 例1:下列SQL条件语句中的列都建有恰当的索引,但30万行数据情况下执行速度却非常慢: select * from record where  substrb(CardNo,1,4)='5378'(13秒) select * from record where  amount/30< 1000(11秒) select * from recor