sql优化

taifeng123 2011-01-07 03:18:18
数据库为db2,数据量在百万左右。
如何修改才能提高速度。
原来的sql:
update NWH.GCUST_MOWN a
set expgroup_date = (select expgroup_date
from nwh.gcust_history b
where bc_flag = '0'
and a.cust_id = b.cust_id
and a.bc_flag = b.bc_flag
and time_id = 20110104)
where a.bc_flag = '0' with ur
...全文
71 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dmewy 2011-01-12
  • 打赏
  • 举报
回复
百万级? 首先很小..
其次 select expgroup_date
from nwh.gcust_history b
where bc_flag = '0'
and a.cust_id = b.cust_id
and a.bc_flag = b.bc_flag
and time_id = 20110104 这条应该只会返回一条记录吧?

所以百万级应该是在UPDATE那条..
只要保证a.bc_flag有index
你分两次做就好了. 先查 再update. 效能不差的..
zhiliyang 2011-01-12
  • 打赏
  • 举报
回复
select count(1)
from (
select distinct cust_id, bc_flag
from nwh.gcust_history
where time_id = 20110104 ) as tmp

看 B 表里面不同 的 CUST_ID, BC_FLAG 的 combination case 有多少
如果小于 1W, 可以用 declare 一个 global temp table, 然后 select expgroup_date,cust_id, bc_flag from nwh.gcust_history 去填充 temp table
然后再在temp table的基础上join, 来update

从而避免两个大表的join
yangxiao_jiang 2011-01-10
  • 打赏
  • 举报
回复
如果改成merg into会不会好点?
taifeng123 2011-01-10
  • 打赏
  • 举报
回复
不管用。看能否有好的方法
taifeng123 2011-01-08
  • 打赏
  • 举报
回复
举个例子,我看看,咋建立联合索引??
zhaojianmi1 2011-01-08
  • 打赏
  • 举报
回复
create index index_name on table(col_name, col_name)
wwwwb 2011-01-07
  • 打赏
  • 举报
回复
在两表的cust_id 和 bc_flag 上建立联合索引,
bc_flag有多少 种值
time_id有多少 种值,如果多的话,在B表上建立此索引
zhaojianmi1 2011-01-07
  • 打赏
  • 举报
回复
a表 cust_id 和 bc_flag 建立联合索引
b表 cust_id 和 bc_flag 和 time_id 建立联合索引

如果cust_id有很好的区分度,那么可以只在cust_id上建立索引

5,889

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧