select count(*)和select count(1)的區別和效率差異,selectcount

來源:互聯網
上載者:User

select count(*)和select count(1)的區別和效率差異,selectcount

 一般情況下,Select Count (*)和Select Count(1)兩著返回結果是一樣的

    假如表沒有主鍵(Primary key), 那麼count(1)比count(*)快,

    如果有主鍵的話,那主鍵作為count的條件時候count(主鍵)最快

    如果你的表只有一個欄位的話那count(*)就是最快的

   count(*) 跟 count(1) 的結果一樣,都包括對NULL的統計,而count(column) 是不包括NULL的統計

 

1、select 1 與 select *的區別 
selelct 常量 from ... 對應所有行,返回的永遠只有一個值,即常量。所以正常只會用來判斷是否有還是沒有(比如exists子句)。而select * from ... 是返回所有行的所有列。 
效能上的差異,關鍵看你的from和where子句。比如說如果你的where條件中可以通過索引,那顯然 select 1 from ... 的效能比 select * from ... 好。 
2、select sum(1)的使用 
select count(*)返回所有滿足條件的記錄數,此時同select sum(1) 
但是sum()可以傳任一數字,負數、浮點數都可以,返回的值是傳入值n*滿足條件記錄數m

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.