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