2012-02-17 41 views
0

我有以下查詢。當我通過查詢底部的陳述刪除訂單時,它會在幾秒鐘內運行。SQL順序通過造成大的運行時間

當我按順序添加命令可能需要幾分鐘。

這個查詢有什麼明顯的錯誤會讓它變慢嗎?

SELECT  TOP (10) addedBy, contactName, title_name, title_short_description,  private, Art_medium, Artist_collection, Art_permanent_collection, dateEntered, dateModified, 
title_translated_description, weblink, type, start_date, end_Date, Subcategory, title_art_description, id, title_name_translated, translated_short_description, pickDate, 
pickOfTheMonth, event_periods, other_categories, rating, cnt, Artists_represented, city, country, county, district, latitude, longitude, openingTimes, Short_description, 
sub_categories, venue_address1, venue_address2, venue_Name, venue_translated_name, title_id, venue_id, isLive, venueLive, tags, invite_only, featured_venue, 
translated_description, venue_addedBy 
FROM  tblTitle_popular AS title 
WHERE  (id NOT IN (SELECT TOP (91) id 
         FROM tblTitle_popular AS title 
         WHERE (0 = 0) 
         AND (start_date >= '27-Jan-2012') 
         AND (start_date <= '31-Dec-2999') 
         AND (isLive = 1) 
         AND (venueLive = 1) 
         AND (private <> 1 OR private IS NULL) 
         ORDER BY pickOfTheMonth DESC, pickDate DESC, featured_venue DESC, start_date, city)) 
AND (0 = 0) 
AND title.start_date >= '27-Jan-2012' 
AND (title.start_date <= '31-Dec-2999') 
AND (title.isLive = 1) 
AND (title.venueLive = 1) 
AND 
(title.private <> 1 OR title.private IS NULL) 
/* 
ORDER BY title.pickOfTheMonth DESC, title.pickDate DESC, title.featured_venue DESC, title.start_date, title.city 
*/ 
+2

可能有些缺失的索引。 – 2012-02-17 16:07:01

+0

'id不在(SELECT ... >>> ORDER BY ... <<<)'是不必要的,但它可能被忽略。 – biziclop 2012-02-17 16:09:44

+0

你可以嘗試使用'ROW_NUMBER'來做這個分頁。 @biziclop - 這是SQL Server或sybase,'ORDER BY'是指定'TOP'的地方。 '@ James' - 請指定RDBMS和版本幷包含執行計劃。 – 2012-02-17 16:10:10

回答

0

假設你的SQL Server 2005 +(如您使用的TOP用括號),你可以嘗試

WITH title 
    AS (SELECT *, 
       ROW_NUMBER() OVER (ORDER BY pickOfTheMonth DESC, 
              pickDate DESC, 
              featured_venue DESC, 
              start_date, 
              city) AS RN 
     FROM tblTitle_popular 
     WHERE (start_date >= '27-Jan-2012') 
       AND (start_date <= '31-Dec-2999') 
       AND (isLive = 1) 
       AND (venueLive = 1) 
       AND (private <> 1 
         OR private IS NULL)) 
SELECT addedBy, 
     contactName, 
     title_name, 
     /*Other Columns*/ 
     venue_addedBy 
FROM title 
WHERE RN BETWEEN 92 AND 101 
ORDER BY RN 

請上傳實際執行計劃的XML版本,如果你想建議,以改善現有查詢的性能。

0

Order by在任何數據庫使查詢更長。看到你的查詢中有2 Order by聲明,我會建議根據這些建立一些索引。