[英]Join two identical tables on rows with different values
我有兩個相同的表,一個具有行的當前值,另一個具有新值。 我試圖僅從新值表中選擇行,其中新值表中的任何列都具有與舊值表中的列不同的值。 我正在使用的查詢現在看起來像:
SELECT `new`.`item_id`
FROM `new_items` AS `new`
JOIN `items` AS `old`
WHERE new.item_id = old.item_id
AND (new.price != old.price ||
new.description != old.description ||
new.description_long != old.description_long ||
new.image_small != old.image_small ||
new.image_large != old.image_large ||
new.image_logo1 != old.image_logo1 )
但是,此查詢花費的時間太長,無法執行。 MySQL是否有更好的方法來做到這一點,還是有人知道更有效的查詢?
采用:
SELECT n.item_id
FROM NEW_ITEMS n
WHERE EXISTS (SELECT NULL
FROM OLD_ITEMS o
WHERE o.item_id = n.item_id
AND (o.price <> n.price
OR o.description <> n.description
OR o.description_long <> n.description_long
OR o.image_small <> n.image_small
OR o.image_large <> n.image_large
OR o.image_logo1 <> n.image_logo1))
索引所有要比較的列。
如果item_id(sku)相當唯一(表中沒有很多重復的item_id),那么通過在item_id上添加索引,您將看到性能上的顯着提高。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.