查询优化 - 窗口排序

时间:2016-10-14 00:15:49

标签: database oracle query-optimization

我使用rank()生成了报告。

select sa.COUNTRY,
   sa.CITY,
   to_char(sum(r.TOTAL_SERVICE_COST)) as Total_Service_Cost,
   rank() OVER (PARTITION BY sa.COUNTRY
   ORDER BY SUM(r.TOTAL_SERVICE_COST) Desc) as City_Rank_by_country
from ROUTESFACT r,SOURCEAIRPORT_DIM sa
where r.SOURCEAIRPORTID=sa.SOURCE_AIRPORTID
GROUP BY sa.COUNTRY,sa.CITY;

我得到以下执行计划。 enter image description here

如您所见,窗口排序和散列分组未得到有效优化。 优化这个的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

首先,使用正确的join语法来纠正您的查询。一个整洁,正确编写的查询更容易改进:

select sa.COUNTRY, sa.CITY,
       to_char(sum(r.TOTAL_SERVICE_COST)) as Total_Service_Cost,
       rank() OVER (PARTITION BY sa.COUNTRY
                    ORDER BY SUM(r.TOTAL_SERVICE_COST) Desc
                   ) as City_Rank_by_country
from ROUTESFACT r join
     SOURCEAIRPORT_DIM sa
     on r.SOURCEAIRPORTID = sa.SOURCE_AIRPORTID
group by sa.COUNTRY, sa.CITY;

我不确定他们会提供多少帮助,但索引是一种很好的开始方式:SOURCEAIRPORT_DIM(Country, City, SOURCE_AIRPORTID)ROUTESFACT(SOURCEAIRPORTID, TOTAL_SERVICE_COST)

这些可能有助于您的查询。