查询调优性能

时间:2016-12-01 18:01:39

标签: sql oracle11g sql-tuning

我想优化我的查询,如下所示,它返回正确的结果,但我觉得我做错了或者可以用更少的步骤做,我的查询看起来像这样。

Select /*+ Parallel 8 */
    Mes, Score, Solucion, Ruta, Rango_Monto, Conteo, 
    sum(Conteo) over(partition by Mes, Score, Ruta, Rango_ Monto order by Mes, Score, Ruta, Rango_Monto) TotalCountSol,
    round((Conteo / sum(Conteo) over(partition by Mes, Score, Ruta, Rango_Monto order by Mes, Score, Ruta, Rango_Monto)) * 100, 2) ||'%' "%"
from
    (Select 
         Mes, Score, Solucion, Ruta,
         case
            when Monto <= 2000 then '<2000'
            when Monto between 2001 and 3000 then '2to3k'
            when Monto between 3001 and 4000 then '3to4k'
            when Monto between 4001 and 5000 then '4to5k'
            when Monto between 5001 and 10000 then '5to10k'
            else '10k+'
         end Rango_Monto,
         count(1) Conteo
     from
         (select /*+ Parallel 16 */
              Mes, Score, PNR_ID, Ruta, Solucion,
              sum(case 
                     when Moneda = 'USD' then ((Monto_Total)*19)
                     else Monto_Total
                  end) Monto
          from 
              (select /*+ parallel(h,8) */ 
                   extract(month from h.ts_0002) Mes,
                   case
                      when h.SCORING1_SCORE between 2000 and 3000 then '2k-3k'
                      when h.SCORING1_SCORE between 3000 and 5000 then '3k-5k'
                      when h.SCORING1_SCORE between 5000 and 10000 then '5k-10k'
                      when h.SCORING1_SCORE between 10000 and 50000 then '10k-50k'
                      when h.SCORING1_SCORE between 50000 and 75000 then '50k-75k'
                      when h.SCORING1_SCORE >= 75000 then '75k+'
                   end Score,
                   h.transaction_id PNR_ID,
                   h.text_0338 || h.text_0208 Ruta,
                   case
                      when h.resolution1_id in ('5237260000000000003', '5237260000000000004') then 'Allowed'
                      when h.resolution1_id is null then 'Nulo'
                      else 'Fraude'
                   end Solucion,
                   d.det_text_0053 Moneda,
                   d.det_numeric_0016 Monto_Total
               from 
                   data_headers1_hist h,
                   data_details1_hist d
               where
                   h.id = d.data_header_id
                   and h.scoring1_score > 2000
                   and h.virtual_table_id = '5237260000000002621'
                   and TO_CHAR(h.ts_0002, 'YYYY/MM/DD HH24:MI:SS') >= '2016/05/01 00:00:00'
                   and TO_CHAR(h.ts_0002, 'YYYY/MM/DD HH24:MI:SS') <= '2016/05/31 23:59:59'
                   and d.det_text_0019 in ('APPROVED') --Respuesta banco Emisor
                   and SCORING1_RULES_TRIPPED LIKE '%5237260000000390676:500%'
                   --and h.transaction_id = 'CYYPHA__280716'
                   /*and h.text_0338 = 'CUN'
                     and h.text_0208 = 'MEX'*/
                   --and h.text_0338 || h.text_0208 in ('CUNMEX','MEXCUN')
              )
         group by
             Mes, Score, PNR_ID, Ruta, Solucion) 
     group by
         Mes, Score, Solucion, Ruta,
         case
            when Monto <= 2000 then '<2000'
            when Monto between 2001 and 3000 then '2to3k'
            when Monto between 3001 and 4000 then '3to4k'
            when Monto between 4001 and 5000 then '4to5k'
            when Monto between 5001 and 10000 then '5to10k'
            else '10k+'
         end)
order by
    Mes, Score, Rango_Monto, Ruta, Solucion
;

正如您所看到的,我必须通过子查询进行查询,但是当我尝试移动算术运算和组时,它会影响我的结果。

0 个答案:

没有答案