简体   繁体   中英

Speeding up temp table joins in SQL Server

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?

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

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?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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