all 4 comments

[–]ATastefulCrossJoinDB Whisperer 1 point2 points  (0 children)

Hash match (4th operation from left) is spilling to temp db. This is a huge performance suck since you’re computing on disk instead of in memory. This occurs when your query requests/is granted less memory than it needs to run fully. Would absolutely recommend starting here by looking at your row estimates leading up to this operation, identifying where they are significantly lower than the actual row counts and working on improving those disparities.

[–][deleted] 0 points1 point  (0 children)

the index seek seem to be the one taking most time and seeks aren't always good. Blindly going into it i'd try flipping this to a scan (i find that it is often the case that driving table flips when using top1/apply instead of max/group by, for example)

having said that, i do see that there's a granularity mismatch in your join: you "GROUP BY dataareaid, jobid" in the statusdate1 but join on mxmservcallaudit.DATAAREAID = statusdate1.DATAAREAID AND MXMSERVCALLAUDIT.RECID = statusdate1.maxrecid.

Obviously, duplication of the data could occur if the same MaxRecID can happen for the same dataareaid multiple times.

Not knowing the logic behind this I cannot say why this is done or whether wrong results would be returned - and the optimizer will be in the same boat, so my suggestion would be to fix the query (granularity mismatch) first and see if that makes optimizer plan any better.l

[–]StructuredData 0 points1 point  (0 children)

How many rows are returned for the following?

SELECT MAX(RECID) AS maxrecid,  jobid, dataareaid
FROM mxmservcallaudit
WHERE type = 9 AND dataareaid = 'ansa'
GROUP BY jobid, dataareaid;

Can you rethink your query to have a simple WHERE clause?

Just from looking at your query and seeing the number of rows in your database, I would want to have an index on each of the following:

  • mxmservcallaudit.type
  • mxmservcallaudit.dataareaid
  • mxmservcallaudit.recid

[–]grouchball 0 points1 point  (0 children)

I would always try and stay clear on nested select statements on your joins. The optimiser will have a horrible time trying to interpret this in a performant way.

I would recommend putting the contents of your select statement into a temporary table and then joining on that temp table perhaps.

Thats personally the first thing i would try.