以不同的值在行上连接两个相同的表

问题描述:

我有两个相同的表,其中一个具有行的当前值,另一个具有新值。我试图只选择新值表中的行,其中新值表中的任何列的值与旧值表中的列的值不同。我正在使用的查询如下所示:以不同的值在行上连接两个相同的表

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是否有更好的方法来做到这一点,或者是否有人知道更高效的查询?

+0

如果您的数据对于此类操作来说太大而不是使用查询使用过程。但为此,你必须焚烧你的油。 – Ankit 2011-05-11 04:42:29

+0

每个表中有多少行?什么是索引定义? – 2011-05-11 04:46:48

+0

每表大约12,000行没有索引,我是否应该为每个这些字段添加一个索引? – chris 2011-05-11 04:48:21

用途:

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)) 

所有列的指标进行比较。

+0

嗯,我要去测试这个,看看它是否比我更快,但我不断收到一个SQL错误'你的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在'第10行'附近使用正确的语法,错误位于AND附近,但我无法弄清楚这个查询有什么问题 – chris 2011-05-11 05:27:06

+0

ahh错过了关闭paren – chris 2011-05-11 05:28:05

+0

@Chris:Try现在 - 它缺少一个支架 – 2011-05-11 05:28:15

如果item_id(sku)是相当唯一的(表中没有多个重复的item_id),那么只需在item_id上添加一个索引即可获得显着的性能提升。