Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I writing code here i am executing this query it take 10 sec time. present in my db only 4 thousand records are there....in future 50 thousand records in db it will take more time to execute how can i solve query faster ....?
this is my select query.... is there any chance to optimization this query...?

SQL
CREATE TABLE #Temp(UserId int);
INSERT INTO #Temp(UserId) SELECT DISTINCT(UserId) FROM tblUserPost WHERE visibility=2;

SELECT 
    UP.UserId,
    UP.FileId,
    UP.Text,
    UP.FileType,
    UP.IsChekin,
    UP.CreatedOn,
    UP.ModifiedOn,
    UP.ID ,
    UF.FileName,
    UF.MimeType,
    UF.FileSize,
    UF.MediaType,
    UF.FilePath,
    UF.ThumbnailUrl,
    (SELECT COUNT(*) FROM tblLike L WHERE L.PostId=UP.ID),
    (SELECT COUNT(*) FROM tblComment C WHERE C.PostId =UP.ID),
    (CASE
    WHEN (SELECT COUNT(*) FROM tblLike LK WHERELK.PostId= UP.ID AND LK.UserId = @UserId ) > 0 THEN  CAST(1 as bit) 
    ELSE CAST(0 as bit)  END) AS IsLike ,
    UP.isShare,
    UP.ShareText,
    UP.OwnerId,
    UP.ShareTime,
    UP.CheckinLatitude,
    UP.CheckinLongitude,
    UP.Address,
    UP.Checkinurl,0,0,UF.ThumbHeight,UF.ThumbWidth

FROM #Temp T 

INNER JOIN tblUserPost UP ON UP.UserId=T.UserId
LEFT JOIN OUTER JOIN tblPostFile UF ON UF.ID =UP.FileId

WHERE  UP.visibility=2

ORDER BY UP.ID DESC;
Posted
Updated 20-Jun-14 1:07am
v2
Comments
King Fisher 20-Jun-14 8:06am    
better you avoid subqueries.

Start the query analyzer in sql studio. It can help you optimize and also will tell you what part of the query is most expensive.

Good luck!
 
Share this answer
 
Drop the temp table and add an index on tblUserPost(visibility,ID).
The temp table is joined with its own basetable using the same condition, so it's redundant, but now the optimizer can't really use an eventual index on tblUserPost.visibility anymore .
 
Share this answer
 
Comments
Gujula Ravindra Reddy 24-Jun-14 1:34am    
Thanks for u r replay......

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