Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询比较_Sql - Fatal编程技术网

SQL查询比较

SQL查询比较,sql,Sql,问题1 Tables A and B in action. A [commonfield, otherfields] B [commonfield] 问题2 select A.otherfields from A,B where A.commonfield = B.commonfield and some filters ( A.commonfield ) 查询1相当于查询2。在(a)内存使用和(b)速度方面,哪一个更好?最好的

问题1

Tables A and B in action.
A [commonfield, otherfields]
B [commonfield]
问题2

select
        A.otherfields
from
        A,B
where
        A.commonfield = B.commonfield
        and some filters ( A.commonfield )

查询1相当于查询2。在(a)内存使用和(b)速度方面,哪一个更好?

最好的ANSI标准方法是

问题3:

另一种可能性:

select
        A.otherfields
from
        A
inner join
        B ON A.commonfield = B.commonfield
where
        some filters ( A.commonfield )
Where exists在SQL Server中的速度往往最快,但并非在所有数据库中都最快


我会测试所有的可能性(除了使用隐式联接的可能性,隐式联接本身就是一种糟糕的编程技术,除非您的数据库太旧,无法使用显式联接,否则不应使用隐式联接))确定particualr数据库和设置的最佳性能。

这两个查询将在执行前编译

性能将取决于SQL Server供应商对查询编译器的实现

一个供应商可以将两者优化为要执行的相同代码

我使用的是SQLServer2000,这两个表达式生成的代码都具有相同的性能

查询1不等同于查询2

当B中有多个匹配项时,Query1将返回多行。查询2将只返回A中的行,没有重复项

因为它们不会返回等价的结果集,所以我认为比较它们的性能不是一个好主意


但是,查询1可能会执行得更好,因为它没有执行重复消除。另一方面,如果您有适当的索引,则两者可能相似,或者查询2可能更好。

-旧样式的逗号分隔表列表样式因ANSI-92 SQL标准(20多年前)内存使用而中断。第二个查询更好,由于您已经限制了对B的选择,然后根据您查询A的结果进行选择。请进入21世纪,停止使用隐式联接。它们是SQL反模式,更难维护,更容易出错。性能问题可能取决于您使用的数据库。因此,在知道答案之前,我们无法回答这个问题。使用oracle,感谢您的建议。
select
        A.otherfields
from
        A
inner join
        B ON A.commonfield = B.commonfield
where
        some filters ( A.commonfield )
SELECT
        A.otherfields
FROM
        A
WHERE EXISTS (SELECT * FROM B WHERE A.commonfield = B.commonfield)
        AND some filters ( A.commonfield )