ANSI JOIN 与非 ANSI JOIN 查询的性能会不同吗?

2023-07-18数据库问题
1

本文介绍了ANSI JOIN 与非 ANSI JOIN 查询的性能会不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在大约 7000 行 T-SQL 存储过程中有我的业务逻辑,其中大部分都有 next JOIN 语法:

I have my business-logic in ~7000 lines of T-SQL stored procedures, and most of them has next JOIN syntax:

SELECT A.A, B.B, C.C
FROM aaa AS A, bbb AS B, ccc AS C
WHERE
    A.B = B.ID
AND B.C = C.ID
AND C.ID = @param

如果我用这个替换这样的查询,我会得到性能增长吗:

Will I get performance growth if I will replace such query with this:

SELECT A.A, B.B, C.C
FROM aaa AS A
JOIN bbb AS B
   ON A.B = B.ID
JOIN ccc AS C
   ON B.C = C.ID
   AND C.ID = @param

或者它们是一样的?

推荐答案

这两个查询是一样的,除了第二个是 ANSI-92 SQL 语法,第一个是没有合并 join 子句的旧 SQL 语法.它们应该生成完全相同的内部查询计划,尽管您可能想检查一下.

The two queries are the same, except the second is ANSI-92 SQL syntax and the first is the older SQL syntax which didn't incorporate the join clause. They should produce exactly the same internal query plan, although you may like to check.

出于多种原因,您应该使用 ANSI-92 语法

You should use the ANSI-92 syntax for several of reasons

  • JOIN 子句的使用分隔关系逻辑从过滤逻辑(WHERE),因此更清晰、更容易理解.
  • 这个特定查询无关紧要,但在某些情况下,旧的外连接语法(使用 + )不明确,因此查询结果取决于实现 - 或者根本无法解析查询.这些在 ANSI-92 中不会发生
  • 这是一种很好的做法,因为现在大多数开发人员和 dba 都将使用 ANSI-92,您应该遵循该标准.当然,所有现代查询工具都会生成 ANSI-92.
  • 正如@gbn 所指出的,它确实倾向于避免意外的交叉连接.

我自己 我抵制 ANSI-92 有一段时间了,因为旧语法在概念上有一点优势,因为它更容易将 SQL 设想为所有使用的表的大规模笛卡尔连接,然后是过滤操作 - 一种心理技术可用于掌握 SQL 查询在做什么.然而,几年前我决定我需要与时俱进,经过一段相对较短的调整期后,我现在非常喜欢它——主要是因为上面给出的第一个原因.唯一应该背离 ANSI-92 语法或不使用该选项的地方是使用隐含危险的自然连接.

Myself I resisted ANSI-92 for some time as there is a slight conceptual advantage to the old syntax as it's a easier to envisage the SQL as a mass Cartesian join of all tables used followed by a filtering operation - a mental technique that can be useful for grasping what a SQL query is doing. However I decided a few years ago that I needed to move with the times and after a relatively short adjustment period I now strongly prefer it - predominantly because of the first reason given above. The only place that one should depart from the ANSI-92 syntax, or rather not use the option, is with natural joins which are implicitly dangerous.

这篇关于ANSI JOIN 与非 ANSI JOIN 查询的性能会不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12