简体   繁体   English

SQL Server链接服务器性能

[英]SQL Server linked server performance

I am using SQL Server 2008 Enterprise. 我正在使用SQL Server 2008 Enterprise。 And I am using Linked Server technologies to link another SQL Server 2008 Enterprise instance from another server. 我正在使用链接服务器技术从另一台服务器链接另一个SQL Server 2008 Enterprise实例。 I write TSQL to manipulate objects (eg tables) from both server instances. 我编写TSQL来处理来自两个服务器实例的对象(例如表)。

My question is, for linked server, is there a big performance issue? 我的问题是,对于链接服务器,是否存在很大的性能问题? If yes, what is the key performance bottleneck and best practice we should follow? 如果是,我们应遵循的关键性能瓶颈和最佳实践是什么?

thanks in advance, George 乔治,提前谢谢

My question is, for linked server, is there a big performance issue? 我的问题是,对于链接服务器,是否存在很大的性能问题? If yes, what is the key performance bottleneck and best practice we should follow? 如果是,我们应遵循的关键性能瓶颈和最佳实践是什么?

Compared to what? 相比什么? For what queries? 有什么疑问? of course it all depends on what you are doing. 当然这一切都取决于你在做什么。 For some queries the performance hit will be negligible for others massive. 对于某些查询,对于其他大量的搜索,性能损失可以忽略不计。

There are a bunch of concerns you should keep in mind: 你应该记住一些问题:

  • If you will be joining 2 tables from DB1 to 2 tables from DB2, and the tables are big, stuff can get ugly. 如果你将从DB1加入2个表到DB2的2个表,并且表很大,那么东西就会变得很难看。 At the end of the day, the queries will execute somewhere. 在一天结束时,查询将在某处执行。 The db will have to pull all the results in to the main DB and maintain transactional integrity on the main db. db必须将所有结果提取到主数据库并在主数据库上维护事务完整性。 This can be really expensive. 这可能非常昂贵。
  • If you start running distributed transactions stuff can get ugly , fast. 如果你开始运行分布式事务,那么事情会变得很丑陋 ,快速。
  • When joining stuff across servers your indexes on the remote server can be rendered useless. 当跨服务器连接东西时,远程服务器上的索引可能变得无用。 The data all has to move somewhere for the joins. 所有数据都必须移动到某个连接处。
  • Linked server links can go down at unexpected times and lead to hard to diagnose bugs. 链接的服务器链接可能会在意外时间发生故障并导致难以诊断错误。

In the past I have found situations where it was a few orders of magnitude faster to move the remote data locally, and index it before joining into it. 在过去,我发现在本地移动远程数据的速度要快几个数量级,并在加入之前对其进行索引。

It depends on what you are doing. 这取决于你在做什么。

If you are running queries that join between tables in the two server instances, and transferring large amounts of data, then you have a bottleneck that you need to be aware of. 如果您正在运行在两个服务器实例中的表之间连接的查询,并传输大量数据,那么您有一个需要注意的瓶颈。

If the servers are on their own subnet with a 1GB link, then you should not have to worry a great deal. 如果服务器位于拥有1GB链接的自己的子网上,那么您不必担心太多问题。 I would be concerned if the two servers are connected by a shared, slow link. 如果两个服务器通过共享的慢速链接连接,我会担心。

You're going to take a bit of a hit to transfer the results across the wire twice (linked server to SQL Server to your machine). 你需要点击一下才能将结果传输两次(将服务器连接到SQL Server到你的机器)。 Secondly, it's got to resolve the name and log in, which isn't much of a hit, but it's a hit nonetheless. 其次,它必须解析名称并登录,这不是很重要,但它仍然是一个打击。

Anyway, I've found the only major bottleneck is jumping servers, since it has to transmit the information twice. 无论如何,我发现唯一的主要瓶颈是跳跃服务器,因为它必须传输两次信息。

I use linked servers frequently to synchronise data between environments, mainly because I found it to be the easiest solution to code and manage. 我经常使用链接服务器在环境之间同步数据,主要是因为我发现它是最简单的代码和管理解决方案。

One tip I found, but may not be an option for others, was to run any procedures on the server that has the most data or is doing the most updating/inserting. 我找到的一个提示,但可能不是其他人的选择,是在服务器上运行具有最多数据或正在进行最多更新/插入的任何过程。 For example I have a procedure that compares two tables and inserts/updates from A to B. If I ran this on server A it would take many times longer than running the procedure on B. If you don't have a choice where to run our code, and you are stuck on, say, server A, then this advice may not help. 例如,我有一个过程比较两个表和插入/更新从A到B.如果我在服务器A上运行它,它将比在B上运行过程花费很多倍。如果你没有选择在哪里运行我们的代码,你被卡在服务器A上,然后这个建议可能没有帮助。

Another tip is to reduce the data returned to the minimum necessary. 另一个提示是将返回的数据减少到必要的最小值。 Whereas you might normally have data returned almost instantly on a local server, if a linked server is some distance away then the latency can be very painful. 虽然您通常可以在本地服务器上立即返回数据,但如果链接服务器距离较远,那么延迟可能会非常痛苦。 Be stricter than normal in accessing only those columns you need. 在访问您需要的列时要比正常更严格。

I've found that if you're doing outer joins (left/right) the performance degrades fast. 我发现如果你正在进行外连接(左/右),性能会快速下降。 It's sometimes faster to select the data from the remote server into a temp table and index it rather than joining across the network. 有时候从远程服务器中选择数据到临时表并对其进行索引而不是通过网络加入有时会更快。 Mostly, the best strategy is to write the query the way it makes sense and then only tune it if performance is a real problem. 大多数情况下,最好的策略是以有意义的方式编写查询,然后只在性能成为真正问题时才调整查询。

@George2, @ George2,

Sam Saffron is correct in this case. Sam Saffron在这种情况下是正确的。 When a join is performed locally then SQL Server uses indexes to perform a join and then does lookups for the columns not included into an index definition. 在本地执行连接时,SQL Server使用索引执行连接,然后查找未包含在索引定义中的列。

With linked server to do a join all table needs to be transferred from a remote server first, then join is performed. 使用链接服务器进行连接,首先需要从远程服务器传输所有表,然后执行连接。 This is a bottle neck. 这是一个瓶颈。 If you can pre-filter all remoted tables before joining them to local tables it will considerably improve performance (eg select into #temp tables with good filter to reduce number of rows), then if you need to perform multiple operations against that table you are better off creating an index right away. 如果您可以在将所有远程表连接到本地表之前对其进行预过滤,那么它将显着提高性能(例如,选择具有良好过滤器的#temp表以减少行数),然后如果您需要对该表执行多个操作,那么最好立即创建一个索引。

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

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