SQL Server 表与索引的同义词

发布于 2024-08-03 08:18:59 字数 774 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱桃奶球 2024-08-10 08:18:59

这是 Microsoft 已修复的错误:请参阅 MS KB 963684

在 Microsoft SQL Server 2005 中,您
为表创建同义词。你跑
针对同义词的查询。查询
使用 INDEX 优化器提示强制
一个索引。如果你检查执行情况
为查询生成的计划,
你可能会发现执行计划确实
不使用强制索引。

This is a bug that Microsoft have fixed: see MS KB 963684

In Microsoft SQL Server 2005, you
create a synonym for a table. You run
a query against the synonym. The query
uses the INDEX optimizer hint to force
an index. If you examine the execution
plan that is generated for the query,
you may find the execution plan does
not use the forced index.

慢慢从新开始 2024-08-10 08:18:59

我测试了同样的事情,似乎查询优化器在通过同义词完成时忽略了该提示。详细信息是我对任意表执行了 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".

静谧 2024-08-10 08:18:59

同义词的 WITH INDEX 提示似乎被忽略。

CREATE SYNONYM syn_master FOR master

SELECT  *
FROM    syn_master WITH (INDEX (wow_i_can_write_everything_here))

尽管我的架构中没有名为 wow_i_can_write_everything_here 的索引,但编译并运行正常。

WITH INDEX hints seems to be ignored for synonyms.

CREATE SYNONYM syn_master FOR master

SELECT  *
FROM    syn_master WITH (INDEX (wow_i_can_write_everything_here))

compiles and runs allright despite the fact I don't have an index named wow_i_can_write_everything_here in my schema.

淡忘如思 2024-08-10 08:18:59

您的案例需要提示吗? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文