简体   繁体   English

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

[英]Slow query when connecting to linked server

I've got this query 我有这个问题

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

This takes 23 seconds to select and update one row 这需要23秒来选择和更新一行

But if I use openquery (which I don't want to) then it only takes half a second. 但是如果我使用openquery(我不想这样做)那么它只需要半秒钟。

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. 我不想使用openquery的原因是我可以安全地为我的查询添加参数,并且可以安全地进行SQL注入。

Does anyone know of any reason for it to be running so slowly? 有谁知道它运行这么慢的任何原因?

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'

If you're looking for the why , here's a possibility from Linchi Shea's Blog : 如果您正在寻找原因 ,可以参考Linchi Shea的博客

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. 如果链接服务器是SQL Server的实例,要获取所有可用统计信息,用户必须拥有该表,或者是sysadmin固定服务器角色,db_ownerfixed数据库角色或linkedserver上的db_ddladmin固定数据库角色的成员。

(Because of Linchi's post, this clarification has been added to the latest BooksOnline SQL documentation). (由于Linchi的帖子,这个澄清已被添加到最新的BooksOnline SQL文档中)。

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. 换句话说,如果使用具有有限权限的用户设置链接服务器,则SQL无法检索表的准确统计信息,并且可能选择执行查询的不良方法,包括检索所有行。

Here's a related SO question about linked server query performance. 这是关于链接服务器查询性能的相关SO问题 Their conclusion was: use OpenQuery for best performance. 他们的结论是:使用OpenQuery获得最佳性能。

Update: some additional excellent posts about linked server performance from Linchi's blog. 更新:Linchi博客中关于链接服务器性能的一些其他优秀帖子

Is column1 primary key? 是column1的主键吗? 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. 尝试使用主键(其中PK_field = xxx)选择要更新的记录,否则(有时?)将读取所有记录以查找要更新的记录的PK。

Is column1 a varchar field? column1是varchar字段吗? Is that why are you surrounding the value 1234 with single-quotation marks? 这就是为什么你用单引号围绕值1234? Or is that simply a typo in your question? 或者这只是你问题中的拼写错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM