简体   繁体   English

在SQL中,是不是查询越短,运行越快

[英]In SQL, is it true that the shorter the query, the faster it run

I'm very curious to know because when I run these two query in MySQL the shorter run faster我很想知道,因为当我在 MySQL 中运行这两个查询时,运行时间越短越快

SELECT FirstName, LastName, City, State 
FROM Person
LEFT JOIN Address ON Person.PersonId = Address.PersonId;

and

SELECT FirstName, LastName, City, State 
FROM Person
LEFT OUTER JOIN Address ON Person.PersonId = Address.PersonId;

In addition, I want to ask if the same thing happen in the other RDBMS such as Posrgres, MS SQL Server, Oracle and SQLite?另外,我想问一下Posrgres、MS SQL Server、Oracle和Z497757A9C5B2EC17DED656170B51C788等其他RDBMS中是否发生同样的事情?

LEFT OUTER JOIN is just a synonym for LEFT JOIN , and the execution plan for both of your queries should be identical. LEFT OUTER JOIN只是LEFT JOIN的同义词,两个查询的执行计划应该相同。 Therefore, I would attribute any difference in performance to things other than the use of OUTER in the left join syntax.因此,我会将性能上的任何差异归因于除了在左连接语法中使用OUTER之外的其他因素。 You should check what other tasks might be running on your database during the test, and also what other processes might be running on the OS.您应该检查在测试期间您的数据库上可能正在运行哪些其他任务,以及操作系统上可能正在运行哪些其他进程。

First, performance of a query is not based on how long it is.首先,查询的性能不取决于它的长度。 There are many "obvious" reasons for this.这有很多“明显”的原因。 But perhaps the most is that the size of the data is determined by the size of tables in the FROM clause, and that has nothing to do with how many characters are in the name.但也许最多的是,数据的大小是由FROM子句中表的大小决定的,这与名称中的字符数无关。

Second, LEFT JOIN and LEFT OUTER JOIN are synonyms for each other.其次, LEFT JOINLEFT OUTER JOIN是同义词。 No doubt, the OUTER takes a new compute cycles more on a modern computer to parse.毫无疑问, OUTER在现代计算机上需要更多的新计算周期来解析。 But the difference would be measured in fractions of a microsecond (probably) and are pretty immeasurable.但是差异将以微秒(可能)的几分之一为单位进行测量,并且是无法测量的。

Some of the things that affect the performance of queries are:影响查询性能的一些因素是:

  • The size of the data.数据的大小。
  • Operations such as GROUP BY AND ORDER BY . GROUP BYORDER BY等操作。
  • JOIN s, particularly without indexes. JOIN s,尤其是没有索引的情况下。
  • And much, much more.还有很多很多。

No, you cannot correlate the length of a SQL statement's text to its execution duration.不,您不能将 SQL 语句文本的长度与其执行持续时间相关联。 It's more accurate to correlate a statement's execution duration to its execution plan with respect to the cardinality of the plan's row source operations.相对于计划的行源操作的基数,将语句的执行持续时间与其执行计划相关联更为准确。

Existence proof:存在证明:

Take a billion row table, T, with a single column primary key, P取一个具有单列主键 P 的十亿行表 T

The full table scan query全表扫描查询

select * from T

will take longer to execute than the unique index scan query (followed by a table access by index rowid):将比唯一索引扫描查询花费更长的时间执行(随后是按索引 rowid 访问表):

select * from T where P = :pval

Except for some odd maintenance operations (eg global index rebuild over a partitioned table), the shorter query will no doubt take longer to execute than the longer query.除了一些奇怪的维护操作(例如,在分区表上重建全局索引),较短的查询无疑会比较长的查询花费更长的时间来执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM