数据库优化查询更新操作/查询最大值
2012-08-12 19:33
351 查看
1- 在数据查询时面对大批量数据,如何关联查询。这个对查询效率影响很大。这里有两种方法提高查询效率。更新大批量数据时通过rowid查找
在单表查询时
可以增加并行度
select /*+ parallel(tcustomerinfo 8) */
c_custno
from tcustomerinfo ti
多表查询
select /*+ all_rows leading(hm) use_hash(h) no_merge(h) */
h.rowid ,hm.d_opendate
from (
select c_custno, d_opendate
from(
select /*+ parallel(TACCOINFO 8) */
c_custno,
d_opendate,
row_number() over(partition by c_custno order by d_opendate desc nulls last) rn
from TACCOINFO
where c_custtype = ''1'' )
where rn=1
) hm ,TORGEXINFO h
where hm.c_custno =h.c_custno order by h.rowid
2- 大数据量更新时选择rowid查找更新,这样可以直接找到物理地址。形式如下
declare
type refcursor is ref cursor;
v_cursor refcursor;
type array_rowid is table of rowid;
type array_value is table of varchar2(20);
a_rowid array_rowid := array_rowid();
a_value array_value := array_value();
begin
open v_cursor for 'select h.rowid,hm.d_lastconfirmdate from (
select c_custno,to_char(to_date(d_cdate),''yyyy-mm-dd'') d_lastconfirmdate from(
select /*+ parallel(tconfirm 8) */
c_custno,d_cdate,row_number() over(partition by c_custno order by d_cdate desc nulls last) rn from tconfirm
where c_businflag in (''02'', ''39'')
and c_custtype = ''1''
)
where rn=1) hm ,tperexp h where hm.c_custno =h.c_custno order by h.rowid';
loop
fetch v_cursor bulk collect into a_rowid,a_value limit 5000;
if a_rowid.count() > 0 then
forall i in a_rowid.first .. a_rowid.last
update tperexp set d_lastconfirmdate =a_value(i) where rowid = a_rowid(i);
end if;
exit when v_cursor%notfound;
end loop;
close v_cursor;
end;
/
3- 查询最大值如下查询方式要比直接使用max效率高
select c_custno, d_opendate
from(
select /*+ parallel(TACCOINFO 8) */
c_custno,
d_opendate,
row_number() over(partition by c_custno order by d_opendate desc nulls last) rn
from TACCOINFO
where c_custtype = ''1'' )
where rn=1
在单表查询时
可以增加并行度
select /*+ parallel(tcustomerinfo 8) */
c_custno
from tcustomerinfo ti
多表查询
select /*+ all_rows leading(hm) use_hash(h) no_merge(h) */
h.rowid ,hm.d_opendate
from (
select c_custno, d_opendate
from(
select /*+ parallel(TACCOINFO 8) */
c_custno,
d_opendate,
row_number() over(partition by c_custno order by d_opendate desc nulls last) rn
from TACCOINFO
where c_custtype = ''1'' )
where rn=1
) hm ,TORGEXINFO h
where hm.c_custno =h.c_custno order by h.rowid
2- 大数据量更新时选择rowid查找更新,这样可以直接找到物理地址。形式如下
declare
type refcursor is ref cursor;
v_cursor refcursor;
type array_rowid is table of rowid;
type array_value is table of varchar2(20);
a_rowid array_rowid := array_rowid();
a_value array_value := array_value();
begin
open v_cursor for 'select h.rowid,hm.d_lastconfirmdate from (
select c_custno,to_char(to_date(d_cdate),''yyyy-mm-dd'') d_lastconfirmdate from(
select /*+ parallel(tconfirm 8) */
c_custno,d_cdate,row_number() over(partition by c_custno order by d_cdate desc nulls last) rn from tconfirm
where c_businflag in (''02'', ''39'')
and c_custtype = ''1''
)
where rn=1) hm ,tperexp h where hm.c_custno =h.c_custno order by h.rowid';
loop
fetch v_cursor bulk collect into a_rowid,a_value limit 5000;
if a_rowid.count() > 0 then
forall i in a_rowid.first .. a_rowid.last
update tperexp set d_lastconfirmdate =a_value(i) where rowid = a_rowid(i);
end if;
exit when v_cursor%notfound;
end loop;
close v_cursor;
end;
/
3- 查询最大值如下查询方式要比直接使用max效率高
select c_custno, d_opendate
from(
select /*+ parallel(TACCOINFO 8) */
c_custno,
d_opendate,
row_number() over(partition by c_custno order by d_opendate desc nulls last) rn
from TACCOINFO
where c_custtype = ''1'' )
where rn=1
相关文章推荐
- Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']
- shell脚本操作mysql数据库—创建数据库,在该数据库中创建表(插入,查询,更新,删除操作也可以做)
- (三)数据库故障处理的案例-数据库可以查询不能做更新操作
- PHP Yii 框架的数据库操作笔记一、查询,更新,删除的方法(AR模式)!
- Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式'
- Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]
- yii Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法
- QTP中对数据库的操作(查询,更新和删除等)
- MongoDB创建表步骤,Mongo常用的数据库操作命令,查询,添加,更新,删除_MongoDB 性能监测
- MongoDB创建表步骤,Mongo常用的数据库操作命令,查询,添加,更新,删除_MongoDB 性能监测
- SqlDataReader查询,更新,删除并保持和数据库实时操作
- Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]
- 线段树学习笔记(单点更新+区间查询最大值+lazy标记+pushdown操作+区间更新+求区间和)
- mysql自学,数据库基本操作语句_查询_插入_更新
- Yii 1.0数据库操作 查询、增加、更新、删除
- Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']
- 使用Java对数据库进行基本的查询和更新操作
- MySQL基本操作:数据库的创建、查询、更新、插入、删除表格
- 查询数据库最大的索引、静态类与非静态类的区别、后台操作DIV样式的方法、C#操作TreeView组件中的一些常用方法及具体实现
- 在MyEclipse中利用Hibernate框架完成数据库的基本操作(查询、更新、删除)