连接到链接服务器时查询速度慢

发布于 2024-10-03 11:41:26 字数 251 浏览 1 评论 0原文

我有这个查询

UPDATE linkeddb...table SET field1 = 'Y' WHERE column1 = '1234'

这需要 23 秒来选择和更新一行

但是如果我使用 openquery (我不这样做)不想)那么只需要半秒钟。

我不想使用 openquery 的原因是这样我可以安全地向查询添加参数并避免 SQL 注入。

有谁知道它运行如此缓慢的任何原因吗?

I've got this query

UPDATE linkeddb...table SET field1 = 'Y' WHERE column1 = '1234'

This takes 23 seconds to select and update one row

But if I use openquery (which I don't want to) then it only takes half a second.

The reason I don't want to use openquery is so I can add parameters to my query securely and be safe from SQL injections.

Does anyone know of any reason for it to be running so slowly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

陌伤浅笑 2024-10-10 11:41:26

这是一个替代方案。在远程服务器上创建一个存储过程来执行更新,然后从本地实例调用该过程。

/* On remote server */
create procedure UpdateTable
    @field1 char(1),
    @column1 varchar(50)
as
    update table
        set field1 = @field1
        where column1 = @column1
go

/* On local server */
exec linkeddb...UpdateTable @field1 = 'Y', @column1 = '1234'

Here's a thought as an alternative. Create a stored procedure on the remote server to perform the update and then call that procedure from your local instance.

/* On remote server */
create procedure UpdateTable
    @field1 char(1),
    @column1 varchar(50)
as
    update table
        set field1 = @field1
        where column1 = @column1
go

/* On local server */
exec linkeddb...UpdateTable @field1 = 'Y', @column1 = '1234'
樱娆 2024-10-10 11:41:26

如果您正在寻找原因,这里有一个可能来自Linchi Shea 的博客

创建最佳查询计划
您正在使用链接上的表格
服务器,查询处理器必须有
数据分布统计
链接服务器。有限制的用户
对任何列的权限
表可能没有足够的
获得所有有用的权限
统计数据,并且可能会收到更少的
高效的查询计划和体验
表现不佳。如果链接的
server 是 SQL Server 的一个实例,
获取所有可用的统计数据,
用户必须拥有该表或者是成员
sysadmin 固定服务器角色的
db_owner固定数据库角色,或
db_ddladmin 固定数据库角色
链接服务器。

(由于 Linchi 的帖子,此说明已添加到最新的 BooksOnline SQL 文档中)。

换句话说,如果链接服务器设置的用户权限有限,则 SQL 无法检索表的准确统计信息,并且可能会选择较差的方法来执行查询,包括检索所有行。

这是关于链接服务器查询性能的相关SO问题。他们的结论是:使用 OpenQuery 以获得最佳性能。

更新:一些关于链接服务器性能的其他优秀帖子来自林奇的博客。

If you're looking for the why, here's a possibility from Linchi Shea's Blog:

To create the best query plans when
you are using a table on a linked
server, the query processor must have
data distribution statistics from the
linked server. Users that have limited
permissions on any columns of the
table might not have sufficient
permissions to obtain all the useful
statistics, and might receive aless
efficient query plan and experience
poor performance. If the linked
serveris an instance of SQL Server, to
obtain all available statistics, the
user must own the table or be a member
of the sysadmin fixed server role, the
db_ownerfixed database role, or the
db_ddladmin fixed database role on the
linkedserver.

(Because of Linchi's post, this clarification has been added to the latest BooksOnline SQL documentation).

In other words, if the linked server is set up with a user that has limited permissions, then SQL can't retrieve accurate statistics for the table and might choose a poor method for executing a query, including retrieving all rows.

Here's a related SO question about linked server query performance. Their conclusion was: use OpenQuery for best performance.

Update: some additional excellent posts about linked server performance from Linchi's blog.

江湖彼岸 2024-10-10 11:41:26

column1 是主键吗?可能不会。尝试使用主键(其中 PK_field=xxx)选择要更新的记录,否则(有时?)将读取所有记录以查找要更新的记录的 PK。

Is column1 primary key? Probably not. Try to select records for update using primary key (where PK_field=xxx), otherwise (sometimes?) all records will be read to find PK for records to update.

夜吻♂芭芘 2024-10-10 11:41:26

column1 是 varchar 字段吗?这就是为什么你要用单引号将值 1234 引起来吗?或者这只是您问题中的一个拼写错误?

Is column1 a varchar field? Is that why are you surrounding the value 1234 with single-quotation marks? Or is that simply a typo in your question?

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