I have a problem with CROSS APPLY with a parameterized table function. Here is a simplified pseudo-code example:
SELECT * FROM ( SELECT lor.* FROM LOT_OF_ROWS_TABLE lor WHERE ... ) AS lor CROSS APPLY dbo.HeavyTableValuedFunction(lor.ID) AS htvf INNER JOIN ANOTHER_TABLE AS at ON lor.ID = at.ID WHERE ...
- The internal selection in the
LOT_OF_ROWS_TABLE table returns many rows. - The
LOT_OF_ROWS_TABLE and ANOTHER_TABLE returns only one or more rows. - A table-dependent function takes a lot of time and when calling a large number of rows, the selection lasts a very long time.
My problem:
The function is called for all rows returned from LOT_OF_ROWS_TABLE , regardless of the fact that the data will be limited with a simple ANOTHER_TABLE .
The choice should be in the format shown - it is generated and in fact it is much more dense.
When I try to rewrite it, it can be very fast, but it cannot be rewritten as follows:
SELECT * FROM ( SELECT lor.* FROM LOT_OF_ROWS_TABLE lor WHERE ... ) AS lor INNER JOIN ANOTHER_TABLE AS at ON lor.ID = at.ID CROSS APPLY dbo.HeavyTableValuedFunction(at.ID) AS htvf WHERE ...
I'd like to know:
Is there any parameter or tooltip or something that makes the call function only for permanently restricted lines?
Thanks.
EDIT:
The tabular rating function is very complex: http://pastebin.com/w6azRvxR . The choice we are talking about is "user configured" and generated: http://pastebin.com/bFbanY2n .
Pavel hodek
source share