简体   繁体   English

表别名如何影响性能?

[英]How does table alias names affect performance?

While reading about tuning SQL queries, I read somewhere that 'Always use table alias and prefix all column names with the aliases when you are using more than one table.'在阅读有关调整 SQL 查询的信息时,我在某处读到“在使用多个表时,始终使用表别名并在所有列名前加上别名。”

How does table alias names affect performance?表别名如何影响性能? Or Do they actually affect?或者他们真的有影响吗?

The alias doesn't affect performance in any practical or measurable way at all (italics added on edit).别名根本不会以任何实际或可衡量的方式影响性能(编辑时添加斜体)。 That is, it would add a barely (if it all) measurable delay to query compilation.也就是说,它会为查询编译添加几乎(如果全部)可测量的延迟。 Once compiled (and re-used), it has no effect.一旦编译(并重新使用),它就没有效果。

An alias removes ambiguity when you have more than one table because you know which table it comes from.当您有多个表时,别名可以消除歧义,因为您知道它来自哪个表。 It also prevents future table changes from breaking a query.它还可以防止将来的表更改破坏查询。 Say, you add an audit column to one table where it already exists in another table.假设您将审计列添加到一个表中,而该列已存在于另一个表中。 Queries without aliases using both tables will break.使用两个表的没有别名的查询将中断。

An alias is also mandatory in some cases eg schema bound views.在某些情况下,别名也是必需的,例如模式绑定视图。

The SQL parsing engine (that reads all queries before executing them, and uses the information to cache the compiled queries in the future for faster execution) is the only thing that looks at the aliases, and uses it to help remove ambiguities in symbol lookups. SQL 解析引擎(在执行之前读取所有查询,并使用这些信息在将来缓存已编译的查询以加快执行速度)是唯一查看别名的东西,并使用它来帮助消除符号查找中的歧义。 The system would already produce symbols, just like any other compilable statement in any other language, when it's being parsed prior to execution-storage.当在执行存储之前对其进行解析时,系统将已经生成符号,就像任何其他语言中的任何其他可编译语句一样。

Almost not at all, the performance impact is negligible, but you'll have a much better time reading the query.几乎不是,性能影响可以忽略不计,但您将有更好的时间阅读查询。 It's just for your convenience.这只是为了您的方便。

The performance impact is allocating a few kb of memory to store alias names, etc. in the SQL Server program itself.性能影响是分配几 kb 的内存来存储 SQL Server 程序本身中的别名等。 Compared to the rest of the operations needed to execute your query, this is almost nothing.与执行查询所需的其余操作相比,这几乎算不了什么。

It certainly can.它当然可以。 I came here from Google and this was the first result.我从谷歌来到这里,这是第一个结果。 And all I can say is the accepted answer given here might be incorrect.我只能说这里给出的公认答案可能不正确。 And it all comes down to how you use aliases.这一切都取决于您如何使用别名。 In my case, ORM was generating an alias for every column in the table.在我的例子中,ORM 正在为表中的每一列生成一个别名。 And the performance decrease was significant.并且性能下降显着。 Double execution time to be precise.准确地说是双倍的执行时间。

PostgreSQL example version 15: PostgreSQL 示例版本 15:


Query generated by ORM: ORM 生成的查询:

SELECT id AS id_0, uuid AS uuid_1, login AS login_2, service_status AS service_status_3, customer AS customer_4, cli AS cli_5, description AS description_6, address AS address_7, reference AS reference_8, postcode AS postcode_9, chain_id AS chain_id_10, interface_name AS interface_name_11, interface_ip AS interface_ip_12, tariff AS tariff_13, term_length AS term_length_14, contract_start_date AS contract_start_date_15, additional_data AS additional_data_16, observium_port_id AS observium_port_id_17, status AS status_18, location_lat AS location_lat_19, location_lon AS location_lon_20 FROM internet_service

Resulted in:结果:

"Seq Scan on internet_service  (cost=0.00..106.03 rows=503 width=1493) (actual time=0.010..0.204 rows=503 loops=1)"
"Planning Time: 0.058 ms"
"Execution Time: 0.230 ms"

Query without generating aliases:不生成别名的查询:

SELECT * FROM internet_service

Resulted in:结果:

"Seq Scan on internet_service  (cost=0.00..106.03 rows=503 width=1493) (actual time=0.007..0.097 rows=503 loops=1)"
"Planning Time: 0.054 ms"
"Execution Time: 0.119 ms"

I have experience with alias the query take significantly more time compare to without alias.我有使用别名的经验,与没有别名的情况相比,查询花费的时间要长得多。

I have experience this with PostgreSQL, my query are following.我有 PostgreSQL 的经验,我的查询如下。

Without Alias无别名

select
    applicant.application_id,
    form_data,
    application_defect.defect_id
from
    applicant
INNER JOIN
    audit_trail on applicant.email = audit_trail.email
inner join
    application_defect on applicant.application_id = application_defect.application_id
where
    application_defect.defect_id like '1%'
and
    form_data like '%FaceApi%';

With Alias使用别名

select
    ap.application_id,
    au.form_data,
    ad.defect_id
from
    applicant ap
INNER JOIN
    audit_trail au on ap.email = au.email
inner
    join application_defect ad on ap.application_id = ap.application_id
where
    ad.defect_id like '1%'
and
    form_data like '%FaceApi%';

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

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