【问题标题】:why query is taking too long为什么查询时间过长
【发布时间】:2017-06-15 17:56:23
【问题描述】:

我有一个应该返回大约 10000 行的查询。数据库本身非常大。我运行了一个简单的查询,它在不到 3 秒的时间内返回了一个结果。但是当一个更复杂的代码需要太长的时间。

在我的代码中,我做了一个嵌套选择和一个 case 语句。但是,当我运行我的代码时,需要一个多小时才能返回结果。我可以对减少执行时间的代码做些什么。

SELECT ticker_symb, day_sum_eff, cusip, 
clos_prc, 
nclos_prc,
 case
     when  clos_prc is null and nclos_prc is not null 
     then (nclos_prc - LAG( nclos_prc ignore nulls) OVER (ORDER BY cusip)) 
     when  clos_prc is not null and nclos_prc is null
       then (LEAD( nclos_prc ignore nulls) OVER (ORDER BY cusip)- LAG( nclos_prc ignore nulls) OVER (ORDER BY cusip))
       else NULL
   end DIFF
FROM (SELECT  
      day_sum_eff, 
      cusip,
      ticker_symb, 
      clos_prc, 
      nclos_prc,
      case
           when clos_prc is null and nclos_prc is not null 
           then (nclos_prc - LAG( nclos_prc ignore nulls) OVER (ORDER BY cusip)) 
           when  clos_prc is not null and nclos_prc is null
           then LEAD( nclos_prc ignore nulls) OVER (ORDER BY cusip)- LAG( nclos_prc ignore nulls) OVER (ORDER BY cusip)
          else NULL
          end DIFF
    from  MKTDATA.MARKET_DAILY_SUMMARY 
    WHERE day_sum_eff >=  '1-JUN-2017' and 
          day_sum_eff <=  '10-JUN-2017' )
order by  day_sum_eff_,fmr_iss_cusip OFFSET 0 ROWS FETCH NEXT 3 ROW ONLY;

执行计划表

PLAN_TABLE_OUTPUT

计划哈希值:831959278

----------------------------------------------------------
| Id  | Operation                 | Name                 |
----------------------------------------------------------
|   0 | SELECT STATEMENT          |                      |
|   1 |  VIEW                     |                      |
|   2 |   WINDOW SORT PUSHED RANK |                      |
|   3 |    WINDOW SORT            |                      |
|   4 |     PARTITION RANGE SINGLE|                      |
|   5 |      TABLE ACCESS FULL    | MARKET_DAILY_SUMMARY |
----------------------------------------------------------

【问题讨论】:

  • 你有合适的索引吗?
  • 该查询的执行计划在哪里?表上有哪些可用索引?
  • 你为什么在外部查询中重复整个 CASE 事情?为什么不直接选择diff
  • @APC 为了让我能够访问 DIFF 别名列,我不必将它嵌套在另一个选择中吗?
  • 但是你已经有一个外部查询

标签: sql oracle performance long-running-processes


【解决方案1】:

试试这个:-

在 day_sum_eff 列上创建索引,然后再次运行查询,看看执行时间是否有任何变化。

这可能是工作。

【讨论】:

    【解决方案2】:

    试试这个

    WITH q1 AS (
    SELECT  
          day_sum_eff, 
          cusip,
          ticker_symb, 
          clos_prc, 
          nclos_prc,
          case
               when clos_prc is null and nclos_prc is not null 
               then (nclos_prc - LAG( nclos_prc ignore nulls) OVER (ORDER BY 
    cusip)) 
               when  clos_prc is not null and nclos_prc is null
               then LEAD( nclos_prc ignore nulls) OVER (ORDER BY cusip)- LAG( 
    nclos_prc ignore nulls) OVER (ORDER BY cusip)
              else NULL
              end DIFF
        from  MKTDATA.MARKET_DAILY_SUMMARY 
        WHERE day_sum_eff >=  '1-JUN-2017' and 
              day_sum_eff <=  '10-JUN-2017' )
    )
    SELECT ticker_symb, 
           day_sum_eff, 
           cusip, 
           clos_prc, 
           nclos_prc,
           diff
      FROM q1
    ORDER BY  day_sum_eff_,fmr_iss_cusip OFFSET 0 ROWS FETCH NEXT 3 ROW ONLY
    

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2011-08-27
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多