2016-09-21 109 views
1

我有此UPDATE查詢改變的記錄的狀態(狀態= 1)在videos表對於具有的10個記錄在thumbnail表計數,其中thubmnails.status爲1所有id_videoUPDATE查詢是很慢的

由於縮略圖表的大尺寸,查詢的工作原理非常緩慢。有關如何提高查詢速度的任何建議?

UPDATE videos 
SET videos.status = 1 
WHERE videos.id_video IN (SELECT thumbnails.id_video 
          FROM thumbnails 
          WHERE thumbnails.status = 1 
          GROUP BY thumbnails.id_video 
          HAVING Count(thumbnails.id_thumbnail) = 10) 
     AND videos.status = 2; 

索引對於id_video設置兩個表中& id_thumbnail的縮略圖表。並且還索引status列。

+1

如何在'thumbnails.status'和'videos.status'上添加索引 –

+0

如果這不起作用,請將「explain select ...」的輸出與表定義一起添加到您的問題中。 –

+0

對不起,我忘了提及,有'thumbnails.status'和'videos.status'上的索引。更新後。 – user2980769

回答

1

我通過使查詢更加去除IN子句簡單

UPDATE videos v 
SET v.status = 1 
WHERE v.status = 2 AND (
    SELECT COUNT(t.id_thumbnail) 
    FROM thumbnails t 
    WHERE t.id_video = v.id_video AND t.status = 1 
) = 10; 

視頻v記錄當它們具有狀態2並且比它們具有10個縮略圖t並且具有視頻標識爲v和狀態1時更新。

0

什麼如果轉換IN第一個JOIN條件像

UPDATE videos v 
JOIN (SELECT thumbnails.id_video 
          FROM thumbnails 
          WHERE thumbnails.status = 1 
          GROUP BY thumbnails.id_video 
          HAVING Count(thumbnails.id_thumbnail) = 10) xxx 
ON v.id_video = xxx.id_video 
SET v.status = 1 
WHERE v.status = 2; 
0

您也可以嘗試使用exists代替IN:

UPDATE videos 
SET videos.status = 1 
WHERE videos.status = 2 
AND EXISTS (SELECT thumbnails.id_video 
      FROM thumbnails 
      WHERE thumbnails.status = 1 
      GROUP BY thumbnails.id_video 
      HAVING Count(thumbnails.id_thumbnail) = 10 
      AND thumbnails.id_video = videos.id_video);