select c.store_no,c.n_card_phy_id,c.n_txn_amt,c.cday,c.msg_type, c.sub_type,c.ctime, d.inv_no
from
(select a.com_no,a.store_no,b.order_primarykey,b.n_card_phy_id,b.n_txn_amt,to_char(b.create_time,'yyyy-MM-dd') cday,
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime
from pos a,bwhotran b
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
between '2012-09-01' and '2012-09-01'
order by a.store_no,b.create_time) c
left join invoice d on c.com_no = d.com_no
and c.order_primarykey = d.order_primarykey
and d.status_flg in ('1','Y')invoice 这张表有很大的数据,其d.com_no,d.order_primarykey
d.status_flg 均是索引!!现在运行这条语句需要5-6min,在程序里面显示出这张报表需要的时间要10分钟左右,
请问有什么方法对该SQL进行优化一下!!!
from
(select a.com_no,a.store_no,b.order_primarykey,b.n_card_phy_id,b.n_txn_amt,to_char(b.create_time,'yyyy-MM-dd') cday,
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime
from pos a,bwhotran b
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
between '2012-09-01' and '2012-09-01'
order by a.store_no,b.create_time) c
left join invoice d on c.com_no = d.com_no
and c.order_primarykey = d.order_primarykey
and d.status_flg in ('1','Y')invoice 这张表有很大的数据,其d.com_no,d.order_primarykey
d.status_flg 均是索引!!现在运行这条语句需要5-6min,在程序里面显示出这张报表需要的时间要10分钟左右,
请问有什么方法对该SQL进行优化一下!!!
解决方案 »
- SQL语句参数化问题
- Failed to commit: ORA-01996: GRANT 失败: 口令文件 '' 已满
- ORACLE更新语句问题
- select 'select * from 'table_name from user_tables;
- 求助:java.sql.SQLException: ORA-00932: inconsistent datatypes: expected %s got %s
- shell调用存储过程时参数的传递
- [请教一个比较难搞的sql查询问题]关于合并子表记录成一个字符串和主表列一起显示的查询
- 有人知道Oracle能象Excel一样可以处理多元线性回归吗?
- 请问如何创建一个oracle instance?
- ORA-12203是什么错误啊?
- 如何让解决视图迁移时遇到的依赖问题
- 问一个ARRAY型,为什么不能赋值??
第一'and d.status_flg in ('1','Y')‘ 改成
select c.store_no,c.n_card_phy_id,c.n_txn_amt,c.cday,c.msg_type, c.sub_type,c.ctime, d.inv_no
from
(select a.com_no,a.store_no,b.order_primarykey,b.n_card_phy_id,b.n_txn_amt,to_char(b.create_time,'yyyy-MM-dd') cday,
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime
from pos a,bwhotran b
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
between '2012-09-01' and '2012-09-01'
order by a.store_no,b.create_time) c
left join invoice d on c.com_no = d.com_no
and c.order_primarykey = d.order_primarykey
and d.status_flg =‘1’
union all
select c.store_no,c.n_card_phy_id,c.n_txn_amt,c.cday,c.msg_type, c.sub_type,c.ctime, d.inv_no
from
(select a.com_no,a.store_no,b.order_primarykey,b.n_card_phy_id,b.n_txn_amt,to_char(b.create_time,'yyyy-MM-dd') cday,
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime
from pos a,bwhotran b
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
between '2012-09-01' and '2012-09-01'
order by a.store_no,b.create_time) c
left join invoice d on c.com_no = d.com_no
and c.order_primarykey = d.order_primarykey
and d.status_flg =‘Y’第二 ‘and a.store_no in(1222)’ 为什么不直接等于
第二:a.store_no in(1222)采用这种方式是因为门店是可选的,可以选择多个比如说
a.store_no in(1222,1223,1224)。
(select a.com_no,a.store_no,b.order_primarykey,b.n_card_phy_id,b.n_txn_amt,to_char(b.create_time,'yyyy-MM-dd') cday,
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime
from pos a,bwhotran b
where a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
AND b.com_no='1001'
and to_char(b.create_time,'yyyy-MM-dd')
between '2012-09-01' and '2012-09-01'
)
select c.store_no,c.n_card_phy_id,c.n_txn_amt,c.cday,c.msg_type, c.sub_type,c.ctime, d.inv_no
from AA C
left join invoice d on c.com_no = d.com_no
and c.order_primarykey = d.order_primarykey
WITH AA AS
(
select a.com_no,a.store_no,b.order_primarykey,b.n_card_phy_id,b.n_txn_amt,to_char(b.create_time,'yyyy-MM-dd') cday,
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime
from pos a,bwhotran b
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
between '2012-09-01' and '2012-09-01'
)
select c.store_no,c.n_card_phy_id,c.n_txn_amt,c.cday,c.msg_type, c.sub_type,c.ctime, d.inv_no
from AA C
left join invoice d on c.com_no = d.com_no
and c.order_primarykey = d.order_primarykey
and d.status_flg in ('1','Y')
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime ,c.inv_no
from pos a,bwhotran b , invoice c
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
and b.com_no=c.com_no(+)
and b.order_primarykey=c.order_primarykey(+)
between '2012-09-01' and '2012-09-01'
order by a.store_no,b.create_time
b.msg_type, b.sub_type,to_char(b.create_time,'HH24:mi:ss') ctime ,c.inv_no
from pos a,bwhotran b , invoice c
where b.com_no='1001'
and a.com_no = b.com_no
and a.pos_no=b.pos_no
and a.store_no in(1222)
and to_char(b.create_time,'yyyy-MM-dd')
and b.com_no=c.com_no(+)
and b.order_primarykey=c.order_primarykey(+)
and d.status_flg(+) in ('1','Y')
between '2012-09-01' and '2012-09-01'
order by a.store_no,b.create_time