Check if two “select”s are equivalent

前端 未结 4 604
忘掉有多难
忘掉有多难 2020-12-15 17:22

Is there a way to check if two (non-trivial) select are equivalent?

Initially I was hoping for a formally equivalence between two selects, but the answers in proving

4条回答
  •  独厮守ぢ
    2020-12-15 17:39

    For

    (select * from query1 EXCEPT select * from query2)
     UNION ALL 
    (select * from query2 EXCEPT select * from query1)
    

    I did some trial on postgres 9.4, and here are my results.

    [1] Minus is not supported,so need to use EXCEPT as told by @Bogdan

    [2] Using only EXCEPT does not consider duplicates so had to use EXCEPT ALL

    [3] EXCEPT ALL require that column order in the resultant should be same so in above query QUERY1 and QUERY2 should either return the same column order or we have to wrap the query and make sure the column order are same.(may be this happens in application logic)

    So i think if we keep above 3 points in mind we may be 100% sure that the data returned by two queries on the given data set is exactly the same.

    will update if i come across more edge case which may fail.

提交回复
热议问题