繁体   English   中英

在具有不同值的行上联接两个相同的表

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM