简体   繁体   English

加速SQL Server中的临时表连接

[英]Speeding up temp table joins in SQL Server

I have 2 temp tables #temp1 and #temp. 我有2个临时表#temp1和#temp。 Both have a key and date columns. 两者都有一个键和日期列。 Both have around 25k rows. 两者都有大约25,000行。 And I'm left joining them on the basis of the key and date which are unique on all rows. 我将根据所有行上唯一的键和日期加入它们。 It's taking around 4 minutes for this join to complete. 这次加入需要大约4分钟才能完成。 Is there any way to speed it up or any alternative methods? 有没有办法加快速度或任何替代方法?

我相信您可以像在任何其他表上一样在临时表上创建索引。

If your join of 25k tables takes 4 minutes, there's something wrong with it. 如果您加入25k表需要4分钟,那就有问题了。

Most probably you put a wrong JOIN condition which leads to a cartesian join (or something close to it), which results in 25k * 25k = 625M records returned. 很可能你25k * 25k = 625M错了JOIN条件导致笛卡尔连接(或接近它的东西),这导致返回25k * 25k = 625M条记录。

This can take 4 minutes indeed if not more, but I don't think it was what you wanted. 这可能需要4分钟,如果不是更多,但我不认为这是你想要的。

Probably you have DISTINCT / GROUP BY clauses in your query, which makes the query to return correct resultset but in a non-optimal way. 您的查询中可能有DISTINCT / GROUP BY子句,这使得查询以非最佳方式返回正确的结果集。

Could you please post your query so that I can tell the exact reason? 您能否发布您的查询,以便我确切地说出原因?

I have a suspicion that your whole query might want looking at. 我怀疑你的整个查询可能需要查看。 There may be another way to skin this particular rabbit. 可能有另一种方法来修饰这只特殊的兔子。

Could you leave some more detail? 你能留下更多细节吗?

取决于你正在做什么,你可以完全避免临时表,并有一个基于集合的解决方案(运行速度更快,扩展性更好),但很难知道你的查询是什么。

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

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