Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Using rownum greater than or equal to or less than or equal to the result of paging is incorrect in Oracle #490

Closed
dreapur opened this issue Dec 11, 2017 · 3 comments

Comments

@dreapur
Copy link

dreapur commented Dec 11, 2017

在Oracle下使用Shardingjdbc的分页时遇到一个问题,比如我的SQL是:SELECT * FROM (SELECT A1., ROWNUM RN1 FROM (SELECT * FROM t_order ORDER BY user_id DESC) A1 WHERE ROWNUM <= 10) B WHERE B.RN1 >= 1,正常情况下应该返回10条数据,但是Shardingjdbc返回了9条数据,漏掉了本来的第一条数据。我看了一下在每个分片的执行语句:SELECT * FROM (SELECT A1., ROWNUM RN1 FROM (SELECT * FROM t_order_0 ORDER BY user_id DESC) A1 WHERE ROWNUM <= 10) B WHERE B.RN1 >= 0,这里RowNum变成从0开始的,是Shardingjdbc默认RowNum从0开始吗,这里Shardingjdbc的处理逻辑是怎样的呢,还是说这里存在Bug?

@terrymanu
Copy link
Member

sql改写会改为0,但归并时会按1归并。这个不是bug。
关于为什么取的数据不对,到有可能是bug。请提供可重现的代码以便我们定位

@dreapur
Copy link
Author

dreapur commented Dec 12, 2017

@terrymanu 使用的是官网提供的"sharding-jdbc-raw-jdbc-java-example"这个工程,作了简单的修改:
1.DataSourceUtils改成连接Oracle数据库
2.t_order表使用标准分片策略,算法就是例子提供的算法,PreciseShardingAlgorithm泛型改为Integer,“order_id”作分片字段
String shardingColumn = "order_id"; String algorithmClassNameForModulo = "io.shardingjdbc.example.jdbc.java.algorithm.ModuloShardingTableAlgorithm"; StandardShardingStrategyConfiguration sSSC = new StandardShardingStrategyConfiguration(shardingColumn, algorithmClassNameForModulo); orderTableRuleConfig.setTableShardingStrategyConfig(sSSC);
3.建表语句和插入语句
CREATE TABLE t_order (order_id NUMBER(2) NOT NULL, user_id NUMBER(2) NOT NULL, status VARCHAR2(50))
for (int i = 1; i < 40; i++) { String sql = "INSERT INTO t_order (order_id, user_id,status) VALUES (" + i + ", " + (i + 1) + ",'INIT')"; execute(dataSource,sql); }
4.查询语句
SELECT * FROM (SELECT A1.*, ROWNUM RN1 FROM (SELECT * FROM t_order ORDER BY user_id DESC) A1 WHERE ROWNUM <= 10) B WHERE B.RN1 >= 1
预期结果有10条数据,实际结果只有9条,缺少了"order_id:39,user_id:40"这条记录
order_id:38, user_id:39,
order_id:37, user_id:38,
order_id:36, user_id:37,
order_id:35, user_id:36,
order_id:34, user_id:35,
order_id:33, user_id:34,
order_id:32, user_id:33,
order_id:31, user_id:32,
order_id:30, user_id:31,

@terrymanu
Copy link
Member

fixed at 2.0.1

haocao added a commit that referenced this issue Dec 15, 2017
haocao added a commit that referenced this issue Dec 15, 2017
terrymanu added a commit that referenced this issue Dec 15, 2017
haocao added a commit that referenced this issue Dec 15, 2017
@terrymanu terrymanu changed the title Oracle分页问题 Using rownum greater than or equal to or less than or equal to the result of paging is incorrect in Oracle Aug 8, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants