连接到链接服务器时查询速度慢
我有这个查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个替代方案。在远程服务器上创建一个存储过程来执行更新,然后从本地实例调用该过程。
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.
如果您正在寻找原因,这里有一个可能来自Linchi Shea 的博客:
(由于 Linchi 的帖子,此说明已添加到最新的 BooksOnline SQL 文档中)。
换句话说,如果链接服务器设置的用户权限有限,则 SQL 无法检索表的准确统计信息,并且可能会选择较差的方法来执行查询,包括检索所有行。
这是关于链接服务器查询性能的相关SO问题。他们的结论是:使用 OpenQuery 以获得最佳性能。
更新:一些关于链接服务器性能的其他优秀帖子来自林奇的博客。
If you're looking for the why, here's a possibility from Linchi Shea's Blog:
(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.
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.
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?