【发布时间】:2016-03-28 20:04:14
【问题描述】:
有没有办法直接在Oracle中的表中按行号查询?换句话说,用一些基本的语言,如 C 或 Java,实现与在数组中进行普通查找相同的效果。我还没有尝试过虚拟列。
例如,下面是一个高效查询的示例,但它会浪费磁盘空间:
create table ary (row_position_id number(10) NOT NULL,
datum binary_float NOT NULL);
declare i pls_integer;
begin
for i in 0..10000000
loop
insert into ary values (i, dbms_random.normal());
end loop;
commit;
end;
create unique index ary_rp on ary(row_position_id);
现在,我将创建一组查询值以存储在另一个“参数”表中:
create table query_values (qval number(10) NOT NULL);
declare i pls_integer;
begin
for i in 0..10000
loop
insert into query_values (abs(dbms_random.random() % 10000000));
end loop;
commit;
end;
现在,有了这些查询值,我将查询原始表
select d.* from ary d where exists (select 0 from query_values v
where d.row_position_id = v.qval);
现在,这个查询就可以了——它将使用 INDEX UNIQUE SCAN 和 ROWID 的 TABLE 访问。我遇到的问题是 row_position_id 在表块中占用的空间与实际数据(DATUM 列)一样多。
我知道索引组织表和虚拟列(不能与 IOT 一起使用)。当然,像 ROWNUM 和 ROW_NUMBER 这样的东西在这里是无关紧要的(除非我误解了什么)。
另外值得指出的是,这个表是静态数据——一旦加载,它就永远不会改变。我可能会做一个 ALTER TABLE ARY READ ONLY;
我真正想要的是:
create table ary (datum binary_float not null);
-- load rows in a specific order
-- efficiently query this table by implicit row position
非常感谢!
亨利
【问题讨论】:
标签: oracle11g query-optimization