views:

62

answers:

2

I am joining a table that has two record id fields (record1, record2) to a view twice--once on each record--and selecting the top 1000. The view consists of several rather large tables, and it's id field is a string concatenation of their respective Ids (this was necessary for some third party software that requires a unique ID for the view. Row numbering was abysmally slow). There is also a where clause in the view calling a function that compares dates.

The estimated execution plan produces a "No Join Predicate" warning unless I use OPTION(FORCE ORDER). With forcing the ordering, the execution plan has multiple nodes displaying 100% cost. In both cases, the estimated subtree cost at the endpoint is thirteen orders of magnitude smaller than just one of it's nodes (it's doing a lot or nested loop joins with cpu costs as high 35927400000000)

What is going on here with the numbers in the execution plan? And why is SQL Server having such a hard time optimizing the query?

Simply adding an index to the view on the concatenated string and using the NOEXPAND table hint fixed the problem entirely. It ran in all of 12 seconds. But why did sql stumble so bad (even requiring the noexpand hint after I added the index)?

Running SQL Server 2008 SP1 with CU 8.

The View:

SELECT
    dbo.fnGetCombinedTwoPartKey(N.NameID,A.AddressID) AS NameAddressKey,
    [other fields]

FROM     
    [7 joined tables]
WHERE dbo.fnDatesAreOverlapping(N.dtmValidStartDate,N.dtmValidEndDate,A.dtmValidStartDate,A.dtmValidEndDate) = 1

The Query

SELECT TOP 1000
    vw1.strFullName,
    vw1.strAddress1,
    vw1.strCity,
    vw2.strFullName,
    vw2.strAddress1,
    vw2.strCity
FROM tblMatches M
JOIN vwImportNameAddress vw1 ON vw1.NameAddressKey = M.Record1 
JOIN vwImportNameAddress vw2 ON vw2.DetailAddressKey = M.Record2 
+1  A: 

It would have to parse your function (fnGetCombinedTwoPartKey) to determine what columns are fetched to create the result column. It can't so it's going to assume all columns are necessary. If your indexes are covering indexes then your estimate is going to be wrong.

Jay
+1  A: 

Looks like you're already pretty close to the explanation. It's because of this:

The view consists of several rather large tables, and it's id field is a string concatenation of their respective Ids...

This creates a non-sargable join predicate condition, and prevents SQL server from using any of the indexes on the base tables. Thus, the engine has to perform a full scan of all the underlying tables for each join (two in your case).

Perhaps in order to avoid doing several full table scans (one for each table, multiplied by the number of joins), SQL Server has decided that it will be faster to simply use the cartesian product and filter afterward (hence the "no join predicate" warning). When you FORCE ORDER, it dutifully performs all of the full scans and nested loops that you originally asked it for.

I do agree with some of the comments that this view is underlying a problematic data model, but the short-term workaround, as you've discovered, is to index the computed ID column in the view, which (obviously) makes it sargable again because it has hashes of the actual generated ID.


Edit: I also missed this on the first read-through:

WHERE dbo.fnDatesAreOverlapping(N.dtmValidStartDate,N.dtmValidEndDate,A.dtmValidStartDate,A.dtmValidEndDate) = 1

This, again, is a non-sargable predicate which will lead to poor performance. Wrapping any columns in a UDF will cause this behaviour. Indexing the view also materializes it, which may also factor into the speed of the query; without the index, this predicate has to be evaluated every time and forces a full scan on the base tables, even without the composite ID.

Aaronaught
I'm curious, though, why sql server doesn't properly take advantage of the index without the noexpand hint. Looks like I should chalk up the bad numbers in the estimate as sql not being able to handle a very bad query on very large datasets. Similarly for the plan itself.
Brian
@Brian: In the absence of evidence to the contrary, I would probably blame out-of-date statistics. If you have to use `NOEXPAND` then it means that the optimizer thinks it will be cheaper to query the base tables instead of using the index on the view; the only reasons I can think of for that are either (a) more non-sargable predicates that aren't shown in your final query examples, or (b) the optimizer thinks that the base table queries will be way cheaper than they really are (which is usually due to bad statistics). If you're sure that the predicates are fine, try `sp_updatestats`.
Aaronaught
Oh - it could also be the result of a non-covering index. If the materialized view doesn't actually have all of the necessary output columns, then it effectively has to join the view to every single base table, which it might deem to be very expensive. Make sure you're using `INCLUDE` properly on your index.
Aaronaught
I think I have a pretty good idea on the general opinion of keeping the UDF and relying on the hint vs replacing with the underlying logic (which gets very verbose as the number of date ranges that must be overlapping increases)...
Brian