简体   繁体   English

sql temp表连接服务器

[英]sql temp table join between servers

So I have a summary i need to return to the end user application. 所以我有一个总结,我需要返回到最终用户应用程序。

It should accept 3 parameters DateType, StartDate, EndDate. 它应该接受3个参数DateType,StartDate,EndDate。

Date Type will determine the date field I use to filter the data. 日期类型将确定我用于过滤数据的日期字段。

The way i accomplished this was putting all the IDs of the records for a datetype into a TEMP table and then joining my summary to the list of IDs. 我完成此操作的方法是将日期类型的记录的所有ID放入TEMP表,然后将我的摘要加入ID列表。

This worked fine when running on the query on the SQL server that houses the data. 当在包含数据的SQL服务器上运行查询时,这很好。

However, that is a replicated server, so when I compiled to a stored proc that would be on the server with the rest of the application data, it slowed the query down. 但是,这是一个复制的服务器,因此当我编译到存储过程中,该存储过程将与服务器上的其余应用程序数据一起使用时,它会减慢查询速度。 IE 2 seconds vs 50 seconds. IE 2秒vs 50秒。

I think the cross join from the temp table that is created on the SQL server then joining to the tables on the replciation server, is causing the slow down. 我认为从SQL服务器上创建的临时表交叉连接然后加入到replciation服务器上的表,导致速度减慢。

Are there any methods or techniques that I can use to get around this and build this all in one stored procedure? 是否有任何方法或技术可用于解决此问题并在一个存储过程中构建所有这些方法或技术?

If I create 3 stored procedures with their own date range, then they are fast again. 如果我创建3个具有自己日期范围的存储过程,那么它们又快了。 However, this means maintaining multiple stored procs for the same thing. 但是,这意味着为同一件事维护多个存储过程。

First off, if you are running a version of SQL Server older than 2012 SP1, one problem is that users who aren't allowed to run DBCC SHOW_STATISTICS (which is most users who aren't sysadmins, see the "Permissions" section in the documentation) don't get access to statistics on remote tables. 首先,如果您运行的是早于2012 SP1的SQL Server版本,则一个问题是不允许运行DBCC SHOW_STATISTICS用户(大多数不是系统管理员的用户,请参阅中的“权限”部分)文档)无法访问远程表的统计信息。 This can severely cripple the optimizer's ability to generate a good execution plan. 这可能严重削弱优化器生成良好执行计划的能力。 Upgrading SQL Server or granting more permissions can help there. 升级SQL Server或授予更多权限可以帮助您。

If your query involves filtering or joining on a character column, make sure the remote server is flagged in the linked server options as "collation compatible". 如果您的查询涉及过滤或加入字符列,请确保远程服务器在链接服务器选项中标记为“collat​​ion compatible”。 If this option is off, SQL Server can't assume strings can be compared across the servers and it will start pumping entire tables up and down just to make sure the data ends up where the comparison has to be made. 如果禁用此选项,则SQL Server无法假设可以跨服务器比较字符串,并且它将开始向上和向下泵送整个表,以确保数据最终在必须进行比较的位置。

If the execution plan is as good as it gets and it's still not good enough, one general (lame) technique is to transfer all data locally first ( SELECT * INTO #localtable FROM remote.db.schema.table ), then run the query as a non-distributed query. 如果执行计划尽可能好并且仍然不够好,一种通用(跛脚)技术是首先在本地传输所有数据( SELECT * INTO #localtable FROM remote.db.schema.table ),然后运行查询作为非分布式查询。 Obviously, in order for this to work, the remote table cannot be "too big" and in some cases this actually has worse performance, depending on how many rows are involved. 显然,为了使其工作,远程表不能“太大”,并且在某些情况下,这实际上具有更差的性能,这取决于涉及多少行。 But it's always worth considering, because the optimizer does a better job with local tables. 但它总是值得考虑,因为优化器在本地表上做得更好。

Another approach that avoids pulling tables together across servers is packing up data in parameters to remote stored procedure calls. 另一种避免跨服务器将表拉到一起的方法是将参数中的数据打包到远程存储过程调用中。 Entire tables can be passed as XML through an NVARCHAR(MAX) , since neither XML columns nor table-valued parameters are supported in distributed queries. 整个表可以通过NVARCHAR(MAX)作为XML传递,因为分布式查询中既不支持XML列也不支持表值参数。 The basic idea is the same: avoid the need for the the optimizer to figure out an efficient distributed query. 基本思想是相同的:避免优化器需要找出有效的分布式查询。 The best approach greatly depends on your data and your query, obviously. 显然,最好的方法很大程度上取决于您的数据和查询。

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

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