开发者

In MySQL (5.1) what is best way to detect one set of columns is not equal to another

I have two tables a source and a destination. I want to flag the destination table to be updated if any of the corresponding columns in source columns are different. The columns can be null.

Currently this seems very clumsy:

UPDATE destination d
JOIN source s
   ON d.id = s.id
SET d.updateFlag = 1
WHERE (    (d.col1 IS NULL AND s.col1 IS NOT NULL) 
        OR (d.col1 IS NOT NULL AND s.col1 IS NULL)
        OR (d.col1 <> s.col1)
      )
      OR
      (    (d.col2 IS NULL AND s.col2 IS NOT NULL) 
        OR (d.col2 IS NOT NULL AND s.col2 IS NULL)
        OR (d.col2 <> s.col2)
      )
      ...etc...
      OR
      (    (d.colN IS NULL AND s.colN IS NOT NULL) 
        OR (d.colN IS NOT NULL AND s.c开发者_如何学编程olN IS NULL)
        OR (d.colN <> s.colN)
      )

Ideally I could do something like:

UPDATE destination d
JOIN source s
   ON d.id = s.id
SET d.updateFlag = 1
WHERE HASH(d.col1,d.col2,...etc...,d.colN) <> HASH(s.col1,s.col2,...etc...,s.colN)

Some additional info. The columns are all different data types (some ints, some bits, some strings) and we are using a flavor of MySQL 5.1.


You can simplify your statement by using the NULL-safe equal to operator:

UPDATE destination d
JOIN source s
   ON d.id = s.id
SET d.updateFlag = 1
WHERE !(d.col1 <=> s.col1)
   OR !(d.col2 <=> s.col2)
...etc

As an alternative, you can use a UNION to find duplicate rows:

  SELECT tbl, id, col1, col2, col3
    FROM (
  SELECT 't1' AS tbl, id, col1, col2, col3
    FROM t1
   UNION ALL
  SELECT 't2' AS tbl, id, col1, col2, col3
    FROM t2
       ) tmp
GROUP BY id, col1, col2, col3 HAVING COUNT(*) = 1;

You can then use the result from that in your update query:

UPDATE destination d
   SET d.updateFlag = 1
 WHERE EXISTS (
    SELECT NULL FROM (
        SELECT id, col1, col2, col3 FROM t1
        UNION All
        SELECT id, col1, col2, col3 FROM t2
     ) tmp
     WHERE d.id = tmp.id
  GROUP BY id, col1, col2, col3
    HAVING COUNT(*) = 1
)


You might want to check out mk-table-checksum, which is one of the tools in Maatkit. This does what you're describing, calculating a checksum for the entire row and comparing it to the corresponding row in another database.

Many people use this tool to verify that a slave is a true replica of its master, but you can compare any two databases that have similar metadata.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜