日期:2014-05-16  浏览次数:20744 次

下面这个sql能不能优化啊,急等高手解答
计算CY closing day 前10日, 每日的累计交柜量的百分比的规律(D-10包括10天以前)?

select count(*) from vessel_move_log a , voyage_member b where a.voyid_company_code = b.voyid_company_code and a.voyid_vessel_code = b.voyid_vessel_code 
  and a.voyid_voyage_code = b.voyid_voyage_code and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2' and a.service_code ='LPAW' and b.ob_cntr_close_datetime <=to_date('2011-09-27','yyyy/mm/dd HH:MI:SS')  
union all  
select count(*) from vessel_move_log a , voyage_member b where a.voyid_company_code = b.voyid_company_code and a.voyid_vessel_code = b.voyid_vessel_code 
  and a.voyid_voyage_code = b.voyid_voyage_code and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2' and a.service_code ='LPAW' and b.ob_cntr_close_datetime <=to_date('2011-09-26','yyyy/mm/dd HH:MI:SS') 
  union all 
  select count(*) from vessel_move_log a , voyage_member b where a.voyid_company_code = b.voyid_company_code and a.voyid_vessel_code = b.voyid_vessel_code 
  and a.voyid_voyage_code = b.voyid_voyage_code and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2' and a.service_code ='LPAW' and b.ob_cntr_close_datetime <=to_date('2011-09-25','yyyy/mm/dd HH:MI:SS')  
  union all 
  select count(*) from vessel_move_log a , voyage_member b where a.voyid_company_code = b.voyid_company_code and a.voyid_vessel_code = b.voyid_vessel_code 
  and a.voyid_voyage_code = b.voyid_voyage_code and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2' and a.service_code ='LPAW' and b.ob_cntr_close_datetime <=to_date('2011-09-24','yyyy/mm/dd HH:MI:SS')  
  union all
  select count(*) from vessel_move_log a , voyage_member b where a.voyid_company_code = b.voyid_company_code and a.voyid_vessel_code = b.voyid_vessel_code 
  and a.voyid_voyage_code = b.voyid_voyage_code and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2' and a.service_code ='LPAW' and b.ob_cntr_close_datetime <=to_date('2011-09-23','yyyy/mm/dd HH:MI:SS') 
union all .....

------解决方案--------------------
这样的写法怎么执行效率会高呢?
第一种:create table #temp ---临时表
如果在应用程序用的话最好还是采取这种方式效率还是比较高的
然后把查询的结果放在临时表中,在应用程序中调用临时表中的数据,这样的效率会高些的。


如果单纯的查询时间段的数据就可以无需用union all

------解决方案--------------------

select count(*)
from vessel_move_log a, voyage_member b
 where a.voyid_company_code = b.voyid_company_code
and a.voyid_vessel_code = b.voyid_vessel_code
and a.voyid_voyage_code = b.voyid_voyage_code
and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'
and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2'
and a.service_code = 'LPAW'
and b.ob_cntr_close_datetime <=
to_date('2011-09-27', 'yyyy/mm/dd HH:MI:SS')
union all
select count(*)
from vessel_move_log a, voyage_member b
 where a.voyid_company_code = b.voyid_company_code
and a.voyid_vessel_code = b.voyid_vessel_code
and a.voyid_voyage_code = b.voyid_voyage_code
and a.MOVEMENT_CNTR_INTERCHANGE_MODE = 'PI'
and a.CRANE_MOVE_CONFIRMATION_MODE = 'L2'
and a.service_code = 'LPAW'
and b.ob_cntr_close_datetime <=
to_date('2011-09-26', 'yyyy/mm/dd HH:MI:SS')
union all
select count(*)
from vessel_move_log a, voyage_m