简体   繁体   English

在链接服务器表上加入时提高查询性能

[英]Improve query performance when joining on a linked server table

Unfortunately as a temporary measure whilst we fix our datacentre I need to run the following cross server query, can anyone suggest how I can get the performance to increase... 不幸的是,作为我们修复数据中心的临时措施,我需要运行以下跨服务器查询,任何人都可以建议我如何提高性能...

as have to run this statement at the start of a cursor 因为必须在游标开头运行此语句

SELECT t.SearchId, t.VisitSourceId, t.SiteDomainId, t.trpUTMid, t.FlightPlus, t.StartDate,
t.CountryId, t.ProvinceId, t.Locationid, t.PlaceId, t.EstabId, t.CheckInDate, 
t.CheckOutDate, t.Rooms, t.Room1Adults, t.Room1Children, t.Room2Adults, t.Room2Children, 
t.Room3Adults, t.Room3Children, tc.OutcomeDate, tc.OutcomeId, tc.HotelsFound, tc.Notes

FROM [MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl t
INNER JOIN TrackingAcomSearchesOutcome_tbl tc
ON t.trpUTMid = tc.trpUTMid
LEFT JOIN [YAZOO].[MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl tid
ON t.trpUTMid = tid.trpUTMid
WHERE tid.trpUTMid IS NULL

One possible option would be to get the remote table replicated to the local server. 一种可能的选择是将远程表复制到本地服务器。 This would put all tables on the same server, enabling the optimizer to use statistics to generate the best plan. 这会将所有表放在同一台服务器上,使优化器能够使用统计信息生成最佳计划。 Since you are only using trpUTMid, you could just replicate that column. 由于您只使用trpUTMid,因此您可以复制该列。

Another option would be to use temp tables. 另一种选择是使用临时表。 Same idea, get all tables on the same server: 同样的想法,在同一台服务器上获取所有表:

SELECT trpUTMid
INTO #Yazoo_TrackingAcomSearches_tbl
FROM [YAZOO].[MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl


SELECT t.SearchId, t.VisitSourceId, t.SiteDomainId, t.trpUTMid, t.FlightPlus, t.StartDate,
t.CountryId, t.ProvinceId, t.Locationid, t.PlaceId, t.EstabId, t.CheckInDate, 
t.CheckOutDate, t.Rooms, t.Room1Adults, t.Room1Children, t.Room2Adults, t.Room2Children, 
t.Room3Adults, t.Room3Children, tc.OutcomeDate, tc.OutcomeId, tc.HotelsFound, tc.Notes

FROM [MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl t
INNER JOIN TrackingAcomSearchesOutcome_tbl tc
ON t.trpUTMid = tc.trpUTMid
LEFT JOIN #Yazoo_TrackingAcomSearches_tbl tid
ON t.trpUTMid = tid.trpUTMid
WHERE tid.trpUTMid IS NULL

You could also create an index on the temp table to improve performance. 您还可以在临时表上创建索引以提高性能。

I'd also recommend checking for lack of existence rather than the old left join where null ( read this for more info ): 我还建议检查是否存在缺乏而不是旧的左连接为null(阅读此内容以获取更多信息):

SELECT trpUTMid
INTO #Yazoo_TrackingAcomSearches_tbl
FROM [YAZOO].[MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl


SELECT t.SearchId, t.VisitSourceId, t.SiteDomainId, t.trpUTMid, t.FlightPlus, t.StartDate,
t.CountryId, t.ProvinceId, t.Locationid, t.PlaceId, t.EstabId, t.CheckInDate, 
t.CheckOutDate, t.Rooms, t.Room1Adults, t.Room1Children, t.Room2Adults, t.Room2Children, 
t.Room3Adults, t.Room3Children, tc.OutcomeDate, tc.OutcomeId, tc.HotelsFound, tc.Notes

FROM [MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl t
INNER JOIN TrackingAcomSearchesOutcome_tbl tc
ON t.trpUTMid = tc.trpUTMid
WHERE NOT EXISTS (
    SELECT NULL 
    FROM #Yazoo_TrackingAcomSearches_tbl
    WHERE trpUTMid = t.trpUTMid
)

Try the below query 请尝试以下查询

;with cte
    as(
    SELECT tc.OutcomeDate, tc.OutcomeId, tc.HotelsFound, tc.Notes
    GROUP BY [BatchId]  FROM TrackingAcomSearchesOutcome_tbl tc
    )
     SELECT t.SearchId, t.VisitSourceId, t.SiteDomainId, t.trpUTMid, t.FlightPlus, t.StartDate,
    t.CountryId, t.ProvinceId, t.Locationid, t.PlaceId, t.EstabId, t.CheckInDate, 
    t.CheckOutDate, t.Rooms, t.Room1Adults, t.Room1Children, t.Room2Adults, t.Room2Children, 
    t.Room3Adults, t.Room3Children

    FROM [MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl t
    INNER JOIN cte as tbl ON t.trpUTMid = tbl .trpUTMid
    LEFT JOIN [YAZOO].[MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl tid
    ON t.trpUTMid = tbl .trpUTMid
    WHERE tid.trpUTMid IS NULL

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

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