鏈接服務器表上的INNER JOIN比Sub-Query慢得多

[英]INNER JOIN on Linked Server Table much slower than Sub-Query


I came across this very odd situation, and i thought i would throw it up to the crowd to find out the WHY.

我遇到了這種非常奇怪的情況,我想我會把它扔到人群中找出原因。

I have a query that was joining a table on a linked server:

我有一個查詢加入鏈接服務器上的表:

select a.*, b.phone
from table_a a, 
join remote.table_b b on b.id = a.id
 (lots of data on A, but very few on B)

this query was talking forever (never even found out the actual run time), and that is when I noticed B had no index, so I added it, but that didn't fix the issue. Finally, out of desperation I tried:

這個查詢是永遠在說話(從來沒有發現實際的運行時間),那是我注意到B沒有索引,所以我添加了它,但這並沒有解決問題。最后,出於絕望,我試過:

select a.*, b.phone
from table_a a, 
join (select id, phone from remote.B) as b on b.id = a.id

This version of the query, in my mind as least, should have the same results, but lo and behold, its responding immediately!

在我看來,這個版本的查詢應該具有相同的結果,但是請注意,它會立即響應!

Any ideas why one would hang and the other process quickly? And yes, I did wait to make sure the index had been built before running both.

任何想法為什么會掛起而另一個過程很快?是的,我確實等待確保在運行這兩個索引之前已經構建了索引。

4 个解决方案

#1


4  

It's because sometimes(very often) execution plans automatically generated by sql server engine are not as good and obvious as we want to. You can look at execution plan in both situations. I suggest use hint in first query, something like that: INNER MERGE JOIN.

這是因為sql server引擎自動生成的有時(很多時候)執行計划並不像我們想的那么好和明顯。您可以在兩種情況下查看執行計划。我建議在第一個查詢中使用提示,類似於:INNER MERGE JOIN。

Here is some more information about that:

這里有一些關於它的更多信息:

http://msdn.microsoft.com/en-us/library/ms181714.aspx

http://msdn.microsoft.com/en-us/library/ms181714.aspx

#2


3  

For linked servers 2nd variant prefetches all the data locally and do the join, since 1st variant may do inner loop join roundtrip to linked server for every row in A

對於鏈接服務器,第二個變量在本地預取所有數據並進行連接,因為第一個變量可以對A中的每一行進行內部循環連接往返鏈接服務器

#3


1  

Remote table as in not on that server? Is it possible that the join is actually making multiple calls out to the remote table while the subquery is making a single request for a copy of the table data, thus resulting in less time waiting on network?

遠程表,因為不在該服務器上?當子查詢對表數據的副本進行單個請求時,連接是否可能實際上對遠程表進行多次調用,從而減少了在網絡上等待的時間?

#4


1  

I'm just going to have a guess here. When you access remote.b is it a table on another server?

我只想猜測一下。當你訪問remote.b它是另一台服務器上的表嗎?

If it is, the reason the second query is faster is because, you do one query to the other server and get all the fields you need from b, before processing the data. In the first query you are processing data and at the same time you are making several requests to the other server.

如果是,第二個查詢更快的原因是,在處理數據之前,您對另一個服務器執行一個查詢並從b獲取所需的所有字段。在第一個查詢中,您正在處理數據,同時您正在向另一個服務器發出多個請求。

Hope this help you.

希望這對你有所幫助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/25/49c89822f4d5f21bd5aaeb8277bf9ef2.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com