如何证明2个sql语句是等价的

时间:2011-02-01 21:51:40

标签: sql theory performance

我打算用连接和子语句重写一个复杂的SQL语句,并获得一个更简单的语句。我通过在同一数据集上运行并获得相同的结果集来测试它。一般来说,我如何(从概念上)证明任何给定数据集中的2个语句是相同的?

1 个答案:

答案 0 :(得分:0)

我建议学习关系代数(正如Mchl所指出的那样)。如果您想认真考虑优化查询和正确设计数据库,那么这是您需要的最重要的概念。

但是,如果您有足够的数据可以测试,我会建议使用一种丑陋的暴力方法来帮助您确保正确的结果:创建两个版本的视图(使比较更容易管理)并比较结果。我的意思是

create view original as select xxx yyy zzz;
create view new as select xxx yyy zzz;
-- If the amount differs something is quite obviously very wrong
select count(*) from original;
select count(*) from new;
-- What is missing from the new one?
select *
from original o
where not exists (
 select * 
 from new n
 where o.col1=n.col2 and o.col2=n.col2 --and so on
);
-- Did something extra appear?
select *
from new o
where not exists (
 select *
 from old n
 where o.col1=n.col2 and o.col2=n.col2 --and so on
)

正如其他人在评论中指出的那样,您可以将这两个查询提供给您正在使用的产品的优化器。大多数情况下,你会得到一些可以用人类解析的东西,执行路径的完整图纸以及子查询对性能的影响等。通常使用像

这样的东西来完成。
explain plan for 
select * 
from ...
where ...
etc