Hi Friends,
Today I faced a issue where the sql query was taking a long time to diplay data. This was because data in the main table had huge volume. The query is something like below
Select A.col1,A.col2,A.col3,A.col4,B.col1,B.col2,C.col1......
from TableA A
left join TableB B
on...
left join TableC C
on....
left Join TableD D
on...
Where
some filters
Date filter
Group By clause
To improve the performance we used CTE
i,e the Table A had 20 lakh records and each left join would take very long time
So a CTE was introduced first to get required data and then do left join
With CTE
As
(
Select A.col1,A.col2,A.col3,A.col4
from TableA A
Where Date Condition
)
Select CTE.col1, CTE.col2, CTE.col3 CTEA.col4,B.col1,B.col2,C.col1......
from TableA A
left join TableB B
on...
left join TableC C
on....
left Join TableD D
on...
Where
some filters
Date filter
Group By clause
The reason why this worked is because SQL query execution happens in the below order
From
Where
Select
GroupBY
OrderBy
So when we put filter in the initial from clause the number of rows returned were less . And then we can apply left joins