簡體   English   中英

sql棘手的查詢與不同

[英]sql tricky query with distinct

沒有人可以幫我重寫此查詢而沒有明顯的從句嗎?

   select     
       a.rec_type ,
       count(distinct a.a_tel_id) as count_distinct,       
       a.time_key as hour 
   from v_f_view as a with (nolock)
   group by rec_type,time_key

我對執行計划感到困惑,認為該查詢花費的時間太長,我想對其進行優化。

查詢計划: http : //postimage.org/image/p1myi9tw/

您是否已將rec_type,time_key索引為鍵? 這是必須的。

如果a_tel_id有重復項,並且您想將它們計為一個,那么您就無法通過某種方式神奇地刪除distinct來對其進行優化。

您可能嘗試在分組依據的列上添加索引。

指標

您的問題不是DISTINCT子句,而是缺少索引。 可以用兩個GROUP BY子句代替DISTINCT子句,但這很可能會遭受相同的性能損失。

SQL語句

SELECT  a.rec_type
        , COUNT(*) as count_distinct
        , a.time_key as hour
FROM    (        
          SELECT a.rec_type
                  , a.a_tel_id
                  , a.time_key
          FROM    v_f_Logicacdrs21 a        
          GROUP BY
                  a.rec_type
                  , a.a_tel_id
                  , a.time_key
        ) a                  
GROUP BY
        a.rec_type
        , a.time_key

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM