当前位置:数据库 > DB2 >>

急求大侠们对DB2 SQL 语句调优!!!

select product_no from A where vip_id not in(1,2,4,5) and product_no not in( select product_no from B_01 where order_source = '123' union select product_no from B_02 where order_source = '123') 求大侠们指点迷津! A表 和 B_表都有超十万条数据。。。
补充:也就是说我现在想去除A表中与B_表中 字段product_no相同的数据行。。。
追问:你这exists 和 not in  的效率一样吧!?试过了,成本显示特别高(大于10亿)。。。可以麻烦写一下么?
答案:select product_no 
from A where vip_id not in(1,2,4,5)
and not exists(select 1 from B_01 WHERE order_source = '123' AND product_no = A.product_no)
AND NOT EXISTS(select 1 from B_02 WHERE order_source = '123' AND product_no = A.product_no) 

建议:
如果可以,把这些data放到视图当中
select product_no from B_01 where order_source = '123'
union
select product_no from B_02 where order_source = '123'
其他:不要用union用左链接或者右链接,消除笛卡尔基 如果数据量大的话,可以建索引,索引内容是product_no

最后在差集下

最好不好用union,这个是left join跟right join的结合,耗时间

大型数据时候,in不太好,性能跟分开写的条件是一样的,因为他还要判断在里面 select * from person p 
inner join company c
on p.org_id=c.org_id
inner join personname n
on n.person_id=p.person_id
inner join (select person_id, max(nameType) nameType from personname group by person_id ) m
on n.person_id=m.person_id and n.nameType=m.nameType
where c.company_id=#CompanyId#
 用except语法试试! 

上一个:在DB2数据库中怎么比较两个数据库中表的结构!
下一个:我们单位的DB2数据库在LINUX主机上面,我想在自己windows7电脑下通过coolsql连接到LINUX的db2数据库怎么弄

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,