I have a table that has the details of households and another that has the details of all the persons associated with the households. For the household table I have a primary key defined using two columns in it -
Using the sorting dictated by the clustered indexing on primary keys, I have generated a unique ID for each household
Now, my next step is to associate each person with the corresponding households ie; map a
I have a total of 1928783 household records and 5239842 person records. The execution time is currently very high.
Now, my questions:
The execution plan generated by SQL Server 2008 for the whole script is attached.
By RazorXsr 16 asked Jul 27, 2013 at 07:39 PM
I'm pretty certain the effective table definitions are close to this:
I don't have statistics for these tables or your data, but the following will at least set the table cardinality correct (the page counts are a guess):
Query Plan Analysis
The query you have now is:
This generates the rather inefficient plan:
The main problems in this plan are the hash join and sort. Both require a memory grant (the hash join needs to build a hash table, and the sort needs room to store the rows while sorting progresses). Plan Explorer shows this query was granted 765 MB:
This is quite a lot of server memory to dedicate to one query! More to the point, this memory grant is fixed before execution starts based on row count and size estimates.
If the memory turns out to be insufficient at execution time, at least some data for the hash and/or sort will be written to physical tempdb disk. This is known as a 'spill' and it can be a very slow operation. You can trace these spills (in SQL Server 2008) using the Profiler events Hash Warnings and Sort Warnings.
The estimate for the hash table's build input is very good:
The estimate for the sort input is less accurate:
You would have to use Profiler to check, but I suspect the sort will spill to tempdb in this case. It is also possible that the hash table spills too, but that is less clear-cut.
Note that the memory reserved for this query is split between the hash table and sort, because they run concurrently. The Memory Fractions plan property shows the relative amount of the memory grant expected to be used by each operation.
Why Sort and Hash?
The sort is introduced by the query optimizer to ensure that rows arrive at the Clustered Index Update operator in clustered key order. This promotes sequential access to the table, which is often much more efficient than random access.
The hash join is a less obvious choice, because it's inputs are similar sizes (to a first approximation, anyway). Hash join is best where one input (the one that builds the hash table) is relatively small.
In this case, the optimizer's costing model determines that hash join is the cheaper of the three options (hash, merge, nested loops).
The cost model does not always get it right. It tends to over-estimate the cost of parallel merge join, especially as the number of threads increases. We can force a merge join with a query hint:
This produces a plan that does not require as much memory (because merge join does not need a hash table):
The problematic sort is still there, because merge join only preserves the order of its join keys (tempId, n) but the clustered keys are (tempId, n, sporder). You may find the merge join plan performs no better than the hash join plan.
Nested Loops Join
We can also try a nested loops join:
The plan for this query is:
This query plan is considered the worst by the optimizer's costing model, but it does have some very desirable features. First, nested loops join does not require a memory grant. Second, it can preserve the key order from the
The big drawback with the nested loops plan is that it runs on a single thread. It is likely this query benefits from parallelism, but the optimizer decides there is no advantage in doing that here. This is not necessarily correct either. Unfortunately, there is no built-in query hint to get a parallel plan, but there is an undocumented way:
Enabling trace flag 8649 with the
Now we have a plan that avoids the sort, requires no extra memory for the join, and uses parallelism effectively. You ought to find this query performs much better than the alternatives.
More information on parallelism and the 8649 flag is here: http://sqlblog.com/blogs/paul_white/archive/2011/12/23/forcing-a-parallel-query-execution-plan.aspx
By SQLkiwi ♦ 6.6k answered Jul 28, 2013 at 12:57 AM