CROSS APPLY - drives me mad

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 1
Reputation: FSMk45U4su2F is an unknown quantity at this point 
Solved Threads: 0
FSMk45U4su2F FSMk45U4su2F is offline Offline
Newbie Poster

CROSS APPLY - drives me mad

 
0
  #1
Apr 22nd, 2008
Allright.

I have two queries, which provide the exact same result, yet the simpler one is roughly 100 times slower than the more complicated one.

I have absolutely no idea what is going on here.

And let me add, there is no recursion so this is no idiocyncracy of the query execution plan.

These are the queries:

  1. SELECT dub.*
  2. FROM
  3. (
  4. SELECT top 1 path
  5. FROM tblFamily
  6. WHERE 0 = 0
  7. AND lvl = 3
  8. ) AS sub
  9. CROSS apply
  10. (
  11. SELECT path
  12. FROM tblFamily
  13. WHERE 0 = 0
  14. AND id IN
  15. (
  16. SELECT number
  17. FROM dbo.util_list2tbl (sub.path)
  18. )
  19. ) AS dub
  20.  
  21. SELECT lub.*
  22. FROM
  23. (
  24. SELECT top 1 path
  25. FROM tblFamily
  26. WHERE 0 = 0
  27. AND lvl = 3
  28. ) AS sub
  29. CROSS apply
  30. (
  31. SELECT number
  32. FROM dbo.util_list2tbl (sub.path)
  33. ) AS dub
  34. CROSS apply
  35. (
  36. SELECT path
  37. FROM tblFamily
  38. WHERE 0 = 0
  39. AND id = dub.number
  40. ) AS lub

1. tblFamily has a primary key on path, which is a condensed path of [(ancestor_id,)*this_id,]
There are no problems with that whatsoever in the various procedures accessing it.
2. util_list2tbl is simply Erland Sommarskog's table-valued function which splits a comma-delimited string og numbers into a table of (number, position_in_list)*.
No problems with that either.

As already mentioned, the first query shows up in the execution plan with 99% of the load, and the latter with 1%.
It takes 1:45 minutes on my home computer.

This is the output from io and time:
  1. SQL Server parse AND compile time:
  2. CPU time = 0 ms, elapsed time = 1 ms.
  3.  
  4. (3 row(s) affected)
  5. TABLE '#1367E606'. Scan count 24422, logical reads 24422, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
  6. TABLE 'tblFamily'. Scan count 2, logical reads 183, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
  7.  
  8. (1 row(s) affected)
  9.  
  10. SQL Server Execution Times:
  11. CPU time = 82781 ms, elapsed time = 105673 ms.
  12.  
  13. (3 row(s) affected)
  14. TABLE 'tblFamily'. Scan count 1, logical reads 13, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
  15. TABLE '#1367E606'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
  16.  
  17. (1 row(s) affected)
  18.  
  19. SQL Server Execution Times:
  20. CPU time = 16 ms, elapsed time = 139 ms.
  21. SQL Server parse AND compile time:
  22. CPU time = 0 ms, elapsed time = 1 ms.
  23.  
  24. SQL Server Execution Times:
  25. CPU time = 0 ms, elapsed time = 1 ms.

So I am wondering whether the whole implementation of cross apply is, erm, not-so-good.
Anyone?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum


Views: 2280 | Replies: 0
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC