2009-10-16 89 views
0

當我在以下子查詢中包含2條註釋掉的行時,似乎需要等到我的Sybase 12.5 ASE服務器獲得任何結果爲止。沒有這兩行,查詢運行正常。這個分組有什麼問題?爲什麼在子查詢中分組會導致問題

select days_played.day_played, count(distinct days_played.user_id) as OLD_users 
from days_played inner join days_received 
on days_played.day_played = days_received.day_received 
and days_played.user_id = days_received.user_id 
where days_received.min_bulk_MT > days_played.min_MO 
and days_played.user_id in 

(select sgia.user_id 
from days_played as sgia 
where sgia.day_played < days_played.day_played 
--group by sgia.user_id 
--having sum(sgia.B_first_msg) = 0 
) 

group by days_played.day_played 

回答

0

確定我發現我有什麼問題是 包括子查詢的用戶ID:「凡days_played.user_id = sgia.user_id and sgia.day_played < days_played.day_played「

+1

問題在於,在這種情況下使用'exists'比使用'in'更好,因爲您必須使用索引。 – 2009-10-16 13:56:18

0

通過使用showplan來顯示解釋,找出查詢的功能。

在這種情況下你不能通過使它成爲主要查詢的一部分來消除子查詢嗎?

0

你可以嘗試重寫查詢如下?

select days_played.day_played, 
     count(distinct days_played.user_id) as OLD_users 
    from days_played 
inner join days_received on days_played.day_played = days_received.day_received 
         and days_played.user_id = days_received.user_id 
where days_received.min_bulk_MT > days_played.min_MO 
    and 0 = (select sum(sgia.B_first_msg) 
       from days_played as sgia 
      where sgia.user_id = days_played.user_id 
       and sgia.day_played < days_played.day_played 
      ) 
group by days_played.day_played 

我想這應該給你更好的性能...

+0

非常感謝Hosam Aly! – 2009-10-16 11:13:31

+0

不客氣。 :)我很好奇它做了多少改進...... – 2009-10-16 11:23:59

相關問題