Click here to Skip to main content
15,885,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


SQL
SELECT * FROM GETFINALCTE CTE
    LEFT OUTER JOIN
    (
       SELECT COUNT(city_name) YTD, mvmt.override_payee_id PAYEEID
       --,STP.city_name CITY_NAME
       FROM dbo.movement mvmt
       INNER JOIN dbo.stop stp ON mvmt.id=stp.movement_id
       WHERE STP.city_name ='CHICAGO' -- @origincity
       GROUP BY mvmt.override_payee_id
    ) AS X ON X.PAYEEID = CTE.CARRIERID AND X.PAYEEID IS NOT NULL



This is my join query and the query returns around 800 rows and is taking a bit of time
i.e 20 seconds.

If i run the query individually
i.e SELECT * FROM GETFINALCTE CTE and

SQL
SELECT COUNT(city_name) YTD, mvmt.override_payee_id PAYEEID
      --,STP.city_name CITY_NAME
      FROM dbo.movement mvmt
      INNER JOIN dbo.stop stp ON mvmt.id=stp.movement_id
      WHERE STP.city_name ='CHICAGO' -- @origincity
      GROUP BY mvmt.override_payee_id



separately, each one gives 1200 and 144 rows respectively and takes only 1 seconds to retrieve the rows.

But when i join these two as shown in my first query it is taking 20 seconds to retrieve the rows.

Can anyone tell me the reason why this is happening. Is it the COUNT() function in join that is creating the problem?

What is causing this increase in time only after join even though both query returns limit number of rows?

Can anyone guide me on the necessary changes that i could make to optimize the query and retrieve the data faster may be 1 second.

Thanks in advance!!
Posted
Comments
AndrewCharlz 24-Feb-15 6:25am    
SELECT * FROM GETFINALCTE CTE
LEFT OUTER JOIN
(
SELECT COUNT(1) YTD, mvmt.override_payee_id PAYEEID
FROM dbo.movement mvmt
left outer JOIN dbo.stop stp ON mvmt.id=stp.movement_id
WHERE stp.movement_id is not null and STP.city_name ='CHICAGO'
GROUP BY mvmt.override_payee_id
) AS X ON X.PAYEEID = CTE.CARRIERID AND X.PAYEEID IS NOT NULL
AndrewCharlz 24-Feb-15 6:26am    
try the above
_Asif_ 24-Feb-15 6:59am    
Are PAYEEID and CARRIERID columns are indexed? if not do create an index and re-execute the query
Tomas Takac 24-Feb-15 10:03am    
Why you are using left join and then add condition 'X.PAYEEID IS NOT NULL'? Why not to use inner join directly?
deepakdynamite 25-Feb-15 7:24am    
Is it solved? if no then please write you complete query in question

1 solution

@andrewCharlz,_Asif,Tomas,deepak,aravinth04

Thanks guys for your replies!!

Sorry i didnt have the time to implement your fix.

But did a fix of my own,

i put the result set from

SQL
SELECT COUNT(city_name) YTD, mvmt.override_payee_id PAYEEID
      --,STP.city_name CITY_NAME
      FROM dbo.movement mvmt
      INNER JOIN dbo.stop stp ON mvmt.id=stp.movement_id
      WHERE STP.city_name ='CHICAGO' -- @origincity
      GROUP BY mvmt.override_payee_id


the above query into a temp table and joined it on the temp table, that solved the issue.

Thanks for your reply.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900