2015-03-25 54 views
1

我想選擇所有沒有與其關聯的樣式的圖片。我有一張樣式表,可以將樣式與其圖片進行映射。以下是我現在的查詢:重寫查詢以避免不進入並提升性能

SELECT picture.id, picture.caption, picture.lowresimageurl, picture.medresimageurl 
        FROM instagram_shop_picture picture 
        INNER JOIN instagram_shop shop ON shop.id = picture.shop_id 
        WHERE picture.deletedAt IS NULL 
         AND picture.isLocked = 0 
         AND picture.isShown = 1 
         AND picture.isTestimonial = 0 
         AND shop.deletedAt IS NULL 
         AND shop.isLocked = 0 
         AND shop.expirydate IS NOT NULL 
         AND shop.expirydate > now() 
         AND picture.id NOT IN (SELECT style.picture_id FROM instagram_picture_style style) 

此查詢由於某種原因運行緩慢。我怎麼能改寫這個不必IN

回答

0
SELECT picture.id, picture.caption, picture.lowresimageurl, picture.medresimageurl 
        FROM instagram_shop_picture picture 
        INNER JOIN instagram_shop shop ON shop.id = picture.shop_id 
        left join instagram_picture_style style on style.picture_id = picture.id 
        WHERE picture.deletedAt IS NULL 
         AND picture.isLocked = 0 
         AND picture.isShown = 1 
         AND picture.isTestimonial = 0 
         AND shop.deletedAt IS NULL 
         AND shop.isLocked = 0 
         AND shop.expirydate IS NOT NULL 
         AND shop.expirydate > now() 
         and style.picture_id is null 

不改變使用從子查詢,左連接在instagram_picture_style表並添加新條件style.picture_id is null

Joinwill be faster thansubquery

+0

考慮了表決: ) – Backtrack 2015-03-25 04:50:30