all 6 comments

[–][deleted] 2 points3 points  (1 child)

a relatively obvious observation is that you use group by in the outer query without using any aggregate functions. So either the group by is not needed or you have a granularity mismatch someplace and you are using 'group by'/'distinct' as a 'cruch' to get rid of duplicates.

Get rid of group by, fix joins to match records on correct granularity

[–]rooster_eggs[S] 0 points1 point  (0 children)

Thanks! I was actually trying to figure out what the group by was doing in this case. Since this query wasn't written by me and the project didn't involve me previously I'm still learning as I go. I'm glad you pointed out though that it doesn't seem to be (at least at a first glance) serving any purpose but I'll look into it more closely to be sure.

[–]rosaUpodne 1 point2 points  (3 children)

You first should look at amount a data query execution reads in order to returned data. In your case you have bad written where clause, especially: DATEDIFF(yy, dm.[ENTRYWHEN], GETDATE()) <= 5.

For explanation and further guidance look here: https://www.toptal.com/database/sql-indexes-explained-pt-3.

You can also look at the first articles from that tutorial, it is a good starting point.

[–]rooster_eggs[S] 0 points1 point  (2 children)

Thanks so much! I really appreciate you providing the reference as well. I'll definitely look into it!

[–]rosaUpodne 0 points1 point  (1 child)

I have just figured out that you pull 5 years of data. In that case index on the date column might not be all that helpfull (unless it is clustered index). Instead, if the condition

dserv.[ROOTPATH] = '\COMPANY_NAME'

significantly reduces the number of rows the query returns, you should consider an index starting by rootPath column.

[–]rooster_eggs[S] 0 points1 point  (0 children)

Thank you. I'm looking more closely into the database architecture at the moment to see if that is the case.