国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

關于mysql innodb count(*)速度慢的解決辦法

2024-07-24 12:48:52
字體:
來源:轉載
供稿:網友
innodb引擎在統計方面和myisam是不同的,Myisam內置了一個計數器,所以在使用 select count(*) from table 的時候,直接可以從計數器中取出數據。而innodb必須全表掃描一次方能得到總的數量。要初步解決這個問題,需要做不同于myisam的一些工作:

1、使用第二索引(一般不使用主鍵索引),并且添加where條件,如:

復制代碼 代碼如下:

select count(*) from product where comp_id>=0 ;
show index from product ;
id primary key
comp_id index


2、如果只需要粗略統計的話也可使用

show status from product; 來得到大約值
這種方法可在數據分頁中使用!

3、使用外部計數器,比如建立一個觸發器來計數或者在程序上使用緩存方式定時計數,缺陷是這些方法會額外消耗一些資源!

參考資料:

mysql高性能:http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/
mysql DBA:http://imysql.cn/2008_06_24_speedup_innodb_count

COUNT(*) for Innodb Tables

I guess note number one about MyISAM to Innodb migration is warning what Innodb is very slow in COUNT(*) queries. The part which I often however see omitted is fact it only applies to COUNT(*) queries without WHERE clause.
So if you have query like SELECT COUNT(*) FROM USER It will be much faster for MyISAM (MEMORY and some others) tables because they would simply read number of rows in the table from stored value. Innodb will however need to perform full table scan or full index scan because it does not have such counter, it also can't be solved by simple singe counter for Innodb tables as different transactions may see different number of rows in the table.
If you have query like SELECT COUNT(*) FROM IMAGE WHERE USER_ID=5 this query will be executed same way both for MyISAM and Innodb tables by performing index rage scan. This can be faster or slower both for MyISAM and Innodb depending on various conditions.
In real applications there are much more queries of second type rather than first type so it is typically not as bad problem as it may look. Most typically count of rows is needed by admin tools which may show it in table statistics, it may also be used in application stats to show something like “We have 123.345 users which have uploaded 1.344.656 images” but these are normally easy to remove.
So remember Innodb is not slow for ALL COUNT(*) queries but only for very specific case of COUNT(*) query without WHERE clause. It does not mean I would not like to see it fixed though, it is pretty annoying.

轉自:http://www.sphinxsearch.org/archives/89

您可能感興趣的文章:

mysql更改引擎(InnoDB,MyISAM)的方法可以改善mysql性能的InnoDB配置參數MySQL Innodb表導致死鎖日志情況分析與歸納MySQL存儲引擎中的MyISAM和InnoDB區別詳解MySQL不支持InnoDB的解決方法MySQL數據庫INNODB表損壞修復處理過程分享Mysql InnoDB刪除數據后釋放磁盤空間的方法Mysql5.5 InnoDB存儲引擎配置和優化mysql Innodb表空間卸載、遷移、裝載的使用方法MySQL判別InnoDB表是獨立表空間還是共享表空間的方法詳解
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江口县| 德格县| 万山特区| 丹东市| 泸水县| 杭锦后旗| 抚州市| 江源县| 比如县| 炎陵县| 利辛县| 景泰县| 安康市| 达拉特旗| 濉溪县| 攀枝花市| 裕民县| 库车县| 榆树市| 罗江县| 鹤峰县| 黑河市| 大庆市| 泰顺县| 辛集市| 班戈县| 万年县| 高平市| 岗巴县| 湘潭县| 宜君县| 历史| 息烽县| 桑日县| 庆安县| 松潘县| 庆安县| 松原市| 绵阳市| 治县。| 新疆|