SQL Server 表与索引的同义词
我在 SQL Server 2005 的单个实例上有多个数据库。我在一个数据库上创建了一个同义词来访问另一个数据库上的表,并且在编写查询时,我想使用特定的索引,但是,在评估执行计划,它似乎没有使用它。如果我编写查询来显式访问数据库,它可以工作,但我似乎无法使用同义词让它工作。例如:
select *
from testdb..testtable with (index(testindex))
|--Nested Loops(Inner Join, OUTER REFERENCES:([testdb].[dbo].[testtable].[id]))
|--Index Scan(OBJECT:([testdb].[dbo].[testtable].[testindex]))
|--Clustered Index Seek(OBJECT:([testdb].[dbo].[testtable].[PK_testtable]), SEEK:([testdb].[dbo].[testtable].[id]=[testdb].[dbo].[testtable].[id]) LOOKUP ORDERED FORWARD)
不会产生与同义词相同的执行计划
select *
from testdb_synonym with (index(testindex))
|--Clustered Index Scan(OBJECT:([testdb].[dbo].[testtable].[PK_testtable]))
这是同义词的限制还是我需要做一些特定的事情才能使其正常工作?
I have multiple databases on a single instance of SQL Server 2005. I've created a synonym on one database to access a table on another database and when writing my queries, I'd like to utilize a specific index, however, when evaluating the execution plan, it doesn't appear to use it. If I write the query to access the database explicitly, it works, but I can't seem to get it to work using a synonym. For example:
select *
from testdb..testtable with (index(testindex))
|--Nested Loops(Inner Join, OUTER REFERENCES:([testdb].[dbo].[testtable].[id]))
|--Index Scan(OBJECT:([testdb].[dbo].[testtable].[testindex]))
|--Clustered Index Seek(OBJECT:([testdb].[dbo].[testtable].[PK_testtable]), SEEK:([testdb].[dbo].[testtable].[id]=[testdb].[dbo].[testtable].[id]) LOOKUP ORDERED FORWARD)
does not yield the same execution plan as
select *
from testdb_synonym with (index(testindex))
|--Clustered Index Scan(OBJECT:([testdb].[dbo].[testtable].[PK_testtable]))
Is this a limitation with Synonyms or is there something specific I need to do to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 Microsoft 已修复的错误:请参阅 MS KB 963684
This is a bug that Microsoft have fixed: see MS KB 963684
我测试了同样的事情,似乎查询优化器在通过同义词完成时忽略了该提示。详细信息是我对任意表执行了 select * 操作,并带有索引提示以使用非聚集索引。如果没有同义词,它会执行书签查找/嵌套循环连接。有了它,它就会进行表扫描。由于创建同义词语法上没有选项,我只能假设索引提示被忽略。 BOL 中没有详细说明原因。我将其称为“功能”。
I tested the same thing and it seems that the query optimizer ignores that hint when done via a synonym. The details are I did a select * against an arbitrary table with an index hint to use a non-clustered index. Without the synonym, it does a bookmark lookup/nested loop join. With it, it does a table scan. Since there are no options on the create synonym syntax, I can only assume that the index hint is ignored. No details in BOL as to why. I would chalk it up as a "feature".
同义词的
WITH INDEX
提示似乎被忽略。尽管我的架构中没有名为
wow_i_can_write_everything_here
的索引,但编译并运行正常。WITH INDEX
hints seems to be ignored for synonyms.compiles and runs allright despite the fact I don't have an index named
wow_i_can_write_everything_here
in my schema.您的案例需要提示吗? MS 建议尽可能避免索引提示,因为这可能会使更优化的计划失效。即使今天优化了,明天由于数据加载等原因可能会效率低下。
我尝试在 SQL Server 2008 中使用没有提示的同义词,并得到了与完全限定名称(database.schema.txt)相同的执行计划。桌子)。
我什至尝试使用带有索引提示的同义词,并成功强制执行非聚集索引查找(以及键查找以获取其余数据),并且我得到了具有完全限定名称的相同执行计划。
您的统计数据更新了吗?您是否有选择性索引,或者 SQL Server 是否认为使用表扫描更有效。
Do you need the hint in your case? MS recommendations is to avoid index hints if it is possible due to the fact that may invalidate a more optimized plan. Even if it is optimized today it may be inefficiens tomorrow due to data load etc.
I tried to use a synonym without the hint in SQL server 2008 and got the same execution plan with the synonym as with the fully qualified name (database.schema.table).
I even tried to use the synonym with an index hint and successfully forced a non clustered index seek (and a key lookup to get the rest of the data), and i get the same execution plan with fully qualified name.
Are your statisitics updated? Do you have a selective index or does SQL server think it is more efficient to use a table scan.