![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I try to generall supply real DDL and sample data, but the problem I have only shows up in certain cases, with SELECTS against very large tables and I can't supply enough sample data. I have a view (some_view) that joins about eight tables. A query like this: SELECT * FROM some_view WHERE some_column IN ('x', 'y', 'z') Is blazingly fast and yields a very good plan (all SEEKs, no SCANs). But I don't want to build an IN list, and instead would rather join to table that has the "some_column" values for which I want to filter the SELECT on the view. The new query would thus look like this: SELECT * FROM some_view INNER JOIN some_table ON some_view.some_column = some_table.some_column some_table has a primary key on some_column. With the same three values in "some_table" the query goes to hell. The plan ends up SCANing 7 of the 8 tables and it takes forever to return data. Does anyone have some general pointers for me? I know it's difficult without sample data, but if you have some general ideas, I will try them. |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
SELECT * FROM some_view WHERE some_column IN ('x', 'y', 'z') Is blazingly fast and yields a very good plan (all SEEKs, no SCANs). But I don't want to build an IN list, and instead would rather join to table that has the "some_column" values for which I want to filter the SELECT on the view. The new query would thus look like this: SELECT * FROM some_view INNER JOIN some_table ON some_view.some_column = some_table.some_column some_table has a primary key on some_column. With the same three values in "some_table" the query goes to hell. The plan ends up SCANing 7 of the 8 tables and it takes forever to return data. |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
But a reasonable question some may ask me is "why the ROW_NUMBER() function in the first place?" I realize that order is not relationally significant. However, I am working with a commercial system, and unfortunately order IS significant for parts of it, and I can't avoid the issue. The ROW_NUMBER() takes care of the problem. |
|
I think with a hard coded WHERE clause, the ROW_NUMBER() operator was limiting itself to just the values listed in the partition. With the join, the ROW_NUMBER() was operating on the entire result set, even though it should have limited to jus the values in some_table. |
#7
| |||
| |||
|
|
bill (billmacle... (AT) gmail (DOT) com) writes: But a reasonable question some may ask me is "why the ROW_NUMBER() function in the first place?" *I realize that order is not relationally significant. *However, I am working with a commercial system, and unfortunately order IS significant for parts of it, and I can't avoid the issue. *The ROW_NUMBER() takes care of the problem. What problem? You row_number() because you want number rows in your result set, or you use the number to filter later. If you are using row_number() with the intention that you will get a certain order in the output, you have a bug you need to fix. I think with a hard coded WHEREclause, the ROW_NUMBER() operator was limiting itself to just the values listed in the partition. *With the join, the ROW_NUMBER() was operating on the entire result set, even though it should have limited to jus the values in some_table. It is impossible to tell without seeing the query, but adding row_number in a CTE can prevent the optimizer from recasting computation order, since that would affect the result from row_number(). -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
![]() |
| Thread Tools | |
| Display Modes | |
| |