本文介紹了大表中的行數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
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
這篇關于大表中的行數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!