物联网是否保证选择中的订单?

Ben*_*Ben 7 oracle oracle-11g-r2

我们需要向priority表中添加一列,该列每秒被命中约 250 次,大约 170 次选择、125 次插入和 60 次更新。该列将是一个简单的number(1).

priority无关紧要的插入或更新,即不是主键,我将单独执行的一部分。

我们基本上不想order by每秒进行 170 次超范围扫描,因为执行的数量会大幅下降。

是否索引组织表保证priority = 1始终来之前priority = 9运行下面的查询时:

select *
  from my_table
 where rownum < 2
Run Code Online (Sandbox Code Playgroud)

对于稍微更多的上下文,典型的查询将是:

select *
  from my_table
 where mod(to_number(to_char(tstamp,'ss')),1) = 0
   and done is null
   and country = 'gbr'
   and rownum < 2
Run Code Online (Sandbox Code Playgroud)

对于 IOT 的 pk 约束将成为priority, rest of the pk单独的 pk 约束,仅用于结构。done在表的大约 99% 中为空,所以这无论如何都不是很有选择性。

我认为,使用的主要指标是,country, done, to_number(to_char(tstamp,'ss')我们测试了大约 20 种组合,这在很长一段时间内都名列前茅。

我完全不愿意在这些查询中添加任何时间,select 添加的 0.01sa 天是每天 41 分钟。我们宁愿满足于“足够好”而不是完美。

Nic*_*mas 13

不。

唯一的事情保证结果集顺序是ORDER BY在查询子句。

这是一个关于 SQL 的流行问题,因此值得重复我在回答有关SQL ServerMySQL 的类似问题时所写的内容:

在 SQL 世界中,顺序不是一组数据的固有属性。因此,除非您使用 ORDER BY 子句查询数据,否则您无法从 RDBMS 获得数据将按特定顺序(甚至以一致顺序)返回的保证。

在 Oracle 中,索引组织表 (IOT) 将最大限度地减少数据库为按索引的排序顺序排序数据而必须执行的工作量。尽管您可能会发现 Oracle 倾向于以相同的顺序返回从 IOT 中选择的行,但只有在您使用子句查询 IOT 时才能保证该顺序ORDER BY

  • 特别是在 Oracle 9i 或 10g 时间范围内,Oracle 添加了哈希分组。各种 SQL 都崩溃了,因为以前执行 SORT GROUP BY 的操作不再执行了。 (3认同)
  • 我猜如果你不要求并行性会破坏秩序。 (2认同)

Jac*_*las 5

物联网是否保证选择中的订单?

不,没有ORDER BY. 曾经。

但是你可以像这样实现你想要的:

select * from (select * from my_table order by priority) where rownum < 2;
Run Code Online (Sandbox Code Playgroud)

这不一定会导致 Oracle 做更多的工作:

create table foo(priority, id, primary key (priority,id)) organization index as
select mod(level,9), level from dual connect by level<=100000;
Run Code Online (Sandbox Code Playgroud)
select /*+ gather_plan_statistics */ *
from (select * from foo order by priority)
where rownum<2;
Run Code Online (Sandbox Code Playgroud)
优先事项 | ID
-------: | -:
       0 | 9
select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
Run Code Online (Sandbox Code Playgroud)
| -------------------------------------------------- ----------------------------------------------- |
| | 身份证 | 操作 | 姓名 | 开始 | 电子行 | A行 | 时代 | 缓冲器 | |
| -------------------------------------------------- ----------------------------------------------- |
| | 0 | 选择语句 | | 1 | | 1 |00:00:00.01 | 2 | |
| |* 1 | 计数STOPKEY | | 1 | | 1 |00:00:00.01 | 2 | |
| | 2 | 查看 | | 1 | 1 | 1 |00:00:00.01 | 2 | |
| | 3 | 索引全扫描| SYS_IOT_TOP_26495 | 1 | 100K| 1 |00:00:00.01 | 2 | |
| -------------------------------------------------- ----------------------------------------------- |
| |
| 谓词信息(由操作 ID 标识):|
| -------------------------------------------------- - |
| |
| 1 - 过滤器(ROWNUM<2) |
| |

dbfiddle在这里

的意思COUNT STOPKEY是Oracle不必经过所有行FULL SCAN