MySQL InnoDB 儲存引擎 count加速

來源:互聯網
上載者:User

SELECT COUNT(1) from table USE INDEX (UNIQUE KEY);

比 使用主鍵索引要快,我測試的是1000W生產環境資料,快了至少3倍.....

 
  1. EXPLAIN SELECT COUNT(1) from `order` USE INDEX (PRIMARY)\G;  
  2. *************************** 1. row ***************************  
  3.            id: 1  
  4.   select_type: SIMPLE  
  5.         table: order  
  6.          type: index  
  7. possible_keys: NULL  
  8.           key: PRIMARY  
  9.       key_len: 8  
  10.           ref: NULL  
  11.          rows: 1  
  12.         Extra: Using index  
  13. 1 row in set (0.01 sec)  

 
  1. EXPLAIN SELECT COUNT(1) from `order` USE INDEX (UNQ_p)\G;  
  2. *************************** 1. row ***************************  
  3.            id: 1  
  4.   select_type: SIMPLE  
  5.         table: order  
  6.          type: index  
  7. possible_keys: NULL  
  8.           key: UNQ_p  
  9.       key_len: 99  
  10.           ref: NULL  
  11.          rows: 1  
  12.         Extra: Using index  
  13. 1 row in set (0.00 sec)  

這個非叢集索引比主鍵索引還列寬還長些....

 
  1. SELECT COUNT(1) from `order` USE INDEX (UNQ_p);            
  2. +----------+   
  3. | COUNT(1) |  
  4. +----------+   
  5. | 10984918 |  
  6. +----------+   
  7. 1 row in set (36.60 sec)  

 
  1. SELECT COUNT(1) from `order` USE INDEX (PRIMARY);            
  2. +----------+   
  3. | COUNT(1) |  
  4. +----------+   
  5. | 10984918 |  
  6. +----------+   
  7. 1 row in set (1 min 31.57 sec)  

相關文章

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.