Proving SQL query equivalency

后端 未结 9 2428
一整个雨季
一整个雨季 2020-12-15 05:33

How would you go about proving that two queries are functionally equivalent, eg they will always both return the same result set.


As I had a specific query in

9条回答
  •  情歌与酒
    2020-12-15 06:01

    The best you can do is compare the 2 query outputs based on a given set of inputs looking for any differences. To say that they will always return the same results for all inputs really depends on the data.

    For Oracle one of the better if not best approaches (very efficient) is here (Ctrl+F Comparing the Contents of Two Tables):
    http://www.oracle.com/technetwork/issue-archive/2005/05-jan/o15asktom-084959.html

    Which boils down to:

    select c1,c2,c3, 
           count(src1) CNT1, 
           count(src2) CNT2
      from (select a.*, 
                   1 src1, 
                   to_number(null) src2 
              from a
            union all
            select b.*, 
                   to_number(null) src1, 
                   2 src2 
              from b
           )
    group by c1,c2,c3
    having count(src1) <> count(src2);
    

提交回复
热议问题