3

I have a query in sql server that takes close to 3 minutes to run. Interestingly enough, when I change the order of one of the joins the performance improves dramatically and it takes only about 20 seconds. Also interesting, that when I run it on another server, even the original query takes under 20 seconds.

I would like to find out four things:

  1. Why does the original query take so long on server A (3 minutes)?
  2. Why does reordering the join improve the performance?
  3. Why is it still taking a relatively ling time (20 seconds)?
  4. Why does the original query run so much quicker on server B?

Here are the 3 plans:

Additional note: The first time I ran the original query to completion on server A, it took ~20 minutes. The second time ~15 minutes. After that it has been taking ~3 minutes.

4
  • 1
    You seem to have a lot of views in these queries. What are those views doing? Do you have nested views? Have you updated statistics on your databases? There seems to be some large discrepancies between estimated and actual rows in several places. You also have hash matches all over the place. These are awfully big queries to have nothing more than an execution plan to unravel the performance issues.
    – Sean Lange
    Commented Jan 26, 2023 at 22:15
  • 6
    The links for pastetheplan are the preferred and recommended way of sharing an execution plan for this type of question
    – sTTu
    Commented Jan 26, 2023 at 22:47
  • In plan (a) the slowest bit is the repeated full scans and sorts of AssetValueHistory (that looks to be doing some sort of window function where the predicate on AssetId isn't pushed down to the table but it ends up ranking the entire table 1624 times and cumulatively reading 192,578,792 rows across those 1,624 executions. That subtree takes 2 mins 33 seconds Commented Jan 26, 2023 at 23:27
  • You should update statistics on the server A. Some of used statistics were last updated in 2020 and have nonzero modification count. Commented Jan 27, 2023 at 14:48

1 Answer 1

0

It looks like the first plan is the only plan using parallelism. There are some queries that when they use parallelism, the plan is built by sql to try and load a bunch of data from big tables into memory in parallel. This can sometimes result in a bad plan built because the statistics on the tables force the plan optimizer to grossly overestimate or underestimate certain operations. This is evident in your query since the in the first query, there is an excessive memory grant warning of over 3.5GB memory estimated to be used, and an actual memory usage of only 86MB.

You could try a couple different things or a combination of them.

  • Add OPTION (MAXDOP 1) at the end of your query to remove parallelism for the execution of the query.
  • Rebuild statistics on all tables.
  • Further optimize your query

An example of further optimization would be that you have an inner join to the AccountsFundedDates table, where you select none of those columns or use that table as a linkage to join other tables to. There are some circumstances, such as when columns being joined on are not unique, that writing them as an EXISTS statement below in the WHERE clause can be faster than a join, as doing so can potentially reduce the total number of rows being built in memory by eliminating duplicates prior to the final select. I don't know if this specifically applies to you, but something to consider.

... 
WHERE a.AccountStatus = @accountStatus_Active
      AND EXISTS
(
    SELECT 1
    FROM dbo.AccountsFundedDates f
    WHERE f.AccountId = a.AccountID
          AND f.DateFunded IS NOT NULL
)
      AND a.AccountType NOT IN ( @accountType_Internal, @accountType_Test )
...

You asked why it was different with rearranged joins or ran on a different server - If you change the joins around, sql treats it as a new query and builds a new plan for it with the statistics and information is has available to it, same as how the original query on a different server generated a non-parallel plan as well. A different first execution with different stats or different resources available will result in a plan that sql thinks is best at the time, even if it's bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.