大表中的行数

number of rows in big table(大表中的行数)
本文介绍了大表中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

SELECT COUNT(*) FROM BigTable_1

如果我有超过 10 亿行,我应该使用哪种方式来获取表中的行数?

Which way I should to use to get number of rows in table if I have more than 1 billion of rows?

更新:例如,如果上面的查询出现超时问题",有什么方法可以优化它吗?如何更快地完成?

UPDATE: For example, if we have 'a timeout problem' with the query above is there any way to optimize it? How to do it quicker?

推荐答案

如果需要精确计数,必须使用COUNT (*)

If you need an exact count, you have to use COUNT (*)

如果您可以进行粗略计数,则可以使用分区中的行总和

If you are OK with a rough count, you can use a sum of rows in the partitions

SELECT SUM (Rows)
FROM sys.partitions
WHERE 1=1
And index_id IN (0, 1)
And OBJECT_ID = OBJECT_ID('Database.schema.Table');

如果你想用你的 COUNT 开玩笑,你可以执行以下操作

If you want to be funny with your COUNT, you can do the following

select COUNT (1/0) from BigTable_1

这篇关于大表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Dynamically group by with quot;Group Headerquot;(使用“组头动态分组)
How to retrieve leaf path in parent-child table in SQL Server with root ID?(如何使用根 ID 在 SQL Server 中的父子表中检索叶路径?)
Tsql union in while loop(while循环中的Tsql联合)
Pass a column as parameter to dateadd in SQL Server(将列作为参数传递给 SQL Server 中的 dateadd)
Is it possible to convert rows to a variable number of columns in T-SQL?(是否可以在 T-SQL 中将行转换为可变数量的列?)
Swap values between two rows of data(在两行数据之间交换值)