加快 SQL Server 中的临时表连接速度

发布于 2024-08-03 00:31:03 字数 111 浏览 8 评论 0原文

我有 2 个临时表#temp1 和#temp。两者都有一个键和日期列。两者都有大约 25k 行。我根据所有行中唯一的键和日期加入它们。此连接大约需要 4 分钟才能完成。有什么办法可以加快速度或者有其他方法吗?

I have 2 temp tables #temp1 and #temp. Both have a key and date columns. Both have around 25k rows. 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. Is there any way to speed it up or any alternative methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪都笑了 2024-08-10 00:31:03

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

I believe you can create indexes on temporary tables as on any other tables.

风苍溪 2024-08-10 00:31:03

如果您连接 25k 表需要 4 分钟,则说明有问题。

很可能您输入了错误的 JOIN 条件,导致笛卡尔连接(或类似的连接),从而导致返回 25k * 25k = 625M 记录。

这实际上可能需要 4 分钟(如果不是更长的话),但我认为这不是您想要的。

可能您的查询中有 DISTINCT / GROUP BY 子句,这使得查询以非最佳方式返回正确的结果集。

能否请您发表您的疑问,以便我能告诉您确切的原因?

If your join of 25k tables takes 4 minutes, there's something wrong with it.

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.

This can take 4 minutes indeed if not more, but I don't think it was what you wanted.

Probably you have DISTINCT / GROUP BY clauses in your query, which makes the query to return correct resultset but in a non-optimal way.

Could you please post your query so that I can tell the exact reason?

缺⑴份安定 2024-08-10 00:31:03

我怀疑您的整个查询可能需要查看。可能还有另一种方法可以给这只兔子剥皮。

您能留下更多细节吗?

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?

[浮城] 2024-08-10 00:31:03

根据您正在做的事情,您可能可以完全避免临时表并拥有基于集合的解决方案(它将运行得更快并且扩展得更好),但是如果不知道您的查询是什么,就很难知道这一点。

depending on what you're doing you could probably avoid the temp tables altogether and have a set-based solution (which will run a lot quicker and scale better), but that's hard to know without any idea of what your query is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文