【问题标题】:What is that makes temp tables more efficient than table variables when working with large data?在处理大数据时,是什么让临时表比表变量更有效?
【发布时间】:2018-07-31 20:48:00
【问题描述】:

在 SQL Server 中,在处理大型数据(例如插入或更新 100000 行)时,与表变量相比,临时表的性能(在时间方面)要好得多(参考:SQL Server Temp Table vs Table Variable Performance Testing

我看过很多比较临时表和表变量的文章,但仍然不明白是什么让临时表在处理大数据时更高效?仅仅是它们被设计成的行为方式还是其他什么?

【问题讨论】:

标签: sql-server temp-tables table-variable


【解决方案1】:

Table variables没有statistics,所以表变量的基数估计为1。

您可以使用recompile 选项强制至少正确的cardinality estimation,但绝对不能生成column statistics,即不存在temporary tables 的列值的数据分布。

后果很明显:每一个使用table variable的查询都会有低估

另一个骗局是这个:

插入(或以其他方式修改)@table_variables 的查询不能 有并行计划,#temp_tables 不受此限制。

您可以在这里阅读更多内容:

Parallelism with temp table but not table variable?

该主题中的答案有另一个非常有帮助的附加阅读链接

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2021-12-02
    • 2010-09-06
    • 2014-12-27
    相关资源
    最近更新 更多