How does table alias names affect performance?

前端 未结 3 2011
暖寄归人
暖寄归人 2020-11-27 21:25

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.\'

相关标签:
3条回答
  • 2020-11-27 21:54

    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.

    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%' ;
    
    0 讨论(0)
  • 2020-11-27 22:06

    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. Compared to the rest of the operations needed to execute your query, this is almost nothing.

    0 讨论(0)
  • 2020-11-27 22:17

    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 e.g. 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. 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.

    0 讨论(0)
提交回复
热议问题