Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I'm using the following query (as stored procedure) in a news portal, the problem is this query is taking very very high execution time and it's considered as expensive query, i need help optimizing it.

SQL
Select Top(@Count) site_News.NewsID, site_News.Title, count(site_Replies.ReplyID) as 'replies-count'
From site_News Inner Join site_Replies
on site_News.NewsID = site_Replies.NewsID
Inner Join site_Categories on site_News.CategoryID = site_Categories.CategoryID
Where site_news.AddDate > DATEADD (day , -7 , GETDATE() ) And site_Replies.Accept = 1 And site_Replies.Active = 1
And site_Categories.LanguageID = @LanguageID
group by site_News.NewsID, site_News.Title
order by 'replies-count' desc



The query aims to get the news with highest number of active replies, the news must be added in the past 7 days.
Thanks in advance.
Posted
Updated 29-Dec-13 2:12am
v2

You just make sure following columns are indexed. If any one them is not indexed then pls create indexes for those columns and re execute your query in your database.
1.site_News.NewsID
2.site_Replies.NewsID
3.site_News.CategoryID
4.site_Categories.CategoryID
5.site_news.AddDate
6.site_Categories.LanguageID
7.site_Replies.Accept
8.site_Replies.Active
 
Share this answer
 
Comments
WaleedMuh 29-Dec-13 9:33am    
I added index to these columns, as i didn't have index on most of them, and it worked like a charm.
SQL server CPU usage dropped to ~ 3% from 90% !
Thanks!
S. M. Ahasan Habib 29-Dec-13 16:30pm    
you are most welcome
This is quite often asked question, but the answer is: No one can do it for you.

There are several things to do to improve query performance[^].
 
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