2015-08-29 86 views
-1

當我在遠程數據庫上執行此腳本時,它給我提供查詢超時錯誤。我增加了數據庫的超時時間,但仍然存在此錯誤。我被告知如果我能夠優化腳本,使其變得簡單,它可能會工作。查詢超時增加,但腳本執行失敗

SELECT TOP 8 MIN(CASE WHEN pic_alb_love.pic=users_pics.pic 

AND pic_alb_love.email = '[email protected]' THEN 'User'  ELSE 'Guest' END)AS answer_one, 

MIN (CASE WHEN favorites.pic=users_pics.pic AND favorites.email = '[email protected]' THEN 'good' ELSE 'Bad' 
    END)AS answer2, 

    (CASE WHEN RTRIM (users_pics.upload_type) = 'wow' THEN 'loaded' ELSE   

    CASE WHEN RTRIM (users_pics.upload_type)= 'hey' THEN 'added' ELSE  

    CASE WHEN RTRIM (users_pics.upload_type) = 'check' THEN 'Changed' END END END)as up_ans,  

    (CASE WHEN RTRIM (users_pics.upload_type) = 'sample1' THEN 'new' ELSE  

    CASE WHEN RTRIM (users_pics.upload_type) = 'sample2' THEN 'existing'  ELSE 

    CASE WHEN RTRIM (users_pics.upload_type) = 'sample3' THEN 'Profile Picture'  END END END) as exs, 

    COUNT(DISTINCT users_pics.pic) as total,RTRIM (users_pics.wardrobe) as wardrobe, 



fname,users_pics.wardrobe, 

    MIN (make)as make,MIN (htags)as htags, RTRIM (profile.profile_id) as profile_id, 

    users_pics.email,profile.profile_pix, RTRIM (profile.gender) as gender, 

    users_pics.time_group,profile.fpage,up_user_id, MIN (u_pic_id) as u_pic_id, MIN (users_pics.pic) as pic 
FROM users_pics 

LEFT join profile on users_pics.email = profile.email 

LEFT join favorites on users_pics.pic = favorites.pic 

LEFT JOIN pic_alb_love on users_pics.pic = pic_alb_love.pic 

left join friends on users_pics.email = friends.resp_email 


WHERE req_email = '[email protected]' and pic_enable='enable' or pic_view='Public' 

GROUP BY users_pics.upload_type,profile.fname,profile.profile_id,users_pics.wardrobe, 

users_pics.email, profile.gender,users_pics.time_group,profile.profile_pix, profile.fpage,up_user_id 


ORDER BY MIN (users_pics.u_pic_id) DESC 
+0

請確保您的查詢未被阻止,您可以選擇它查詢DMV或安裝'sp_whoisactive' – lad2025

回答

0

增加超時可以提供幫助,但是您還應該檢查您的查詢是否未被其他操作(如INSERT/UPDATE或打開事務)阻止。

最簡單的方法是安裝和使用sp_whoisactive程序。

第二你不需要窩案像你這樣:

(CASE WHEN RTRIM (users_pics.upload_type) = 'wow' THEN 'loaded' ELSE   

    CASE WHEN RTRIM (users_pics.upload_type)= 'hey' THEN 'added' ELSE  

    CASE WHEN RTRIM (users_pics.upload_type) = 'check' THEN 'Changed' END END END)as up_ans, 

CASE RTRIM (user_pics.upload_type) 
WHEN 'wow' THEN 'loaded' 
WHEN 'hey' THEN 'added' 
WHEN 'check' THEN 'changed' 
ELSE NULL /* or your value like 'unknown' */ 
END AS up_ans 

接下來的事情:你RTRIM幾乎在每一個字符串值,你應該插入過程中淨化你的輸入,除非你需要空格/製表符/換行符等等。 這樣你的查詢將不需要0​​,並可以利用索引,如果存在任何。

/* New values */ 
INSERT INTO table_name(...) VALUES (LTRIM(RTRIM(value...))) 

/* Existing ones */ 
UPDATE table_name 
SET col = LTRIM(RTRIM(col)) 

SQL解析器會明白牆上的文字的,人類將需要時間來做到這一點。

我知道我們可以爭論代碼風格,但請記住您爲人們編寫代碼。好可讀的代碼讓你更早發現錯誤,這是一個地獄更容易在未來保持你和你的繼任者:

1)一個選擇的值一行

2)選擇和分組順序相同BY

3)在結束

4)彙總列可以使用別名不需要完全合格的名稱

5)沒有明確的列名稱,始終指定從哪個表

6)SQL語句大寫字母

7)Allign代碼

你的查詢更可讀來源:

SELECT TOP 8 
    [up_user_id] /* Always add from which table even if it is unique column name, because in future you may get ambigous column */ 
    ,[fname] 
    ,[profile_id] = RTRIM(profile.profile_id) 
    ,[up_ans]  = CASE RTRIM(users_pics.upload_type) 
        WHEN 'wow' THEN 'loaded' 
        WHEN 'hey' THEN 'added' 
        WHEN 'check' THEN 'changed' 
        ELSE NULL 
        END 
    ,[exs]  = CASE RTRIM(users_pics.upload_type) 
        WHEN 'sample1' THEN 'new' 
        WHEN 'sample2' THEN 'existing' 
        WHEN 'sample3' THEN 'Profile Picture' 
        ELSE NULL 
        END 
    ,[wardrobe]  = RTRIM(users_pics.wardrobe) 
    ,users_pics.email 
    ,[gender]   = RTRIM(profile.gender) 
    ,users_pics.time_group 
    ,profile.profile_pix 
    ,profile.fpage 
    ,[answer_one]  = MIN(CASE 
          WHEN pic_alb_love.pic=users_pics.pic THEN 'User' 
          ELSE 'Guest' 
          END) 
    ,[answer2]  = MIN(CASE 
          WHEN favorites.pic = users_pics.pic AND favorites.email = '[email protected]' WHEN 'good' 
          ELSE 'Bad' 
         END) 
    ,[total]   = COUNT(DISTINCT users_pics.pic) 
    ,[make]   = MIN(make) 
    ,[htags]   = MIN(htags) 
    ,[u_pic_id]  = MIN(u_pic_id) 
    ,[pic]   = MIN(users_pics.pic) 
FROM users_pics  /* you can use alias like AS up */ 
LEFT JOIN profile 
    ON users_pics.email = profile.email 
LEFT JOIN favorites 
    ON users_pics.pic = favorites.pic 
LEFT JOIN pic_alb_love 
    ON users_pics.pic = pic_alb_love.pic 
LEFT JOIN friends 
    ON users_pics.email = friends.resp_email 
WHERE 
     req_email = '[email protected]' 
    AND pic_enable = 'enable' 
    OR pic_view = 'Public' 
GROUP BY      
    up_user_id 
    ,profile.fname 
    ,profile.profile_id 
    ,users_pics.upload_type 
    ,users_pics.wardrobe 
    ,users_pics.email 
    ,profile.gender 
    ,users_pics.time_group 
    ,profile.profile_pix 
    ,profile.fpage 
ORDER BY MIN(users_pics.u_pic_id) DESC 

後您檢查選擇的數據您在不堵塞查詢可以考慮一下:

  • 檢查表格上的索引
  • 添加WHERE條件來獲取較小的集合,也許你可以使用一些update_date > current_date - 2 weeks
  • 認爲優化查詢,因爲現在它分組和排序需要時間來完成。
  • WHERE條件,你確定它不應該是:

WHERE (req_email = '[email protected]' 
AND pic_enable = 'enable') 
OR pic_view = 'Public'