I have two identical tables, one with current values for rows, and one with new values. I am trying to select only the rows from the new values table where any column in the new values table has a different value than the column in the old values table. The query I am using now looks like:
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 )
However, this query takes WAY too long to execute. Does MySQL have a better way to do this or does anyone know a more efficient query?
Use:
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))
Index all of the columns being compared.
如果item_id(sku)相当唯一(表中没有很多重复的item_id),那么通过在item_id上添加索引,您将看到性能上的显着提高。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.