1

I have 2 Sql Server instances, and one query:

SELECT 
    [DetailDescription],
    [SubTotal]
FROM [dbo].[CRR] WITH (INDEX (IX_CORM_CORMId))
WHERE CORM_CORMId >= 5933168 AND CORM_CORMId <= 5955843

This leads to 2 query execution plans: enter image description here enter image description here

Here is uploaded version

One with Sort gets 301740 rows and takes 48s, other gets 286743 rows without Sort takes 5s. One db is a bit outdated copy of other. Order of rows number in table is 98 419 368.

My questions are:

  1. I don't understand why Sort is needed for Nested Loops join? I've tried to disable it with 'OPTION (QUERYTRACEON 2340)'. The option makes no difference at all.
  2. Why is such big differnce in execution time? How to avoid this?

I use Sql Server 2014.

Update:

With STATISTICS IO:

Table 'CynergyResidualRecord'. Scan count 1, logical reads 1226357, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Vladislav Kostenko
  • 1,155
  • 11
  • 18

2 Answers2

3

The sort is to get the rows in order of CynergyResidualRecordId prior to performing the key lookups.

This is an optimisation so the lookups are less random IO discussed here.

It certainly shouldn't take 48 seconds to sort 300K rows - the plan you posted shows the Actual elapsed Time for the sort operator is 281 ms.

The actual elapsed time for the whole plan was 2.496 seconds so I'm not sure where you are getting 48 seconds from. Maybe the 48 second run encountered blocking or there is a problem with your measuring methodology.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • That was interesting, thank you. Even if I'd post the link to the **first** part where it's explained: https://blogs.msdn.microsoft.com/craigfr/2009/02/25/optimizing-io-performance-by-sorting-part-1/ – sepupic Jul 28 '17 at 13:31
  • 2
    Thank you guys, looks like this was a networking issue. I've executed the query on slow instance and it took 3s to complete there. First time I executed queries from remote host. I'm sorry bothering you with this. – Vladislav Kostenko Jul 28 '17 at 16:42
0

I think this happens due to Out-dated statistics. consider updating statistics for this table.

UPDATE STATISTICS [dbo].[CRR];
Hamza Rabah
  • 59
  • 1
  • 3