MSSQL how to improve the performance of database Association query

Source: Internet
Author: User
Tags mssql

MSSQL How to improve the Database Tutorial Association query performance

--> test data: [A1]
If object_id (' [A1] ') is not null drop table [A1]
CREATE TABLE [A1] ([ID] int,[sortid1] int,[sortid2] int,[info] varchar (5))
Insert [A1]
Select 1,1,3, ' info ' UNION ALL
Select 2,2,4, ' Info2 '

--> test data: [B2]
If object_id (' [B2] ') is not null drop table [B2]
CREATE TABLE [B2] ([SortID] int,[name] varchar (5))
Insert [B2]
Select 1, ' Categorize 1 ' union ALL
Select 2, ' Categorize 2 ' union ALL
Select 3, ' Categorize 3 ' union ALL
Select 4, ' Category 4 '

SELECT * FROM [A1]
select * FROM [B2]


Select A1.ID,SORTID1,B2.NAME,A1.SORTID2,B.NAME,A1. [INFO]
From dbo.a1
Left Join b2 on a1.sortid1 = B2.sortid
Left join B2 b on a1.sortid2 = B.sortid

/*
ID SORTID1 name Sortid2 name info
----------- ----------- ----- ----------- ----- -----
1 1 Classification 1 3 Classification 3 Info
2 2 Classification 2 4 classification 4 Info2

(2 rows affected)
*/

Original method

Select Id,rortid1,b.name as name1, sortid2,c.name as Name2,info
From a LEFT join B on A.rortid1=b.rortid
Left join B c on A.rortid2=b.rortid

A more practical method

Views can simplify operations and do not improve performance. You can try to establish an indexed view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.