有个百万级记录的表共3109447条记录
我用两个方法查询,不知道哪个效率更高些declare
table_id NUMBER;
start_time timestamp;
end_time timestamp;
begin
select localtimestamp into start_time from dual;
select max(table_a_id) into table_id from table_a;
select localtimestamp into end_time from dual;
dbms_output.put_line('start_time is'||start_time);
dbms_output.put_line('end time is'||end_time);
dbms_output.put_line('exute time is:'||(end_time-start_time));
end; declare
table_id NUMBER;
start_time timestamp;
end_time timestamp;
begin
select localtimestamp into start_time from dual;
select table_a_id into table_id from table_a where rownum=1 order by table_a_id desc;
select localtimestamp into end_time from dual;
dbms_output.put_line('start_time is'||start_time);
dbms_output.put_line('end time is'||end_time);
dbms_output.put_line('exute time is:'||(end_time-start_time));
end;
我用两个方法查询,不知道哪个效率更高些declare
table_id NUMBER;
start_time timestamp;
end_time timestamp;
begin
select localtimestamp into start_time from dual;
select max(table_a_id) into table_id from table_a;
select localtimestamp into end_time from dual;
dbms_output.put_line('start_time is'||start_time);
dbms_output.put_line('end time is'||end_time);
dbms_output.put_line('exute time is:'||(end_time-start_time));
end; declare
table_id NUMBER;
start_time timestamp;
end_time timestamp;
begin
select localtimestamp into start_time from dual;
select table_a_id into table_id from table_a where rownum=1 order by table_a_id desc;
select localtimestamp into end_time from dual;
dbms_output.put_line('start_time is'||start_time);
dbms_output.put_line('end time is'||end_time);
dbms_output.put_line('exute time is:'||(end_time-start_time));
end;
只要table_a_id字段有最为起始列的index,区别应该不会很大。因为index是排过序的,读其中的最大、最小值会很快。
如果没有排过序呢,好像max就慢的不行,我测试过,要11秒多,而用order的却相差不大,没有什么影响
排过序的情况下
两个的时间也都是在0.0055500秒左右,有时候都会用到0.01......秒
整体相差也不是很大,到底是什么原因,是不是rownum=1起到很大作用
max有时很慢,是因为你的index可能经历过大量数据的delete或者insert,导致index不平衡了,执行alter index rebuild后就会ok的。order by很快跟rownum=1很有关系哦。
不好意思,这句我不是很明白。。烦请指点一二
如果不用table_a_id,而用一个很普通的table_a_description呢谢谢
是笔误,应该是:
只要table_a_id字段有做为起始列的index,区别应该不会很大。
也不大明白能指点一下么?
郁闷
到底是
select max(table_a_num) into table_num from table_a;
快
还是
select table_a_num into table_num from table_a where rownum=1 order by table_a_num desc;
快啊
换成一个普普通通的数字类型的table_a_num,什么附加属性都没有的
理论上哪个会快一些
没有索引的情况下两个语句应该是差不多的..
哪位大哥能说明白一点么?
就是一个普通的列,可以为空,可以重复,数字类型,查询出该列值最大的一条数据。
怎么样才能获得很好的查询效率。
最好查询该列的时候还能带上另外几个列,比如
select max(table_a_no),table_a_id........这样的效果。。
也可以分做两条select语句写,反正求效率最高的查询语句。
谨谢!
多谢
trace和逻辑读、物理读,我都没听过,不知道是否可以指点一下,谢谢啦
然后在相同会话中运行你的那两条查询语句,就能看结果了。
不过如果你的oracle版本低于10g,可能还要去建执行计划表。
我的是9I的,怎么建执行计划表呢,呵呵
应该是max效率高一点