在具有不同值的行上连接两个相同的表
我有两个相同的表,一张包含行的当前值,另一张包含新值。我试图仅选择新值表中的行,其中新值表中的任何列的值与旧值表中的列的值不同。我现在使用的查询如下所示:
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 是否有更好的方法来做到这一点,或者有人知道更有效的查询吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用途:
对所有正在比较的列建立索引。
Use:
Index all of the columns being compared.
如果 item_id (sku) 相当唯一(表中没有很多重复的 item_id),那么只需在 item_id 上添加索引,您就会看到性能的巨大提升。
If the item_id (sku) is fairly unique (not with many duplicates item_id in the tables), then you will see big performance improvement by just adding an index on item_id.