站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter 15. Storage Engines and Table Types - MySQL 5.1参考手册中文版

15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

鐩綍

15.1. MyISAM瀛樺偍寮曟搸

15.1.1. MyISAM鍚姩閫夐」

15.1.2.閿鎵闇鐨勭┖闂

15.1.3. MyISAM琛ㄧ殑瀛樺偍鏍煎紡

15.1.4. MyISAM琛ㄧ殑闂

15.2. InnoDB瀛樺偍寮曟搸

15.2.1. InnoDB姒傝堪

15.2.2. InnoDB鑱旂郴淇℃伅

15.2.3. InnoDB閰嶇疆

15.2.4. InnoDB鍚姩閫夐」

15.2.5. 鍒涘缓InnoDB琛ㄧ┖闂

15.2.6. 鍒涘缓InnoDB琛

15.2.7. 娣诲姞鍜屽垹闄InnoDB鏁版嵁鍜屾棩蹇楁枃浠

15.2.8. InnoDB鏁版嵁搴撶殑澶囦唤鍜屾仮澶

15.2.9. InnoDB鏁版嵁搴撶Щ鍒板彟涓鍙版満鍣

15.2.10. InnoDB浜嬪姟妯″瀷鍜岄攣瀹

15.2.11. InnoDB鎬ц兘璋冭妭鎻愮ず

15.2.12. 澶氱増鏈殑瀹炴柦

15.2.13. 琛ㄥ拰绱㈠紩缁撴瀯

15.2.14.鏂浠剁┖闂寸鐞嗗拰纾佺洏I/O

15.2.15. InnoDB閿欒澶勭悊

15.2.16. InnoDB琛ㄧ殑闄愬埗

15.2.17. InnoDB鏁呴殰璇婃柇鍜屾帓闄

15.3. MERGE瀛樺偍寮曟搸

15.3.1. MERGE琛 鏂归潰鐨勯棶棰

15.4. MEMORY(HEAP)瀛樺偍寮曟搸

15.5. BDB(BerkeleyDB)瀛樺偍寮曟搸

15.5.1. BDB鏀寔鐨勬搷浣滅郴缁

15.5.2. 瀹夎BDB

15.5.3. BDB鍚姩閫夐」

15.5.4. BDB琛ㄧ殑鐗规

15.5.5. 淇敼BDB鎵闇鐨勪簨瀹

15.5.6. BDB琛ㄧ殑闄愬埗

15.5.7. 浣跨敤BDB琛ㄦ椂鍙兘 鍑虹幇鐨勯敊璇

15.6. EXAMPLE瀛樺偍寮曟搸

15.7. FEDERATED瀛樺偍寮曟搸

15.7.1. 瀹夎FEDERATED瀛樺偍寮曟搸

15.7.2. FEDERATED瀛樺偍寮曟搸鐨勪粙缁

15.7.3. 濡備綍浣跨敤FEDERATED琛

15.7.4. FEDERATED瀛樺偍寮曟搸鐨勫眬闄

15.8. ARCHIVE瀛樺偍寮曟搸

15.9. CSV瀛樺偍寮曟搸

15.10.BLACKHOLE瀛樺偍寮曟搸

MySQL鏀寔鏁颁釜瀛樺偍寮曟搸浣滀负瀵逛笉鍚岃〃鐨勭被鍨嬬殑澶勭悊鍣ㄣ侻ySQL瀛樺偍寮曟搸鍖呮嫭澶勭悊浜嬪姟瀹夊叏琛ㄧ殑寮曟搸鍜屽鐞嗛潪浜嬪姟瀹夊叏琛ㄧ殑寮曟搸锛

·         MyISAM绠$悊闈炰簨鍔¤〃銆傚畠鎻愪緵楂橀熷瓨鍌ㄥ拰妫绱紝浠ュ強鍏ㄦ枃鎼滅储鑳藉姏銆侻yISAM鍦ㄦ墍鏈塎ySQL閰嶇疆閲岃鏀寔锛屽畠鏄粯璁ょ殑瀛樺偍寮曟搸锛岄櫎闈炰綘閰嶇疆MySQL榛樿浣跨敤鍙﹀涓涓紩鎿庛

·         MEMORY瀛樺偍寮曟搸鎻愪緵鈥滃唴瀛樹腑鈥濊〃銆侻ERGE瀛樺偍寮曟搸鍏佽闆嗗悎灏嗚澶勭悊鍚屾牱鐨凪yISAM琛ㄤ綔涓轰竴涓崟鐙殑琛ㄣ傚氨鍍廙yISAM涓鏍凤紝MEMORY鍜孧ERGE瀛樺偍寮曟搸澶勭悊闈炰簨鍔¤〃锛岃繖涓や釜寮曟搸涔熼兘琚 榛樿鍖呭惈鍦∕ySQL涓

娉ㄩ噴锛MEMORY瀛樺偍寮曟搸姝e紡鍦拌纭畾涓篐EAP寮曟搸銆

·         InnoDB鍜孊DB瀛樺偍寮曟搸鎻愪緵浜嬪姟瀹夊叏琛ㄣ侭DB琚寘鍚湪涓烘敮鎸佸畠鐨勬搷浣滅郴缁熷彂甯冪殑MySQL-Max浜岃繘鍒跺垎鍙戠増閲屻侷nnoDB涔 榛樿琚寘鎷湪鎵鏈塎ySQL 5.1浜岃繘鍒跺垎鍙戠増閲岋紝浣犲彲浠ユ寜鐓у枩濂介氳繃閰嶇疆MySQL鏉ュ厑璁告垨绂佹浠讳竴寮曟搸銆

·         EXAMPLE瀛樺偍寮曟搸鏄竴涓“瀛樻牴”寮曟搸锛屽畠涓嶅仛浠涔堛備綘鍙互鐢ㄨ繖涓紩鎿庡垱寤鸿〃锛屼絾娌℃湁鏁版嵁琚瓨鍌ㄤ簬鍏朵腑鎴栦粠鍏朵腑妫绱€傝繖涓紩鎿庣殑鐩殑鏄湇鍔 锛屽湪MySQL婧愪唬鐮佷腑鐨勪竴涓緥瀛愶紝瀹冩紨绀鸿鏄庡浣曞紑濮嬬紪鍐欐柊瀛樺偍寮曟搸銆傚悓鏍凤紝瀹冪殑涓昏鍏磋叮鏄寮鍙戣

·         NDB Cluster鏄MySQL Cluster鐢ㄦ潵瀹炵幇鍒嗗壊鍒板鍙拌绠楁満涓婄殑琛ㄧ殑瀛樺偍寮曟搸銆傚畠鍦∕ySQL-Max 5.1浜岃繘鍒跺垎鍙戠増閲屾彁渚涖傝繖涓瓨鍌ㄥ紩鎿庡綋鍓嶅彧琚獿inux, Solaris, 鍜孧ac OS X 鏀寔銆傚湪鏈潵鐨凪ySQL鍒嗗彂鐗堜腑锛屾垜浠兂瑕佹坊鍔犲叾瀹冨钩鍙板杩欎釜寮曟搸鐨勬敮鎸侊紝鍖呮嫭Windows銆

·         ARCHIVE瀛樺偍寮曟搸琚敤鏉ユ棤绱㈠紩鍦帮紝闈炲父灏瑕嗙洊瀛樺偍鐨勫ぇ閲忔暟鎹

·         CSV瀛樺偍寮曟搸鎶婃暟鎹互閫楀彿鍒嗛殧鐨勬牸寮忓瓨鍌ㄥ湪鏂囨湰鏂囦欢涓

·         BLACKHOLE瀛樺偍寮曟搸鎺ュ彈浣嗕笉瀛樺偍鏁版嵁锛屽苟涓旀绱㈡绘槸杩斿洖涓涓┖闆嗐

·         FEDERATED瀛樺偍寮曟搸鎶婃暟鎹瓨鍦ㄨ繙绋嬫暟鎹簱涓傚湪MySQL 5.1涓紝瀹冨彧鍜孧ySQL涓璧峰伐浣滐紝浣跨敤MySQL C Client API銆傚湪鏈潵鐨勫垎鍙戠増涓紝鎴戜滑鎯宠璁╁畠浣跨敤鍏跺畠椹卞姩鍣ㄦ垨瀹㈡埛绔繛鎺ユ柟娉曡繛鎺ュ埌鍙﹀鐨勬暟鎹簮銆

瀵绘眰閫夋嫨涓涓瓨鍌ㄥ紩鎿庣殑甯姪锛岃鍙傞槄14.4鑺傦紝鈥滈夋嫨涓涓瓨鍌ㄥ紩鎿”

杩欎竴绔犺杩伴櫎NDB Cluster澶栫殑姣忎竴涓狹ySQL瀛樺偍寮曟搸锛孨DB Cluster鍦绗17绔狅細MySQL Cluster涓粙缁嶃

褰撳勾鍒涘缓涓涓柊琛ㄧ殑鏃跺欙紝浣犲彲浠ラ氳繃娣诲姞涓涓狤NGINE 鎴朤YPE 閫夐」鍒癈REATE TABLE璇彞鏉ュ憡璇塎ySQL浣犺鍒涘缓浠涔堢被鍨嬬殑琛細

CREATE TABLE t (i INT) ENGINE = INNODB;
CREATE TABLE t (i INT) TYPE = MEMORY;

铏界劧TYPE浠嶇劧鍦∕ySQL 5.1涓鏀寔锛岀幇鍦‥NGINE鏄閫夌殑鏈銆

濡傛灉浣犵渷鐣ユ帀ENGINE鎴朤YPE閫夐」锛岄粯璁ょ殑瀛樺偍寮曟搸琚娇鐢ㄣ備竴鑸殑榛樿鏄疢yISAM锛屼絾浣犲彲浠ョ敤--default-storage-engine鎴--default-table-type鏈嶅姟鍣ㄥ惎鍔ㄩ夐」鏉ユ敼鍙樺畠锛屾垨鑰呴氳繃璁剧疆storage_engine鎴杢able_type绯荤粺鍙橀噺鏉ユ敼鍙樸

褰揗ySQL琚敤MySQL閰嶇疆鍚戝瀹夎鍦╓indows骞冲彴涓婏紝InnoDB瀛樺偍寮曟搸鏇夸唬MyISAM瀛樺偍寮曟搸浣滀负鏇夸唬锛岃鍙傞槄2.3.5.1鑺傦紝鈥滀粙缁”

瑕佹妸涓涓〃浠庝竴涓被鍨嬭浆鍒板彟涓涓被鍨嬶紝鍙娇鐢ˋLTER TABLE璇彞锛岃繖涓鍙ユ寚鏄庢柊鐨勭被鍨嬶細

ALTER TABLE t ENGINE = MYISAM;
ALTER TABLE t TYPE = BDB;

璇峰弬闃13.1.5鑺傦紝鈥淐REATE TABLE璇硶”13.1.2鑺傦紝鈥淎LTER TABLE璇硶”

濡傛灉浣犺瘯鐫浣跨敤涓涓湭琚紪璇戣繘MySQL鐨勫瓨鍌ㄥ紩鎿庯紝鎴栬呰瘯鐫鐢ㄤ竴涓缂栬瘧杩汳ySQL浣嗘病鏈夎婵娲荤殑瀛樺偍寮曟搸锛孧ySQL鍙栬屼唬涔嬪湴鍒涘缓涓涓狹yISAM绫诲瀷鐨勮〃銆傚綋浣犲湪鏀寔涓嶅悓瀛樺偍寮曟搸鐨凪ySQL鏈嶅姟鍣ㄤ箣闂存嫹璐濊〃鐨勬椂鍊欙紝涓婅堪鐨勮涓烘槸寰堟柟渚跨殑銆傦紙渚嬪锛屽湪涓涓鍒跺缓绔嬩腑锛屽彲鑳戒綘鐨勪富鏈嶅姟鍣ㄤ负澧炲姞瀹夊叏鑰屾敮鎸 浜嬪姟瀛樺偍寮曟搸锛屼絾浠庢湇鍔″櫒涓烘洿蹇殑閫熷害鑰屼粎浣跨敤闈炰簨鍔″瓨鍌ㄥ紩鎿庛)

鍦ㄤ笉鍙敤鐨勭被鍨嬭鎸囧畾鏃讹紝鑷姩鐢∕yISAM琛ㄦ潵鏇夸唬锛岃繖浼氬MySQL鐨勬柊鐢ㄦ埛閫犳垚娣锋穯銆傛棤璁轰綍鏃朵竴涓〃琚嚜鍔ㄦ敼鍙樹箣鏃讹紝浜х敓涓涓鍛娿

MySQL鎬绘槸鍒涘缓涓涓.frm鏂囦欢鏉ヤ繚鎸佽〃鍜屽垪鐨勫畾涔夈傝〃鐨勭储寮曞拰鏁版嵁鍙兘琚瓨鍌ㄥ湪涓涓垨澶氫釜鏂囦欢閲岋紝杩欏彇鍐充簬琛ㄧ殑绫诲瀷銆傛湇鍔″櫒鍦ㄥ瓨鍌ㄥ紩鎿庣骇鍒箣涓婂垱寤.frm鏂囦欢銆傚崟鐙殑瀛樺偍寮曟搸鍒涘缓浠讳綍闇瑕佺敤鏉ョ鐞嗚〃鐨勯澶栨枃浠躲

涓涓暟鎹簱鍙互鍖呭惈涓嶅悓绫诲瀷鐨勮〃銆

浜嬪姟瀹夊叏琛(TST) 姣旇捣闈炰簨鍔″畨鍏ㄨ〃 (NTST)鏈夊嚑澶т紭鍔匡細

·         鏇村畨鍏ㄣ傚嵆浣縈ySQL宕╂簝鎴栭亣鍒扮‖浠堕棶棰橈紝瑕佷箞鑷姩鎭㈠锛岃涔堜粠澶囦唤鍔犱簨鍔℃棩蹇楁仮澶嶏紝浣犲彲浠ュ彇鍥炴暟鎹

·         浣犲彲浠ュ悎骞惰澶氳鍙ワ紝骞剁敤COMMIT璇彞鍚屾椂鎺ュ彈瀹冧滑鍏ㄩ儴锛堝鏋渁utocommit琚姝㈡帀锛夈

·         浣犲彲浠ユ墽琛孯OLLBACK鏉ュ拷鐣ヤ綘鐨勬敼鍙橈紙濡傛灉autocommit琚姝㈡帀锛夈

·         濡傛灉鏇存柊澶辫触锛屼綘鐨勬墍鏈夋敼鍙橀兘鍙樺洖鍘熸潵銆傦紙鐢ㄩ潪浜嬪姟瀹夊叏琛紝鎵鏈夊彂鐢熺殑鏀瑰彉閮芥槸姘镐箙鐨勶級銆

·         浜嬪姟瀹夊叏瀛樺偍寮曟搸鍙互缁欓偅浜涘綋鍓嶇敤璇诲緱鍒拌澶氭洿鏂扮殑琛ㄦ彁渚涙洿濂界殑閮ㄧ讲銆

铏界劧MySQL鏀寔鏁颁釜浜嬪姟瀹夊叏瀛樺偍寮曟搸锛屼负鑾峰緱鏈濂界粨鏋滐紝浣犱笉搴旇鍦ㄤ竴涓簨鍔¢偅娣峰悎涓嶅悓琛ㄧ被鍨嬨傚鏋滀綘娣峰悎琛ㄧ被鍨嬩細鍙戠敓闂锛屾洿澶氫俊鎭鍙傞槄13.4.1鑺傦紝鈥淪TART TRANSACTION, COMMIT鍜孯OLLBACK Syntax”

濡傛灉浣犳病鏈夋寚瀹氶厤缃肩殑璇濓紝InnoDB浣跨敤榛樿鐨勯厤缃笺傝鍙傞槄15.2.3鑺傦紝鈥淚nnoDB閰嶇疆”

闈炰簨鍔″畨鍏ㄨ〃鑷韩鏈夊嚑涓紭鐐癸紝鍥犱负娌℃湁浜嬪姟寮鏀紝鎵鏈変紭鐐归兘鑳藉嚭鐜帮細

·         鏇村揩

·         闇瑕佹洿灏戠殑纾佺洏绌洪棿

·         鎵ц鏇存柊闇瑕佹洿灏戠殑鍐呭瓨

浣犲彲浠ュ湪鍚屼竴涓鍙ヤ腑鍚堝苟浜嬪姟瀹夊叏鍜岄潪浜嬪姟瀹夊叏琛ㄦ潵鑾峰緱涓よ呮渶濂界殑鎯呭喌銆傚敖绠″姝わ紝鍦╝utocommit琚姝㈡帀鐨勪簨鍔¢噷锛屽彉鎹㈠埌闈炰簨鍔″畨鍏ㄨ〃渚濇棫鍗虫椂鎻愪氦锛屽苟涓斾笉浼氳鍥炴粴銆

15.1. MyISAM瀛樺偍寮曟搸

MyISAM鏄 榛樿瀛樺偍寮曟搸銆傚畠鍩轰簬鏇磋佺殑ISAM浠g爜锛屼絾鏈夊緢澶氭湁鐢ㄧ殑鎵╁睍銆傦紙娉ㄦ剰MySQL 5.1涓嶆敮鎸両SAM锛夈

姣忎釜MyISAM鍦ㄧ鐩樹笂瀛樺偍鎴愪笁涓枃浠躲傜涓涓枃浠剁殑鍚嶅瓧浠ヨ〃鐨勫悕瀛楀紑濮嬶紝鎵╁睍鍚嶆寚鍑烘枃浠剁被鍨嬨.frm鏂囦欢瀛樺偍琛ㄥ畾涔夈傛暟鎹枃浠剁殑鎵╁睍鍚嶄负.MYD (MYData)銆傜储寮曟枃浠剁殑鎵╁睍鍚嶆槸.MYI (MYIndex)銆

瑕佹槑纭〃绀轰綘鎯宠鐢ㄤ竴涓狹yISAM琛ㄦ牸锛岃鐢‥NGINE琛ㄩ夐」鎸囧嚭鏉ワ細

CREATE TABLE t (i INT) ENGINE = MYISAM;

娉ㄩ噴锛氳佺増鏈殑MySQL浣跨敤TYPE鑰屼笉鏄疎NGINE锛堜緥濡傦紝TYPE = MYISAM锛夈侻ySQL 5.1涓哄悜涓嬪吋瀹硅屾敮鎸佽繖涓娉曪紝浣員YPE鐜板湪琚交瑙嗭紝鑰孍NGINE鏄鍏堢殑鐢ㄦ硶銆

涓鑸湴锛孍NGINE閫夐」鏄笉蹇呰鐨勶紱闄ら潪榛樿宸茬粡琚敼鍙樹簡锛孧yISAM鏄粯璁ゅ瓨鍌ㄥ紩鎿庛

浣犲彲浠ョ敤myisamchk宸ュ叿鏉ユ鏌ユ垨淇MyISAM琛ㄣ傝鍙傞槄5.9.5.6鑺傦紝鈥滀娇鐢myisamchk鍋氬穿婧冩仮澶”銆備綘涔熷彲浠ョ敤myisampack鏉ュ帇缂㎝yISAM琛紝璁╁畠浠崰鏇村皯鐨勭┖闂淬傝鍙傞槄8.2鑺傦紝鈥渕yisampack锛屼骇鐢熷帇缂┿佸彧璇荤殑MyISAM琛”

濡備笅鏄疢yISAM瀛樺偍寮曟搸鐨勪竴浜涚壒寰侊細

·         鎵鏈夋暟鎹煎厛瀛樺偍浣庡瓧鑺傘傝繖浣垮緱鏁版嵁鏈哄拰鎿嶄綔绯荤粺鍒嗙銆備簩杩涘埗杞讳究鎬х殑鍞竴瑕佹眰鏄満鍣ㄤ娇鐢ㄨˉ鐮侊紙濡傛渶杩20骞寸殑鏈哄櫒鏈夌殑涓鏍凤級鍜孖EEE娴偣鏍煎紡锛堝湪涓绘祦鏈哄櫒涓篃瀹屽叏鏄富瀵肩殑锛夈傚敮涓涓嶆敮鎸佷簩杩涘埗鍏煎鎬х殑鏈哄櫒鏄祵鍏ュ紡绯荤粺銆傝繖浜涚郴缁熸湁鏃朵娇鐢ㄧ壒娈婄殑澶勭悊鍣ㄣ

鍏堝瓨鍌ㄦ暟鎹綆瀛楄妭骞朵笉涓ラ噸鍦板奖鍝嶉熷害锛涙暟鎹涓殑瀛楄妭涓鑸槸鏈仈鍚堢殑锛屼粠涓涓柟鍚戣鏈仈鍚堢殑瀛楄妭骞朵笉姣斾粠鍙嶅悜璇绘洿鍗犵敤鏇村鐨勮祫婧愩傛湇鍔″櫒涓婄殑鑾峰彇鍒楀肩殑浠g爜涓庡叾瀹冧唬鐮佺浉姣斿苟涓嶆樉寰楁椂闂寸揣銆

·        澶ф枃浠讹紙杈63浣嶆枃浠堕暱搴︼級鍦ㄦ敮鎸佸ぇ鏂囦欢鐨勬枃浠剁郴缁熷拰鎿嶄綔绯荤粺涓婅鏀寔銆

·         褰撴妸鍒犻櫎鍜屾洿鏂板強鎻掑叆娣峰悎鐨勬椂鍊欙紝鍔ㄦ佸昂瀵哥殑琛屾洿灏戠鐗囥傝繖瑕侀氳繃鍚堝苟鐩搁偦琚垹闄ょ殑鍧楋紝浠ュ強鑻ヤ笅涓涓潡琚垹闄わ紝灏辨墿灞曞埌涓嬩竴鍧楁潵鑷姩瀹屾垚銆

·         姣忎釜MyISAM琛ㄦ渶澶х储寮曟暟鏄64銆 杩欏彲浠ラ氳繃閲嶆柊缂栬瘧鏉ユ敼鍙樸傛瘡涓储寮曟渶澶х殑鍒楁暟鏄16涓

·         鏈澶х殑閿暱搴︽槸1000瀛楄妭銆傝繖涔熷彲浠ラ氳繃缂栬瘧鏉ユ敼鍙樸傚浜庨敭闀垮害瓒呰繃250瀛楄妭鐨勬儏鍐碉紝涓涓秴杩1024瀛楄妭鐨勭殑閿潡琚敤涓娿

·         BLOB鍜孴EXT鍒楀彲浠ヨ绱㈠紩銆

·         NULL鍊艰鍏佽鍦ㄧ储寮曠殑鍒椾腑銆傝繖涓崰姣忎釜閿殑0-1涓瓧鑺傘

·         鎵鏈夋暟瀛楅敭鍊间互楂樺瓧鑺備负鍏堣瀛樺偍浠ュ厑璁镐竴涓洿楂樺湴绱㈠紩鍘嬬缉銆

·        褰撹褰曚互鎺掑ソ搴忕殑椤哄簭鎻掑叆锛堝氨鍍忎綘浣跨敤涓涓狝UTO_INCREMENT鍒椾箣鏃讹級锛岀储寮曟爲琚妶寮浠ヤ究楂樿妭鐐逛粎鍖呭惈涓涓敭銆傝繖鏀瑰杽浜嗙储寮曟爲鐨勭┖闂村埄鐢ㄧ巼銆

·         姣忚〃涓涓狝UTO_INCREMEN鍒楃殑鍐呴儴澶勭悊銆侻yISAM涓篒NSERT鍜孶PDATE鎿嶄綔鑷姩鏇存柊杩欎竴鍒椼傝繖浣垮緱AUTO_INCREMENT鍒楁洿蹇紙鑷冲皯10%锛夈傚湪搴忓垪椤剁殑鍊艰鍒犻櫎涔嬪悗灏变笉鑳藉啀鍒╃敤銆傦紙褰揂UTO_INCREMENT鍒楄瀹氫箟涓哄鍒楃储寮曠殑鏈鍚庝竴鍒楋紝鍙互鍑虹幇閲嶄娇鐢ㄤ粠搴忓垪椤堕儴鍒犻櫎鐨勫肩殑鎯呭喌 锛夈侫UTO_INCREMENT鍊煎彲鐢ˋLTER TABLE鎴myisamch鏉ラ噸缃

·         濡傛灉鏁版嵁鏂囦欢涓棿鐨勮〃娌℃湁鑷敱鍧椾簡锛屽湪鍏跺畠绾跨▼浠庤〃璇荤殑鍚屾椂锛屼綘鍙互INSERT鏂拌鍒拌〃涓傦紙杩欒璁よ瘑涓哄苟鍙戞搷浣 锛夈傝嚜鐢卞潡鐨勫嚭鐜版槸浣滀负鍒犻櫎琛岀殑缁撴灉锛屾垨鑰呮槸鐢ㄦ瘮褰撳墠鍐呭澶氱殑鏁版嵁瀵瑰姩鎬侀暱搴﹁鏇存柊鐨勭粨鏋溿傚綋鎵鏈夎嚜鐢卞潡琚敤瀹岋紙濉弧锛夛紝鏈潵鐨勬彃鍏ュ張鍙樻垚骞跺彂銆

·         浣犲彲浠ユ妸鏁版嵁鏂囦欢鍜岀储寮曟枃浠舵斁鍦ㄤ笉鍚岀洰褰曪紝鐢―ATA DIRECTORY鍜孖NDEX DIRECTORY閫夐」CREATE TABLE浠ヨ幏寰楁洿楂樼殑閫熷害锛岃鍙傞槄13.1.5鑺傦紝鈥淐REATE TABLE璇硶”

·         姣忎釜瀛楃鍒楀彲浠ュ張涓嶅悓鐨勫瓧绗﹂泦锛岃鍙傞槄绗10绔 锛氣滃瓧绗﹂泦鏀寔鈥

·         鍦∕yISAM绱㈠紩鏂囦欢閲屽張涓涓爣蹇楋紝瀹冭〃鏄庤〃鏄惁琚纭叧闂傚鏋滅敤--myisam-recover閫夐」鍚姩mysqld锛孧yISAM琛ㄥ湪鎵撳紑寰楁椂鍊欒鑷姩妫鏌ワ紝濡傛灉琚〃琚笉鎭板綋鍦板叧闂紝灏变慨澶嶈〃銆

·         濡傛灉浣犵敤--update-state閫夐」杩愯myisamchk锛屽畠鏍囨敞琛ㄤ负宸叉鏌ャmyisamchk --fast鍙鏌ラ偅浜涙病鏈夎繖涓爣蹇楃殑琛ㄣ

·         myisamchk --analyze涓洪儴鍒嗛敭瀛樺偍缁熻淇℃伅锛屼篃涓烘暣涓敭瀛樺偍缁熻淇℃伅銆

·         myisampack鍙互鎵撳寘BLOB鍜孷ARCHAR鍒椼

MyISAM涔熸敮鎸佷笅鍒楃壒寰侊細

·         鏀寔true VARCHAR绫诲瀷锛沄ARCHAR鍒椾互瀛樺偍鍦2涓瓧鑺備腑鐨勯暱搴︽潵寮濮嬨

·         鏈塚ARCHAR鐨勮〃鍙互鏈夊浐瀹氭垨鍔ㄦ佽褰曢暱搴︺

·         VARCHAR鍜孋HAR鍒楀彲浠ュ杈64KB銆

·         涓涓鎼炰贡鐨勫凡璁$畻绱㈠紩瀵瑰彲瀵筓NIQUE鏉ヤ娇鐢ㄣ傝繖鍏佽浣犲湪琛ㄥ唴浠讳綍鍒楃殑鍚堝苟涓婃湁UNIQUE銆傦紙灏界濡傛锛屼綘涓嶈兘鍦ㄤ竴涓猆NIQUE宸茶绠楃储寮曚笂鎼滅储锛夈

瀵筂yISAM瀛樺偍寮曟搸锛屾湁涓涓洿璇︾粏鐨勮鍧涘湪http://forums.mysql.com/list.php?21

15.1.1MyISAM鍚姩閫夐」

涓嬪垪瀵mysqld 鐨勯夐」鍙敤鏉ユ敼鍙楳yISAM琛ㄧ殑琛屼负锛

·         --myisam-recover=mode

璁剧疆涓哄穿婧僊yISAM琛ㄨ嚜鍔ㄦ仮澶嶇殑妯″紡銆

·         --delay-key-write=ALL

瀵逛换浣昅yISAM琛ㄧ殑鍐欐搷浣滀箣闂翠笉瑕佸埛鏂伴敭缂撳啿鍖恒

娉ㄩ噴锛濡傛灉浣犺杩欎箞鍋氥傚綋琛ㄥ湪浣跨敤涓箣鏃讹紝浣犲簲璇ヤ笉浣跨敤鏉ヨ嚜鍙︿竴涓▼搴忕殑MyISAM琛紙姣斿浠庡彟涓涓狹ySQL鏈嶅姟鍣ㄦ垨鐢myisamchk锛夈傝繖涔堝仛浼氬鑷寸储寮曡鐮村潖銆

瀵逛娇鐢--delay-key-write鐨勮〃锛屼娇鐢--external-locking娌℃湁甯姪銆

璇峰弬闃5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」”

涓嬪垪绯荤粺鍙橀噺褰卞搷MyISAM琛ㄧ殑琛屼负锛

·         bulk_insert_buffer_size

鐢ㄥ湪鍧楁彃鍏ヤ紭鍖栦腑鐨勬爲缂撳啿鍖虹殑澶у皬銆娉ㄩ噴锛氳繖鏄竴涓per thread鐨勯檺鍒躲

·         (OBSOLETE) myisam_max_extra_sort_file_size

杩欎釜鍙傛暟宸茬粡涓嶅湪MySQL涓娇鐢ㄣ

·         myisam_max_sort_file_size

濡傛灉涓存椂鏂囦欢浼氬彉寰楄秴杩囩储寮曪紝涓嶈浣跨敤蹇熸帓搴忕储寮曟柟娉曟潵鍒涘缓涓涓储寮曘娉ㄩ噴锛氳繖涓弬鏁颁互瀛楄妭鐨勫舰寮忕粰鍑恒

·         myisam_sort_buffer_size

璁剧疆鎭㈠琛ㄤ箣鏃朵娇鐢ㄧ殑缂撳啿鍖虹殑灏哄銆

璇峰弬闃5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺”

濡傛灉鐢--myisam-recover閫夐」鍚姩mysqld锛岃嚜鍔ㄦ仮澶嶈婵娲汇傚湪杩欑鎯呭喌涓嬶紝褰撴湇鍔″櫒鎵撳紑涓涓狹yISAM琛ㄤ箣鏃讹紝鏈嶅姟鍣ㄤ細妫鏌ユ槸鍚﹁〃琚爣娉ㄤ负宕╂簝锛屾垨鑰呰〃鐨勬墦寮璁℃暟鍙橀噺鏄惁涓嶄负0涓斾綘姝g敤--skip-external-locking杩愯鏈嶅姟鍣ㄣ傚鏋滆繖浜涙潯浠剁殑浠讳綍涓涓负鐪燂紝涓嬪垪鎯呭喌鍙戠敓锛

·         琛ㄨ鏌ラ敊銆

·         濡傛灉鏈嶅姟鍣ㄥ彂鐜颁竴涓敊璇紝瀹冭瘯鐫鍋氬揩閫熻〃淇锛堟帓搴忎笖涓嶉噸鏂板垱寤烘暟鎹枃浠讹級銆

·         濡傛灉淇鍥犱负鏁版嵁鏂囦欢涓殑涓涓敊璇屽け璐ワ紙渚嬪锛屼竴涓噸澶嶉敭閿欒锛夛紝鏈嶅姟鍣ㄤ細鍐嶆灏濊瘯淇锛岃繖涓娆¢噸寤烘暟鎹枃浠躲

·         濡傛灉淇浠嶇劧澶辫触锛屾湇鍔″櫒鐢ㄦ棫淇閫夐」鏂规硶鍐嶉噸璇曚竴娆′慨澶嶏紙涓琛屾帴涓琛屽湴鍐欙紝涓嶆帓搴忥級銆傝繖涓柟娉曞簲璇ヨ兘淇浠讳綍绫诲瀷鐨勯敊璇紝骞朵笖闇瑕佸緢浣庣殑纾佺洏绌洪棿銆

濡傛灉鎭㈠涓嶈兘澶熶粠鍏堝墠瀹屾垚鐨勮鍙ラ噷鎭㈠鎵鏈夎锛岃屼笖浣犱笉鑳藉湪--myisam-recover閫夐」鍊兼寚瀹欶ORCE锛岃嚜鍔ㄤ慨澶嶄細缁堟锛屽苟鍦ㄩ敊璇棩蹇楅噷鍐欎竴鏉¢敊璇俊鎭細

Error: Couldn't repair table: test.g00pages

濡傛灉浣犳寚瀹欶ORCE锛屽彇鑰屼唬涔嬪湴锛岀被浼艰繖鏍风殑涓涓鍛婅缁欏嚭锛

Warning: Found 344 of 354 rows when repairing ./test/g00pages

娉ㄩ噴锛氬鏋滆嚜鍔ㄦ仮澶嶅煎寘鎷珺ACKUP锛屾仮澶嶈繘绋嬪垱寤烘枃浠跺苟鐢tbl_name-datetime.BAK褰㈠紡鍙栧悕銆備綘搴旇鏈変竴涓cron鑴氭湰锛屽畠鑷姩鎶婅繖浜涙枃浠朵粠鏁版嵁搴撶洰褰曠Щ鍒板浠藉獟璐ㄤ笂銆

15.1.2.閿墍闇鐨勭┖闂

MyISAM琛ㄤ娇鐢˙鍨嬫爲绱㈠紩銆備綘鍙互绮楃暐鍦拌绠楃储寮曟枃浠剁殑澶у皬涓(key_length+4)/0.67, 鍔犱笂鎵鏈夌殑閿箣鍜屻傚綋鎵鏈夐敭浠ユ帓搴忕殑椤哄簭鎻掑叆骞朵笖琛ㄦ病鏈変换浣曞帇缂╃殑閿箣鏃讹紝浠ヤ笂浼拌鏄鏈鍧忕殑鎯呭喌鐨勩

瀛楃涓茬储寮曟槸琚┖闂村帇缂╃殑銆傚鏋滅涓涓瓧绗︿覆绱㈠紩閮ㄥ垎鏄瓧绗︿覆锛屽畠涔熻鍔犲墠缂鍘嬬缉銆傚鏋滃瓧绗︿覆鍒楁湁璁稿鎷栨洺绌洪棿锛屾垨瀛楃涓 鍒楁槸涓涓绘槸涓嶇敤瀹屽叏闀垮害鐨刅ARCHAR鍒楋紝绌洪棿鍘嬬缉浣垮緱绱㈠紩鏂囦欢姣旀渶鍧忔儏鍐垫椂鐨勬暟鍊艰灏忋傚墠缂鍘嬬缉琚敤鍦ㄤ互瀛楃涓插紑濮嬬殑閿笂銆傚鏋滄湁璁稿鍏锋湁鍚屼竴鍓嶇紑鐨勫瓧绗︿覆锛屽墠缂鍘嬬缉鏄湁甯姪鐨勩

鍦∕yISAM琛紝浣犱篃鍙互鍦ㄥ垱寤鸿〃鐨勬椂鍊欓氳繃鎸囧畾PACK_KEYS=1鏉ュ墠缂鍘嬬缉鏁板瓧銆傚綋鏁板瓧琚互楂樺瓧鑺備紭鍏堝瓨鍌ㄤ箣鏃讹紝鑻ヤ綘鏈夎澶氬叿鏈夊悓涓鍓嶇紑鐨勬暣鏁 閿紝涓婅堪鏂规硶鏄湁甯姪鐨勩

15.1.3MyISAM琛ㄧ殑瀛樺偍鏍煎紡

MyISAM鏀寔涓夌涓嶅悓瀛樺偍鏍煎紡銆傚叾涓袱涓紙鍥哄畾鏍煎紡鍜屽姩鎬佹牸寮)鏍规嵁姝d娇鐢ㄧ殑鍒楃殑绫诲瀷鏉ヨ嚜鍔ㄩ夋嫨銆傜涓変釜锛屽嵆宸插帇缂╂牸寮忥紝鍙兘浣跨敤myisampack宸ュ叿鏉ュ垱寤恒

褰撲綘CREATE鎴朅LTER涓涓病鏈塀LOB鎴朤EXT鍒楃殑琛紝浣犲彲浠ョ敤ROW_FORMAT琛ㄩ夐」寮哄埗琛ㄧ殑鏍煎紡涓篎IXED鎴朌YNAMIC銆傝繖 浼氬鑷碈HAR鍜孷ARCHAR鍒楀洜FIXED鏍煎紡鍙樻垚CHAR锛屾垨鍥燚YNAMIC鏍煎紡鍙樻垚VARCHAR銆

閫氳繃鐢ˋLTER TABLE鎸囧畾ROW_FORMAT={COMPRESSED | DEFAULT}锛屼綘鍙互鍘嬬缉鎴栬В鍘嬬缉琛紝璇峰弬闃13.1.5鑺傦紝鈥淐REATE TABLE璇硶”

15.1.3.1. 闈欐侊紙鍥哄畾闀垮害锛夎〃鐗瑰緛

闈欐佹牸寮忔槸MyISAM琛ㄧ殑榛樿瀛樺偍鏍煎紡銆傚綋琛ㄤ笉鍖呭惈鍙橀噺闀垮害鍒楋紙VARCHAR, BLOB, 鎴朤EXT锛夋椂锛屼娇鐢ㄨ繖涓牸寮忋傛瘡涓琛岀敤鍥哄畾瀛楄妭鏁板瓨鍌ㄣ

MyISAM鐨勪笁绉嶅瓨鍌ㄦ牸寮忎腑锛岄潤鎬佹牸寮忓氨鏈绠鍗曚篃鏄渶瀹夊叏鐨勶紙鑷冲皯瀵逛簬宕╂簝鑰岃█锛夈傞潤鎬佹牸寮忎篃鏄渶蹇殑on-disk鏍煎紡銆傚揩閫熸潵鑷簬鏁版嵁鏂囦欢涓殑琛屽湪纾佺洏涓婅鎵惧埌鐨勫鏄撴柟寮忥細褰撴寜鐓х储寮曚腑鐨勮鍙锋煡鎵句竴涓鏃讹紝鐢ㄨ闀垮害涔樹互琛屽彿銆傚悓鏍凤紝褰撴壂鎻忎竴涓〃鐨 鏃跺欙紝寰堝鏄撶敤姣忎釜纾佺洏璇绘搷浣滆涓瀹氭暟閲忕殑璁板綍銆

褰揗ySQL鏈嶅姟鍣ㄦ寰涓涓浐瀹氭牸寮廙yISAM鏂囦欢鍐欑殑鏃跺欙紝濡傛灉璁$畻鏈哄穿婧冧簡锛屽畨鍏ㄦ槸鏄剧劧鐨勩傚湪杩欑鎯呭喌涓嬶紝myisamchk鍙互瀹规槗鍦板喅瀹氭瘡琛屼粠鍝噷寮濮嬪埌鍝噷缁撴潫锛屾墍浠ュ畠閫氬父鍙互鏀跺洖鎵鏈夎褰曪紝闄や簡鍐欎簡涓閮ㄥ垎鐨勮褰曘傛敞鎰忥紝鍩轰簬鏁版嵁琛岋紝MyISAM琛ㄧ储寮曞彲浠ヤ竴鐩磋閲嶆柊鏋勫缓銆

闈欐佹牸寮忚〃鐨勪竴鑸壒寰侊細

·         CHAR鍒楀鍒楀搴︽槸绌洪棿濉ˉ鐨勩

·         闈炲父蹇

·         瀹规槗缂撳瓨銆

·         宕╂簝鍚庡鏄撻噸寤猴紝鍥犱负璁板綍浣嶄簬鍥哄畾浣嶇疆銆

·         閲嶆柊缁勭粐鏄笉蹇呰鐨勶紝闄ら潪浣犲垹闄ゅ法閲忕殑璁板綍骞朵笖甯屾湜涓烘搷浣滅郴缁熻吘鍑虹鐩樼┖闂淬備负姝わ紝鍙娇鐢∣PTIMIZE TABLE鎴栬myisamchk -r

·         閫氬父姣斿姩鎬佹牸寮忚〃闇瑕佹洿澶氱殑纾佺洏绌洪棿銆

15.1.3.2. 鍔ㄦ佽〃鐗瑰緛

濡傛灉涓涓狹yISAM琛ㄥ寘鍚换浣曞彲鍙橀暱搴 鍒楋紙VARCHAR, BLOB鎴朤EXTDynamic锛夛紝鎴栬呭鏋滀竴涓〃琚敤ROW_FORMAT=DYNAMIC閫夐」鏉ュ垱寤猴紝鍔ㄦ佸瓨鍌ㄦ牸寮忚浣跨敤銆

杩欎釜鏍煎紡鏇翠负澶嶆潅涓鐐癸紝鍥犱负姣忚鏈変竴涓〃鏄庤鏈夊闀跨殑澶淬傚綋涓涓褰曞洜涓烘洿鏂扮殑缁撴灉琚彉寰楁洿闀匡紝璇ヨ褰曚篃鍙互鍦ㄨ秴杩囦竴涓綅缃缁撴潫銆

浣犲彲浠ヤ娇鐢∣PTIMIZE TABLE鎴myisamchk鏉ュ涓涓〃鏁寸悊纰庣墖銆傚鏋滃湪涓涓〃涓湁浣犻绻佽闂垨鏀瑰彉鐨勫浐瀹氶暱搴 鍒楋紝琛ㄤ腑涔熸湁涓浜涘彲鍙橀暱搴﹀垪锛屼粎涓洪伩鍏嶇鐗囪屾妸杩欎簺鍙彉闀垮害鍒楃Щ鍒板叾瀹冭〃鍙兘鏄竴涓ソ涓绘剰銆

鍔ㄦ佹牸寮忚〃鐨勪竴鑸壒寰侊細

·         闄や簡闀垮害灏戜簬4鐨勫垪澶栵紝鎵鏈夌殑瀛楃涓插垪鏄姩鎬佺殑銆

·         鍦ㄦ瘡涓褰曞墠闈㈡槸涓涓綅鍥撅紝璇ヤ綅鍥捐〃鏄庡摢涓鍒楀寘鍚┖瀛楃涓诧紙瀵逛簬瀛楃涓插垪锛夋垨鑰0锛堝浜庢暟瀛楀垪锛夈傛敞鎰忥紝杩欏苟涓嶅寘鎷寘鍚玁ULL鍊肩殑鍒椼傚鏋滀竴涓瓧绗﹀垪鍦ㄦ嫋鏇崇┖闂寸Щ闄ゅ悗闀垮害涓洪浂锛屾垨鑰呬竴涓暟瀛 鍒椾负闆跺硷紝杩欓兘鍦ㄤ綅鍥句腑鏍囨敞浜嗕笖鍒椾笉琚繚瀛樺埌纾佺洏銆 闈炵┖瀛楃涓茶瀛樹负涓涓暱搴﹀瓧鑺傚姞瀛楃涓茬殑鍐呭銆

·         閫氬父姣斿浐瀹氶暱搴﹁〃闇瑕佹洿灏戠殑纾佺洏绌洪棿銆

·         姣忎釜璁板綍浠呬娇鐢ㄥ繀闇澶у皬鐨勭┖闂淬傚敖绠″姝わ紝濡傛灉涓涓褰曞彉澶э紝瀹冨氨鎸夐渶瑕佽鍒嗗紑鎴愬鐗囷紝閫犳垚璁板綍纰庣墖鐨勫悗鏋溿傛瘮濡傦紝浣犵敤鎵╁睍琛岄暱搴︾殑淇℃伅鏇存柊涓琛岋紝璇ヨ灏卞彉寰楁湁纰庣墖銆傚湪杩欑鎯呭喌涓嬶紝浣犲彲浠ユ椂涓嶆椂杩愯OPTIMIZE TABLE鎴myisamchk -r鏉ユ敼鍠勬ц兘銆傚彲浣跨敤myisamchk -ei鏉ヨ幏鍙栬〃鐨勭粺璁℃暟鎹

·         鍔ㄦ佹牸寮忚〃鍦ㄥ穿婧冨悗瑕佹瘮闈欐佹牸寮忚〃鏇撮毦閲嶅缓锛屽洜涓轰竴涓褰曞彲鑳借鍒嗕负澶氫釜纰庣墖涓旈摼鎺ワ紙纰庣墖锛夊彲鑳借涓㈠け銆

·         鍔ㄦ佸昂瀵歌褰曟湡鏈涚殑琛岄暱搴︾敤涓嬪垪琛ㄨ揪寮忔潵璁$畻锛

·                3
·                + (number of columns + 7) / 8
·                + (number of char columns)
·                + (packed size of numeric columns)
·                + (length of strings)
·                + (number of NULL columns + 7) / 8

瀵规瘡涓摼鎺ラ渶瑕侀澶栫殑6瀛楄妭銆傚湪涓涓洿鏂板鑷翠竴涓褰曠殑鎵╁ぇ涔嬫椂锛屼竴涓姩鎬佽褰曡閾炬帴浜嗐傛瘡涓柊閾炬帴鑷冲皯鏄20瀛楄妭锛屾墍浠ヤ笅涓涓墿澶у彲鑳藉湪鍚屾牱鐨勯摼鎺ラ噷杩涜銆傚鏋滀笉鏄紝鍒欏彟涓涓摼鎺ュ皢琚缓绔嬨備綘鍙互浣跨敤myisamchk -ed鏉ユ壘鍑洪摼鎺ョ殑鏁扮洰銆傛墍鏈夌殑閾炬帴鍙互鐢myisamchk -r鏉ョЩ闄ゃ

15.1.3.3. 宸插帇缂╄〃鐗瑰緛

宸插帇缂╁瓨鍌ㄦ牸寮忔槸鐢myisampack宸ュ叿鍒涘缓鐨勫彧璇绘牸寮忋

鎵鏈塎ySQL鍒嗗彂鐗堥噷閮介粯璁ゅ寘鎷myisampack銆傚凡鍘嬬缉琛ㄥ彲浠ョ敤myisamchk鏉ヨВ鍘嬬缉銆

宸插帇缂╄〃鏈変笅鍒楃壒寰侊細

·         宸插帇缂╄〃鍗犳嵁闈炲父灏忕殑纾佺洏绌洪棿銆傝繖鏈灏忓寲浜嗙鐩樼敤閲忥紝褰撲娇鐢ㄧ紦鎱㈢殑纾佺洏锛堝CD-ROM)涔嬫椂锛岃繖鏄緢鏈夌敤鐨勩

·         姣忎釜璁板綍鏄鍗曠嫭鍘嬬缉鐨勶紝鎵浠ュ彧鏈夐潪甯稿皬鐨勮闂紑鏀備緷鎹〃涓渶澶х殑璁板綍锛屼竴涓褰曠殑澶村湪姣忎釜琛ㄤ腑鍗犳嵁1鍒3涓瓧鑺傘傛瘡涓 鍒楄涓嶅悓鍦板帇缂┿傞氬父姣忎釜鍒楁湁涓涓笉鍚岀殑Huffman鏍戙備竴浜涘帇缂╃被鍨嬪涓嬶細

o        鍚庣紑绌洪棿鍘嬬缉銆

-        鍓嶇紑绌洪棿鍘嬬缉銆

-        闆跺肩殑鏁扮敤涓涓綅鏉ュ瓨鍌ㄣ

-        濡傛灉鍦ㄤ竴涓暣鍨嬪垪涓殑鍊兼湁涓涓皬鐨勮寖鍥达紝鍒楄鐢ㄦ渶灏忓彲鑳界殑绫诲瀷鏉ュ瓨鍌ㄣ傛瘮濡傦紝涓涓狟IGINT鍒楋紙8瀛楄妭锛夛紝濡傛灉鎵鏈夊畠鐨勫煎湪-128鍒127鑼冨洿鍐咃紝瀹冨彲浠ヨ瀛樺偍涓篢INYINT鍒楋紙1瀛楄妭锛

-        濡傛灉涓涓 鍒椾粎鏈変竴灏忕粍鍙兘鐨勫硷紝鍒楃殑绫诲瀷琚浆鍖栨垚ENUM銆

-        涓涓 鍒楀彲浠ヤ娇鐢ㄥ厛鍓嶅帇缂╃被鍨嬬殑浠绘剰鍚堝苟銆

·         鍙互澶勭悊鍥哄畾闀垮害鎴栧姩鎬侀暱搴﹁褰曘

15.1.4MyISAM琛 鏂归潰鐨勯棶棰

MySQL鐢ㄦ潵瀛樺偍鏁版嵁鐨勬枃浠舵牸寮忓凡缁忚骞挎硾娴嬭瘯杩囷紝浣嗘绘槸鏈夊鑷存暟鎹〃鍙樺緱鎹熷潖鐨勭幆澧冦

15.1.4.1. 鎹熷潖鐨凪yISAM琛

鍗充娇MyISAM琛ㄦ牸寮忛潪甯稿彲闈狅紙SQL璇彞瀵硅〃鍋氱殑鎵鏈夋敼鍙樺湪璇彞杩斿洖涔嬪墠琚啓涓嬶級锛屽鏋滀笅鍒椾换浣曚簨浠跺彂鐢燂紝浣犱緷鐒跺彲浠ヨ幏寰楁崯鍧忕殑琛細

·         mysqld杩涚▼鍦ㄥ啓涓棿琚潃鎺夈

·         鍙戠敓鏈鏈熺殑璁$畻鏈哄叧闂紙渚嬪锛岃绠楁満琚叧闂級銆

·         纭欢鏁呴殰銆

·         浣犲彲浠ュ悓鏃跺湪姝h鏈嶅姟鍣ㄤ慨鏀圭殑琛ㄤ笂浣跨敤澶栭儴绋嬪簭锛堝myisamchk锛夈

·         MySQL鎴朚yISAM浠g爜鐨勮蒋浠剁己闄枫

涓涓崯鍧忕殑琛ㄧ殑鍏稿瀷鐥囩姸濡備笅锛

·         褰撳湪浠庤〃涓夋嫨鏁版嵁涔嬫椂锛屼綘寰楀埌濡備笅閿欒锛

·                Incorrect key file for table: '...'. Try to repair it

·         鏌ヨ涓嶈兘鍦ㄨ〃涓壘鍒拌鎴栬繑鍥炰笉瀹屽叏鐨勬暟鎹

浣犲彲浠ョ敤CHECK TABLE statement璇彞鏉ユ鏌MyISAM鐨勫仴搴凤紝骞剁敤REPAIR TABLE淇涓涓 鎹熷潖鐨凪yISAM琛ㄣ傚綋mysqld涓嶈繍琛屼箣鏃讹紝浣犱篃鍙互鐢myisamchk鍛戒护妫鏌ユ垨淇悊涓涓〃銆傝鍙傞槄13.5.2.3鑺傦紝鈥淐HECK TABLE璇硶” 13.5.2.6鑺傦紝鈥淩EPAIR TABLE璇硶”锛屽拰5.9.5鑺傦紝鈥渕yisamchk — MyISAM琛ㄧ淮鎶ゅ伐鍏”

濡傛灉浣犵殑琛ㄥ彉寰楅绻佹崯鍧忥紝浣犲簲璇ヨ瘯鐫纭畾涓轰粈涔堜細杩欐牱鐨勫師鍥犮傝鏄庣櫧鐨勬渶閲嶈鐨勪簨鏄〃鍙樺緱鎹熷潖鏄笉鏄洜涓烘湇鍔″櫒宕╂簝鐨勭粨鏋溿備綘鍙互鍦ㄩ敊璇棩蹇椾腑鏌ユ壘鏈杩戠殑restarted mysqld娑堟伅鏉ユ棭鏈熼獙璇佽繖涓傚鏋滃瓨鍦ㄨ繖鏍蜂竴涓秷鎭紝鍒欒〃鎹熷潖鏄湇鍔″櫒姝绘帀鐨勪竴涓粨鏋滄槸寰堟湁鍙兘鐨勩傚惁鍒欙紝鎹熷潖鍙兘鍦ㄦ甯告搷浣滀腑鍙戠敓銆傝繖鏄竴涓己闄枫備綘搴旇璇曠潃鍒涘缓涓涓睍绀鸿繖涓棶棰樼殑鍙噸澶嶇敓鎴愮殑娴嬭瘯妗堜緥銆傝鍙傞槄A.4.2鑺傦紝鈥滃鏋淢ySQL淇濇寔宕╂簝锛岃鎬庝箞鍋”E.1.6鑺傦紝鈥滃鏋滃嚭鐜拌〃宕╂簝锛岃鐢熸垚娴嬭瘯妗堜緥”

15.1.4.2. 鏈閫傚綋鍏抽棴鐨勮〃鐨勯棶棰

姣忎釜MyISAM绱㈠紩鏂囦欢(.MYI)鍦ㄥご鏈変竴涓鏁板櫒锛屽畠鍙互琚敤鏉ユ鏌ヤ竴涓〃鏄惁琚伆褰撳湴鍏抽棴銆傚鏋滀綘浠嶤HECK TABLE鎴myisamchk寰楀埌涓嬪垪璀﹀憡锛屾剰鍛崇潃杩欎釜璁℃暟鍣ㄥ凡缁忎笉鍚屾浜嗭細

clients are using or haven't closed the table properly

杩欎釜璀﹀憡骞朵笉鏄畬鍏ㄦ剰鍛崇潃琛ㄥ凡琚牬鍧忥紝浣嗕綘鑷冲皯搴旇妫鏌ヨ〃銆

璁℃暟鍣ㄧ殑宸ヤ綔鏂瑰紡濡備笅锛

·         琛ㄥ湪MySQL涓涓娆¤鏇存柊锛岀储寮曟枃浠跺ご鐨勮鏁板櫒鍔犱竴銆

·         鍦ㄦ湭鏉ョ殑鏇存柊涓紝璁℃暟鍣ㄤ笉琚敼鍙樸

·         褰撹〃鐨勬渶鍚庡疄渚嬭鍏抽棴锛堝洜涓轰竴涓搷浣淔LUSH TABLE鎴栧洜涓哄湪琛ㄧ紦鍐插尯涓病鏈夌┖闂达級涔嬫椂锛岃嫢琛ㄥ凡缁忓湪浠讳綍鐐硅鏇存柊锛屽垯璁℃暟鍣ㄥ噺涓銆

·         褰撲綘淇悊鎴栨鏌ヨ〃骞朵笖鍙戠幇琛ㄥ畬濂戒箣鏃讹紝璁℃暟鍣ㄨ閲嶇疆涓洪浂銆

·         瑕侀伩鍏嶄笌鍏跺畠鍙兘妫鏌ヨ〃鐨勮繘绋嬭繘琛屼簨鍔$殑闂锛岃嫢璁℃暟鍣ㄤ负闆讹紝鍦ㄥ叧闂椂璁℃暟鍣ㄤ笉鍑忎竴銆

鎹㈠彞璇濇潵璇达紝璁℃暟鍣ㄥ彧鏈夊湪涓嬪垪鎯呭喌浼氫笉鍚屾锛

·         MyISAM琛ㄤ笉闅忕涓娆″彂鍑虹殑LOCK TABLES鍜孎LUSH TABLES琚鍒躲

·         MySQL鍦ㄤ竴娆℃洿鏂板拰鏈鍚庡叧闂箣闂村穿婧冿紙娉ㄦ剰锛岃〃鍙兘渚濈劧瀹屽ソ锛屽洜涓篗ySQL鎬绘槸鍦ㄦ瘡涓鍙ヤ箣闂翠负姣忎欢浜嬪彂鍑哄啓鎿嶄綔锛夈

·         涓涓〃琚myisamchk --recovermyisamchk --update-state淇敼锛屽悓鏃惰mysqld浣跨敤銆

·         澶氫釜mysqld鏈嶅姟鍣ㄦ浣跨敤琛紝骞朵笖涓涓湇鍔″櫒鍦ㄤ竴涓〃涓婃墽琛孯EPAIR TABLE鎴朇HECK TABLE锛屽悓鏃惰琛ㄤ篃琚彟涓涓湇鍔″櫒浣跨敤銆傚湪杩欎釜缁撴瀯涓紝浣跨敤CHECK TABLE鏄畨鍏ㄧ殑锛岃櫧鐒朵綘鍙兘浠庡叾瀹冩湇鍔″櫒涓婂緱鍒拌鍛娿傚敖绠″姝わ紝REPAIR TABLE搴旇琚伩鍏嶏紝鍥犱负褰撲竴涓湇鍔″櫒鐢ㄤ竴涓柊鐨勬暟鎹枃浠舵浛浠f棫鐨勪箣鏃讹紝杩欏苟娌℃湁鍙戦佷俊鍙峰埌鍏跺畠鏈嶅姟鍣ㄤ笂銆

鎬荤殑鏉ヨ锛屽湪澶氭湇鍔″櫒涔嬮棿鍒嗕韩涓涓暟鎹洰褰曟槸涓涓潖涓绘剰銆傝鍙傞槄5.12鑺傦紝鈥滃湪鍚屼竴涓満鍣ㄤ笂杩愯澶氫釜MySQL鏈嶅姟鍣” 鑾峰緱鏇村鍦拌璁

15.2. InnoDB瀛樺偍寮曟搸

15.2.1InnoDB姒傝堪

InnoDB缁橫ySQL鎻愪緵 浜嗗叿鏈夋彁浜わ紝鍥炴粴鍜屽穿婧冩仮澶嶈兘鍔涚殑浜嬪姟瀹夊叏锛圓CID鍏煎锛夊瓨鍌ㄥ紩鎿庛侷nnoDB閿佸畾鍦ㄨ绾у苟涓斾篃鍦⊿ELECT璇彞鎻愪緵涓涓狾racle椋庢牸涓鑷寸殑闈為攣瀹氳銆傝繖浜涚壒鑹插鍔 浜嗗鐢ㄦ埛閮ㄧ讲鍜屾ц兘銆傛病鏈夊湪InnoDB涓墿澶ч攣瀹氱殑闇瑕侊紝鍥犱负鍦↖nnoDB涓绾ч攣瀹氶傚悎闈炲父灏忕殑绌洪棿銆侷nnoDB涔熸敮鎸丗OREIGN KEY寮哄埗銆傚湪SQL鏌ヨ涓紝浣犲彲浠ヨ嚜鐢卞湴灏咺nnoDB绫诲瀷鐨勮〃涓庡叾瀹僊ySQL鐨勮〃鐨勭被鍨嬫贩鍚堣捣鏉ワ紝鐢氳嚦鍦ㄥ悓涓涓煡璇腑涔熷彲浠ユ贩鍚堛

InnoDB鏄负澶勭悊宸ㄥぇ鏁版嵁閲忔椂鐨勬渶澶фц兘璁捐銆傚畠鐨凜PU鏁堢巼鍙兘鏄换浣曞叾瀹冨熀浜庣鐩樼殑鍏崇郴鏁版嵁搴撳紩鎿庢墍涓嶈兘鍖规晫鐨勩

InnoDB瀛樺偍寮曟搸琚畬鍏ㄤ笌MySQL鏈嶅姟鍣ㄦ暣鍚堬紝InnoDB瀛樺偍寮曟搸涓哄湪涓诲唴瀛樹腑缂撳瓨鏁版嵁鍜岀储寮曡岀淮鎸佸畠鑷繁鐨勭紦鍐叉睜銆侷nnoDB瀛樺偍瀹冪殑琛紗绱㈠紩鍦ㄤ竴涓〃绌洪棿涓紝琛ㄧ┖闂村彲浠ュ寘鍚暟涓枃浠讹紙鎴栧師濮嬬鐩樺垎鍖猴級銆傝繖涓嶮yISAM琛ㄤ笉鍚岋紝姣斿鍦∕yISAM琛ㄤ腑姣忎釜琛ㄨ瀛樺湪鍒嗙鐨勬枃浠朵腑銆侷nnoDB 琛ㄥ彲浠ユ槸浠讳綍灏哄锛屽嵆浣垮湪鏂囦欢灏哄琚檺鍒朵负2GB鐨勬搷浣滅郴缁熶笂銆

InnoDB榛樿鍦拌鍖呭惈鍦∕ySQL浜岃繘鍒鍒嗗彂涓俉indows Essentials installer浣縄nnoDB鎴愪负Windows涓奙ySQL鐨 榛樿琛ㄣ

InnoDB琚敤鏉ュ湪浼楀闇瑕侀珮鎬ц兘鐨勫ぇ鍨嬫暟鎹簱绔欑偣涓婁骇鐢熴傝憲鍚嶇殑Internet鏂伴椈绔欑偣Slashdot.org杩愯鍦↖nnoDB涓娿侻ytrix, Inc.鍦↖nnoDB涓婂瓨鍌ㄨ秴杩1TB鐨勬暟鎹紝杩樻湁涓浜涘叾瀹冪珯鐐瑰湪InnoDB涓婂鐞嗗钩鍧囨瘡绉800娆℃彃鍏/鏇存柊鐨勮礋鑽枫

InnoDB鍦ㄥ拰MySQL涓鏍峰湪鍚屼竴涓狦NU GPL璇佷功锛岀2鐗堬紙1991骞6鏈堢増锛変笅鍙戣銆傛洿澶氭湁鍏矼ySQL璇佷功鐨勪俊鎭紝璇峰弬闃http://www.mysql.com/company/legal/licensing/

鍏充簬InnoDB瀛樺偍寮曟搸锛屽湪http://forums.mysql.com/list.php?22鏈変竴涓缁嗙殑璁哄潧銆

15.2.2InnoDB鑱旂郴淇℃伅

InnoDB寮曟搸鐨勫巶瀹剁殑鑱旂郴淇℃伅锛孖nnobase Oy鐨勮仈绯绘柟寮忓涓嬶細

Web site: http://www.innodb.com/
Email: <sales@innodb.com>
Phone: +358-9-6969 3250 (office)
       +358-40-5617367 (mobile)
 
Innobase Oy Inc.
World Trade Center Helsinki
Aleksanterinkatu 17
P.O.Box 800
00101 Helsinki
Finland

15.2.3InnoDB閰嶇疆

InnoDB瀛樺偍寮曟搸鏄粯璁ゅ湴琚厑璁哥殑銆傚鏋滀綘涓嶆兂鐢↖nnoDB琛紝浣犲彲浠ユ坊鍔爏kip-innodb閫夐」鍒癕ySQL閫夐」鏂囦欢銆

琚獻nnoDB瀛樺偍寮曟搸绠$悊鐨勪袱涓噸瑕佺殑鍩轰簬纾佺洏鐨勮祫婧愭槸InnoDB琛ㄧ┖闂存暟鎹枃浠跺拰瀹冪殑鏃ュ織鏂囦欢銆

濡傛灉浣犳寚瀹氭棤InnoDB閰嶇疆閫夐」锛孧ySQL灏嗗湪MySQL鏁版嵁鐩綍涓嬪垱寤轰竴涓悕涓篿bdata1鐨10MB澶у皬鐨勮嚜鍔ㄦ墿灞曟暟鎹枃浠讹紝浠ュ強涓や釜鍚嶄负ib_logfile0鍜宨b_logfile1鐨5MB澶у皬鐨勬棩蹇楁枃浠躲

娉ㄩ噴锛欼nnoDB缁橫ySQL鎻愪緵鍏锋湁鎻愪氦锛 鍥炴粴鍜屽穿婧冩仮澶嶈兘鍔涚殑浜嬪姟瀹夊叏锛圓CID鍏煎锛夊瓨鍌ㄥ紩鎿庛傚鏋滄嫙杩愯鐨勬搷浣滅郴缁熷拰纭欢涓嶈兘濡傚箍鍛婅鐨勯偅鏍疯繍琛岋紝InnoDB灏变笉鑳藉疄鐜板涓婅兘鍔銆傝澶氭搷浣滅郴缁熸垨纾佺洏瀛愮郴缁熷彲鑳戒负鏀瑰杽鎬ц兘鑰屽欢杩熸垨璁板綍鍐欐搷浣溿傚湪涓浜涙搷浣滅郴缁熶笂锛屽氨鏄郴缁熻皟鐢紙fsync()锛 涔熻绛夌潃锛岀洿鍒版墍鏈夋湭鍐欏叆宸茶鍒锋柊鏂囦欢鐨勬暟鎹湪琚埛鏂板埌绋冲畾鍐呭瓨涔嬪墠鍙互纭疄杩斿洖浜嗐傚洜涓鸿繖涓紝鎿嶄綔绯荤粺宕╂簝鎴栨帀鐢靛彲鑳芥崯鍧忓綋鍓嶆彁浜ょ殑鏁版嵁锛屾垨鑰呭湪鏈鍧忕殑 鎯呭喌锛屽洜涓哄啓鎿嶄綔宸茶璁板綍浜嗭紝鐢氳嚦鐮村潖浜嗘暟鎹簱銆傚鏋滄暟鎹畬鏁存у浣犲緢閲嶈锛屼綘搴旇鍦ㄧ敤浠讳綍绋嬪簭浜庣敓浜т腑涔嬪墠鍋氫竴浜“pull-the-plug”娴嬭瘯銆侻ac OS X 10.3 鍙婁互鍚庣増鏈紝InnoDB浣跨敤涓涓壒鍒殑fcntl()鏂囦欢 鍒锋柊鏂规硶銆傚湪Linux涓嬶紝寤鸿绂佹鍥炲啓缂撳瓨銆

鍦ˋTAPI纭洏涓婏紝涓涓被浼糷dparm -W0 /dev/hda鍛戒护鍙兘璧蜂綔鐢ㄣ灏忓績鏌愪簺椹卞姩鍣ㄦ垨鑰呯鐩樻帶鍒跺櫒鍙兘涓嶈兘绂佹鍥炲啓缂撳瓨銆

娉ㄩ噴锛氳鑾峰緱濂界殑鎬ц兘锛屼綘搴旇濡備笅闈緥瀛愭墍璁ㄨ閭f牱锛屾槑纭彁渚汭nnoDB鍙傛暟銆傝嚜鐒跺湴锛屼綘搴旇缂栬緫璁剧疆鏉ラ傚悎浣犵殑纭欢鍜岃姹傘

瑕佸缓绔婭nnoDB琛ㄧ┖闂存枃浠讹紝鍦╩y.cnf閫夐」鏂囦欢閲岀殑[mysqld]鑺傞噷浣跨敤innodb_data_file_path閫夐」銆傚湪Windows涓婏紝浣犲彲浠ユ浛浠e湴浣跨敤my.ini鏂囦欢銆俰nnodb_data_file_path鐨勫煎簲璇ヤ负涓涓垨澶氫釜 鏁版嵁鏂囦欢瑙勬牸鐨勫垪琛ㄣ傚鏋滀綘鍛藉悕涓涓互涓婄殑鏁版嵁鏂囦欢锛岀敤 鍒嗗彿(‘;’)鍒嗛殧瀹冧滑锛

innodb_data_file_path=datafile_spec1[;datafile_spec2]...

渚嬪锛氭妸鏄庣‘鍒涘缓鐨勫叿鏈夌浉鍚岀壒寰佺殑琛ㄧ┖闂翠綔涓洪粯璁よ缃殑璁剧疆鎿嶄綔濡備笅锛

[mysqld]
innodb_data_file_path=ibdata1:10M:autoextend

杩欎釜璁剧疆閰嶇疆涓涓彲鎵╁睍澶у皬鐨勫昂瀵镐负10MB鐨勫崟鐙枃浠讹紝鍚嶄负ibdata1銆傛病鏈夌粰鍑烘枃浠剁殑浣嶇疆锛屾墍浠ラ粯璁ょ殑鏄湪MySQL鐨勬暟鎹洰褰曞唴銆

灏哄澶у皬鐢∕鎴栬匞鍚庣紑鏉ユ寚瀹氳鏄庡崟浣嶆槸MB鎴栬匞B銆

涓涓〃绌洪棿锛屽畠鍦ㄦ暟鎹洰褰曢噷鍖呭惈涓涓悕涓篿bdata1鐨勫浐瀹氬昂瀵50MB鐨勬暟鎹枃浠跺拰涓涓悕涓篿bdata2澶у皬涓50MB鐨勮嚜鍔ㄦ墿灞曟枃浠讹紝鍏跺彲浠 鍍忚繖鏍疯閰嶇疆锛

[mysqld]
innodb_data_file_path=ibdata1:50M;ibdata2:50M:autoextend

涓涓寚瀹氭暟鎹枃浠剁殑瀹屽叏鍚庣紑鍖呮嫭鏂囦欢鍚嶏紝瀹冪殑灏哄鍜屾暟涓彲閫夊睘鎬э細

file_name:file_size[:autoextend[:max:max_file_size]]

autoextend灞炴у拰鍚庨潰璺熺潃鐨勫睘鎬у彧鍙鐢ㄦ潵瀵筰nnodb_data_file_path琛岄噷鏈鍚庝竴涓暟鎹枃浠躲

濡傛灉浣犲鏈鍚庣殑鏁版嵁鏂囦欢鎸囧畾autoextend閫夐」銆傚鏋滄暟鎹枃浠惰楀敖浜嗚〃绌洪棿涓殑鑷敱绌洪棿锛孖nnoDB灏辨墿灞曟暟鎹枃浠躲傛墿灞曠殑骞呭害鏄瘡娆8MB銆

濡傛灉纾佺洏宸叉弧锛屼綘鍙兘鎯宠鎶婂叾瀹冩暟鎹坊鍔犲埌鍙︿竴涓‖鐩樹笂銆傞噸鏂伴厤缃竴涓凡瀛樺湪琛ㄧ┖闂寸殑鎸囦护瑙15.2.7鑺傦紝鈥滄坊鍔犲拰鍒犻櫎InnoDB鏁版嵁鍜屾棩蹇楁枃浠”

InnoDB骞朵笉鎰熺煡鏈澶ф枃浠跺昂瀵革紝鎵浠ヨ灏忓績鏂囦欢绯荤粺锛屽湪閭d笂闈㈡渶澶х殑鏂囦欢灏哄鏄2GB銆傝涓轰竴涓嚜鍔ㄦ墿灞曟暟鎹枃浠舵寚瀹氭渶澶у昂瀵革紝璇蜂娇鐢╩ax灞炴с備笅鍒楅厤缃厑璁竔bdata1娑ㄥ埌鏋侀檺鐨500MB锛

[mysqld]
innodb_data_file_path=ibdata1:10M:autoextend:max:500M

InnoDB榛樿鍦板湪MySQL鏁版嵁鐩綍鍒涘缓琛ㄧ┖闂存枃浠躲傝鏄庣‘鎸囧畾涓涓綅缃紝璇蜂娇鐢╥nnodb_data_home_dir閫夐」銆傛瘮濡傦紝瑕佷娇鐢ㄤ袱涓悕涓篿bdata1鍜宨bdata2鐨勬枃浠讹紝浣嗘槸瑕佹妸浠栦滑鍒涘缓鍒/ibdata锛 鍍忓涓嬩竴鏍烽厤缃甀nnoDB锛

[mysqld]
innodb_data_home_dir = /ibdata
innodb_data_file_path=ibdata1:50M;ibdata2:50M:autoextend

娉ㄩ噴锛欼nnoDB涓嶅垱寤虹洰褰曪紝鎵浠ュ湪鍚姩鏈嶅姟鍣ㄤ箣鍓嶈纭/ibdata鐩綍鐨勭‘瀛樺湪銆傝繖瀵逛綘閰嶇疆鐨勪换浣曟棩蹇楁枃浠剁洰褰曟潵璇翠篃鏄湡瀹炵殑銆備娇鐢║nix鎴朌OS鐨刴kdir鍛戒护鏉ュ垱寤轰换浣曞繀闇鐨勭洰褰曘

閫氳繃鎶奿nnodb_data_home_dir鐨勫煎師鍘熸湰鏈湴閮ㄧ讲鍒版暟鎹枃浠跺悕锛屽苟鍦ㄩ渶瑕佺殑鍦版柟娣诲姞鏂滄潬鎴栧弽鏂滄潬锛孖nnoDB涓烘瘡涓暟鎹枃浠跺舰鎴愮洰褰曡矾寰勩傚鏋渋nnodb_data_home_dir閫夐」鏍规湰娌℃湁鍦╩y.cnf涓彁鍒帮紝 榛樿鍊兼槸“dot”鐩綍 ./锛岃繖鎰忔濇槸MySQL鏁版嵁鐩綍銆

濡傛灉浣犳寚瀹歩nnodb_data_home_dir涓轰竴涓┖瀛楃涓诧紝浣犲彲浠ヤ负鍒楀湪innodb_data_file_path鍊奸噷鐨勬暟鎹枃浠舵寚瀹氱粷瀵硅矾寰勩備笅闈㈢殑渚嬪瓙绛変环浜庡墠闈㈤偅涓緥瀛愶細

[mysqld]
innodb_data_home_dir =
innodb_data_file_path=/ibdata/ibdata1:50M;/ibdata/ibdata2:50M:autoextend

涓涓畝鍗曠殑my.cnf渚嬪瓙銆鍋囪浣犳湁涓鍙伴厤澶128MB鍐呭瓨鍜屼竴涓‖鐩樼殑璁$畻鏈恒備笅闈㈢殑渚嬪瓙鏄剧ず鍦╩y.cnf鎴杕y.ini閲屽InnoDB鍙兘鐨勯厤缃弬鏁帮紝鍖呮嫭autoextend灞炴с

杩欎釜渚嬪瓙閫傚悎澶у鏁板湪Unix鍜學indows涓婏紝涓嶆兂鍒嗛厤InnoDB鏁版嵁鏂囦欢鍜屾棩蹇楁枃浠跺埌鏁颁釜纾佺洏涓婄殑鐢ㄦ埛銆傚畠鍦∕ySQL鏁版嵁鐩綍鍒涘缓涓涓嚜鍔ㄦ墿灞曟暟鎹枃浠秈bdata1鍜屼袱涓棩蹇楁枃浠秈b_logfile0鍙奿b_logfile1銆傚悓鏍凤紝InnoD鍦ㄦ暟鎹洰褰曢噷鑷姩鍒涘缓鐨勫皬鍨嬫。妗圛nnoDB鏃ュ織鏂囦欢ib_arch_log_0000000000涔熺粨鏉熴

[mysqld]
# You can write your other MySQL server options here
# ...
# Data files must be able to hold your data and indexes.
# Make sure that you have enough free disk space.
innodb_data_file_path = ibdata1:10M:autoextend
#
# Set buffer pool size to 50-80% of your computer's memory
set-variable = innodb_buffer_pool_size=70M
set-variable = innodb_additional_mem_pool_size=10M
#
# Set the log file size to about 25% of the buffer pool size
set-variable = innodb_log_file_size=20M
set-variable = innodb_log_buffer_size=8M
#
innodb_flush_log_at_trx_commit=1

璇风‘璁ySQL鏈嶅姟鍣ㄦ湁閫傚綋鐨勬潈闄愬湪鏁版嵁鐩綍閲屽垱寤烘枃浠躲傛洿涓鑸湴锛屾湇鍔″櫒蹇呴』瀵逛换浣曞畠闇瑕佸垱寤烘暟鎹枃浠舵垨鏃ュ織鏂囦欢鐨勭洰褰曟湁璁块棶鏉冮檺銆

娉ㄦ剰锛屽湪涓浜涙枃浠剁郴缁熶笂锛屾暟鎹枃浠跺繀闇灏忎簬2GB銆傛暟鎹枃浠剁殑鍚堝苟灏哄蹇呴』鑷冲皯10MB銆

褰撲綘绗竴娆″垱寤轰竴涓狪nnoDB琛ㄧ┖闂达紝鏈濂戒粠鍛戒护琛屾潵鍚姩MySQL鏈嶅姟鍣ㄣ侷nnoDB鐒跺悗鎶婃暟鎹簱鍒涘缓鐨勪俊鎭墦鍗板埌灞忓箷锛屾墍浠ヤ綘鍙互鐪嬭姝e湪鍙戠敓浠涔堛傛瘮濡傦紝鍦╓indows涓婏紝濡傛灉mysqld-max浣嶄簬C:\mysql\bin锛屼綘鍙互濡備笅鏉ュ惎鍔ㄥ畠锛

C:\> C:\mysql\bin\mysqld-max --console

濡傛灉浣犱笉鍙戦佹湇鍔″櫒杈撳嚭鍒板睆骞曚笂锛岃妫鏌ユ湇鍔″櫒鐨勯敊璇棩蹇楁潵鐪嬪湪鍚姩杩囩▼涓璉nnoDB鎵撳嵃浜嗕粈涔堛

璇峰弬闃15.2.5鑺傦紝鈥滃垱寤篒nnoDB琛ㄧ┖闂”锛屼互鑾峰緱InnoDB鏄剧ず鐨勪俊鎭湅璧锋潵搴旇 鍍忎粈涔堢殑渚嬪瓙銆

Windows涓婂浣曟寚瀹氶夐」? 鍦╓indows涓婇夐」鏂囦欢鐨勮鍒欏涓嬶細

·         鍙簲璇ュ垱寤轰竴涓猰y.cnf鎴栨枃浠躲

·         my.cnf鏂囦欢搴旇琚斁鍦–鐩樻牴鐩綍銆

·         my.ini鏂囦欢搴旇琚斁缃湪WINDIR鐩綍锛涗緥濡侰:\WINDOWS鎴朇:\WINNT銆備綘鍙互鍦╓indows鎺у埗鍙扮殑鍛戒护鎻愮ず绗︿娇鐢⊿ET鍛戒护鏉ユ墦鍗癢INDIR鐨勫硷細

·                C:\> SET WINDIR
·                windir=C:\WINNT

·         濡傛灉浣犵殑PC鍦–鐩樹笉鏄惎鍔ㄧ洏鐨勫湴鏂逛娇鐢ㄥ惎鍔ㄨ杞芥満锛屼綘鍞竴鐨勯夋嫨鏄娇鐢╩y.ini鏂囦欢銆

·         濡傛灉浣犱娇鐢ㄥ畨瑁呭拰閰嶇疆鍚戝瀹夎鐨凪ySQL锛宮y.ini鏂囦欢琚斁鍦∕ySQL鐨勫畨瑁呯洰褰曘傝鍙傞槄2.3.5.14鑺傦紝鈥渕y.ini鏂囦欢鐨勪綅缃”

Unix涓婂湪鍝噷鎸囧畾閫夐」锛 鍦║nix涓婏紝mysqld浠庝笅鍒楁枃浠讹紝濡傛灉瀹冧滑瀛樺湪鐨勮瘽銆備互涓嬪垪鐨勯『搴忚鍙栭夐」锛

·         /etc/my.cnf

鍏ㄥ眬閫夐」銆

·         $MYSQL_HOME/my.cnf

鏈嶅姟鍣ㄤ笓鐢ㄩ夐」銆

·         defaults-extra-file

--defaults-extra-file閫夐」鎸囧畾鐨勬枃浠躲

·         ~/.my.cnf

鐢ㄦ埛涓撶敤閫夐」銆

MYSQL_HOME浠h〃鐜鍙橀噺锛屽畠鍐呭惈鐫鍒板寘鍚湇鍔″櫒涓撶敤my.cnf鏂囦欢鐨勭洰褰曠殑璺緞銆

濡傛灉浣犵‘淇mysqld鍙粠鎸囧畾鏂囦欢璇诲彇閫夐」锛屼綘鍙互鍦ㄥ惎鍔ㄦ湇鍔″櫒涔嬫椂鍦ㄥ懡浠よ浣跨敤--defaults-option浣滀负绗竴涓夐」锛

mysqld --defaults-file=your_path_to_my_cnf

涓涓珮绾х殑my.cnf渚嬪瓙銆鍋囪浣犳湁涓鍙癓inux璁$畻鏈猴紝鏈2GB鍐呭瓨鍜屼笁涓60GB纭洏锛堝湪鐩綍璺緞/, /dr2鍜/dr3)銆備笅鍒椾緥瀛愭樉绀 浜嗗湪my.cnf閲屽InnoDB鍙兘鐨勯厤缃弬鏁般

[mysqld]
# You can write your other MySQL server options here
# ...
innodb_data_home_dir =
#
# Data files must be able to hold your data and indexes
innodb_data_file_path = /ibdata/ibdata1:2000M;/dr2/ibdata/ibdata2:2000M:autoextend
#
# Set buffer pool size to 50-80% of your computer's memory,
# but make sure on Linux x86 total memory usage is < 2GB
innodb_buffer_pool_size=1G
innodb_additional_mem_pool_size=20M
innodb_log_group_home_dir = /dr3/iblogs
#
innodb_log_files_in_group = 2
#
# Set the log file size to about 25% of the buffer pool size
innodb_log_file_size=250M
innodb_log_buffer_size=8M
#
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=50
#
# Uncomment the next lines if you want to use them
#innodb_thread_concurrency=5

娉ㄦ剰锛岃渚嬪瓙鎶婁袱涓暟鎹枃浠舵斁鍦ㄤ笉鍚岀鐩樹笂銆侷nnoDB寮濮嬬敤绗竴涓暟鎹枃浠跺~鍏呰〃绌洪棿銆傚湪涓浜涙儏鍐典笅锛屽鏋滄墍鏈夋暟鎹笉琚斁缃湪鍚屼竴鐗╃悊纾佺洏涓婏紝杩欐牱灏嗘敼鍠勬暟鎹簱鐨勬ц兘銆傛妸鏃ュ織鏂囦欢鏀惧湪涓庢暟鎹枃浠朵笉鍚岀殑纾佺洏涓婂鎬ц兘鏄粡甯稿緢鏈夊ソ澶勭殑銆備綘涔熷彲浠ヤ娇鐢ㄥ師濮嬬鐩樺垎鍖猴紙鍘熷璁惧锛変綔涓篒nnoDB鏁版嵁鏂囦欢锛岃繖鏍峰彲浠ュ姞閫烮/O銆傝鍙傞槄15.2.14.2鑺傦紝鈥滀负琛ㄧ┖闂翠娇鐢ㄥ師濮嬭澶”

璀﹀憡锛氬湪32浣岹NU/Linux x86涓,浣犲繀椤昏灏忓績涓嶈璁剧疆杩囬珮鐨勫唴瀛樼敤閲忋俫libc鍙兘鍏佽杩涚▼鍫嗙Н鍦ㄧ嚎绋嬪爢鏍堜笂鍙戝睍锛屽畠浼氶犳垚浣犵殑鏈嶅姟鍣ㄥ穿婧冦傚鏋滀笅鍒楄〃杈惧紡鐨勫兼帴杩戞垨鑰呰秴杩2GB锛岀郴缁熶細闈复鍗辨満锛

innodb_buffer_pool_size
+ key_buffer_size
+ max_connections*(sort_buffer_size+read_buffer_size+binlog_cache_size)
+ max_connections*2MB

姣忎釜绾跨▼浣跨敤涓涓爢鏍堬紙閫氬父鏄2MB锛屼絾鍦∕ySQL AB浜岃繘鍒鍒嗗彂鐗閲屽彧鏈256KB锛夊苟涓斿湪鏈鍧忕殑鎯呭喌涓嬩篃浣跨敤sort_buffer_size + read_buffer_size闄勫姞鍐呭瓨銆

浣犲彲浠ヨ嚜宸辩紪璇慚ySQL锛屽湪32浣峎indows涓婁娇鐢ㄩ珮杈64GB鐗╃悊鍐呭瓨銆傝鍙傞槄15.2.4鑺傦紝鈥淚nnoDB鍚姩閫夐」”閲屽innodb_buffer_pool_awe_mem_mb鐨勬弿杩般

濡備綍璋冩暣鍏跺畠mysqld鏈嶅姟鍣ㄥ弬鏁帮紵涓嬪垪鍊兼槸鍏稿瀷鐨勶紝涓旈傜敤浜庡鏁扮敤鎴凤細

[mysqld]
skip-external-locking
max_connections=200
read_buffer_size=1M
sort_buffer_size=1M
#
# Set key_buffer to 5 - 50% of your RAM depending on how much
# you use MyISAM tables, but keep key_buffer_size + InnoDB
# buffer pool size < 80% of your RAM
key_buffer_size=value

15.2.4InnoDB鍚姩閫夐」

杩欎竴鑺傚彊杩癐nnoDB鐩稿叧鐨勬湇鍔″櫒閫夐」锛屾墍鏈夎繖浜涢夐」鍙互浠--opt_name=value鐨勫舰寮忓湪鍛戒护琛屾垨鍦ㄩ夐」鏂囦欢閲岃鎸囧畾銆

·         innodb_additional_mem_pool_size

InnoDB鐢ㄦ潵瀛樺偍鏁版嵁鐩綍淇℃伅锛嗗叾瀹冨唴閮ㄦ暟鎹粨鏋勭殑鍐呭瓨姹犵殑澶у皬銆備綘搴旂敤绋嬪簭閲岀殑琛ㄨ秺澶氾紝浣犻渶瑕佸湪杩欓噷鍒嗛厤瓒婂鐨勫唴瀛樸傚鏋淚nnoDB鐢ㄥ厜浜嗚繖涓睜鍐呯殑鍐呭瓨锛孖nnoDB寮濮嬩粠鎿嶄綔绯荤粺鍒嗛厤鍐呭瓨锛屽苟涓斿線MySQL閿欒鏃ュ織鍐欒鍛婁俊鎭 榛樿鍊兼槸1MB銆

·         innodb_autoextend_increment

褰撹嚜鍔ㄦ墿灞曡〃绌洪棿琚~婊′箣鏃讹紝涓烘墿灞曡屽鍔犵殑灏哄锛圡B涓哄崟浣嶏級銆 榛樿鍊兼槸8銆傝繖涓夐」鍙互鍦ㄨ繍琛屾椂浣滀负鍏ㄥ眬绯荤粺鍙橀噺鑰屾敼鍙樸

·         innodb_buffer_pool_awe_mem_mb

濡傛灉缂撳啿姹犺鏀惧湪32浣峎indows鐨凙WE鍐呭瓨閲岋紝杩欎釜鍙傛暟灏辨槸缂撳啿姹犵殑澶у皬锛圡B涓哄崟浣嶏級銆(浠呭湪32浣峎indows涓婄浉鍏筹級濡傛灉浣犵殑32浣峎indows鎿嶄綔绯荤粺浣跨敤鎵璋撶殑“鍦板潃绐楀彛鎵╁睍锛AWE)”鏀寔瓒呰繃4GB鍐呭瓨锛屼綘鍙互鐢ㄨ繖涓弬鏁版妸InnoDB缂撳啿姹犲垎閰嶈繘AWE鐗╃悊鍐呭瓨銆傝繖涓弬鏁版渶澶х殑鍙兘鍊兼槸64000銆傚鏋滆繖涓弬鏁拌鎸囧畾浜嗭紝innodb_buffer_pool_size鏄湪32浣嶅湴鍧绌洪棿鐨mysqld鍐呯殑绐楀彛锛孖nnoDB鎶婇偅涓狝WE鍐呭瓨鏄犲皠涓婂幓銆傚innodb_buffer_pool_size鍙傛暟锛屼竴涓瘮杈冨ソ鐨勫兼槸500MB銆

·         innodb_buffer_pool_size

InnoDB鐢ㄦ潵缂撳瓨瀹冪殑鏁版嵁鍜岀储寮曠殑鍐呭瓨缂撳啿鍖虹殑澶у皬銆備綘鎶婅繖涓艰寰楄秺楂橈紝璁块棶琛ㄤ腑鏁版嵁闇瑕佸緱纾佺洏I/O瓒婂皯銆傚湪涓涓笓鐢ㄧ殑鏁版嵁搴撴湇鍔″櫒涓婏紝浣犲彲浠ヨ缃繖涓弬鏁拌揪鏈哄櫒鐗╃悊鍐呭瓨澶у皬鐨80%銆傚敖绠″姝わ紝杩樻槸涓嶈鎶婂畠璁剧疆寰楀お澶э紝鍥犱负瀵圭墿鐞嗗唴瀛樼殑绔炰簤鍙兘鍦ㄦ搷浣滅郴缁熶笂瀵艰嚧鍐呭瓨璋冨害銆

·         innodb_checksums

InnoDB鍦ㄦ墍鏈夊纾佺洏鐨勯〉闈㈣鍙栦笂浣跨敤鏍¢獙鍜岄獙璇佷互纭繚棰濆瀹归敊闃叉纭欢鎹熷潖鎴栨暟鎹枃浠躲傚敖绠″姝わ紝鍦ㄤ竴浜涘皯瑙佺殑鎯呭喌涓嬶紙姣斿杩愯鏍囧噯妫鏌ヤ箣鏃讹級杩欎釜棰濆鐨勫畨鍏ㄧ壒寰佹槸涓嶅繀瑕佺殑銆傚湪杩欎簺鎯呭喌涓嬶紝杩欎釜閫夐」锛 榛樿鏄厑璁哥殑锛夊彲浠ョ敤--skip-innodb-checksums鏉ュ叧闂

·         innodb_data_file_path

鍒板崟鐙暟鎹枃浠跺拰瀹冧滑灏哄鐨勮矾寰勩傞氳繃鎶奿nnodb_data_home_dir杩炴帴鍒拌繖閲屾寚瀹氱殑姣忎釜璺緞锛屽埌姣忎釜鏁版嵁鏂囦欢鐨勫畬鏁寸洰褰曡矾寰勫彲琚幏寰椼傛枃浠跺ぇ灏忛氳繃缁欏昂瀵稿煎熬鍔燤鎴朑浠B鎴栬匞B锛1024MB锛変负鍗曚綅琚寚瀹氥傛枃浠跺昂瀵哥殑鍜岃嚦灏戞槸10MB銆傚湪涓浜涙搷浣滅郴缁熶笂锛屾枃浠跺繀椤诲皬浜2GB銆傚鏋滀綘娌℃湁鎸囧畾innodb_data_file_path锛屽紑濮嬬殑榛樿琛屼负鏄垱寤轰竴涓崟鐙殑澶у皬10MB鍚嶄负ibdata1鐨勮嚜鎵╁睍鏁版嵁鏂囦欢銆傚湪閭d簺鏀寔澶ф枃浠剁殑鎿嶄綔绯荤粺涓婏紝浣犲彲浠ヨ缃枃浠跺ぇ灏忚秴杩4GB銆備綘涔熷彲浠ヤ娇鐢ㄥ師濮嬬鐩樺垎鍖轰綔涓烘暟鎹枃浠讹紝璇峰弬闃15.2.14.2鑺傦紝鈥滀负琛ㄧ┖闂翠娇鐢ㄥ師濮嬭澶”

·         innodb_data_home_dir

鐩綍璺緞瀵规墍鏈塈nnoDB鏁版嵁鏂囦欢鐨勫叡鍚岄儴鍒嗐傚鏋滀綘涓嶈缃繖涓硷紝 榛樿鏄疢ySQL鏁版嵁鐩綍銆備綘涔熷彲浠ユ寚瀹氳繖涓间负涓涓┖瀛楃涓诧紝鍦ㄨ繖绉嶆儏鍐典笅锛屼綘鍙互鍦╥nnodb_data_file_path涓娇鐢ㄧ粷瀵规枃浠惰矾寰勩

·         innodb_doublewrite

榛樿鍦帮紝InnoDB瀛樺偍鎵鏈夋暟鎹袱娆★紝绗竴娆″瓨鍌ㄥ埌doublewrite缂撳啿锛岀劧鍚庡瓨鍌ㄥ埌纭疄鐨勬暟鎹枃浠躲傝繖涓夐」鍙互琚敤鏉ョ姝㈣繖涓姛鑳姐傜被浼间簬innodb_checksums锛岃繖涓夐」 榛樿鏄厑璁哥殑锛涘洜涓烘爣鍑嗘鏌ユ垨鍦ㄥ椤剁骇鎬ц兘鐨勯渶瑕佽秴杩囧鏁版嵁瀹屾暣鎬ф垨鍙兘鏁呴殰鐨勫叧娉ㄤ箣鏃讹紝杩欎釜閫夐」鐢--skip-innodb-doublewrite鏉ュ叧闂

·         innodb_fast_shutdown

濡傛灉浣犳妸杩欎釜鍙傛暟璁剧疆涓0锛孖nnoDB鍦ㄥ叧闂箣鍓嶅仛涓涓畬鍏ㄥ噣鍖栧拰涓涓彃鍏ョ紦鍐插悎骞躲傝繖浜涙搷浣滆鑺卞嚑鍒嗛挓鏃堕棿锛岃缃湪鏋佺鎯呭喌涓嬭鍑犱釜灏忔椂銆傚鏋滀綘璁剧疆杩欎釜鍙傛暟涓1锛孖nnoDB鍦ㄥ叧闂箣鏃惰烦杩囪繖浜涙搷浣溿 榛樿鍊间负1銆傚鏋滀綘璁剧疆杩欎釜鍊间负2 (鍦∟etware鏃犳鍊)锛 InnoDB灏嗗埛鏂板畠鐨勬棩蹇楃劧鍚庡喎鍏虫満锛屼豢浣汳ySQL宕╂簝涓鏍枫傚凡鎻愪氦鐨勪簨鍔′笉浼氳涓㈠け锛屼絾鍦ㄤ笅涓娆″惎鍔ㄤ箣鏃朵細鍋氫竴涓穿婧冩仮澶嶃

·         innodb_file_io_threads

InnoDB涓枃浠禝/O绾跨▼鐨勬暟閲忋傛甯稿湴锛岃繖涓弬鏁版槸鐢 榛樿鐨勶紝榛樿鍊兼槸4锛屼絾鏄ぇ鏁板煎Windows纾佺洏I/O鏈夌泭銆傚湪Unix涓婏紝澧炲姞杩欎釜鏁版病鏈夋晥鏋滐紝InnoDB鎬绘槸浣跨敤榛樿鍊笺

·         innodb_file_per_table

杩欎釜閫夐」鑷翠娇InnoDB鐢ㄨ嚜宸辩殑.ibd鏂囦欢涓哄瓨鍌ㄦ暟鎹拰绱㈠紩鍒涘缓姣忎竴涓柊琛紝鑰屼笉鏄湪鍏变韩琛ㄧ┖闂翠腑鍒涘缓銆傝鍙傞槄15.2.6.6鑺傦紝鈥滀娇鐢≒er-Table琛ㄧ┖闂”

·         innodb_flush_log_at_trx_commit

褰搃nnodb_flush_log_at_trx_commit琚 璁剧疆涓0锛屾棩蹇楃紦鍐叉瘡绉掍竴娆″湴琚啓鍒版棩蹇楁枃浠讹紝骞朵笖瀵规棩蹇楁枃浠跺仛鍒扮鐩樻搷浣滅殑鍒锋柊锛屼絾鏄湪涓涓簨鍔℃彁浜や笉鍋氫换浣曟搷浣溿傚綋杩欎釜鍊间负1锛堥粯璁ゅ硷級涔嬫椂锛屽湪姣忎釜浜嬪姟鎻愪氦鏃讹紝鏃ュ織缂撳啿琚啓鍒版棩蹇楁枃浠讹紝瀵规棩蹇楁枃浠跺仛鍒扮鐩樻搷浣滅殑 鍒锋柊銆傚綋璁剧疆涓2涔嬫椂锛屽湪姣忎釜鎻愪氦锛屾棩蹇楃紦鍐茶鍐欏埌鏂囦欢锛屼絾涓嶅鏃ュ織鏂囦欢鍋氬埌纾佺洏鎿嶄綔鐨勫埛鏂般傚敖绠″姝わ紝鍦ㄥ鏃ュ織鏂囦欢鐨勫埛鏂板湪鍊间负2鐨勬儏鍐典篃姣忕鍙戠敓涓娆°傛垜浠繀椤绘敞鎰忓埌锛屽洜涓鸿繘绋嬪畨鎺掗棶棰橈紝姣忕涓娆$殑 鍒锋柊涓嶆槸100%淇濊瘉姣忕閮藉彂鐢熴備綘鍙互閫氳繃璁剧疆杩欎釜鍊间笉涓1鏉ヨ幏寰楄緝濂界殑鎬ц兘锛屼絾闅忎箣浣犱細鍦ㄤ竴娆″穿婧冧腑鎹熷け浜屽垎涔嬩竴浠峰肩殑浜嬪姟銆傚鏋滀綘璁剧疆杩欎釜鍊间负0锛岄偅涔堜换浣mysqld杩涚▼鐨勫穿婧冧細鍒犻櫎宕╂簝鍓嶆渶鍚庝竴绉掔殑浜嬪姟锛屽鏋滀綘璁剧疆杩欎釜鍊间负2锛岄偅涔堝彧鏈夋搷浣滅郴缁熷穿婧冩垨鎺夌數鎵嶄細鍒犻櫎鏈鍚庝竴绉掔殑浜嬪姟銆傚敖绠″姝わ紝InnoDB鐨勫穿婧冩仮澶嶄笉鍙楀奖鍝嶏紝鑰屼笖鍥犱负杩欐牱宕╂簝鎭㈠寮濮嬩綔鐢ㄨ屼笉鑰冭檻杩欎釜鍊笺傛敞鎰忥紝璁稿鎿嶄綔绯荤粺鍜屼竴浜涚鐩樼‖浠朵細娆洪獥 鍒锋柊鍒扮鐩樻搷浣溿傚敖绠″埛鏂版病鏈夎繘琛岋紝浣犲彲浠ュ憡璇mysqld鍒锋柊宸茬粡杩涜銆傚嵆浣胯缃繖涓间负1锛屼簨鍔$殑鎸佷箙绋嬪害涓嶈淇濊瘉锛屼笖鍦ㄦ渶鍧忔儏鍐典笅鎺夌數鐢氳嚦浼氱牬鍧廔nnoDB鏁版嵁搴撱傚湪SCSI纾佺洏鎺у埗鍣ㄤ腑锛屾垨鍦ㄧ鐩樿嚜韬腑锛屼娇鐢ㄦ湁鍚庡鐢垫睜鐨勭鐩樼紦瀛樹細鍔犻熸枃浠 鍒锋柊骞朵笖浣垮緱鎿嶄綔鏇村畨鍏ㄣ備綘涔熷彲浠ヨ瘯鐫浣跨敤Unix鍛戒护hdparm鏉ュ湪纭欢缂撳瓨涓姝㈢鐩樺啓缂撳瓨锛屾垨浣跨敤鍏跺畠涓浜涘纭欢鎻愪緵鍟嗕笓鐢ㄧ殑鍛戒护銆傝繖涓夐」鐨 榛樿鍊兼槸1銆

·         innodb_flush_method

杩欎釜閫夐」鍙鍦║nix绯荤粺涓婃湁鏁堛傚鏋滆繖涓夐」琚缃负fdatasync 锛堥粯璁ゅ硷級锛孖nnoDB浣跨敤fsync()鏉ュ埛鏂版暟鎹拰鏃ュ織鏂囦欢銆傚鏋滆璁剧疆涓篛_DSYNC锛孖nnoDB浣跨敤O_SYNC鏉ユ墦寮骞跺埛鏂版棩蹇楁枃浠讹紝浣嗕娇鐢╢sync()鏉 鍒锋柊鏁版嵁鏂囦欢銆傚鏋淥_DIRECT琚寚瀹氫簡锛堝湪涓浜汫NU/Linux鐗堟湰鍟嗗彲鐢級锛孖nnoDB浣跨敤O_DIRECT鏉ユ墦寮鏁版嵁鏂囦欢锛屽苟浣跨敤fsync()鏉ュ埛鏂版暟鎹拰鏃ュ織鏂囦欢銆傛敞鎰忥紝InnoDB浣跨敤fsync()鏉ユ浛浠datasync()锛屽苟涓斿畠 榛樿涓嶄娇鐢∣_DSYNC锛屽洜涓鸿繖涓煎湪璁稿Unix鍙樼涓婂凡缁忓彂鐢熼棶棰樸

·         innodb_force_recovery

璀﹀憡锛氳繖涓夐」浠呭湪涓涓揣鎬ユ儏鍐典笅琚畾涔夛紝褰撴椂浣犳兂瑕佷粠鎹熷潖鐨勬暟鎹簱杞偍琛ㄣ傚彲鑳界殑鍊间负浠1鍒6銆傝繖浜涘肩殑鎰忔濆湪15.2.8.1鑺傦紝鈥滃己鍒舵仮澶”涓彊杩般備綔涓轰竴涓畨鍏ㄦ帾鏂斤紝褰撹繖涓夐」鍊煎ぇ浜庨浂涔嬫椂锛孖nnoDB闃绘鐢ㄦ埛淇敼鏁版嵁銆

·         innodb_lock_wait_timeout

InnoDB浜嬪姟鍦ㄨ鍥炴粴涔嬪墠鍙互绛夊緟涓涓攣瀹氱殑瓒呮椂绉掓暟銆侷nnoDB鍦ㄥ畠鑷繁鐨 閿佸畾琛ㄤ腑鑷姩妫娴嬩簨鍔℃閿佸苟涓斿洖婊氫簨鍔°侷nnoDB鐢↙OCK TABLES璇彞娉ㄦ剰鍒伴攣瀹氳缃傞粯璁ゅ兼槸50绉掋

涓哄湪涓涓鍒跺缓绔嬩腑鏈澶у彲鑳界殑鎸佷箙绋嬪害鍜岃繛璐э紝浣犲簲璇ュ湪涓绘湇鍔″櫒涓婄殑my.cnf鏂囦欢閲屼娇鐢╥nnodb_flush_log_at_trx_commit=1鍜宻ync-binlog=1銆

·         innodb_locks_unsafe_for_binlog

杩欎釜閫夐」鍦↖nnoDB鎼滅储鍜岀储寮曟壂鎻忎腑鍏抽棴涓嬩竴閿攣瀹氥傝繖涓夐」鐨 榛樿鍊兼槸鍋囷紙false锛夈

姝e父鍦帮紝InnoDB浣跨敤涓涓绉颁负next-key locking鐨勭畻娉曘傚綋鎼滅储鎴栨壂鎻忎竴涓〃绱㈠紩涔嬫椂锛孖nnoDB浠ヨ繖鏍蜂竴绉嶆柟寮忓疄琛岃绾ч攣瀹氾紝瀹冨浠讳綍閬囧埌鐨勭储寮曡褰曡缃叡浜殑鎴栫嫭鍗犵殑閿佸畾銆傚洜姝わ紝琛岀骇閿佸畾瀹為檯鏄储寮曡褰曢攣瀹氥侷nnoDB瀵圭储寮曡褰曡缃殑閿佸畾涔熷奖鍝嶈閿佸畾绱㈠紩璁板綍涔嬪墠鐨“gap”銆傚鏋滀竴涓敤鎴峰鏌愪竴绱㈠紩鍐呯殑璁板綍R鍙堝叡浜殑鎴栫嫭鍗犵殑閿佸畾锛屽彟涓涓敤鎴蜂笉鑳界珛鍗冲湪R涔嬪墠浠ョ储寮曠殑椤哄簭鎻掑叆涓涓柊鐨勭储寮曡褰曘傝繖涓夐」瀵艰嚧InnoDB涓嶅湪鎼滅储鎴栫储寮曟壂鎻忎腑浣跨敤涓嬩竴 閿攣瀹氥備笅涓閿攣瀹氫粛鐒惰鐢ㄦ潵纭繚澶栭敭寮哄埗鍙婇噸澶嶉敭鏍告煡銆傛敞鎰忥紝浣跨敤杩欎釜閫夐」鍙兘浼氬鑷翠竴浜涜寮傜殑闂锛氬亣璁句綘鎯宠鐢ㄥ煎ぇ浜100鐨勬爣璇嗙浠庡瓙琛ㄩ噷璇诲彇骞堕攣瀹氭墍鏈夌殑瀛愯褰曪紝鍚屾椂 鍚戦殢鍚庡湪閫夊畾鐨勮鏇存柊涓浜涘垪锛

SELECT * FROM child WHERE id > 100 FOR UPDATE;

鍋囪鍦id鍒楁湁涓涓储寮曘傛煡璇粠id澶т簬100鐨勭涓涓褰曞紑濮嬫壂鎻忕储寮曘傚鏋滃湪绱㈠紩璁板綍涓婄殑閿佸畾涓嶆妸鍦ㄩ棿闅欏鐢熸垚鐨勬彃鍏ユ帓闄ら攣瀹氾紝鍚屾椂涓涓柊琛岃鎻掕繘琛ㄤ腑銆傚鏋滀綘鍦ㄥ悓涓涓簨鍔′箣鍐呮墽琛屽悓鏍风殑SELECT锛屼綘浼氬湪鏌ヨ杩斿洖鐨勭粨鏋滃寘閲岀湅鍒颁竴涓柊琛屻傝繖涔熸剰鍛崇潃锛屽鏋滄柊鏉$洰琚姞杩涙暟鎹簱锛孖nnoDB涓嶄繚璇佽繛缁э紱灏界濡傛锛 瀵瑰簲杩炵画鎬т粛琚繚璇併傚洜姝わ紝濡傛灉杩欎釜閫夐」琚娇鐢紝InnoDB鍦ㄥぇ澶氭暟瀛ょ珛绾у埆淇濊瘉READ COMMITTED銆

杩欎釜閫夐」鐢氳嚦鏇翠笉瀹夊叏銆侷nnoDB鍦ㄤ竴涓猆PDATE鎴朌ELETE涓彧閿佸畾瀹冩洿鏂版垨鍒犻櫎鐨勮銆傝繖澶уぇ鍑忓皯浜嗘閿佺殑鍙兘鎬э紝浣嗘槸鍙互鍙戠敓姝婚攣銆傛敞鎰忥紝鍗充娇鍦ㄥ綋绫讳技鐨勬搷浣滃奖鍝嶄笉鍚岃鏃剁殑鎯呭喌涓嬶紝杩欎釜閫夐」浠嶇劧涓嶅厑璁歌濡俇PDATE杩欐牱鐨勬搷浣滃帇鍊掔浉浼奸夐」锛堟瘮濡傚彟涓涓猆PDATE锛夈傝冭檻涓嬪垪渚嬪瓙锛

CREATE TABLE A(A INT NOT NULL, B INT);
INSERT INTO A VALUES (1,2),(2,3),(3,2),(4,3),(5,2);
COMMIT;

濡傛灉涓涓繛鎺ユ墽琛屼竴涓煡璇細

SET AUTOCOMMIT = 0;
UPDATE A SET B = 5 WHERE B = 3;

骞朵笖鍏跺畠杩炴帴璺熺潃绗竴涓繛鎺ユ墽琛屽叾瀹冩煡璇細

SET AUTOCOMMIT = 0;
UPDATE A SET B = 4 WHERE B = 2;

鎺ョ潃鏌ヨ2瑕佺瓑鏌ヨ1鐨勬彁浜ゆ垨鍥炴粴锛屽洜涓烘煡璇1瀵硅锛2锛3锛夋湁涓涓嫭鍗犵殑閿佸畾锛屽苟涓旀煡璇2鍦ㄦ壂鎻忚鐨勫悓鏃朵篃璇曠潃瀵瑰畠涓嶈兘閿佸畾鐨勫悓涓涓锛2锛3锛夐噰鍙栦竴涓嫭鍗犵殑閿佸畾銆傝繖鏄洜涓哄綋innodb_locks_unsafe_for_binlog閫夐」琚娇鐢ㄤ箣鏃讹紝鏌ヨ2棣栧厛瀵逛竴涓閲囧彇涓涓嫭鍗犵殑閿佸畾锛岀劧鍚庣‘瀹氭槸鍚﹁繖涓灞炰簬缁撴灉鍖咃紝骞朵笖濡傛灉涓嶅睘浜庯紝灏遍噴鏀句笉蹇呰鐨勯攣瀹氥

鍥犳锛屾煡璇1鎸夊涓嬫墽琛岋細

x-lock(1,2)
unlock(1,2)
x-lock(2,3)
update(2,3) to (2,5)
x-lock(3,2)
unlock(3,2)
x-lock(4,3)
update(4,3) to (4,5)
x-lock(5,2)
unlock(5,2)

骞朵笖鏌ヨ2鎸夊涓嬫墽琛岋細

x-lock(1,2)
update(1,2) to (1,4)
x-lock(2,3) - 绛夊緟鏌ヨ1鎻愪氦鎴栧洖婊

·         innodb_log_arch_dir

濡傛灉鎴戜滑浣跨敤鏃ュ織妗f锛岃瀹屾暣鍐欏叆鐨勬棩蹇楁枃浠舵墍鍦ㄧ殑鐩綍涔熻褰掓。銆傝繖涓弬鏁板煎鏋滆浣跨敤浜嗭紝搴旇琚缃緱涓innodb_log_group_home_dir涓鏍枫傚敖绠″姝わ紝瀹冧笉鏄繀闇鐨勩

·         innodb_log_archive

杩欎釜鍊煎綋鍓嶈璁句负0銆傚洜涓篗ySQL浣跨敤瀹冭嚜宸辩殑鏃ュ織鏂囦欢浠庡浠芥潵鎭㈠锛屾墍浠ュ綋鍓嶆病鏈夊繀瑕佹潵褰掓。InnoDB鏃ュ織鏂囦欢銆傝繖涓夐」鐨 榛樿鍊兼槸0銆

·         innodb_log_buffer_size

InnoDB鐢ㄦ潵寰纾佺洏涓婄殑鏃ュ織鏂囦欢鍐欐搷浣滅殑缂撳啿鍖虹殑澶у皬銆傛槑鏅虹殑鍊兼槸浠1MB鍒8MB銆 榛樿鐨勬槸1MB銆備竴涓ぇ鐨勬棩蹇楃紦鍐插厑璁稿ぇ鍨嬩簨鍔¤繍琛岃屼笉闇瑕佸湪浜嬪姟鎻愪氦涔嬪墠寰纾佺洏鍐欐棩蹇椼傚洜姝わ紝濡傛灉浣犳湁澶у瀷浜嬪姟锛屼娇鏃ュ織缂撳啿鍖烘洿澶т互鑺傜害纾佺洏I/O銆

·         innodb_log_file_size

鍦ㄦ棩蹇楃粍閲屾瘡涓棩蹇楁枃浠剁殑澶у皬銆傚湪32浣嶈绠楁満涓婃棩蹇楁枃浠剁殑鍚堝苟澶у皬蹇呴』灏戜簬4GB銆 榛樿鏄5MB銆傛槑鏅虹殑鍊间粠1MB鍒N鍒嗕箣涓缂撳啿姹犲ぇ灏忥紝鍏朵腑N鏄粍閲屾棩蹇楁枃浠剁殑鏁扮洰銆傚艰秺澶э紝鍦ㄧ紦鍐叉睜瓒婂皯闇瑕佹鏌ョ偣鍒锋柊琛屼负锛屼互鑺傜害纾佺洏I/O銆備絾鏇村ぇ鐨勬棩蹇楁枃浠朵篃鎰忓懗杩欏湪宕╂簝鏃舵仮澶嶅緱鏇存參銆

·         innodb_log_files_in_group

鍦ㄦ棩蹇楃粍閲屾棩蹇楁枃浠剁殑鏁扮洰銆侷nnoDB浠ュ惊鐜柟寮忓啓杩涙枃浠躲傞粯璁ゆ槸2锛堟帹鑽愶級銆

·         innodb_log_group_home_dir

鍒癐nnoDB鏃ュ織鏂囦欢鐨勭洰褰曡矾寰勩傚畠蹇呴』鏈夊拰innodb_log_arch_dir涓鏍风殑鍊笺傚鏋滀綘涓嶆寚瀹氫换浣旾nnoDB鏃ュ織鍙傛暟锛 榛樿鐨勬槸鍦∕ySQL鏁版嵁鐩綍閲屽垱寤轰袱涓5MB澶у皬鍚嶄负ib_logfile0鍜宨b_logfile1鐨勬枃浠躲

·         innodb_max_dirty_pages_pct

杩欐槸涓涓寖鍥翠粠0鍒100鐨勬暣鏁般傞粯璁ゆ槸90銆侷nnoDB涓殑涓荤嚎绋嬭瘯鐫浠庣紦鍐叉睜鍐欓〉闈紝浣垮緱鑴忛〉锛堟病鏈夎鍐欑殑椤甸潰锛夌殑鐧惧垎姣斾笉瓒呰繃杩欎釜鍊笺傚鏋滀綘鏈塖UPER鏉冮檺锛岃繖涓櫨鍒嗘瘮鍙互鍦ㄦ湇鍔″櫒杩愯鏃舵寜涓嬮潰鏉ユ敼鍙橈細

SET GLOBAL innodb_max_dirty_pages_pct = value;

·         innodb_max_purge_lag

杩欎釜閫夐」鎺у埗鍦ㄥ噣鍖栨搷浣滆婊炲悗涔嬫椂锛屽浣曞欢杩烮NSERT, UPDATE鍜孌ELETE鎿嶄綔銆傦紙璇峰弬闃15.2.12鑺傦紝鈥滃鐗堟湰鐨勫疄鏂”锛夈傝繖涓弬鏁扮殑 榛樿鍊兼槸闆讹紝鎰忎负鏃犲欢杩熴傝繖涓夐」鍙互鍦ㄨ繍琛屾椂浣滀负鍏ㄥ眬绯荤粺鍙橀噺鑰岃鏀瑰彉銆

InnoDB浜嬪姟绯荤粺缁存寔涓涓簨鍔″垪琛紝璇ュ垪琛ㄦ湁琚玌PDATE鎴朌ELETE鎿嶄綔鏍囧織涓哄垹闄ょ殑绱㈠紩璁板綍銆傝杩欎釜鍒楄〃鐨勯暱搴︿负purge_lag銆傚綋purge_lag瓒呰繃innodb_max_purge_lag涔嬫椂锛屾瘡涓狪NSERT, UPDATE鍜孌ELETE鎿嶄綔寤惰繜 ((purge_lag/innodb_max_purge_lag)*10)-5姣銆傚湪鍑鍖栨壒澶勭悊鐨勫紑濮嬶紝寤惰繜姣忛殧10绉掕绠椼傚鏋滃洜涓轰竴涓棫鐨勫彲浠ョ湅鍒拌琚噣鍖栫殑涓鑷寸殑璇绘煡鐪嬶紝 鍒犻櫎鎿嶄綔涓嶈寤惰繜銆

瀵规湁闂鐨勫伐浣滈噺锛屽吀鍨嬭缃彲鑳芥槸1鐧句竾锛屽亣璁炬垜浠殑浜嬪姟寰堝皬锛屽彧鏈100瀛楄妭澶у皬锛屾垜浠氨鍙互鍏佽鍦ㄦ垜浠殑琛ㄤ箣涓湁100MB鏈噣鍖栫殑琛屻

·         innodb_mirrored_log_groups

鎴戜滑涓烘暟鎹簱淇濇寔鐨勬棩蹇楃粍鍐呭悓鏍锋嫹璐濈殑鏁伴噺銆傚綋鍓嶈繖涓煎簲璇ヨ璁句负1銆

·         innodb_open_files

鍦↖nnoDB涓紝杩欎釜閫夐」浠呬笌浣犱娇鐢ㄥ琛ㄧ┖闂存椂鏈夊叧銆傚畠鎸囧畾InnoDB涓娆″彲浠ヤ繚鎸佹墦寮鐨.ibd鏂囦欢鐨勬渶澶ф暟鐩傛渶灏忓兼槸10銆 榛樿鍊300銆

瀵.ibd鏂囦欢鐨勬枃浠舵弿杩扮鏄粎瀵笽nnoDB鐨勩傚畠浠嫭绔嬩簬閭d簺鐢--open-files-limit鏈嶅姟鍣ㄩ夐」鎸囧畾鐨勬弿杩扮锛屼笖涓嶅奖鍝嶈〃缂撳瓨鐨勬搷浣溿

·         innodb_status_file

杩欎釜閫夐」璁㊣nnoDB涓哄懆鏈熺殑SHOW INNODB STATUS杈撳嚭鍒涘缓涓涓枃浠<datadir>/innodb_status.<pid>

·         innodb_support_xa

褰撹璁剧疆涓篛N鎴栬1锛堥粯璁ゅ湴锛夛紝杩欎釜鍙橀噺鍏佽InnoDB鏀寔鍦╔A浜嬪姟涓殑 鍙屽悜鎻愪氦銆傚厑璁竔nnodb_support_xa瀵艰嚧涓涓澶栫殑瀵逛簨鍔″噯澶囩殑纾佺洏鍒锋柊銆傚鏋滀綘瀵逛娇鐢╔A骞朵笉鍏冲績锛屼綘鍙互閫氳繃璁剧疆杩欎釜閫夐」涓篛FF鎴0鏉ョ姝㈣繖涓彉閲忥紝浠ュ噺灏戠鐩 鍒锋柊鐨勬鏁板苟鑾峰緱鏇村ソ鐨処nnoDB鎬ц兘銆

·         innodb_table_locks

InnoDB閲嶈LOCK TABLES锛岀洿鍒版墍鏈夊叾瀹冪嚎绋嬪凡缁忛噴鏀句粬浠墍鏈夊琛ㄧ殑閿佸畾锛孧ySQL鎵嶄粠LOCK TABLE .. WRITE杩斿洖銆傞粯璁ゅ兼槸1锛岃繖鎰忎负LOCK TABLES璁㊣nnoDB鍐呴儴閿佸畾涓涓〃銆傚湪浣跨敤AUTOCOMMIT=1鐨勫簲鐢ㄩ噷锛孖nnoDB鐨勫唴閮ㄨ〃閿佸畾浼氬鑷存閿併備綘鍙互鍦╩y.cnf鏂囦欢锛圵indows涓婃槸my.ini鏂囦欢锛夐噷璁剧疆innodb_table_locks=0 鏉 娑堥櫎杩欎釜闂銆

·         innodb_thread_concurrency

InnoDB璇曠潃鍦↖nnoDB鍐呬繚鎸佹搷浣滅郴缁熺嚎绋嬬殑鏁伴噺灏戜簬鎴栫瓑浜庤繖涓弬鏁扮粰鍑虹殑闄愬埗銆傚鏋滄湁鎬ц兘闂锛屽苟涓擲HOW INNODB STATUS鏄剧ず璁稿绾跨▼鍦ㄧ瓑寰呬俊鍙凤紝鍙互璁╃嚎绋“thrashing” 锛屽苟涓旇缃繖涓弬鏁版洿灏忔垨鏇村ぇ銆傚鏋滀綘鐨勮绠楁満鏈夊涓鐞嗗櫒鍜岀鐩橈紝浣犲彲浠ヨ瘯鐫杩欎釜鍊兼洿澶т互鏇村ソ鍦板埄鐢ㄨ绠楁満鐨勮祫婧愩備竴涓帹鑽愮殑鍊兼槸绯荤粺涓婂鐞嗗櫒鍜岀鐩樼殑涓暟涔嬪拰銆傚间负500鎴栨瘮500澶т細绂佹 璋冪敤骞跺彂妫鏌ャ傞粯璁ゅ兼槸20锛屽苟涓斿鏋滆缃ぇ浜庢垨绛変簬20锛屽苟鍙戞鏌ュ皢琚姝€

·         innodb_status_file

杩欎釜閫夐」璁㊣nnoDB涓哄懆鏈熺殑SHOW INNODB STATUS杈撳嚭鍒涘缓涓涓枃浠<datadir>/innodb_status.<pid>

15.2.5. 鍒涘缓InnoDB琛ㄧ┖闂

鍋囪浣犲凡缁忓畨瑁呬簡MySQL锛屽苟涓斿凡缁忕紪杈戜簡閫夐」鏂囦欢锛屼娇寰楀畠鍖呭惈蹇呰鐨処nnoDB閰嶇疆鍙傛暟銆傚湪鍚姩MySQL涔嬪墠锛屼綘搴旇楠岃瘉浣犱负InnoDB鏁版嵁鏂囦欢鍜屾棩蹇楁枃浠舵寚瀹氱殑鐩綍鏄惁瀛樺湪锛屽苟涓擬ySQL鏈夎闂繖浜涚洰褰曠殑鏉冮檺銆侷nnoDB涓嶈兘鍒涘缓鐩綍锛屽彧鑳藉垱寤烘枃浠躲備篃妫鏌ヤ綘鏈夎冻澶熺殑绌洪棿鏉ユ斁鏁版嵁鍜屾棩蹇楁枃浠躲

褰撳垱寤篒nnoDB鏁版嵁搴撴椂锛屾渶濂戒粠鍛戒护鎻愮ず绗﹁繍琛孧ySQL鏈嶅姟鍣mysqld锛 鑰屼笉瑕佷粠mysqld_safe鍖呰鎴栦綔涓篧indows鐨勬湇鍔℃潵杩愯銆傚綋浣犱粠鍛戒护鎻愮ず绗﹁繍琛岋紝浣犲彲鐪嬭mysqld鎵撳嵃浠涔堜互鍙婂彂鐢熶簡浠涔堛傚湪Unix涓婏紝鍙渶瑕佽皟鐢mysqld銆傚湪Windows涓婏紝浣跨敤--console閫夐」銆

褰撳湪閫夐」鏂囦欢閲屽垵濮嬪湴閰嶇疆InnoDB鍚庯紝寮濮嬪惎鍔∕ySQL鏈嶅姟鍣ㄤ箣鏃讹紝InnoDB鍒涘缓涓涓暟鎹枃浠跺拰鏃ュ織鏂囦欢銆侷nnoDB鎵撳嵃濡備笅涓浜涗笢瑗匡細

InnoDB: The first specified datafile /home/heikki/data/ibdata1
did not exist:
InnoDB: a new database to be created!
InnoDB: Setting file /home/heikki/data/ibdata1 size to 134217728
InnoDB: Database physically writes the file full: wait...
InnoDB: datafile /home/heikki/data/ibdata2 did not exist:
new to be created
InnoDB: Setting file /home/heikki/data/ibdata2 size to 262144000
InnoDB: Database physically writes the file full: wait...
InnoDB: Log file /home/heikki/data/logs/ib_logfile0 did not exist:
new to be created
InnoDB: Setting log file /home/heikki/data/logs/ib_logfile0 size
to 5242880
InnoDB: Log file /home/heikki/data/logs/ib_logfile1 did not exist:
new to be created
InnoDB: Setting log file /home/heikki/data/logs/ib_logfile1 size
to 5242880
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
InnoDB: Started
mysqld: ready for connections

涓涓柊鐨処nnoDB鏁版嵁搴撹鍒涘缓浜嗐備綘鍙互鐢mysql杩欐牱閫氬父鐨凪ySQL瀹㈡埛绔▼搴忚繛鎺ュ埌MySQL鏈嶅姟鍣ㄣ傚綋浣犵敤mysqladmin shutdown鍏抽棴MySQL鏈嶅姟鍣ㄤ箣鏃讹紝杈撳嚭绫讳技濡備笅锛

010321 18:33:34  mysqld: Normal shutdown
010321 18:33:34  mysqld: Shutdown Complete
InnoDB: Starting shutdown...
InnoDB: Shutdown completed

浣犲彲浠ョ湅鏁版嵁鏂囦欢鍜屾棩蹇楁枃浠讹紝骞朵笖浣犲彲浠ョ湅瑙佹枃浠惰鍒涘缓銆傛棩蹇楃洰褰曚篃鍖呭惈涓涓悕涓篿b_arch_log_0000000000鐨勫皬鏂囦欢銆傝繖涓枃浠舵槸鏁版嵁搴撹鍒涘缓鐨勭粨鏋滐紝鏁版嵁搴撹鍒涘缓涔嬪悗InnoDB鍒囨柇鏃ュ織褰掓。銆傚綋MySQL鍐嶆鍚姩涔嬫椂锛屾暟鎹枃浠讹紗鏃ュ織鏂囦欢宸茬粡琚垱寤猴紝鎵浠ヨ緭鍑烘洿绠娲侊細

InnoDB: Started
mysqld: ready for connections

浣犲彲浠ユ坊鍔爄nnodb_file_per_table閫夐」鍒癿y.cnf鏂囦欢锛屽苟涓旇InnoDB瀛樺偍姣忎竴涓〃鍒癕ySQL鏁版嵁搴撶洰褰曢噷鑷繁鐨.ibd鏂囦欢銆傝鍙傞槄15.2.6.6鑺傦紝鈥滀娇鐢≒er-Table琛ㄧ┖闂”

15.2.5.1. 澶勭悊InnoDB鍒濆鍖栭棶棰

濡傛灉InnoDB鍦ㄤ竴涓枃浠舵搷浣滀腑鎵撳嵃涓涓搷浣滅郴缁熼敊璇紝閫氬父闂鏄涓嬩腑鐨勪竴涓細

·         浣犳病鏈夊垱寤轰竴涓狪nnoDB鏁版嵁鏂囦欢鐩綍鎴朓nnoDB鏃ュ織鐩綍銆

·         mysqld娌℃湁璁块棶杩欎簺鐩綍鐨勬潈闄 浠ュ垱寤烘枃浠躲

·         mysqld涓嶈兘鎭板綋鍦拌鍙杕y.cnf鎴杕y.ini閫夐」鏂囦欢锛屽洜姝や笉鑳界湅鍒颁綘鎸囧畾鐨勯夐」銆

·         纾佺洏宸叉弧锛屾垨鑰呰秴鍑虹鐩橀厤棰濄

·         浣犲凡缁忓垱寤轰竴涓瓙鐩綍锛屽畠鐨勫悕瀛椾笌浣犳寚瀹氱殑鏁版嵁鏂囦欢鐩稿悓銆

·         鍦╥nnodb_data_home_dir鎴杋nnodb_data_file_path鏈変竴涓娉曢敊璇

褰揑nnoDB璇曠潃鍒濆鍖栧畠鐨勮〃绌洪棿鎴栨棩蹇楁枃浠朵箣鏃讹紝濡傛灉鍑洪敊浜嗭紝浣犲簲璇ュ垹闄nnoDB鍒涘缓鐨勬墍鏈夋枃浠躲傝繖鎰忓懗鐫鏄墍鏈塱bdata鏂囦欢鍜屾墍鏈塱b_logfiles鏂囦欢銆備竾涓浣犲垱寤轰簡涓浜汭nnoDB琛紝涓鸿繖浜涜〃涔熶粠MySQL鏁版嵁搴撶洰褰曞垹闄ょ浉搴旂殑.frm鏂囦欢(濡傛灉浣犱娇鐢ㄥ閲嶈〃绌洪棿鐨勮瘽锛屼篃鍒犻櫎浠讳綍.ibd鏂囦欢锛夈傜劧鍚庝綘鍙互璇曠潃鍐嶆鍒涘缓InnoDB鏁版嵁搴撱傛渶濂芥槸浠庡懡浠ゆ彁绀虹鍚姩MySQL鏈嶅姟鍣 锛屼互渚夸綘鍙互鏌ョ湅鍙戠敓浜嗕粈涔堛

15.2.6. 鍒涘缓InnoDB

鍋囧浣犵敤mysql test鍛戒护鍚姩MySQL瀹㈡埛绔傝鍒涘缓涓涓狪nnoDB琛紝浣犲繀椤诲湪琛ㄥ垱寤篠QL璇彞涓寚瀹欵NGINE = InnoDB鎴栬匱YPE = InnoDB閫夐」锛

CREATE TABLE customers (a INT, b CHAR (20), INDEX (a)) ENGINE=InnoDB;
CREATE TABLE customers (a INT, b CHAR (20), INDEX (a)) TYPE=InnoDB;

SQL璇彞鍦ㄨ〃绌洪棿鐨勫垪涓婂垱寤轰竴涓〃鍜岀储寮曪紝琛ㄧ┖闂村寘鍚綘鍦╩y.cnf鎸囧畾鐨勬暟鎹枃浠躲傛澶栵紝MySQL鍦∕ySQL鏁版嵁搴撶洰褰曚笅鐨則est鐩綍閲屽垱寤轰竴涓悕涓篶ustomers.frm鐨勬枃浠躲傚唴閮ㄥ湴锛孖nnoDB涓'test/customers'琛ㄥ線鑷繁鐨勬暟鎹洰褰曟坊鍔犱竴涓潯鐩傝繖鎰忓懗杩欎綘鍙互鍦ㄥ叾瀹冩暟鎹簱鍒涘缓涓涓叿鏈夌浉鍚屽悕瀛梒ustomers鐨勮〃锛岃〃鐨勫悕瀛椾笉浼氫笌InnoDB鍐呯殑鍐茬獊銆

浣犲彲浠ュ浠讳綍InnoDB锛岄氳繃浣跨敤SHOW TABLE STATUS璇彞锛屾煡璇㈠湪InnoDB琛ㄧ┖闂村唴绌洪棽绌洪棿鐨勬暟閲忋傝〃绌洪棿鍐呯┖闂茬┖闂寸殑鏁伴噺鍑虹幇鍦SHOW TABLE STATUS鐨勮緭鍑虹粨鏋滃唴鐨Comment鑺傞噷銆備緥濡傦細

SHOW TABLE STATUS FROM test LIKE 'customers'

娉ㄦ剰锛岀粺璁$殑SHOW鍙粰鍑哄叧浜嶪nnoDB琛ㄧ殑澶ф鎯呭喌銆傚畠浠鐢ㄤ簬SQL浼樺寲銆傚彲鏄紝琛ㄥ拰绱㈠紩淇濈暀鐨勫ぇ灏忥紝浠ュ瓧鑺備负鍗曚綅鏄噯纭殑銆

15.2.6.1. 濡備綍鍦↖nnoDB涓敤涓嶅悓鐨凙PI鏉ヤ娇鐢ㄤ簨鍔

榛樿鍦帮紝姣忎釜杩炴帴鍒癕ySQL鏈嶅姟鍣ㄧ殑瀹㈡埛绔紑濮嬩箣鏃舵槸鍏佽鑷姩鎻愪氦妯″紡鐨勶紝杩欎釜妯″紡鑷姩鎻愪氦浣犺繍琛岀殑姣忎釜SQL璇彞銆傝浣跨敤澶氳鍙ヤ簨鍔★紝浣犲彲浠ョ敤SQL璇彞SET AUTOCOMMIT = 0绂佹鑷姩鎻愪氦锛屽苟涓旂敤COMMIT鍜孯OLLBACK鏉ユ彁浜ゆ垨鍥炴粴浣犵殑浜嬪姟銆 濡傛灉浣犳兂瑕乤utocommit淇濇寔鎵撳紑鐘舵侊紝鍙互鍦⊿TART TRANSACTION涓嶤OMMIT鎴朢OLLBACK涔嬮棿灏佽浣犵殑浜嬪姟銆備笅鍒楃殑渚嬪瓙婕旂ず涓や釜浜嬪姟銆傜涓涓槸琚彁浜ょ殑锛岀浜屼釜鏄 鍥炴粴鐨勶細

shell> mysql test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 3.23.50-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> CREATE TABLE CUSTOMER (A INT, B CHAR (20), INDEX (A))
    -> ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO CUSTOMER VALUES (10, 'Heikki');
Query OK, 1 row affected (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql> SET AUTOCOMMIT=0;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO CUSTOMER VALUES (15, 'John');
Query OK, 1 row affected (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM CUSTOMER;
+------+--------+
| A    | B      |
+------+--------+
|   10 | Heikki |
+------+--------+
1 row in set (0.00 sec)
mysql>

鍦ㄧ被浼糚HP, Perl DBI/DBD, JDBC, ODBC, 鎴栬匨ySQL鐨勬爣鍑咰璋冪敤鎺ュ彛杩欐牱鐨凙PI涓婏紝浣犺兘澶熶互瀛楃涓插舰寮忓彂閫佷簨鍔℃帶鍒惰鍙ワ紝濡侰OMMIT锛屽埌MySQL鏈嶅姟鍣紝灏卞儚鍏跺畠浠讳綍鐨凷QL璇彞 閭f牱锛岃濡係ELECT鎴朓NSERT銆備竴浜汚PI涔熸彁渚涘崟鐙殑涓撻棬鐨勪簨鍔℃彁浜ゅ拰鍥炴粴鍑芥暟鎴栬呮柟娉曘

15.2.6.2. 杞崲MyISAM琛ㄥ埌InnoDB

瑕佺偣锛氫綘涓嶅簲璇ュ湪mysql鏁版嵁搴擄紙姣斿锛寀ser鎴栬卙ost锛夐噷鎶奙ySQL绯荤粺琛ㄨ浆鎹负InnoDB绫诲瀷銆傜郴缁熻〃鎬绘槸MyISAM鍨嬨

濡傛灉浣犳兂瑕佹墍鏈夛紙闈炵郴缁燂級琛ㄩ兘琚垱寤烘垚InnoDB琛紝浣犲彲浠ョ畝鍗曞湴鎶奷efault-table-type=innodb琛屾坊鍔犲埌my.cnf鎴杕y.ini鏂囦欢鐨刐mysqld]鑺傞噷銆

InnoDB瀵筂yISAM瀛樺偍寮曟搸閲囩敤鐨勫崟鐙储寮曞垱寤烘柟娉曟病鏈夊仛涓撻棬鐨勪紭鍖栥傚洜姝わ紝瀹冧笉鍊煎緱瀵煎嚭鎴栧鍏ヨ〃浠ュ強闅忓悗鍒涘缓绱㈠紩銆傛敼鍙樹竴涓〃涓篒nnoDB鍨嬫渶蹇殑鍔炴硶灏辨槸鐩存帴鎻掑叆杩涗竴涓狪nnoDB琛ㄣ傚嵆锛屼娇鐢ˋLTER TABLE ... ENGINE=INNODB锛屾垨鐢ㄧ浉鍚岀殑瀹氫箟鍒涘缓涓涓┖InnoDB琛紝骞朵笖鐢↖NSERT INTO ... SELECT * FROM ...鎻掑叆琛屻

濡傛灉浣犲绗簩涓敭鏈塙NIQUE绾︽潫锛屼綘鍙互鍦ㄥ鍏ラ樁娈佃缃細SET UNIQUE_CHECKS=0锛屼互涓存椂鍏虫帀鍞竴鎬ф鏌ュソ鍔犻熻〃鐨勫鍏ャ傚浜庡ぇ琛紝杩欒妭鐪佷簡澶ч噺鐨勭鐩業/O锛屽洜涓篒nnoDB闅忓悗鍙互浣跨敤瀹冪殑鎻掑叆缂撳啿鍖烘潵绗簩涓储寮曡褰曚綔涓轰竴鎵规潵鍐欏叆銆

涓鸿幏寰楀鎻掑叆杩涚▼鐨勬洿濂芥帶鍒讹紝鍒嗘鎻掑叆澶ц〃鍙兘姣旇緝濂斤細

INSERT INTO newtable SELECT * FROM oldtable
   WHERE yourkey > something AND yourkey <= somethingelse;

鎵鏈夎褰曞凡缁忔湰鎻掑叆涔嬪悗锛屼綘鍙互閲嶅懡鍚嶈〃銆

鍦ㄥぇ琛ㄧ殑杞崲涓紝浣犲簲璇ュ鍔營nnoDB缂撳啿姹犵殑澶у皬鏉ュ噺灏戠鐩業/O銆傚敖绠″姝わ紝涓嶈浣跨敤瓒呰繃80%鐨勫唴閮ㄥ唴瀛樸備綘涔熷彲浠ュ鍔營nnoDB鏃ュ織鏂囦欢鍜屾棩蹇楁枃浠剁殑澶у皬銆

纭俊浣犳病鏈夊~婊¤〃绌洪棿锛欼nnoDB琛ㄦ瘮MyISAM琛ㄩ渶瑕佸ぇ寰楀鐨勭鐩樼┖闂淬傚鏋滀竴涓狝LTER TABLE鑰楀敖浜嗙┖闂达紝瀹冨氨寮濮嬩竴涓 鍥炴粴锛屽苟涓斿鏋滃畠鏄鐩樼粦瀹氱殑锛屽洖婊氬彲鑳借鍑犱釜灏忔椂銆傚浜庢彃鍏ワ紝InnoDB浣跨敤鎻掑叆缂撳啿鍖烘潵浠ユ垚鎵瑰湴鍚堝苟绗簩涓储寮曡褰曞埌绱㈠紩涓傞偅鏍疯妭鐪佷簡澶ч噺纾佺洏I/O銆傚湪鍥炴粴涓紝娌℃湁浣跨敤杩欐牱鐨勬満鍒讹紝鑰屽洖婊氳鑺辨瘮鎻掑叆闀30鍊嶇殑鏃堕棿鏉ュ畬鎴愩

鍦ㄥけ鎺х殑鍥炴粴鎯呭喌涓嬶紝濡傛灉浣犲湪鏁版嵁搴撲腑娌℃湁鏈変环鍊肩殑鏁版嵁锛屾瘮杈冩槑鏅虹殑鏄潃鎺夋暟鎹簱杩涚▼鑰屼笉鏄瓑鍑犵櫨涓囦釜纾佺洏I/O琚畬鎴愩 瀹屾暣鐨勮繃绋嬶紝璇峰弬闃15.2.8.1鑺傦紝鈥滃己鍒舵仮澶”

15.2.6.3. AUTO_INCREMENT鍒楀湪InnoDB閲屽浣曞伐浣

濡傛灉浣犱负涓涓〃鎸囧畾AUTO_INCREMENT鍒楋紝鍦ㄦ暟鎹瘝鍏搁噷鐨処nnoDB琛ㄥ彞鏌勫寘鍚竴涓悕涓鸿嚜鍔ㄥ闀胯鏁板櫒鐨勮鏁板櫒锛屽畠琚敤鍦ㄤ负璇 鍒楄祴鏂板笺傝嚜鍔ㄥ闀胯鏁板櫒浠呰瀛樺偍鍦ㄤ富鍐呭瓨涓紝鑰屼笉鏄瓨鍦ㄧ鐩樹笂銆

InnoDB浣跨敤涓嬪垪绠楁硶鏉ヤ负鍖呭惈涓涓悕涓篴i_col鐨凙UTO_INCREMENT鍒楃殑琛═鍒濆鍖栬嚜鍔ㄥ闀胯鏁板櫒锛氭湇鍔″櫒鍚姩涔嬪悗锛屽綋涓涓敤鎴峰琛═鍋氭彃鍏ヤ箣鏃讹紝InnoDB鎵ц绛変环濡備笅璇彞鐨勫姩浣滐細

SELECT MAX(ai_col) FROM T FOR UPDATE;

璇彞鍙栧洖鐨勫奸愭鍔犱竴锛屽苟琚祴缁欏垪鍜岃嚜鍔ㄥ闀胯鏁板櫒銆傚鏋滆〃鏄┖鐨勶紝鍊1琚祴浜堣鍒椼傚鏋滆嚜鍔ㄥ闀胯鏁板櫒娌℃湁琚垵濮嬪寲锛岃屼笖鐢ㄦ埛璋冪敤涓鸿〃T鏄剧ず杈撳嚭鐨凷HOW TABLE STATUS璇彞锛屽垯璁℃暟鍣ㄨ鍒濆鍖栵紙浣嗕笉鏄鍔犺鏁帮級骞惰瀛樺偍浠ヤ緵闅忓悗鐨勬彃鍏ヤ娇鐢ㄣ傛敞鎰忥紝鍦ㄨ繖涓垵濮嬪寲涓紝鎴戜滑瀵硅〃鍋氫竴涓甯哥殑鐙崰閿佸畾锛岃繖涓攣鎸佺画鍒颁簨鍔$殑缁撴潫銆

InnoDB瀵逛负鏂板垱寤鸿〃鐨勫垵濮嬪寲鑷姩澧為暱璁℃暟鍣ㄥ厑璁稿悓鏍风殑杩囩▼銆

娉ㄦ剰锛屽鏋滅敤鎴峰湪INSERT涓负AUTO_INCREMENT鍒楁寚瀹歂ULL鎴栬0锛孖nnoDB澶勭悊琛岋紝灏变豢浣涘艰繕娌℃湁琚寚瀹氾紝涓斾负瀹冪敓鎴愪竴涓柊鍊笺

鑷姩澧為暱璁℃暟鍣ㄨ鍒濆鍖栦箣鍚庯紝濡傛灉鐢ㄦ埛鎻掑叆涓涓槑纭寚瀹氳鍒楀肩殑琛岋紝鑰屼笖璇ュ煎ぇ浜庡綋鍓嶈鏁板櫒鍊硷紝鍒欒鏁板櫒琚缃负鎸囧畾 鍒楀笺傚鏋滄病鏈夋槑纭寚瀹氫竴涓硷紝InnoDB缁欒鏁板櫒澧炲姞涓锛屽苟涓旇祴鏂板肩粰璇ュ垪銆

褰撹闂嚜鍔ㄥ闀胯鏁板櫒涔嬫椂锛孖nnoDB浣跨敤涓撶敤鐨勮〃绾х殑AUTO-INC閿佸畾锛岃閿佹寔缁埌褰撳墠SQL璇彞鐨勭粨鏉熻屼笉鏄埌涓氬姟鐨勭粨鏉熴 寮曞叆浜嗕笓鐢ㄩ攣閲婃斁绛栫暐锛屾潵涓哄涓涓惈AUTO_INCREMENT鍒楃殑琛ㄧ殑鎻掑叆鏀瑰杽閮ㄧ讲銆備袱涓簨鍔′笉鑳藉悓鏃跺鍚屼竴琛ㄦ湁AUTO-INC閿佸畾銆

娉ㄦ剰锛屽鏋滀綘鍥炴粴浠庤鏁板櫒鑾峰緱鏁扮殑浜嬪姟锛屼綘鍙兘浼氬湪璧嬬粰AUTO_INCREMENT鍒楃殑鍊肩殑搴忓垪涓彂鐜伴棿闅欍

濡傛灉鐢ㄦ埛缁欏垪璧嬩竴涓祴鍊硷紝鎴栬咃紝濡傛灉鍊煎ぇ杩囧彲琚互鎸囧畾鏁存暟鏍煎紡瀛樺偍鐨勬渶澶ф暣鏁帮紝鑷姩澧為暱鏈哄埗鐨勮涓轰笉琚畾涔夈

鍦–REATE TABLE鍜孉LTER TABLE璇彞涓紝InnoDB鏀寔AUTO_INCREMENT = n 琛ㄩ夐」鏉ヨ缃鏁板櫒鍒濆鍊兼垨鍙樻洿褰撳墠璁℃暟鍣ㄥ笺傚洜鍦ㄦ湰鑺傛棭鍏堣璁虹殑鍘熷洜锛岃繖涓夐」鐨勫奖鍝嶅湪鏈嶅姟鍣ㄩ噸鍚悗灏辨棤鏁堜簡銆

15.2.6.4.澶栭敭绾︽潫

InnoDB涔熸敮鎸佸閿害鏉熴侷nnoDB涓澶栭敭绾︽潫瀹氫箟鐨勮娉曠湅璧锋潵濡備笅锛

[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name, ...)
    [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
    [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]

澶栭敭瀹氫箟鏈嶄粠涓嬪垪鎯呭喌锛

·         鎵鏈塼ables蹇呴』鏄疘nnoDB鍨嬶紝瀹冧滑涓嶈兘鏄复鏃惰〃銆

·         鍦ㄥ紩鐢ㄨ〃涓紝蹇呴』鏈変竴涓储寮曪紝澶栭敭鍒椾互鍚屾牱鐨勯『搴忚鍒楀湪鍏朵腑浣滀负绗竴鍒椼傝繖鏍蜂竴涓储寮曞鏋滀笉瀛樺湪锛屽畠蹇呴』鍦 寮曠敤琛ㄩ噷琚嚜鍔ㄥ垱寤恒

·         鍦ㄥ紩鐢ㄨ〃涓紝蹇呴』鏈変竴涓储寮曪紝琚紩鐢ㄧ殑鍒椾互鍚屾牱鐨勯『搴忚鍒楀湪鍏朵腑浣滀负绗竴鍒椼

·         涓嶆敮鎸佸澶栭敭鍒楃殑绱㈠紩鍓嶇紑銆傝繖鏍风殑鍚庢灉涔嬩竴鏄疊LOB鍜孴EXT鍒椾笉琚寘鎷湪涓涓閿腑锛杩欐槸鍥犱负瀵硅繖浜涘垪鐨勭储寮曞繀椤绘绘槸鍖呭惈涓涓墠缂闀垮害銆

·         濡傛灉CONSTRAINTsymbol琚粰鍑猴紝瀹冨湪鏁版嵁搴撻噷蹇呴』鏄敮涓鐨勩傚鏋滃畠娌℃湁琚粰鍑猴紝InnoDB鑷姩鍒涘缓杩欎釜鍚嶅瓧銆

InnoDB鎷掔粷浠讳綍璇曠潃鍦ㄥ瓙琛ㄥ垱寤轰竴涓閿艰屼笉鍖归厤鍦ㄧ埗琛ㄤ腑鐨勫欓夐敭鍊肩殑INSERT鎴朥PDATE鎿嶄綔銆備竴涓埗琛ㄦ湁涓浜涘尮閰嶇殑琛 鐨勫瓙琛紝InnoDB瀵逛换浣曡瘯鍥炬洿鏂版垨鍒犻櫎璇ョ埗琛ㄤ腑鍊欓夐敭鍊肩殑UPDATE鎴朌ELETE鎿嶄綔鏈夋墍鍔ㄤ綔锛岃繖涓姩浣滃彇鍐充簬鐢‵OREIGN KEY瀛愬彞鐨凮N UPDATE鍜孫N DETETE瀛愬彞鎸囧畾鐨referential action銆傚綋鐢ㄦ埛璇曞浘浠庝竴涓埗琛ㄥ垹闄ゆ垨鏇存柊涓琛屼箣鏃讹紝涓斿湪瀛愯〃涓湁涓涓垨澶氫釜鍖归厤鐨勮锛孖nnoDB鏍规嵁瑕侀噰鍙栫殑鍔ㄤ綔鏈変簲涓夋嫨锛

·         CASCADE: 浠庣埗琛ㄥ垹闄ゆ垨鏇存柊涓旇嚜鍔ㄥ垹闄ゆ垨鏇存柊瀛愯〃涓尮閰嶇殑琛屻侽N DELETE CASCADE鍜孫N UPDATE CASCADE閮藉彲鐢ㄣ傚湪涓や釜琛ㄤ箣闂达紝浣犱笉搴斿畾涔夎嫢骞插湪鐖惰〃鎴栧瓙琛ㄤ腑鐨勫悓涓鍒楅噰鍙栧姩浣滅殑ON UPDATE CASCADE瀛愬彞銆

·         SET NULL: 浠庣埗琛ㄥ垹闄ゆ垨鏇存柊琛岋紝骞惰缃瓙琛ㄤ腑鐨勫閿垪涓篘ULL銆傚鏋滃閿垪娌℃湁鎸囧畾NOT NULL闄愬畾璇嶏紝杩欏氨鏄敮涓鍚堟硶鐨勩侽N DELETE SET NULL鍜孫N UPDATE SET NULL瀛愬彞琚敮鎸併

·         NO ACTION: 鍦ˋNSI SQL-92鏍囧噯涓紝NO ACTION鎰忓懗杩欎笉閲囧彇鍔ㄤ綔锛屽氨鏄鏋滄湁涓涓浉鍏崇殑澶栭敭鍊煎湪琚弬鑰冪殑琛ㄩ噷锛屽垹闄ゆ垨鏇存柊涓昏閿肩殑浼佸浘涓嶈鍏佽杩涜锛圙ruber, 鎺屾彙SQL, 2000:181锛夈 InnoDB鎷掔粷瀵圭埗琛ㄧ殑鍒犻櫎鎴栨洿鏂版搷浣溿

·         RESTRICT: 鎷掔粷瀵圭埗琛ㄧ殑鍒犻櫎鎴栨洿鏂版搷浣溿侼O ACTION鍜孯ESTRICT閮戒竴鏍凤紝鍒犻櫎ON DELETE鎴朞N UPDATE瀛愬彞銆傦紙涓浜涙暟鎹簱绯荤粺鏈夊欢鏈熸鏌ワ紝骞朵笖NO ACTION鏄竴涓欢鏈熸鏌ャ傚湪MySQL涓紝澶栭敭绾︽潫鏄绔嬪嵆妫鏌ョ殑锛屾墍浠O ACTION鍜孯ESTRICT鏄悓鏍风殑锛夈

·         SET DEFAULT: 杩欎釜鍔ㄤ綔琚В鏋愮▼搴忚瘑鍒紝浣咺nnoDB鎷掔粷鍖呭惈ON DELETE SET DEFAULT鎴朞N UPDATE SET DEFAULT瀛愬彞鐨勮〃瀹氫箟銆

褰撶埗琛ㄤ腑鐨勫欓夐敭琚洿鏂扮殑鏃跺欙紝InnoDB鏀寔鍚屾牱閫夋嫨銆閫夋嫨CASCADE锛屽湪瀛愯〃涓殑澶栭敭 鍒楄璁剧疆涓虹埗琛ㄤ腑鍊欓夐敭鐨勬柊鍊笺備互鍚屾牱鐨勬柟寮忥紝濡傛灉鍦ㄥ瓙琛ㄦ洿鏂扮殑鍒楀弬鑰冨湪鍙︿竴涓〃涓殑澶栭敭锛屾洿鏂扮骇鑱斻

娉ㄦ剰锛孖nnoDB鏀寔澶栭敭鍦ㄤ竴涓〃鍐呭紩鐢紝鍦ㄨ繖浜涙儏鍐典笅锛屽瓙琛ㄥ疄闄呬笂鎰忓懗杩欏湪琛ㄥ唴闄勫睘鐨勮褰曘

InnoDB闇瑕佸澶栭敭鍜岃寮曠敤閿殑绱㈠紩浠ヤ究澶栭敭妫鏌ュ彲浠ュ揩閫熻繘琛屼笖涓嶉渶瑕佷竴涓〃鎵弿銆傚澶栭敭鐨勭储寮曡鑷姩鍒涘缓銆傝繖鏄浉瀵逛簬涓浜涜佺増鏈紝鍦ㄨ佺増鏈腑绱㈠紩蹇呴』鏄庣‘鍒涘缓锛屽惁鍒欏閿害鏉熺殑鍒涘缓浼氬け璐ャ

鍦↖nnoDB鍐咃紝澶栭敭閲屽拰琚紩鐢ㄥ垪閲岀浉搴旂殑鍒楀繀椤绘湁绫讳技鐨勫唴閮ㄦ暟鎹被鍨嬶紝浠ヤ究瀹冧滑涓嶉渶绫诲瀷杞崲灏卞彲琚瘮杈冦傛暣鏁扮被鍨嬬殑澶у皬鍜岀鍙峰繀椤荤浉鍚屻傚瓧绗︿覆绫诲瀷鐨勯暱搴︿笉闇瑕佺浉鍚屻傚鏋滀綘鎸囧畾涓涓猄ET NULL鍔ㄤ綔锛岃纭浣犳病鏈夊湪瀛愯〃涓鍛婅 鍒椾负涓NOT NULL

濡傛灉MySQL浠嶤REATE TABLE璇彞鎶ュ憡涓涓敊璇彿1005锛屽苟涓旈敊璇俊鎭瓧绗︿覆鎸囧悜errno 150锛岃繖鎰忔濇槸鍥犱负涓涓閿害鏉熻涓嶆纭舰鎴愶紝琛ㄥ垱寤哄け璐ャ傜被浼煎湴锛屽鏋淎LTER TABLE澶辫触锛屼笖瀹冩寚鍚慹rrno 150锛 閭f剰鍛崇潃瀵瑰凡鍙樻洿鐨勮〃锛屽閿畾涔変細琚笉姝g‘鐨勫舰鎴愩備綘鍙互浣跨敤SHOW INNODB STATUS鏉ユ樉绀轰竴涓鏈嶅姟鍣ㄤ笂鏈杩戠殑InnoDB澶栭敭閿欒鐨勮缁嗚В閲娿

娉ㄩ噴锛InnoDB涓嶅閭d簺 澶栭敭鎴栧寘鍚玁ULL鍒楃殑琚紩鐢ㄩ敭鍊兼鏌ュ閿害鏉熴

瀵筍QL鏍囧噯鐨勮儗绂伙細濡傛灉鍦ㄧ埗琛ㄥ唴鏈夋暟涓锛屽叾涓湁鐩稿悓鐨 琚紩鐢ㄩ敭鍊硷紝鐒跺悗InnoDB鍦ㄥ閿鏌ヤ腑閲囧彇鍔ㄤ綔锛屽氨浠夸經鍏跺畠鏈夌浉鍚岄敭鍊肩殑鐖惰涓嶅瓨鍦ㄤ竴鏍枫備緥濡傦紝濡傛灉浣犲凡瀹氫箟涓涓猂ESTRICT绫诲瀷鐨勭害鏉燂紝骞朵笖鏈変竴涓甫鏁颁釜鐖惰鐨勫瓙琛岋紝InnoDB涓嶅厑璁镐换浣曞杩欎簺鐖惰鐨勫垹闄ゃ

灞呬簬瀵瑰簲澶栭敭绾︽潫鐨勭储寮曞唴鐨勮褰曪紝InnoDB閫氳繃娣卞害浼樺厛閫夋硶鏂借绾ц仈鎿嶄綔銆

瀵筍QL鏍囧噯鐨勮儗绂伙細 濡傛灉ON UPDATE CASCADE鎴朞N UPDATE SET NULL閫掑綊鏇存柊鐩稿悓鐨勮〃锛屼箣鍓嶅湪绾ц仈杩囩▼涓琛ㄤ竴琚洿鏂拌繃锛屽畠灏辫薄RESTRICT涓鏍峰姩浣溿傝繖鎰忓懗鐫浣犱笉鑳戒娇鐢ㄨ嚜寮曠敤ON UPDATE CASCADE鎴栬匫N UPDATE SET NULL鎿嶄綔銆傝繖灏嗛樆姝㈢骇鑱旀洿鏂板鑷寸殑鏃犻檺寰幆銆傚彟涓鏂归潰锛屼竴涓嚜寮曠敤鐨凮N DELETE SET NULL鏄湁鍙兘鐨勶紝灏卞儚涓涓嚜寮曠敤ON DELETE CASCADE涓鏍枫 绾ц仈鎿嶄綔涓嶅彲浠ヨ宓屽瓒呰繃15灞傛繁銆

瀵筍QL鏍囧噯鐨勮儗绂伙細 绫讳技涓鑸殑MySQL锛屽湪涓涓彃鍏ワ紝鍒犻櫎鎴栨洿鏂拌澶氳鐨凷QL璇彞鍐咃紝InnoDB閫愯妫鏌NIQUE鍜孎OREIGN KEY绾︽潫銆傛寜鐓QL鐨勬爣鍑嗭紝 榛樿鐨勮涓哄簲琚欢杩熸鏌ワ紝鍗崇害鏉熶粎鍦ㄦ暣涓猄QL璇彞琚鐞嗕箣鍚庢墠琚鏌ャ傜洿鍒癐nnoDB瀹炵幇寤惰繜鐨勭害鏉熸鏌ヤ箣鍓嶏紝涓浜涗簨鎯呮槸涓嶅彲鑳界殑锛屾瘮濡傚垹闄や竴涓氳繃澶栭敭鍙傝冨埌鑷韩鐨勮褰曘

娉ㄩ噴锛褰撳墠锛岃Е鍙戝櫒涓嶈绾ц仈澶栭敭鐨勫姩浣滄縺娲汇

涓涓氳繃鍗曞垪澶栭敭鑱旂郴璧风埗琛ㄥ拰瀛愯〃鐨勭畝鍗曚緥瀛愬涓嬶細

CREATE TABLE parent(id INT NOT NULL,
                    PRIMARY KEY (id)
) TYPE=INNODB;
CREATE TABLE child(id INT, parent_id INT,
                   INDEX par_ind (parent_id),
                   FOREIGN KEY (parent_id) REFERENCES parent(id)
                     ON DELETE CASCADE
) TYPE=INNODB;

濡備笅鏄竴涓洿澶嶆潅鐨勪緥瀛愶紝鍏朵腑涓涓猵roduct_order琛ㄥ鍏跺畠涓や釜琛ㄦ湁澶栭敭銆備竴涓閿紩鐢ㄤ竴涓猵roduct琛ㄤ腑鐨勫弻鍒楃储寮曘傚彟涓涓 寮曠敤鍦╟ustomer琛ㄤ腑鐨勫崟琛岀储寮曪細

CREATE TABLE product (category INT NOT NULL, id INT NOT NULL,
                      price DECIMAL,
                      PRIMARY KEY(category, id)) TYPE=INNODB;
CREATE TABLE customer (id INT NOT NULL,
                      PRIMARY KEY (id)) TYPE=INNODB;
CREATE TABLE product_order (no INT NOT NULL AUTO_INCREMENT,
                      product_category INT NOT NULL,
                      product_id INT NOT NULL,
                      customer_id INT NOT NULL,
                      PRIMARY KEY(no),
                      INDEX (product_category, product_id),
                      FOREIGN KEY (product_category, product_id)
                        REFERENCES product(category, id)
                        ON UPDATE CASCADE ON DELETE RESTRICT,
                      INDEX (customer_id),
                      FOREIGN KEY (customer_id)
                        REFERENCES customer(id)) TYPE=INNODB;

InnoDB鍏佽浣犵敤ALTER TABLE寰涓涓〃涓坊鍔犱竴涓柊鐨 澶栭敭绾︽潫锛

ALTER TABLE yourtablename
    ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name, ...)
    [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
    [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]

璁颁綇鍏堝垱寤洪渶瑕佺殑绱㈠紩銆備綘涔熷彲浠ョ敤ALTER TABLE寰涓涓〃娣诲姞涓涓嚜寮曠敤澶栭敭绾︽潫銆

InnoDB涔熸敮鎸佷娇鐢ALTER TABLE鏉ョЩ闄 澶栭敭锛

ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;

褰撳勾鍒涘缓涓涓閿箣鏃讹紝濡傛灉FOREIGN KEY瀛愬彞鍖呮嫭涓涓狢ONSTRAINT鍚嶅瓧锛屼綘鍙互寮曠敤閭d釜鍚嶅瓧鏉ョЩ闄 澶栭敭銆傚彟澶栵紝褰撳閿鍒涘缓涔嬫椂锛宖k_symbol鍊艰InnoDB鍐呴儴淇濊瘉銆傚綋浣犳兂瑕佺Щ闄や竴涓閿箣鏃讹紝瑕佹壘鍑烘爣璁帮紝璇蜂娇鐢⊿HOW CREATE TABLE璇彞銆備緥瀛愬涓嬶細

mysql> SHOW CREATE TABLE ibtest11c\G
*************************** 1. row ***************************
       Table: ibtest11c
Create Table: CREATE TABLE `ibtest11c` (
  `A` int(11) NOT NULL auto_increment,
  `D` int(11) NOT NULL default '0',
  `B` varchar(200) NOT NULL default '',
  `C` varchar(175) default NULL,
  PRIMARY KEY  (`A`,`D`,`B`),
  KEY `B` (`B`,`C`),
  KEY `C` (`C`),
  CONSTRAINT `0_38775` FOREIGN KEY (`A`, `D`)
REFERENCES `ibtest11a` (`A`, `D`)
ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `0_38776` FOREIGN KEY (`B`, `C`)
REFERENCES `ibtest11a` (`B`, `C`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB CHARSET=latin1
1 row in set (0.01 sec)
 
mysql> ALTER TABLE ibtest11c DROP FOREIGN KEY 0_38775;

InnoDB瑙f瀽绋嬪簭鍏佽浣犲湪FOREIGN KEY ... REFERENCES ...瀛愬彞涓敤`锛坆ackticks)鎶婅〃鍜鍒楀悕鍚嶅瓧鍥磋捣鏉ャ侷nnoDB瑙f瀽绋嬪簭涔熻冭檻鍒發ower_case_table_names绯荤粺鍙橀噺鐨勮缃

InnoDB杩斿洖涓涓〃鐨勫閿畾涔変綔涓篠HOW CREATE TABLE璇彞杈撳嚭鐨勪竴閮ㄥ垎锛

SHOW CREATE TABLE tbl_name;

浠庤繖涓増鏈捣锛mysqldump涔熷皢琛ㄧ殑姝g‘瀹氫箟鐢熸垚鍒拌浆鍌ㄦ枃浠朵腑锛屼笖骞朵笉蹇樿 澶栭敭銆

浣犲彲浠ュ涓嬪涓涓〃鏄剧ず澶栭敭绾︽潫锛

SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';

澶栭敭绾︽潫琚垪鍦ㄨ緭鍑虹殑Comment鍒椼

褰撴墽琛屽閿鏌ヤ箣鏃讹紝InnoDB瀵瑰畠鐓х湅鐫鐨勫瓙鎴栫埗璁板綍璁剧疆鍏变韩鐨勮绾ч攣銆侷nnoDB绔嬪嵆妫鏌ュ閿害鏉燂紝妫鏌ヤ笉瀵逛簨鍔℃彁浜ゅ欢杩熴

瑕佷娇寰楀鏈夊閿叧绯荤殑琛ㄩ噸鏂拌浇鍏ヨ浆鍌ㄦ枃浠跺彉寰楁洿瀹规槗锛mysqldump鑷姩鍦ㄨ浆鍌ㄨ緭鍑轰腑鍖呮嫭涓涓鍙ヨ缃瓼OREIGN_KEY_CHECKS涓0銆傝繖閬垮厤鍦ㄨ浆鍌ㄨ閲嶆柊瑁呰浇涔嬫椂锛屼笌涓嶅緱涓嶈浠ョ壒鍒『搴忛噸鏂拌杞界殑琛ㄧ浉鍏崇殑闂銆備篃鍙互鎵嬪姩璁剧疆杩欎釜鍙橀噺锛

mysql> SET FOREIGN_KEY_CHECKS = 0;
mysql> SOURCE dump_file_name;
mysql> SET FOREIGN_KEY_CHECKS = 1;

濡傛灉杞偍鏂囦欢鍖呭惈瀵瑰閿槸涓嶆纭『搴忕殑琛紝杩欏氨浠ヤ换浣曢『搴忓鍏ヨ琛ㄣ傝繖鏍蜂篃鍔犲揩瀵煎叆鎿嶄綔銆傝缃瓼OREIGN_KEY_CHECKS涓0锛屽浜庡湪LOAD DATA鍜孉LTER TABLE鎿嶄綔涓拷鐣ュ閿檺鍒朵篃鏄潪甯告湁鐢ㄧ殑銆

InnoDB涓嶅厑璁镐綘鍒犻櫎涓涓FOREIGN KEY琛ㄧ害鏉 寮曠敤鐨勮〃锛岄櫎闈炰綘鍋氳缃甋ET FOREIGN_KEY_CHECKS=0銆傚綋浣犵Щ闄や竴涓〃鐨勬椂鍊欙紝鍦ㄥ畠鐨勫垱寤鸿鍙ラ噷瀹氫箟鐨勭害鏉熶篃琚Щ闄ゃ

濡傛灉浣犻噸鏂板垱寤轰竴涓绉婚櫎鐨勮〃锛屽畠蹇呴』鏈変竴涓伒浠庝簬涔熷紩鐢ㄥ畠鐨勫閿害鏉熺殑瀹氫箟銆傚畠蹇呴』鏈夋纭殑鍒楀悕鍜岀被鍨嬶紝骞朵笖濡傚墠鎵杩帮紝瀹冨繀椤诲琚 寮曠敤鐨勯敭鏈夌储寮曘傚鏋滆繖浜涗笉琚弧瓒筹紝MySQL杩斿洖閿欒鍙1005 骞跺湪閿欒淇℃伅瀛楃涓蹭腑鎸囧悜errno 150銆

15.2.6.5. InnoDB鍜孧ySQL澶嶅埗

MySQL澶嶅埗灏卞儚瀵筂yISAM琛ㄤ竴鏍凤紝涔熷InnoDB琛ㄨ捣浣滅敤銆備互鏌愮鏂瑰紡浣跨敤澶嶅埗涔熸槸鍙兘鐨勶紝鍦ㄨ繖绉嶆柟寮忎腑浠庢湇鍔″櫒涓婅〃鐨勭被鍨嬩笉鍚屼簬涓绘湇鍔″櫒涓 鍘熷琛ㄧ殑绫诲瀷銆備緥濡傦紝浣犲彲浠ュ鍒朵慨鏀瑰埌涓绘湇鍔″櫒涓婁竴涓狪nnoDB琛紝鍒颁粠鏈嶅姟鍣ㄤ笂涓涓狹yISAM琛ㄩ噷銆

瑕佷负涓涓富鏈嶅姟鍣ㄥ缓绔嬩竴涓柊鏈嶅姟鍣紝浣犱笉寰椾笉澶嶅埗InnoDB琛ㄧ┖闂村拰鏃ュ織鏂囦欢锛屼互鍙奍nnoDB琛ㄧ殑.frm鏂囦欢锛屽苟涓旂Щ鍔ㄥ浠跺埌浠庢湇鍔″櫒銆 鍏充簬鍏舵伆褰撴楠よ鍙傞槄15.2.9鑺傦紝鈥滅Щ鍔↖nnoDB鏁版嵁搴撳埌鍙︿竴鍙版満鍣”

濡傛灉浣犲彲浠ュ叧闂富鏈嶅姟鍣ㄦ垨鑰呬竴涓瓨鍦ㄧ殑浠庢湇鍔″櫒銆備綘鍙互閲囧彇InnoDB琛ㄧ┖闂村拰鏃ュ織鏂囦欢鐨勫喎澶囦唤锛屽苟鐢ㄥ畠鏉ュ缓绔嬩竴涓粠鏈嶅姟鍣ㄣ傝涓嶅叧闂换浣曟湇鍔″櫒鏉ュ缓绔嬩竴涓柊鐨勪粠鏈嶅姟鍣紝浣犱篃鍙互浣跨敤闈炲厤璐癸紙鍟嗕笟鐨勶級InnoDB鐑浠藉伐鍏

InnoDB澶嶅埗閲屼竴涓皬鐨勯檺鍒舵槸LOAD TABLE FROM MASTER涓嶅InnoDB绫诲瀷琛ㄨ捣浣滅敤銆傛湁涓ょ鍙兘鐨勫伐浣滃尯锛

·         杞偍涓绘湇鍔″櫒涓婄殑琛ㄥ苟瀵煎叆杞偍鏂囦欢鍒颁粠鏈嶅姟鍣ㄣ

·         鍦ㄧ敤LOAD TABLE tbl_name FROM MASTER寤虹珛澶嶅埗涔嬪墠锛屽湪涓绘湇鍔″櫒涓婁娇鐢ˋLTER TABLE tbl_name TYPE=MyISAM锛屽苟涓旈殢鍚庝娇鐢ˋLTER TABLE鎶婁富琛ㄨ浆鎹㈠洖InnoDB銆

鍦ㄤ富鏈嶅姟鍣ㄥけ璐ョ殑浜嬪姟鏍规湰涓嶅奖鍝嶅鍒躲侻ySQL澶嶅埗鍩轰簬浜岃繘鍒舵棩蹇楋紝MySQ鍦ㄩ偅閲屽啓淇敼鏁版嵁鐨凷QL璇彞銆備粠鏈嶅姟鍣ㄨ涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織锛屽苟鎵ц鍚屾牱鐨凷QL璇彞銆傚敖绠″姝わ紝鍦ㄤ簨鍔¢噷鍙戠敓鐨勮鍙ヤ笉琚啓杩涗簩杩涘埗鏃ュ織鐩村埌浜嬪姟鎻愪氦锛屽湪閭d竴鍒伙紝浜嬪姟閲屾墍鏈夎鍙ヨ涓娆℃у啓杩涙棩蹇椼傚鏋滆鍙ュけ璐ヤ簡锛屾瘮濡傦紝鍥犱负澶栭敭杩濅緥锛屾垨鑰咃紝濡傛灉浜嬪姟琚洖婊氾紝娌℃湁SQL璇彞琚啓杩涗簩杩涘埗鏃ュ織锛屽苟涓斾簨鍔℃牴鏈笉鍦ㄤ粠鏈嶅姟鍣ㄤ笂鎵ц銆

15.2.6.6. 浣跨敤Per-Table琛ㄧ┖闂

浣犲彲浠ュ瓨鍌ㄦ瘡涓狪nnoDB琛ㄥ拰瀹冪殑绱㈠紩鍦ㄥ畠鑷繁鐨勬枃浠跺湪涓紝杩欎釜鐗瑰緛琚О涓“澶氳〃绌洪棿” 锛屽洜涓哄疄闄呬笂姣忎釜琛ㄦ湁瀹冭嚜宸辩殑琛ㄧ┖闂淬

瀵归偅浜涙兂鎶婄壒瀹氳〃鏍肩Щ鍒板垎绂荤墿鐞嗙鐩樼殑鐢ㄦ埛锛屾垨鑰呴偅浜涘笇鏈涘揩閫熸仮澶嶅崟涓〃鐨勫浠借屾棤椤绘墦鏂叾浣橧nnoDB琛ㄧ殑浣跨敤鐨勭敤鎴凤紝浣跨敤澶氳〃绌洪棿浼氭槸鏈夌泭鐨勩

浣犲彲浠ュ線my.cnf鐨刐mysqld]鑺傛坊鍔犱笅闈㈣鏉ュ厑璁稿琛ㄧ┖闂达細

[mysqld]
innodb_file_per_table

閲嶅惎鏈嶅姟鍣ㄤ箣鍚庯紝InnoDB瀛樺偍姣忎釜鏂板垱寤虹殑琛ㄥ埌琛ㄦ牸鎵灞炰簬鐨勬暟鎹簱鐩綍涓嬪畠鑷繁鐨勬枃浠tbl_name.ibd閲屻傝繖绫讳技浜嶮yISAM瀛樺偍寮曟搸鎵鍋氱殑锛屼絾MyISAM 鎶婅〃鍒嗘垚鏁版嵁鏂囦欢tbl_name.MYD鍜岀储寮曟枃浠tbl_name.MYI銆傚浜嶪nnoDB锛屾暟鎹拰鎵浠ヨ涓璧峰瓨鍒.ibd鏂囦欢銆tbl_name.frm鏂囦欢鐓ф棫渚濈劧琚垱寤恒

濡傛灉浣犱粠my.cnf鏂囦欢鍒犻櫎innodb_file_per_table琛岋紝骞堕噸鍚湇鍔″櫒锛孖nnoDB鍦ㄥ叡浜殑琛ㄧ┖闂存枃浠堕噷鍐嶆鍒涘缓琛ㄣ

innodb_file_per_table鍙奖鍝嶈〃鐨勫垱寤恒傚鏋滀綘鐢ㄨ繖涓夐」鍚姩鏈嶅姟鍣紝鏂拌〃琚敤.ibd鏂囦欢鏉ュ垱寤猴紝浣嗘槸浣犱粛鏃ц兘璁块棶鍦ㄥ叡浜〃绌洪棿閲岀殑琛ㄣ傚鏋滀綘鍒犳帀杩欎釜閫夐」锛屾柊琛ㄥ湪鍏变韩琛ㄧ┖闂村唴鍒涘缓锛屼絾浣犱粛鏃у彲浠ヨ闂换浣曠敤澶氳〃绌洪棿鍒涘缓鐨勮〃銆

InnoDB鎬绘槸闇瑕佸叡浜爣绌洪棿銆.ibd鏂囦欢瀵笽nnoDB涓嶈冻浠ュ幓杩愯锛屽叡浜〃绌洪棿鍖呭惈鐔熸倝鐨刬bdata鏂囦欢锛孖nnoDB鎶婂唴閮ㄦ暟鎹瘝鍏稿拰鏈綔鏃ュ織鏀惧湪杩欎釜鏂囦欢涓

娉ㄩ噴锛浣犱笉鑳 鍍忓MyISAM涓鏍凤紝鍦ㄦ暟鎹洰褰曚箣闂撮殢鎰忓湴绉诲姩.ibd鏂囦欢銆傝繖鏄洜涓鸿〃瀹氫箟琚瓨鍦↖nnoDB鍏变韩琛ㄧ┖闂村唴锛岃屼笖InnoDB蹇呴』淇濇寔浜嬪姟ID鍜屾棩蹇楅『搴忓彿鐨勪竴鑷存с

鍦ㄤ竴涓粰瀹氱殑MySQL瀹夎閲岋紝浣犲彲浠ョ敤RENAME TABLE璇彞鎶.ibd鏂囦欢鍜屽叧鑱旂殑琛ㄤ粠涓涓暟鎹簱绉诲埌鍙︿竴涓暟鎹簱锛

RENAME TABLE old_db_name.tbl_name TO new_db_name.tbl_name;

濡傛灉浣犳湁.ibd鏂囦欢鐨勪竴涓共鍑鐨勫浠斤紝浣犲彲浠ユ寜濡備笅鎿嶄綔浠庤璧锋簮鐨勫湴鏂规仮澶嶅畠鍒癕ySQL瀹夎涓細

1.    鍙戝嚭杩欎釜ALTER TABLE璇彞锛

2.      ALTER TABLE tbl_name DISCARD TABLESPACE;

璀﹀憡杩欎釜璇彞鍒犻櫎褰撳墠.ibd鏂囦欢銆

3.    鎶婂浠界殑.ibd鏂囦欢鏀惧洖鍒版伆褰撶殑鏁版嵁搴撶洰褰曘

4.   鍙戝嚭杩欎釜ALTER TABLE璇彞:

5.      ALTER TABLE tbl_name IMPORT TABLESPACE;

鍦ㄤ笂涓嬫枃涓紝涓涓.ibd鏂囦欢骞插噣鐨勫浠芥剰涓猴細

·         .ibd鏂囦欢閲屾病鏈夊皻鏈彁浜ょ殑浜嬪姟鍋氱殑淇敼銆

·         .ibd鏂囦欢閲屾棤鏈悎骞剁殑鎻掑叆娣峰厖鏉$洰銆

·         鍑鍖栧凡缁忎粠.ibd鏂囦欢绉婚櫎鎵鏈夊凡鏍囨敞鍒犻櫎鐨勭储寮曡褰曘

·         mysqld宸茬粡鎶.ibd鏂囦欢鐨勬墍鏈夊凡淇敼椤甸潰浠庣紦鍐叉睜 鍒锋柊鍒版枃浠躲

浣犲彲浠ョ敤涓嬪垪鏂规硶鐢熸垚涓涓.ibd鏂囦欢鐨勫共鍑澶囦唤锛

1.    鍋滄鎵鏈夋潵鑷mysqld鏈嶅姟鍣ㄧ殑娲诲姩锛屽苟鎻愪氦鎵鏈変簨鍔°

2.    绛夊緟鐩磋嚦SHOW INNODB STATUS鏄剧ず鍦ㄦ暟鎹簱琚凡缁忔病鏈夋縺娲荤殑浜嬪姟锛屽苟涓擨nnoDB涓荤嚎绋嬬殑鐘舵佹槸Waiting for server activity銆傜劧鍚庝綘灏卞彲浠ュ鍒.ibd鏂囦欢浜嗐

鐢熸垚涓涓.ibd鏂囦欢鐨勫共鍑澶嶅埗鐨勫彟涓涓柟娉曟槸浣跨敤鍟嗕笟鐨InnoDB鐑浠藉伐鍏

1.    浣跨敤InnoDB鐑浠藉伐鍏澶囦唤InnoDB瀹夎銆

2.    鍦ㄥ浠戒笂鍚姩绗簩涓mysqld鏈嶅姟鍣锛岃瀹冩竻娲佸浠介噷鐨.ibd鏂囦欢銆

15.2.7. 娣诲姞鍜屽垹闄InnoDB鏁版嵁鍜屾棩蹇楁枃浠

杩欎竴鑺傛弿杩板湪InnoDB琛ㄧ┖闂磋楀敖绌洪棿涔嬫椂锛屾垨鑰呬綘鎯宠鏀瑰彉鏃ュ織鏂囦欢澶у皬涔嬫椂锛屼綘鍙互鍋氱殑涓浜涗簨鎯呫

鏈绠鍗曠殑锛屽鍔營nnoDB琛ㄧ┖闂村ぇ灏忕殑鏂规硶鏄粠寮濮嬮厤缃畠涓鸿嚜鍔ㄦ墿灞曠殑銆備负琛ㄧ┖闂村畾涔夐噷鐨勬渶鍚庝竴涓暟鎹枃浠舵寚瀹歛utoextend灞炴с傜劧鍚庡湪鏂囦欢鑰楀敖绌洪棿涔嬫椂锛孖nnoDB浠8MB涓 澧為噺鑷姩澧炲姞璇ユ枃浠剁殑澶у皬銆傚鍔犵殑澶у皬鍙互閫氳繃璁剧疆innodb_autoextend_increment鍊兼潵閰嶇疆,杩欎釜鍊间互MB涓哄崟浣嶏紝榛樿鐨勬槸8銆

浣滀负鏇夸唬锛屼綘鍙互閫氳繃娣诲姞鍙︿竴涓暟鎹枃浠舵潵澧炲姞琛ㄧ┖闂寸殑灏哄銆傝杩欎箞鍋氱殑璇濓紝浣犲繀椤诲仠姝ySQL鏈嶅姟鍣紝缂栬緫my.cnf鏂囦欢 锛屾坊鍔犱竴涓柊鏁版嵁鏂囦欢鍒癷nnodb_data_file_path鐨勬湯灏撅紝鐒跺悗鍐嶆鍚姩鏈嶅姟鍣ㄣ

濡傛灉鏈鍚庝竴涓暟鎹枃浠舵槸鐢ㄥ叧閿瓧autoextend瀹氫箟鐨勶紝缂栬緫my.cnf鏂囦欢鐨勬楠ゅ繀椤昏冭檻鏈鍚庝竴涓暟鎹枃浠跺凡缁忓闀垮埌澶氬ぇ銆傝幏鍙栨暟鎹枃浠剁殑灏哄锛屾妸瀹冨洓鑸嶄簲鍏ュ埌鏈鎺ヨ繎涔樼Н1024 × 1024bytes (= 1MB)锛岀劧鍚庡湪innodb_data_file_path涓槑纭寚瀹氬ぇ鑷寸殑灏哄銆傜劧鍚庝綘鍙互娣诲姞鍙︿竴涓暟鎹枃浠躲傝寰楀彧鏈塱nnodb_data_file_path閲屾渶鍚庝竴涓暟鎹彲浠ヨ鎸囧畾涓鸿嚜鍔ㄦ墿灞曘

浣滀负涓涓緥瀛愩傚亣璁捐〃绌洪棿姝eソ鏈変竴涓嚜鍔ㄦ墿灞曟枃浠秈bdata1锛

innodb_data_home_dir =
innodb_data_file_path = /ibdata/ibdata1:10M:autoextend

鍋囪杩欎釜鏁版嵁鏂囦欢杩囦竴娈垫椂闂村凡缁忛暱鍒988MB銆備笅闈㈡槸娣诲姞鍙︿竴涓绘墿灞曟暟鎹枃浠朵箣鍚庣殑閰嶇疆琛岋細

innodb_data_home_dir =
innodb_data_file_path = /ibdata/ibdata1:988M;/disk2/ibdata2:50M:autoextend

褰撲綘娣诲姞涓涓柊鏂囦欢鍒拌〃绌洪棿鐨勪箣鍚庯紝璇风‘淇″畠骞朵笉瀛樺湪銆傚綋浣犻噸鍚湇鍔″櫒涔嬫椂锛孖nnoDB鍒涘缓骞跺垵濮嬪寲杩欎釜鏂囦欢銆

褰撳墠锛屼綘涓嶈兘浠庤〃绌洪棿鍒犻櫎涓涓暟鎹枃浠躲傝澧炲姞琛ㄧ┖闂寸殑澶у皬锛屼娇鐢ㄥ涓嬫楠わ細

1.    浣跨敤mysqldump杞偍鎵鏈塈nnoDB琛ㄣ

2.    鍋滄鏈嶅姟鍣ㄣ

3.    鍒犻櫎鎵鏈夊凡瀛樺湪鐨勮〃绌洪棿鏂囦欢銆

4.    閰嶇疆鏂拌〃绌洪棿銆

5.    閲嶅惎鏈嶅姟鍣ㄣ

6.    瀵煎叆杞偍鏂囦欢銆

濡傛灉浣犳兂瑕佹敼鍙樹綘鐨処nnoDB鏃ュ織鏂囦欢鐨勬暟閲忓拰澶у皬锛屼綘蹇呴』瑕佸仠姝ySQL鏈嶅姟鍣紝骞剁‘淇″畠琚棤閿欒鍦板叧闂傞殢鍚庡鍒舵棫鏃ュ織鏂囦欢鍒颁竴涓畨鍏ㄧ殑鍦版柟浠ラ槻涓囦竴鏌愭牱涓滆タ鍦ㄥ叧闂椂鍑洪敊鑰屼綘闇瑕佺敤瀹冧滑鏉ユ仮澶嶈〃绌洪棿銆備粠鏃ュ織鏂囦欢鐩綍鍒犻櫎鎵鏈夋棫鏃ュ織鏂囦欢锛岀紪杈憁y.cnf鏉ユ敼鍙樻棩蹇楁枃浠堕厤缃紝骞跺啀娆″惎鍔∕ySQL鏈嶅姟鍣ㄣmysqld鍦ㄥ惎鍔ㄤ箣鏃跺彂鐜版病鏈夋棩蹇楁枃浠讹紝鐒跺悗鍛婅瘔浣犲畠姝e湪鍒涘缓涓涓柊鐨勬棩蹇楁枃浠躲

15.2.8澶囦唤鍜屾仮澶InnoDB鏁版嵁搴

瀹夊叏鏁版嵁搴撶鐞嗙殑鍏抽敭鏄畾鏈熷仛澶囦唤銆

InnoDB鐑浠藉伐鍏鏄竴涓湪绾垮浠藉伐鍏凤紝浣犲彲浠ョ敤瀹冩潵鍦↖nnoDB鏁版嵁搴撹繍琛屼箣鏃跺浠戒綘鐨処nnoDB鏁版嵁搴撱InnoDB鐑浠藉伐鍏涓嶈姹備綘鍏抽棴鏁版嵁搴擄紝骞朵笖瀹冧笉璁剧疆浠讳綍閿佸畾鎴栧共鎵颁綘 姝e父鐨勬暟鎹簱澶勭悊銆InnoDB鐑浠藉伐鍏鏄潪鍏嶈垂锛堝晢涓氱殑锛夐檮鍔犺蒋浠讹紝瀹冩瘡骞寸殑璇佷功璐圭敤鏄瘡鍙癕ySQL鏈嶅姟鍣ㄨ繍琛岀殑璁$畻鏈€390銆傝鍙傞槄InnoDB鐑浠涓婚〉浠ヨ幏寰楁洿璇︾粏鐨勪俊鎭互鍙婂睆骞曟埅鍥俱

濡傛灉浣犲彲浠ュ叧闂綘鐨凪ySQL鏈嶅姟鍣紝浣犲彲浠ョ敓鎴愪竴涓寘鍚獻nnoDB鐢ㄦ潵绠$悊瀹冪殑琛ㄧ殑鎵鏈夋枃浠剁殑浜岃繘鍒跺浠姐備娇鐢ㄥ涓嬫楠わ細

1.    鍏抽棴MySQL鏈嶅姟鍣紝纭俊瀹冩槸鏃犻敊璇叧闂

2.  澶嶅埗浣犳墍鏈夋暟鎹枃浠讹紙ibdata鏂囦欢鍜.ibd鏂囦欢锛夊埌涓涓畨鍏ㄧ殑鍦版柟銆 

3.   澶嶅埗浣犳墍鏈塱b_logfile鏂囦欢鍒颁竴涓畨鍏ㄧ殑鍦版柟銆

4.    澶嶅埗my.cnf閰嶇疆鏂囦欢鎴栨枃浠跺埌涓涓畨鍏ㄧ殑鍦版柟銆

5.    涓轰綘InnoDB琛ㄥ鍒.frm鏂囦欢鍒颁竴涓畨鍏ㄧ殑鍦版柟銆

澶嶅埗瀵笽nnoDB琛ㄨ捣浣滅敤锛屾墍浠ヤ綘鍙互浣跨敤MySQL澶嶅埗鑳藉姏鏉ュ湪闇瑕侀珮鍙敤鎬х殑鏁版嵁搴撶珯鐐逛繚鏈変竴浠芥暟鎹簱鐨勫鍒躲

闄や簡鍒氭墠鎻忚堪鐨勪簩杩涘埗澶囦唤锛屼綘涔熷簲璇ュ懆鏈熸у湴鐢mysqldump杞偍浣犵殑鏁版嵁搴撱傝繖涔堝仛鐨勫師鍥犳槸锛屼簩杩涘埗鏂囦欢鍙兘琚牬鍧忚屼綘娌℃湁娉ㄦ剰鍒般傝浆鍌ㄧ殑鏂囦欢琚瓨鍌ㄦ垚涓轰汉鍙鐨勬枃鏈枃浠讹紝鎵浠ュ畾鐐硅〃鐨勬崯鍧 淇鍙樺緱鏇村鏄撱傚啀鑰咃紝鍥犱负褰㈠紡鏇寸畝鍗曪紝涓ラ噸鏁版嵁鎹熷潖鐨勬満浼氬氨鏇村皬銆mysqldump 涔熸湁涓涓--single-transaction閫夐」锛屼綘鍙互鐢ㄥ畠鏉ュ仛涓涓竴鑷寸殑蹇収鑰屼笉鐢ㄦ妸鍏跺畠瀹㈡埛绔帓闄ゅ湪澶栭潰銆

瑕佽兘澶熶粠涓婅堪鐨勪簩杩涘埗澶囦唤鎭㈠InnoDB鏁版嵁搴撳埌鐜板湪锛屼綘蹇呴』璁╀簩杩涘埗鏃ュ織鍔熻兘鎵撳紑姝e湪杩愯鐨凪ySQL鏈嶅姟鍣ㄣ 鐒跺悗浣犲彲浠ュ簲鐢ㄤ簩杩涘埗鏃ュ織鍒板浠芥暟鎹簱浠ュ疄鐜皃oint-in-time鎭澶嶏細

mysqlbinlog yourhostname-bin.123 | mysql

瑕佷粠MySQL鏈嶅姟鍣ㄧ殑宕╂簝鎭㈠锛屽敮涓闇瑕佺殑鏄噸鏂板惎鍔ㄥ畠銆侷nnoDB鑷姩妫鏌ユ棩蹇楀苟鎵ц鍒扮幇鍦ㄧ殑鏁版嵁搴撳墠婊氥侷nnoDB鑷姩鍥炴粴鍦ㄥ穿婧冩椂 鍛堢幇鐨勬湭鎻愪氦鐨勪簨鍔°傚湪鎭㈠杩囩▼涓紝mysqld鏄剧ず濡備笅涓浜涜緭鍑猴細

InnoDB: Database was not shut down normally.
InnoDB: Starting recovery from log files...
InnoDB: Starting log scan based on checkpoint at
InnoDB: log sequence number 0 13674004
InnoDB: Doing recovery: scanned up to log sequence number 0 13739520
InnoDB: Doing recovery: scanned up to log sequence number 0 13805056
InnoDB: Doing recovery: scanned up to log sequence number 0 13870592
InnoDB: Doing recovery: scanned up to log sequence number 0 13936128
...
InnoDB: Doing recovery: scanned up to log sequence number 0 20555264
InnoDB: Doing recovery: scanned up to log sequence number 0 20620800
InnoDB: Doing recovery: scanned up to log sequence number 0 20664692
InnoDB: 1 uncommitted transaction(s) which must be rolled back
InnoDB: Starting rollback of uncommitted transactions
InnoDB: Rolling back trx no 16745
InnoDB: Rolling back of trx no 16745 completed
InnoDB: Rollback of uncommitted transactions completed
InnoDB: Starting an apply batch of log records to the database...
InnoDB: Apply batch completed
InnoDB: Started
mysqld: ready for connections

濡傛灉鏁版嵁搴撹鎹熷潖鎴栫鐩樺嚭閿欙紝浣犲繀椤讳粠澶囦唤鍋氭仮澶嶃傚湪鎹熷潖鐨勬儏鍐典笅锛屼綘棣栧厛搴旇鎵惧嚭涓涓病鏈夎鎹熷潖鐨勫浠姐傛仮澶嶆暟鎹簱澶囦唤涔嬪悗锛屼粠浜岃繘鍒舵棩蹇楁枃浠舵仮澶嶃

鍦ㄤ竴浜涙暟鎹簱鎹熷潖鐨勬儏鍐典笅锛屼粎浠呰浆鍌紝绉婚櫎骞堕噸寤轰竴涓垨鏁颁釜琚牬鍧忕殑琛ㄦ槸瓒冲鐨勩備綘鍙互鐢–HECK TABLE SQL璇彞鏉ユ鏌ヨ〃鏄惁鎹熷潖锛岃櫧鐒禖HECK TABLE姝e父鍦颁笉妫鏌ユ瘡绉嶅彲鑳界殑鎹熷潖锛屼綘鍙互浣跨敤innodb_tablespace_monitor鏉ユ鏌ヨ〃绌洪棿鏂囦欢鍐呮枃浠剁┖闂寸鐞嗙殑瀹屾暣鎬с

鍦ㄤ竴浜涙儏鍐典笅锛屾槑鏄惧湴鏁版嵁搴撴崯鍧忔槸鍥犱负鎿嶄綔绯荤粺鎹熷潖瀹冭嚜宸辩殑鏂囦欢缂撳瓨锛岀鐩樹笂鐨勬暟鎹彲鑳藉畬濂斤紝鏈濂芥槸棣栧厛閲嶅惎璁$畻鏈恒傚畠鍙互娑堥櫎閭d簺鏄惧緱鏄暟鎹簱椤垫崯鍧忕殑閿欒銆

15.2.8.1. 寮哄埗鎭㈠

濡傛灉鏁版嵁搴撻〉琚牬鍧忥紝浣犲彲鑳芥兂瑕佺敤SELECT INTO OUTFILE浠庝粠鏁版嵁搴撹浆鍌ㄤ綘鐨勮〃锛岄氬父浠ヨ繖绉嶆柟娉曡幏鍙栫殑澶у鏁版暟鎹槸瀹屽ソ鐨勩傚嵆浣胯繖鏍凤紝鎹熷潖鍙兘瀵艰嚧SELECT * FROM tbl_name鎴栬匢nnoDB鍚庡彴鎿嶄綔宕╂簝鎴栨柇瑷锛屾垨鑰呯敋鑷充娇寰桰nnoDB鍓嶆粴鎭㈠宕╂簝銆 灏界濡傛锛屼綘鍙互鐢ㄥ畠鏉ュ己鍒禝nnoDB瀛樺偍寮曟搸鍚姩鍚屾椂闃绘鍚庡彴鎿嶄綔杩愯锛屼互渚夸綘鑳借浆鍌ㄤ綘鐨勮〃銆備緥濡傦細浣犲彲浠ュ湪閲嶅惎鏈嶅姟鍣ㄤ箣鍓嶏紝鍦ㄩ夐」鏂囦欢鐨刐mysqld]鑺傛坊鍔犲涓嬬殑琛岋細

[mysqld]
innodb_force_recovery = 4

innodb_force_recovery琚厑璁哥殑闈為浂鍊煎涓嬨備竴涓洿澶х殑鏁板瓧鍖呭惈鎵鏈夋洿灏忔暟瀛楃殑棰勯槻鎺柦銆傚鏋滀綘鑳藉鐢ㄤ竴涓鏁版槸4鐨勯夐」鍊兼潵杞偍浣犵殑琛紝閭d箞浣犳槸姣旇緝瀹夊叏鐨勶紝鍙湁涓浜涘湪鎹熷潖鐨勫崟鐙〉闈笂鐨勬暟鎹細涓㈠け銆備竴涓负6鐨勫兼洿澶稿紶锛屽洜涓烘暟鎹簱椤佃鐣欏湪涓涓檲鏃х殑鐘舵侊紝杩欎釜鐘舵佸弽杩囨潵鍙互寮曞彂瀵笲鏍戝拰鍏跺畠鏁版嵁搴撶粨鏋勭殑鏇村鐮村潖銆

·         1 (SRV_FORCE_IGNORE_CORRUPT)

鍗充娇鏈嶅姟鍣ㄦ娴嬪埌涓涓崯鍧忕殑椤碉紝涔熻鏈嶅姟鍣ㄨ繍琛岀潃锛涜瘯鐫璁㏒ELECT * FROM tbl_name 璺宠繃鎹熷潖鐨勭储寮曡褰曞拰椤碉紝杩欐牱鏈夊姪浜庤浆鍌ㄨ〃銆

·         2 (SRV_FORCE_NO_BACKGROUND)

闃绘涓荤嚎绋嬭繍琛岋紝濡傛灉宕╂簝鍙兘鍦ㄥ噣鍖栨搷浣滆繃绋嬩腑鍙戠敓锛岃繖灏嗛樆姝㈠畠銆

·         3 (SRV_FORCE_NO_TRX_UNDO)

鎭㈠鍚庝笉杩愯浜嬪姟鍥炴粴銆

·         4 (SRV_FORCE_NO_IBUF_MERGE)

涔熼樆姝㈡彃鍏ョ紦鍐插悎骞舵搷浣溿傚鏋滀綘鍙兘浼氬鑷翠竴涓穿婧冦傛渶濂戒笉瑕佸仛杩欎簺鎿嶄綔锛屼笉瑕佽绠楄〃缁熻琛ㄣ

·         5 (SRV_FORCE_NO_UNDO_LOG_SCAN)

鍚姩鏁版嵁搴撲箣鏃朵笉鏌ョ湅鏈畬鎴愭棩蹇楋細InnoDB鎶婃湭瀹屾垚鐨勪簨鍔¤涓哄凡鎻愪氦鐨勩

·         6 (SRV_FORCE_NO_LOG_REDO)

涓嶈鍦ㄦ仮澶嶈繛鎺ヤ腑鍋氭棩蹇楀墠婊氥

鏁版嵁搴撲笉鑳藉彟澶栧湴甯︾潃杩欎簺閫夐」涓鍏佽鐨勯夐」鏉ヤ娇鐢ㄣ浣滀负涓涓畨鍏ㄦ帾鏂斤紝褰搃nnodb_force_recovery琚缃 涓哄ぇ浜0鐨勫兼椂锛孖nnoDB闃绘鐢ㄦ埛鎵цINSERT, UPDATE鎴朌ELETE鎿嶄綔.

鍗充娇寮哄埗鎭㈠琚娇鐢紝浣犱篃鍙互DROP鎴朇REATE琛ㄣ傚鏋滀綘鐭ラ亾涓涓粰瀹氱殑琛ㄦ鍦ㄥ鑷村洖婊氬穿婧冿紝浣犲彲浠ョЩ闄ゅ畠銆備綘涔熷彲浠ョ敤杩欎釜鏉ュ仠姝㈢敱澶辫触鐨勫ぇ瀹楀鍏ユ垨澶辫触鐨凙LTER TABLE瀵艰嚧鐨勫け鎺 鍥炴粴銆備綘鍙互鏉鎺mysqld杩涚▼锛岀劧鍚庤缃甶nnodb_force_recovery涓3锛屼娇寰楁暟鎹簱琚 鎸傝捣鑰屼笉闇瑕佸洖婊氾紝鐒跺悗鑸嶅純瀵艰嚧澶辨帶鍥炴粴鐨勮〃銆

15.2.8.2. 妫鏌ョ偣

InnoDB瀹炵幇涓绉嶈璁よ瘑涓“妯$硦”鏌ョ偣璁剧疆鐨勬鏌ョ偣鏈哄埗銆侷nnoDB浠ュ皬鎵归噺浠庣紦鍐叉睜 鍒锋柊宸蹭慨鏀圭殑鏁版嵁搴撻〉銆傛病蹇呰浠ュ崟涓壒娆″埛鏂扮紦鍐叉睜锛屽崟鎵规鍒锋柊瀹為檯鎿嶄綔涓彲鑳戒細鍦ㄦ鏌ョ偣璁剧疆杩涚▼涓仠姝㈢敤鎴稴QL璇彞鐨勫鐞嗐

鍦ㄥ穿婧冩仮澶嶄腑锛孖nnoDB鎵惧琚啓杩涙棩蹇楃殑妫鏌ョ偣鏍囩銆傚畠鐭ラ亾鎵鏈夊湪璇ユ爣绛句箣鍓嶅鏁版嵁搴撶殑淇敼琚憟鐜板湪鏁版嵁搴撶殑纾佺洏鏄犲儚涓傜劧鍚嶪nnoDB浠庢鏌ョ偣寰鍓嶆壂鎻忔棩蹇楁枃浠讹紝瀵规暟鎹簱搴旂敤宸插啓鍏ユ棩蹇楃殑淇敼銆

InnoDB浠ュ惊鐜柟寮忓啓鏃ュ織鏂囦欢銆傛墍鏈変娇寰楃紦鍐叉睜閲岀殑鏁版嵁搴撻〉涓庣鐩樹笂鐨勬槧鍍忎笉鍚岀殑宸叉彁浜や慨鏀瑰繀椤诲嚭鐜板湪鏃ュ織鏂囦欢涓 锛屼互澶囦竾涓InnoDB闇瑕佸仛涓涓仮澶嶃傝繖鎰忓懗鐫锛屽綋InnoDB寮濮嬮噸鏂颁娇鐢ㄤ竴涓棩蹇楁枃浠讹紝瀹冮渶瑕佺‘璁ゅ湪纾佺洏涓婄殑鏁版嵁搴撻〉鏄犲儚鍖呭惈宸插啓杩汭nnoDB鍑嗗閲嶆柊浣跨敤鐨勬棩蹇楁枃浠堕噷鐨勪慨鏀广傛崲鍙ヨ瘽 璇达紝InnoDB蹇呴』鍒涘缓涓涓鏌ョ偣锛岃繖缁忓父娑夊強宸蹭慨鏀 鏁版嵁搴撻〉鍒扮鐩樼殑鍒锋柊銆

鍓嶉潰鐨勫彊杩拌В閲婁簡涓轰粈涔堜娇浣犵殑鏃ュ織鏂囦欢闈炲父澶т細鍦ㄨ缃鏌ョ偣涓妭绾︾鐩業/O銆傝缃棩蹇楁枃浠舵荤殑澶у皬鍜岀紦鍐叉睜涓鏍峰ぇ鎴栬呯敋鑷虫瘮缂撳啿姹犲ぇ閫氬父鏄湁鎰忎箟鐨勩傚ぇ鏃ュ織鏂囦欢鐨勭己鐐规槸宕╂簝鎭㈠瑕佽姳鏇撮暱鐨勬椂闂达紝鍥犱负鏈夋洿澶氬啓鍏ユ棩蹇楃殑淇℃伅瑕佸簲鐢ㄥ埌鏁版嵁搴撲笂銆

15.2.9. 鎶婁竴涓InnoDB鏁版嵁搴撶Щ鍒板彟涓鍙版満鍣

鍦╓indows涓婏紝 InnoDB 鎬绘槸鍦ㄥ唴閮ㄤ互灏忓啓鍚嶅瓧鐨勬柟寮忓瓨鍌ㄦ暟鎹簱鍜岃〃銆傝浠嶶nix鎶婁簩杩涘埗鏍煎紡鐨勬暟鎹簱绉诲埌Windows锛屾垨鑰呬粠Windows绉诲埌Unix锛屼綘搴旇璁╂墍鏈夎〃鍜屾暟鎹簱鐨勫悕瀛楀皬鍐欍傝瀹炵幇杩欎釜锛屼竴涓柟渚跨殑鏂瑰紡鏄湪鍒涘缓浠讳綍鏁版嵁搴 鍜岃〃涔嬪墠锛屽湪浣犵殑my.cnf鎴杕y.ini鏂囦欢鐨刐mysqld]鑺傚唴娣诲姞濡備笅琛岋細

[mysqld]
lower_case_table_names=1

绫讳技浜嶮yISAM鏁版嵁鏂囦欢锛孖nnoDB鏁版嵁鍜屾棩蹇楁枃浠跺湪鎵鏈夋湁鐩稿悓娴偣鏁版牸寮忕殑骞冲彴涓婃槸浜岃繘鍒跺吋瀹圭殑銆備綘鍙互鎷疯礉鎵鏈夊垪鍦15.2.8鑺傦紝鈥淚nnoDB鏁版嵁搴撶殑澶囦唤鍜屾仮澶嶁閲岀殑鐩稿叧鏂囦欢鏉ョ畝鍗曞湴绉诲姩涓涓狪nnoDB鏁版嵁搴撱傚鏋滄诞鐐规牸寮忎笉鍚岋紝浣嗕綘娌℃湁鍦ㄨ〃涓娇鐢‵LOAT鎴朌OUBLE鏁版嵁绫诲瀷锛屽垯杩囩▼鏄竴鏍凤細绠鍗曞湴鎷疯礉鐩稿叧鏂囦欢銆傚鏋滄牸寮忎笉瀹癸紝涓斾綘鐨勮〃鍖呭惈娴偣鏁版嵁锛屼綘蹇呴』浣跨敤mysqldump鍦ㄤ竴鍙版満鍣ㄨ浆鍌ㄤ綘鐨勮〃锛岀劧鍚庡湪鍙︿竴鍙版満鍣ㄥ鍏ヨ浆鍌ㄦ枃浠躲

鍋囪琛ㄧ┖闂存湁瓒冲鐨勭┖闂翠緵瀵煎叆浜嬪姟浜х敓鐨勫ぇ鍨嬪洖婊氱墖鏂娇鐢紝鍒欐彁楂樻ц兘鐨勪竴涓柟娉曟槸鍦ㄥ鍏ユ暟鎹椂鍏虫帀autocommit妯″紡銆備粎鍦ㄥ鍏ユ暣涓〃鎴栬〃鐨勪竴涓墖鏂箣鍚庢彁浜ゃ

15.2.10InnoDB浜嬪姟妯″瀷鍜岄攣瀹

鍦↖nnoDB浜嬪姟妯″紡涓紝鐩殑鏄妸澶氱増鏈暟鎹簱鐨勬渶濂界壒鎬т笌浼犵粺鐨勪簩鐩搁攣瀹氬悎骞惰捣鏉ャ侷nnoDB浠racle鐨勯鏍硷紝瀵硅绾ц繘琛岄攣瀹氾紝骞朵笖 榛樿杩愯鏌ヨ浣滀负闈為攣瀹氭寔缁銆傚湪InnoDB涓攣瀹氱殑琛ㄨ瀛樺偍寰楀姝よ妭鐪佺┖闂达紝浠ヨ嚦浜庝笉闇瑕侀攣瀹氬澶э細鍏稿瀷鍦帮紝鏁颁釜鐢ㄦ埛琚厑璁稿湪鏁版嵁搴撲腑閿佸畾姣忎竴琛岋紝鎴栬呰鐨勪换浣曢殢鏈哄瓙闆嗭紝鑰孖nnoDB涓嶄細鑰楀敖鍐呭瓨銆

15.2.10.1. InnoDB閿佸畾妯″紡

InnoDB瀹炵幇鏍囧噯琛岀骇閿佸畾锛屽湪杩欓噷鏈変袱绉嶇被鍨嬬殑閿侊細 locks:

·         鍏变韩鐨(S)閿佸厑璁镐竴涓簨鍔″幓璇讳竴琛岋紙tuple锛夈

·         鐙崰鐨勯攣(X)鍏佽涓涓簨鍔℃洿鏂版垨鍒犻櫎涓琛屻

濡傛灉浜嬪姟A 鍦╰uple t涓婃寔鏈夌嫭鍗犻攣瀹氾紝鏉ヨ嚜涓嶅悓浜嬪姟B鐨勫t涓婁换涓绫诲瀷鐨勯攣鐨勮姹備笉琚┈涓婅鍙紝鍙栬屼唬涔嬪湴锛屼簨鍔B 涓嶅緱涓嶇瓑寰呬簨鍔閲婃斁鍦╰uple t涓婄殑閿併

濡傛灉浜嬪姟 A 鍦╰uple t涓婃寔鏈変竴涓叡浜殑閿(S)锛岄偅涔

·         鏉ヨ嚜涓嶅悓鐨勪簨鍔B瀵瑰湪t 涓X鐨勯攣瀹氳姹備笉鑳借椹笂璁稿彲銆

·         鏉ヨ嚜涓嶅悓鐨勪簨鍔B瀵瑰湪tS鐨勯攣瀹氳姹傚彲浠ヨ椹笂鑾峰噯銆傚洜姝AB鎸佹湁t涓婄殑S閿佸畾銆

涓嶄粎濡傛锛孖nnoDB鏀寔澶氶棿闅斿昂瀵搁攣瀹锛屽畠鍏佽璁板綍閿佸拰瀵规暣涓〃鐨勯攣鍏卞瓨銆傝浣垮緱澶氶棿闅斿昂瀵哥骇鍒殑閿佸畾瀹為檯鍖栵紝棰濆绫诲瀷鐨勯攣锛岃绉颁负intention locks琚娇鐢ㄣ傚湪InnoDB涓紝鎰忓浘閿佸畾鏄〃閿佸畾銆 瀵逛簬涓涓簨鍔★紝鎰忓浘閿佸畾涔嬪悗鐞嗘兂鐨勬槸鎸囨槑鍦ㄨ琛ㄤ腑瀵逛竴涓闅忓悗闇瑕佸摢涓绫诲瀷鐨勯攣瀹氾紙鍏变韩杩樻槸鐙崰锛夈傛湁涓ょ鎰忓浘閿佽鐢ㄥ湪InnoDB涓紙鍋囪浜嬪姟T 鍦ㄨ〃R涓姹備竴涓凡鎸囧嚭鐨勭被鍨嬬殑閿侊級锛

·         鎰忓浘鍏变韩(IS)锛氫簨鍔T 鎰忓浘缁欒〃T涓婂崟鐙殑tuple璁剧疆S 閿佸畾銆

·         鎰忓浘鐙崰(IX)锛氫簨鍔T 鎰忓浘缁欒繖浜泃uple璁剧疆X 閿佸畾銆

鎰忓浘閿佸崗璁涓嬶細

·         鍦ㄥ亣璁剧殑浜嬪姟鍙互鑾峰緱瀵规煇鍋囧畾琛岀殑S 閿佸畾涔嬪墠锛屽畠蹇呴』棣栧厛鑾峰緱瀵瑰寘鍚琛岀殑琛ㄧ殑涓涓IS 鎴栬呮洿寮虹殑閿佸畾銆

·         鍦ㄥ亣璁剧殑浜嬪姟鍙互鑾峰緱瀵规煇鍋囧畾琛岀殑X 閿佸畾涔嬪墠锛屽畠蹇呴』棣栧厛鑾峰緱瀵瑰寘鍚琛岀殑琛ㄧ殑涓涓IX 閿佸畾銆

杩欎簺缁撴灉鍙互鏂逛究鍦扮敤涓涓攣绫诲瀷鍏煎鐭╅樀鏉ユ荤粨锛

 

X

IX

S

IS

X

鍐茬獊

鍐茬獊

鍐茬獊

鍐茬獊

IX

鍐茬獊

鍏煎

鍐茬獊

鍏煎

S

鍐茬獊

鍐茬獊

鍏煎

鍏煎

IS

鍐茬獊

鍏煎

鍏煎

鍏煎

濡傛灉涓涓攣瀹氫笌鐜板湪閿佸畾鍏煎鐨勮瘽锛屽畠琚巿缁欎竴涓鎵樹簨鍔°傚鏋滀竴涓攣瀹氫笌鐜板瓨閿佸畾鍐茬獊锛屽畠灏变笉琚巿浜堜竴涓鎵樹簨鍔°備簨鍔$瓑寰呯潃鐩村埌鍐茬獊鐨勭幇瀛橀攣瀹氳閲婃斁鎺夈傚鏋滀竴涓攣瀹氳姹備笌鐜板瓨閿佸畾鐩稿啿绐侊紝涓斾笉鑳借鎺堜簣锛屽洜涓哄畠鍙兘浼氬鑷存閿侊紝涓涓敊璇骇鐢熴

鍥犳锛屾剰鍥鹃攣瀹氫笉闃荤浠讳綍涓滆タ锛岄櫎浜嗗畬鍏ㄨ〃璇锋眰锛堟瘮濡侺OCK TABLES ... WRITE锛夈IXIS閿佸畾鐨勪富瑕佺洰鐨勬槸鏄剧ず鏌愪汉姝i攣瀹氫竴琛岋紝鎴栧皢瑕佸湪琛ㄤ腑閿佸畾涓琛屻

涓嬪垪鐨勪緥瀛愭紨绀哄綋閿佸畾璇锋眰鍙兘浼氬鑷存閿佷箣鏃朵竴涓敊璇細濡備綍鍙戠敓銆備緥瀛愪腑鍖呮嫭涓や釜瀹㈡埛绔疉鍜孊銆

棣栧厛瀹㈡埛绔疉鍒涘缓涓涓寘鍚竴涓鐨勮〃锛岀劧鍚庡紑濮嬩竴涓簨鍔°傚湪杩欎釜浜嬪姟鍐咃紝A閫氳繃鍦ㄥ叡浜ā寮忛夋嫨琛岃幏寰楀琛岀殑S 閿佸畾锛

mysql> CREATE TABLE t (i INT) ENGINE = InnoDB;
Query OK, 0 rows affected (1.07 sec)
 
mysql> INSERT INTO t (i) VALUES(1);
Query OK, 1 row affected (0.09 sec)
 
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT * FROM t WHERE i = 1 LOCK IN SHARE MODE;
+------+
| i    |
+------+
|    1 |
+------+
1 row in set (0.10 sec)

鎺ョ潃锛屽鎴风B寮濮嬩竴涓簨鍔″苟灏濊瘯浠庤琛ㄥ垹闄よ锛

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
 
mysql> DELETE FROM t WHERE i = 1;

鍒犻櫎鎿嶄綔瑕佹眰涓涓X 閿佸畾銆傚洜涓鸿繖涓攣瀹氫笉鍏煎瀹㈡埛绔疉鎸佹湁鐨S閿佸畾锛屾墍浠X 閿佸畾涓嶈 鍏佽锛屾墍浠ヨ姹傝繘鍏ュ琛屽強瀹㈡埛绔樆鎸$殑閿佸畾璇锋眰闃熷垪銆

鏈鍚庯紝瀹㈡埛绔疉涔熻瘯鍥句粠琛ㄤ腑鍒犻櫎璇ヨ锛

mysql> DELETE FROM t WHERE i = 1;
ERROR 1213 (40001): Deadlock found when trying to get lock;
try restarting transaction

鍥犱负瀹㈡埛绔疉闇瑕佷竴涓X 閿佸畾鏉ュ垹闄よ琛岋紝鎵浠ュ湪杩欓噷鍙戠敓姝婚攣銆傚敖绠″姝わ紝閿佸畾璇锋眰涓嶈鍏佽锛屽洜涓哄鎴风B宸茬粡鏈変竴涓X閿佸畾鐨勮姹傚苟涓斿畠姝g瓑寰呭鎴风A閲婃斁S閿佸畾銆傚洜涓哄鎴风B涔嬪墠瀵X 閿佸畾鐨勮姹傦紝琚鎴风A鎸佹湁鐨S閿佸畾涔熶笉鑳藉崌绾у埌X閿佸畾銆傚洜姝わ紝InnoDB瀵瑰鎴风A浜х敓涓涓敊璇紝骞朵笖閲婃斁瀹冪殑閿佸畾銆傚湪閭d竴鐐逛笂锛屽鎴风B鐨勯攣瀹氳姹傚彲浠ヨ璁稿彲锛屽苟涓斿鎴风B浠庤〃涓垹闄よ銆

15.2.10.2. InnoDB鍜 AUTOCOMMIT

鍦↖nnoDB涓紝鎵鏈夌敤鎴疯涓洪兘鍦ㄤ簨鍔″唴鍙戠敓銆傚鏋滆嚜鍔ㄦ彁浜ゆā寮忚鍏佽锛屾瘡涓猄QL璇彞鍦ㄥ畠鑷繁涓婂舰鎴愪竴涓崟鐙殑浜嬪姟銆侻ySQL鎬绘槸甯︾潃鍏佽鑷姩鎻愪氦鏉ュ紑濮嬩竴涓柊杩炴帴銆

濡傛灉鑷姩鎻愪氦妯″紡琚敤SET AUTOCOMMIT = 0鍏抽棴锛岄偅涔堟垜浠彲浠ヨ涓轰竴涓敤鎴锋绘槸鏈変竴涓簨鍔℃墦寮鐫銆備竴涓猄QL COMMIT鎴朢OLLBACK璇彞缁撴潫褰撳墠浜嬪姟骞朵笖涓涓柊浜嬪姟寮濮嬨備袱涓鍙ラ兘閲婃斁鎵鏈夊湪褰撳墠浜嬪姟涓璁剧疆鐨処nnoDB閿佸畾銆備竴涓狢OMMIT璇彞鎰忓懗鐫鍦ㄥ綋鍓嶄簨鍔′腑鍋氱殑鏀瑰彉琚敓鎴愪负姘镐箙鐨勶紝骞朵笖鍙樻垚鍏跺畠鐢ㄦ埛鍙鐨勩備竴涓猂OLLBACK璇彞锛屽湪鍙︿竴鏂归潰锛屾挙閿鎵鏈夊綋鍓嶄簨鍔″仛鐨勪慨鏀广

濡傛灉杩炴帴鏈夎鍏佽鐨勮嚜鍔ㄦ彁浜わ紝閫氳繃鐢ㄦ槑纭殑START TRANSACTION鎴朆EGIN璇彞鏉ュ紑濮嬩竴涓簨鍔★紝骞剁敤COMMIT鎴栬匯OLLBACK璇彞鏉ョ粨鏉熷畠锛岃繖鏍风敤鎴蜂粛鏃у彲浠ユ墽琛屼竴涓閲嶈鍙ヤ簨鍔°

15.2.10.3. InnoDB鍜孴RANSACTION ISOLATION LEVEL

鎸夌収SQL:1992 浜嬪姟闅旂绾у埆锛孖nnoDB榛樿鏄彲閲嶅璇荤殑锛圧EPEATABLE READ锛夈侻ySQL/InnoDB 鎻愪緵SQL鏍囧噯鎵鎻忚堪鐨勬墍鏈夊洓涓簨鍔¢殧绂荤骇鍒備綘鍙互鍦ㄥ懡浠よ鐢--transaction-isolation閫夐」锛屾垨鍦ㄩ夐」鏂囦欢閲岋紝涓烘墍鏈夎繛鎺ヨ缃 榛樿闅旂绾у埆銆備緥濡傦紝浣犲彲浠ュ湪my.inf鏂囦欢鐨刐mysqld]鑺傞噷绫讳技濡備笅璁剧疆璇ラ夐」锛歡lobally

[mysqld]
transaction-isolation = {READ-UNCOMMITTED | READ-COMMITTED
                         | REPEATABLE-READ | SERIALIZABLE}

鐢ㄦ埛鍙互鐢⊿ET TRANSACTION璇彞鏀瑰彉鍗曚釜浼氳瘽鎴栬呮墍鏈夋柊杩涜繛鎺ョ殑闅旂绾у埆銆傚畠鐨勮娉曞涓嬶細

SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL
                       {READ UNCOMMITTED | READ COMMITTED
                        | REPEATABLE READ | SERIALIZABLE}

娉ㄦ剰锛屽--transaction-isolation閫夐」鐨勭骇鍒悕涓湁杩炲瓧绗︼紝浣嗗湪瀵筍ET TRANSACTION璇彞鐨勭骇鍒悕涓病鏈夈

榛樿鐨勮涓烘槸涓轰笅涓涓紙鏈紑濮嬶級浜嬪姟璁剧疆闅旂绾у埆銆傚鏋滀綘浣跨敤GLOBAL鍏抽敭瀛楋紝璇彞鍦ㄥ叏灞瀵逛粠閭g偣寮濮嬪垱寤虹殑鎵鏈夋柊杩炴帴锛堥櫎浜嗕笉瀛樺湪鐨勮繛鎺ワ級璁剧疆 榛樿浜嬪姟绾у埆銆備綘闇瑕丼UPER鍏ㄥ幙鏉ュ仛杩欎釜銆備娇鐢⊿ESSION 鍏抽敭瀛楅泦涓哄皢鏉ュ湪褰撳墠杩炴帴涓婃墽琛岀殑浜嬪姟璁剧疆榛樿浜嬪姟绾у埆 銆

浠讳綍瀹㈡埛绔兘鑳借嚜鐢辨敼鍙樹細璇濋殧绂荤骇鍒紙鐢氳嚦鍦ㄤ簨鍔$殑涓棿锛夛紝鎴栬呬负涓嬩竴涓簨鍔¤缃殧绂荤骇鍒

浣犲彲浠ョ敤涓嬪垪璇彞鏌ヨ鍏ㄥ眬鍜屼細璇濅簨鍔¢殧绂荤骇鍒細

SELECT @@global.tx_isolation;
SELECT @@tx_isolation;

鍦ㄨ绾ч攣瀹氫腑锛孖nnoDB浣跨敤next-key閿佸畾銆傝繖鎰忓懗鐫闄や簡绱㈠紩璁板綍锛孖nnoDB涔熷彲浠ユ妸绱㈠紩璁板綍鍓嶇殑闂撮殭閿佸畾鍒板叾瀹冪敤鎴锋墍鍋氱殑绱ф帴璇ョ储寮曡褰曚箣鍓嶇殑鍧楁彃鍏ヤ笂銆備竴涓猲ext-key閿佸畾鎸囧悜涓涓攣瀹氫綇涓涓储寮曡褰曞拰瀹冧箣鍓嶇殑闂撮殭鐨勯攣瀹氥備竴涓棿闅欓攣瀹氭寚浠呴攣浣忎竴浜涚储寮曡褰曚箣鍓嶇殑闂撮殭鐨勯攣瀹氥

InnoDB涓瘡涓殧绂荤骇鍒殑璇︾粏鎻忚堪濡備笅锛

·         READ UNCOMMITTED

SELECT璇彞浠ラ潪閿佸畾鏂瑰紡琚墽琛岋紝浣嗘槸涓涓彲鑳芥洿鏃╂湡鐗堟湰鐨勮褰曚細琚敤鍒般傚洜姝わ紝浣跨敤杩欎釜闅旂绾у埆锛屾瘮濡傦紝璇绘槸涓嶈繛璐殑銆傜潃涔熻绉颁负“鑴忚”锛dirty read锛夈傚彟澶栵紝杩欎釜闅旂绾у埆璞READ COMMITTED涓鏍蜂綔鐢ㄣ

·         READ COMMITTED

涓涓湁浜涜薄Oracle鐨勯殧绂荤骇鍒傛墍鏈塖ELECT ... FOR UPDATE鍜孲ELECT ... LOCK IN SHARE MOD璇彞浠呴攣瀹氱储寮曡褰曪紝鑰屼笉閿佸畾璁板綍鍓嶇殑闂撮殭锛屽洜鑰屽厑璁搁殢鎰忕揣鎸ㄧ潃宸查攣瀹氱殑璁板綍鎻掑叆鏂拌褰曘俇PDATE鍜孌ELETE璇彞浣跨敤涓涓甫鍞竴鎼滅储鏉′欢鐨勫敮涓鐨勭储寮曚粎閿佸畾鎵惧埌鐨勭储寮曡褰曪紝鑰屼笉鍖呮嫭璁板綍鍓嶇殑闂撮殭銆傚湪鑼冨洿绫诲瀷UPDATE鍜孌ELETE璇彞锛孖nnoDB蹇呴』瀵硅寖鍥磋鐩栫殑闂撮殭璁剧疆next-key閿佸畾鎴栭棿闅欓攣瀹氫互鍙婂叾瀹冪敤鎴峰仛鐨勫潡鎻掑叆銆傝繖鏄緢蹇呰鐨勶紝鍥犱负瑕佽MySQL澶嶅埗鍜屾仮澶嶈捣浣滅敤锛“骞界伒琛”蹇呴』琚樆姝㈡帀銆

鎸佺画璇昏涓哄鍚屽湪Oracle涓細鍗充娇鍦ㄥ悓涓浜嬪姟鍐咃紝 姣忎釜鎸佺画璇昏缃苟璇诲彇瀹冭嚜宸辩殑鏂板揩鐓с傝鍙傞槄15.2.10.4鑺傦紝鈥滄寔缁潪閿佸畾璇”

·         REPEATABLE READ

杩欐槸InnoDB鐨勯粯璁ら殧绂荤骇鍒傚甫鍞竴鎼滅储鏉′欢浣跨敤鍞竴绱㈠紩鐨凷ELECT ... FOR UPDATE, SELECT ... LOCK IN SHARE MODE, UPDATE 鍜孌ELETE璇彞鍙攣瀹氭壘鍒扮殑绱㈠紩璁板綍锛岃屼笉閿佸畾璁板綍鍓嶇殑闂撮殭銆傜敤鍏跺畠鎼滅储鏉′欢锛岃繖浜涙搷浣滈噰鐢╪ext-key閿佸畾锛岀敤next-key閿佸畾鎴栬呴棿闅欓攣瀹氶攣浣忔悳绱㈢殑绱㈠紩鑼冨洿锛屽苟涓旈樆姝㈠叾瀹冪敤鎴风殑鏂版彃鍏ャ

鍦ㄦ寔缁涓紝鏈変竴涓笌涔嬪墠闅旂绾у埆閲嶈鐨勫樊鍒細鍦ㄨ繖涓骇鍒紝鍦ㄥ悓涓浜嬪姟鍐呮墍鏈夋寔缁璇诲彇鐢辩涓娆¤鎵纭畾鐨勫悓涓蹇収銆傝繖涓儻渚嬫剰鍛崇潃濡傛灉浣犲湪鍚屼竴浜嬪姟鍐呭彂鍑烘暟涓棤鏍煎紡SELECT璇彞锛岃繖浜汼ELECT璇彞瀵圭浉浜掍箣闂翠篃鏄寔缁殑锛岃鍙傞槄15.2.10.4鑺傦紝鈥滄寔缁潪閿佸畾璇”

·         SERIALIZABLE

杩欎釜绾у埆绫讳技REPEATABLE READ锛屼絾鏄墍鏈夋棤鏍煎紡SELECT璇彞琚 闅愬紡杞崲鎴怱ELECT ... LOCK IN SHARE MODE銆

15.2.10.4. 鎸佺画闈為攣瀹氳

鎸佺画璇绘剰鍛崇潃InnoDB浣跨敤瀹冪殑澶氱増鏈寲鏉ョ粰涓涓煡璇㈠睍绀烘煇涓椂闂寸偣澶勬暟鎹簱鐨勫揩鐓с傛煡璇㈢湅鍒板湪閭d釜鏃堕棿鐐逛箣鍓嶈鎻愪氦鐨勯偅浜涚‘鍒囦簨鍔″仛鐨勬洿鏀癸紝骞朵笖娌℃湁鍏跺悗鐨勪簨鍔℃垨鏈彁浜や簨鍔″仛鐨勬敼鍙樸傝繖涓鍒欑殑渚嬪鏄紝鏌ヨ鐪嬪埌鍙戝竷璇ユ煡璇㈢殑浜嬪姟鏈韩鎵鍋氱殑鏀瑰彉銆

濡傛灉浣犺繍琛屽湪榛樿鐨凴EPEATABLE READ闅旂绾у埆锛屽垯鍦ㄥ悓涓浜嬪姟鍐呯殑鎵鏈夋寔缁璇诲彇鐢辫浜嬪姟涓涓涓繖鏍风殑璇绘墍纭珛鐨勫揩鐓с備綘鍙互閫氳繃鎻愪氦褰撳墠浜嬪姟骞跺湪鍙戝竷鏂版煡璇㈢殑浜嬪姟涔嬪悗锛屼负浣犵殑鏌ヨ鑾峰緱涓涓洿鏂伴矞鐨勫揩鐓с

鎸佺画璇绘槸榛樿妯″紡锛屽湪鍏朵腑InnoDBzai鍦≧EAD COMMITTED鍜孯EPEATABLE READ闅旂绾у埆澶勭悊SELECT璇彞銆傛寔缁涓嶅湪浠讳綍瀹冭闂殑琛ㄤ笂璁剧疆閿佸畾锛屽洜姝わ紝鍏跺畠鐢ㄦ埛鍙嚜鐢卞湴鍦ㄦ寔缁鍦ㄤ竴涓〃涓婃墽琛岀殑鍚屼竴鏃堕棿淇敼杩欎簺琛ㄣ

娉ㄦ剰锛屾寔缁涓嶅湪DROP TABLE鍜孉LTER TABLE涓婁綔鐢ㄣ傛寔缁涓嶅湪DROP TABLE涓婁綔鐢紝鍥犱负MySQL涓嶈兘浣跨敤宸茬粡琚Щ闄ょ殑琛紝骞朵笖InnoDB 鐮村潖浜嗚琛ㄣ傛寔缁涓嶅湪ALTER TABLE涓婁綔鐢紝鍥犱负瀹冨湪鏌愪簨鍔″唴鎵ц锛岃浜嬪姟鍒涘缓涓涓柊琛紝骞朵笖浠庢棫琛ㄥ線鏂拌〃涓彃鍏ヨ銆傜幇鍦紝褰撲綘閲嶆柊鍙戝嚭鎸佺画璇讳箣鏃讹紝瀹冧笉鑳藉湪鏂拌〃涓湅瑙佷换浣曡锛屽洜涓哄畠浠鎻掑叆鍒颁竴涓湪鎸佺画璇昏鍙栫殑蹇収涓笉鍙鐨勪簨鍔 閲屻

15.2.10.5. 閿佸畾璇籗ELECT ... FOR UPDATE鍜孲ELECT ... LOCK IN SHARE MODE

鍦ㄤ竴浜涚幆澧冧腑锛屼竴涓寔缁鏄笉鏂逛究鐨勩傛瘮濡傦紝浣犲彲鑳芥兂瑕佸線琛ㄧ殑瀛愯〃閲屾坊鍔犱竴涓柊琛岋紝骞剁‘淇¤瀛愯〃鍦ㄧ埗琛ㄤ腑鏈変竴涓牴銆備笅鍒椾緥瀛愭樉绀哄浣曞湪浣犲簲鐢ㄧ▼搴忎唬鐮佷腑瀹炵幇鍙傝冪殑瀹屾暣鎬с

鍋囪浣犱娇鐢ㄤ竴涓寔缁鍘昏鍙栫埗琛ㄥ苟涓旂湅鍒拌〃涓瓙琛ㄧ殑鏍广備笉鑳藉畨鍏ㄥ湴寰瀛愯〃娣诲姞瀛愯鍚楋紵涓嶏紝鍥犱负鍙兘鍚屾椂鍙戠敓涓浜涘叾瀹冪敤鎴蜂粠鐖惰〃鍒犻櫎鐖惰锛岃屼綘娌℃湁娉ㄦ剰鍒板畠鐨勬儏鍐点

瑙e喅鍔炴硶鏄湪浣跨敤LOCK IN SHARE MODE鐨勯攣瀹氭ā寮忔墽琛孲ELECT锛

SELECT * FROM parent WHERE NAME = 'Jones' LOCK IN SHARE MODE;

鍦ㄥ叡浜ā寮忔墽琛屼竴涓鎰忓懗鐫鎴戜滑璇绘渶鏂扮殑鍙敤鏁版嵁锛屽苟鍦ㄦ垜浠鐨勮璁剧疆涓涓叡浜攣瀹氥傚叡浜ā寮忛攣闃叉鍏跺畠浜烘洿鏂版垨鍒犻櫎鎴戜滑宸茶鐨勮銆傚悓鏃讹紝濡傛灉鏈鏂扮殑鏁版嵁灞炰簬鍏跺畠瀹㈡埛绔皻鏈彁浜ょ殑浜嬪姟锛屾垜浠瓑鐫鐭ラ亾閭d釜浜嬪姟琚彁浜ゃ傛垜浠湅鍒板墠杩扮殑鏌ヨ杩斿洖鐖'Jones'锛屾垜浠彲浠ュ畨鍏ㄥ湴 寰瀛愯〃娣诲姞瀛愯褰曞苟鎻愪氦鎴戜滑鐨勪簨鍔°

璁╂垜浠湅鍙﹀涓涓緥瀛愶細鎴戜滑鍦ㄨ〃child_codes 涓湁涓涓暣鏁拌鏁板櫒鍩燂紝鎴戜滑鐢ㄨ琛ㄧ粰姣忎釜娣诲姞鍒板瓙琛ㄩ噷鐨勫瓙椤规寚娲句竴涓敮涓鐨勮瘑鍒銆傛樉鐒讹紝浣跨敤鎸佺画璇绘垨鑰呭叡浜ā寮忚鍘昏鍙栧綋鍓嶈鏁板櫒鐨勫煎苟鏄竴涓ソ涓绘剰锛 鍥犱负鏁版嵁搴撶殑涓や釜鐢ㄦ埛鍙兘鐪嬪埌璁℃暟鍣ㄧ殑鍚屼竴涓硷紝濡傛灉涓や釜鐢ㄦ埛璇曠潃鐢ㄥ悓涓璇嗗埆绗﹀線璇ヨ〃娣诲姞瀛愰」锛屽氨浼氬彂鐢熶竴涓噸澶嶉敭锛坉uplicate-key锛夐敊璇

鍦ㄨ繖閲岋紝濡傛灉涓や釜鐢ㄦ埛鍚屾椂璇昏鏁板櫒锛屽綋璇曞浘鏇存柊璁℃暟鍣ㄤ箣鏃讹紝鑷冲皯瀹冧滑涓湁涓涓細鍙戠敓姝婚攣閿欒骞剁粓姝紝鍥犳LOCK IN SHARE MODE骞朵笉鏄竴涓ソ鐨勮В鍐虫柟娉曘

鍦ㄨ繖绉嶆儏鍐典笅锛屾湁涓や釜濂芥柟娉曞幓瀹炵幇璇昏鏁板櫒鍜屽闀胯鏁板櫒鍊硷細(1) 鍏堟洿鏂拌鏁板櫒锛岃璁℃暟鍣ㄥ煎1锛屼箣鍚庤璁℃暟鍣紝鎴栬(2)鐢ㄩ攣瀹氭ā寮廎OR UPDATE鍏堣璁℃暟鍣紝涔嬪悗璁℃暟鍣ㄥ煎鍔犮傚悗涓涓斿緞鍙濡備笅瀹炵幇锛

SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;

SELECT ... FOR UPDATE璇绘渶鏂扮殑鍙鏁版嵁锛屽湪姣忎釜瀹冭鍙栫殑琛岃缃嫭鍗犻攣瀹氥傚洜姝わ紝瀹冭缃笌鎼滅储鐨凷QL UPDATE鍙兘浼氬湪琛屼笂璁剧疆鐨勯攣瀹氬悓鏍风殑閿佸畾銆

璇锋敞鎰忥紝浠ヤ笂浠呮槸涓涓猄ELECT ... FOR UPDATE濡備綍璧蜂綔鐢ㄧ殑渚嬪瓙銆傚湪MySQL涓紝浜嬪疄涓婄敓鎴愪竴涓敮涓璇嗗埆绗︾殑鐗规畩浠诲姟鍙鐢ㄥ璇ヨ〃鐨勫崟鐙闂潵瀹屾垚锛

UPDATE child_codes SET counter_field = LAST_INSERT_ID(counter_field + 1);
SELECT LAST_INSERT_ID();

SELECT璇彞浠呬粎鍙栧洖璇嗗埆绗︿俊鎭紙涓撻棬瀵瑰綋鍓嶈繛鎺ワ級銆傚畠涓嶈闂换浣曡〃銆

15.2.10.6. Next-Key閿佸畾锛氶伩鍏嶅菇鐏甸棶棰

鍦ㄨ绾ч攣瀹氫腑锛孖nnoDB 浣跨敤涓涓悕涓next-key locking鐨勭畻娉曘侷nnoDB浠ヨ繖鏍蜂竴绉嶆柟寮忔墽琛岃绾ч攣瀹氾細褰撳畠鎼滅储鎴栨壂鎻忚〃鐨勭储寮曚箣鏃讹紝瀹冨閬囧埌鐨勭储寮曡褰曡缃叡浜垨鐙崰閿佸畾銆傚洜姝わ紝琛岀骇閿佸畾浜嬪疄涓婃槸绱㈠紩璁板綍閿佸畾銆

InnoDB瀵圭储寮曡褰曡缃殑閿佸畾涔熸槧鍍忕储寮曡褰曚箣鍓嶇殑“闂撮殭”銆傚鏋滀竴涓敤鎴峰涓涓储寮曚笂鐨勮褰昍鏈夊叡浜垨鐙崰鐨勯攣瀹氾紝鍙︿竴涓敤鎴 涓嶈兘绱ф帴鍦≧涔嬪墠浠ョ储寮曠殑椤哄簭鎻掑叆涓涓柊绱㈠紩璁板綍銆傝繖涓棿闅欑殑閿佸畾琚墽琛屾潵闃叉鎵璋撶殑“骞界伒闂”銆傚亣璁句綘鎯宠浠庢湁涓涓爣璇嗙鍊煎ぇ浜100鐨勫瓙琛ㄨ骞堕攣瀹氭墍鏈夊瓙璁板綍锛屽苟鎯崇潃闅忓悗鍦ㄩ夊畾琛屼腑鏇存柊涓浜 鍒楋細

SELECT * FROM child WHERE id > 100 FOR UPDATE;

鍋囪鍦╥d鍒楁湁涓涓储寮曘傛煡璇粠id澶т簬100鐨勭涓涓褰曞紑濮嬫壂鎻忋傚鏋滆缃湪绱㈠紩璁板綍涓婄殑閿佸畾涓嶆妸鍦ㄩ棿闅欑敓鎴愮殑鎻掑叆鎺掗櫎鍦ㄥ锛屼竴涓柊琛屽彲鑳戒笌姝ゅ悓鏃惰鎻掕繘琛ㄤ腑銆傚鏋滀綘鍦ㄥ悓涓浜嬪姟鍐呮墽琛屽悓鏍风殑SELECT锛屼綘鍙兘浼氬湪璇ユ煡璇㈣繑鍥炵殑缁撴灉鍖呴噷鐪嬪埌涓涓柊琛屻傝繖涓庝簨鍔$殑闅旂鍘熷垯鏄浉鍙嶇殑锛氫竴涓簨鍔″簲璇ヨ兘澶熻繍琛岋紝浠ヤ究瀹冨凡缁忚鐨勬暟鎹湪浜嬪姟杩囩▼涓笉鏀瑰彉銆傚鏋滄垜浠妸涓濂楄瑙嗕负鏁版嵁椤癸紝鏂扮殑“骞界伒”瀛愯褰曞彲鑳戒細杩濆弽杩欎竴闅旂鍘熷垯銆

褰揑nnoDB鎵弿涓涓储寮曚箣鏃讹紝瀹冧篃閿佸畾鎵浠ヨ褰曚腑鏈鍚庝竴涓褰曚箣鍚庣殑闂撮殭銆傚垰鍦ㄥ墠涓涓緥瀛愪腑鍙戠敓锛欼nnoDB璁剧疆鐨勯攣瀹氶槻姝换浣曟彃鍏ュ埌id鍙兘澶ц繃100鐨勮〃銆

浣犲彲浠ョ敤next-key閿佸畾鍦ㄤ綘鐨勫簲鐢ㄧ▼搴忎笂瀹炵幇涓涓敮涓鎬ф鏌ワ細濡傛灉浣犱互鍏变韩妯″紡璇绘暟鎹紝 骞朵笖娌℃湁鐪嬪埌浣犲皢瑕佹彃鍏ョ殑琛岀殑閲嶅锛屽垯浣犲彲浠ュ畨鍏ㄥ湴鎻掑叆浣犵殑琛岋紝骞朵笖鐭ラ亾鍦ㄨ杩囩▼涓浣犵殑琛岀殑缁ф壙鑰呰缃殑next-key閿佸畾涓庢鍚屾椂闃绘浠讳綍浜哄浣犵殑 琛屾彃鍏ヤ竴涓噸澶嶃傚洜姝わ紝the next-key閿佸畾鍏佽浣犻攣浣忓湪浣犵殑琛ㄤ腑骞朵笉瀛樺湪鐨勪竴浜涗笢瑗裤

15.2.10.7. 鎸佺画璇诲浣曞湪InnoDB涓伐浣滅殑渚嬪瓙

鍋囪浣犳杩愯鍦ㄩ粯璁ょ殑REPEATABLE READ闅旂绾у埆銆傚綋浣犲彂鍑轰竴涓寔缁涔嬫椂锛屽嵆涓涓櫘閫氱殑SELECT璇彞锛孖nnoDB鎸夌収浣犵殑鏌ヨ鐪嬪埌鐨勬暟鎹簱锛岀粰浣犵殑浜嬪姟涓涓椂闂寸偣銆傚鏋滃彟涓涓簨鍔″湪浣犵殑鏃堕棿鐐硅鎸囧畾涔嬪悗鍒犻櫎涓琛屽苟鎻愪氦锛屼綘涓嶄細鐪嬭宸茶鍒犻櫎鐨勮銆傛彃鍏ュ拰鏇存柊琚浉浼煎湴澶勭悊銆

浣犲彲浠ラ氳繃鎻愪氦浣犵殑浜嬪姟鏉ユ彁鍓嶄綘鐨勬椂闂寸偣锛岀劧鍚庡仛鍙︿竴涓猄ELECT銆

杩欒绉颁负澶氱増鏈苟鍙戞帶鍒讹紙multi-versioned concurrency control锛

               User A                 User B
 
           SET AUTOCOMMIT=0;      SET AUTOCOMMIT=0;
time
|          SELECT * FROM t;
|          empty set
|                                 INSERT INTO t VALUES (1, 2);
|
v          SELECT * FROM t;
           empty set
                                  COMMIT;
 
           SELECT * FROM t;
           empty set
 
           COMMIT;
 
           SELECT * FROM t;
           ---------------------
           |    1    |    2    |
           ---------------------
           1 row in set

鍦ㄨ繖涓緥瀛愪腑锛屽彧鏈夊綋鐢ㄦ埛B宸茬粡鎻愪氦鎻掑叆锛屽苟涓旂敤鎴稟涔熷凡缁忔彁浜や箣鏃讹紝鐢ㄦ埛A鍙湅瑙佽鐢ㄦ埛B鎻掑叆鐨勮锛屽洜姝ゆ椂闂寸偣 鏄湪鐢ㄦ埛B鎻愪氦涔嬪墠銆

濡傛灉浣犳兂鐪嬫暟鎹簱鐨勬渶鏂扮姸鎬侊紝浣犲簲璇ョ敤READ COMMITTED闅旂绾у埆鎴栫敤涓涓攣瀹氳锛

SELECT * FROM t LOCK IN SHARE MODE;

15.2.10.8. 鍦↖nnoDB涓笉鍚孲QL璇彞璁剧疆鐨勯攣瀹

鍦⊿QL鏌ヨ澶勭悊涓紝涓涓攣瀹氳锛屼竴涓猆PDATE鎴栦竴涓狣ELETE涓鑸湴瀵硅鎵弿鐨勬瘡涓储寮曡褰曡缃褰曢攣瀹氥傚鏋滃湪鏌愭煡璇腑鏈変竴涓猈HERE鏉′欢鏄病浠涔堝叧绯荤殑锛岃岃繖涓煡璇㈠彲鑳戒粠鏌ヨ鐨勭粨鏋滃寘涓帓闄よ銆侷nnoDB涓嶈寰楃‘鍒囩殑WHERE鏉′欢锛屼絾鏄粎鐭ラ亾鍝釜绱㈠紩鑼冨洿琚壂鎻忋傝褰曢攣瀹氭槸姝e父鐨刵ext-key閿佸畾锛屽畠涔熼樆姝㈠绱ф帴鐫璁板綍涔嬪墠鐨勯棿闅欑殑鎻掑叆銆

濡傛灉閿佸畾琚缃负鐙崰锛屽垯InnoDB鎬绘槸鍙栧洖闆嗘潫鐨勭储寮曠洰褰曞苟瀵瑰叾璁剧疆閿佸畾銆

濡傛灉浣犳病鏈夐傚悎鏌ヨ鐨勭储寮曪紝MySQL涓嶅緱涓嶆壂鎻忔暣涓〃鏉ュ鐞嗘煡璇紝琛ㄧ殑姣忚鍙樻垚琚攣瀹氱殑锛岃繖鏍峰弽杩囨潵闃绘鍏跺畠鐢ㄦ埛鐨勬墍鏈夊琛ㄧ殑鎻掑叆銆傚垱寤轰竴涓ソ鐨勭储寮曡浣犵殑鏌ヨ涓嶅繀瑕佹壂鎻忓緢澶氳鏄緢閲嶈鐨勩

·         SELECT ... FROM鏄竴涓寔缁锛岃鍙栨暟鎹簱鐨勫揩鐓у苟涓旇缃笉閿佸畾锛岄櫎闈炰簨鍔¢殧绂荤骇鍒璁句负SERIALIZABLE銆傚浜 SERIALIZABLE绾у埆锛岃繖涓缃瀹冮亣鍒扮殑绱㈠紩璁板綍璁剧疆鍏变韩鐨刵ext-key閿佸畾銆

·         SELECT ... FROM ... LOCK IN SHARE MODE瀵硅閬囧埌鐨勬墍鏈夌储寮曡褰曡缃叡浜殑next-key閿佸畾銆

·         SELECT ... FROM ... FOR UPDATE瀵硅閬囧埌鐨勬墍鏈夌储寮曡褰曡缃嫭鍗犵殑next-key閿佸畾銆

·         INSERT INTO ... VALUES (...)瀵硅鎻掑叆鐨勮璁剧疆鐙崰閿佸畾銆傛敞鎰忥紝杩欎笉鏄竴涓猲ext-key閿佸畾锛屽苟涓斾笉闃绘鍏跺畠鐢ㄦ埛鍦ㄥ凡鎻掑叆琛屼箣鍓嶇殑闂撮殭鎻掑叆銆傚鏋滃彂鐢熼噸澶嶉敭閿欒锛屽閲嶅鐨勭储寮曡褰曡缃叡浜攣瀹氥

·         鍦ㄤ竴涓〃涓婂垵濮嬪寲涔嬪墠鎸囧畾鐨凙UTO_INCREMENT鍒椾箣鏃讹紝InnoDB鍦ㄤ笌AUTO_INCREMENT鍒楃浉鍏宠仈鐨勭储寮曠殑鏈熬璁剧疆鐙崰閿佸畾銆傚湪璁块棶鑷姩澧為暱璁℃暟鍣ㄤ腑锛孖nnoDB浣跨敤涓撶敤鐨勮〃閿佸畾妯″紡AUTO-INC锛屽叾涓攣瀹氫粎鎸佺画鍒板綋鍓峉QL璇彞鐨勭粨鏉燂紝鑰屼笉鏄埌鏁翠釜浜嬪姟鐨勭粨鏉熴 璇峰弬闃15.2.10.2鑺傦紝鈥淚nnoDB鍜孉UTOCOMMIT”

InnoDB鍙栧洖鍏堝墠鍒濆鍖栫殑AUTO_INCREMENT鍒楃殑鍊艰屼笉璁惧畾浠讳綍閿佸畾銆

·         INSERT INTO T SELECT ... FROM S WHERE ... 瀵规瘡涓彃鍏ュ埌T鐨勮璁剧疆鐙崰锛堥潪next-key锛夐攣瀹氥傚畠鍦⊿涓婃妸鎼滅储褰撲綔涓涓寔缁锛屼絾鏄鏋淢ySQL浜岃繘鍒舵棩蹇楀姛鑳借鎵撳紑锛屽畠灏卞S璁剧疆涓涓叡浜殑next-key閿佸畾銆侷nnoDB鍦ㄥ悗涓绉嶆儏鍐典笉寰椾笉璁剧疆閿佸畾锛氬湪浠庝竴涓浠界殑鍓嶆粴鎭㈠涓紝姣忎釜SQL璇彞涓嶅緱涓嶄互涓庡畠鏈鍒濊鎵ц鐨勬柟寮忓畬鍏ㄥ悓鏍风殑鏂瑰紡鎵ц銆

·         CREATE TABLE ... SELECT ... 鎶奡ELECT褰撲綔涓涓寔缁鏉ユ墽琛岋紝鎴栬呭甫鐫鍏变韩閿佸畾鏉ユ墽琛岋紝濡傚墠闈㈢殑鏉$洰鎵杩般

·         濡傛灉鍞竴閿病鏈夊啿绐侊紝REPLACE璞′竴涓彃鍏ヤ竴鏍疯鍋氥傚彟澶栵紝瀵瑰繀椤绘洿鏂扮殑琛岃缃竴涓嫭鍗犵殑next-key閿佸畾銆

·         UPDATE ... WHERE ... 瀵规悳绱㈤亣鍒扮殑姣忎釜璁板綍璁剧疆涓涓嫭鍗犵殑next-key閿佸畾銆

·         DELETE FROM ... WHERE ... 瀵规悳绱㈤亣鍒扮殑姣忎釜璁板綍璁剧疆涓涓嫭鍗犵殑next-key閿佸畾銆

·         濡傛灉瀵逛竴涓〃瀹氫箟FOREIGN KEY绾︽潫锛屼换浣曢渶瑕佹鏌ョ害鏉熸潯浠剁殑鎻掑叆锛屾洿鏂版垨鍒犻櫎瀵瑰畠鐪嬬潃妫鏌ョ害鏉熺殑璁板綍璁剧疆鍏变韩琛岀骇閿佸畾銆侷nnoDB鍦ㄧ害鏉熷け璐ョ殑鎯呭喌涓嬩篃璁剧疆杩欎簺閿佸畾銆

·         LOCK TABLES璁剧疆琛ㄩ攣瀹氾紝浣嗘槸鏄InnoDB灞備箣涓婃洿楂樼殑MySQL灞傝缃繖浜涢攣瀹氥傚鏋渋nnodb_table_locks=1骞朵笖 and AUTOCOMMIT=0锛孖nnoDB鎰 璇嗗埌琛ㄩ攣瀹氾紝骞朵笖InnoDB涔嬩笂鐨凪ySQL灞傜煡閬撹绾ч攣瀹氥傚彟澶栵紝InooDB鐨勮嚜鍔ㄦ閿佹娴嬩笉鑳芥娴嬪湪杩欎釜琛ㄩ攣瀹氳娑夊強涔嬪鐨勬閿併 鍚屾牱锛屾棦鐒舵洿楂樼殑MySQL灞備笉鐭ラ亾琛岀骇閿佸畾锛屽緢鍙兘瀵瑰彟涓涓敤鎴峰綋鍓嶅鍏舵湁琛岄攣瀹氱殑琛ㄨ幏寰椾竴涓〃閿佸畾銆傚敖绠″姝わ紝杩欏苟涓嶇牬鍧忎簨鍔$殑瀹屾暣鎬э紝濡15.2.10.10鑺傦紝鈥滄閿佹娴嬪拰鍥炴粴鈥涓璁虹殑涓鏍枫傝鍙傞槄15.2.16鑺傦紝鈥滃InnoDB琛ㄧ殑闄愬埗”

15.2.10.9.  MySQL浣曟椂 闅愬紡鎻愪氦鎴栧洖婊氫竴涓簨鍔★紵

MySQL浠ラ粯璁ゅ厑璁竌utocommit妯″紡鏉ュ紑濮嬫瘡涓涓鎴风杩炴帴銆傚綋autocommit琚厑璁镐箣鏃讹紝濡傛灉SQL璇彞涓嶈繑鍥為敊璇殑璇濓紝MySQL鍦ㄦ瘡涓猄QL璇彞涔嬪悗锛屽仛涓涓彁浜ゃ

濡傛灉浣犲叧闂璦utocommit妯″紡骞朵笖鍏抽棴涓涓繛鎺ヨ屼笉璋冪敤浣犵殑浜嬪姟鐨勬槑纭彁浜わ紝鍒橫ySQL鍥炴粴浣犵殑浜嬪姟銆

濡傛灉SQL璇彞杩斿洖涓涓敊璇紝鎻愪氦/鍥炴粴琛屼负鍙栧喅浜庤繖涓敊璇傝鍙傞槄15.2.15鑺傦紝鈥淚nnoDB閿欒澶勭悊”

涓嬪垪姣忎竴涓鍙ワ紙浠ュ強瀹冧滑鐨勪换浣曞悓涔夎瘝锛夐殣寮忕粨鏉熶竴涓簨鍔★紝灏卞ソ鍍忓湪鎵ц杩欎釜璇彞涔嬪墠浣犲仛浜嗕竴涓狢OMMIT锛

·         ALTER FUNCTION, ALTER PROCEDURE, ALTER TABLE, BEGIN, CREATE DATABASE, CREATE FUNCTION, CREATE INDEX, CREATE PROCEDURE, CREATE TABLE, DROP DATABASE, DROP FUNCTION, DROP INDEX, DROP PROCEDURE, DROP TABLE, LOAD MASTER DATA, LOCK TABLES, RENAME TABLE, SET AUTOCOMMIT=1, START TRANSACTION, TRUNCATE, UNLOCK TABLES.

·         UNLOCK TABLES 浠呭湪濡傛灉浠讳綍琛ㄥ綋鍓嶈閿佸畾涔嬫椂锛屾彁浜や竴涓簨鍔°

·         鍦↖nnoDB涓殑CREATE TABLE璇彞琚綔涓轰竴涓崟鐙殑浜嬪姟鏉ュ鐞嗐傝繖鎰忓懗鐫鏉ヨ嚜鐢ㄦ埛鐨凴OLLBACK涓嶆挙閿鐢ㄦ埛鍦ㄤ簨鍔¤繃绋嬩腑鐢熸垚鐨凜REATE TABLE璇彞銆

浜嬪姟涓嶈兘琚祵濂椼傚綋浣犲彂鍑篠TART TRANSACTION璇彞鎴栦笌涔嬪悓涔夌殑璇彞涔嬫椂锛岃繖鏄浠讳綍褰撳墠浜嬪姟 闅愬紡鎻愪氦鐨勪竴涓粨鏋溿

15.2.10.10. 姝婚攣妫娴嬪拰鍥炴粴

InnoDB鑷姩妫娴嬩簨鍔$殑姝婚攣锛屽苟鍥炴粴涓涓垨鍑犱釜浜嬪姟鏉ラ槻姝㈡閿併侷nnoDB璇曠潃鎸戦夊皬浜嬪姟鏉ュ洖婊氾紝浜嬪姟鐨勫ぇ灏忛氳繃琚彃鍏ャ佹洿鏂版垨鍒犻櫎鐨勮鐨勬暟閲忔潵纭畾銆

濡傛灉innodb_table_locks=1 锛1鏄粯璁ゅ硷級锛孖nnoDB鎰忚瘑鍒拌〃閿佸畾锛屽叾涓婄殑MySQL灞傜煡閬搑ow-level閿佸畾銆傚彟澶朓nnoDB涓嶈兘鍦∕ySQL LOCK TABLES璁惧畾琛ㄩ攣瀹氱殑鍦版柟鎴栬呮秹鍙奍nnoDB涔嬪鐨勫瓨鍌ㄥ紩鎿庤缃攣瀹氱殑鍦版柟妫娴嬫閿併備綘蹇呴』閫氳繃璁惧畾innodb_lock_wait_timeout绯荤粺鍙橀噺鐨勫兼潵瑙e喅杩欎簺鎯呭喌銆

褰揑nnoD鎵ц瀹屽叏鐨勪簨鍔″洖婊氫箣鏃讹紝璇ヤ簨鍔$殑鎵鏈夐攣瀹氳閲婃斁銆傚敖绠″姝わ紝濡傛灉鍗曚釜SQL璇彞琚洜涓洪敊璇殑鍘熷洜琚 鍥炴粴锛岃SQL璇彞璁惧畾鐨勯儴鍒嗛攣瀹氬彲鑳借淇濈暀銆傝繖鏄洜涓篒nnoDB浠ヤ竴绉嶆柟寮忓瓨鍌ㄨ閿佸畾锛屽湪杩欑鏂瑰紡涓畠涓嶈兘鐭ラ亾闅忓悗鐨勫摢涓攣瀹氭槸琚摢涓猄QL璇彞璁惧畾鐨勩

15.2.10.11. 濡備綍澶勭悊姝婚攣

姝婚攣鏄簨鍔″瀷鏁版嵁搴撳吀鍨嬬殑闂锛屼絾鏄櫎闈炲畠浠绻佸嚭鐜颁互鑷充簬浣犳洿鏈笉鑳借繍琛屾煇涓簨鍔★紝瀹冧滑涓鑸槸涓嶅嵄闄╃殑銆傛甯稿湴锛屼綘蹇呴』缂栧啓浣犵殑搴旂敤绋嬪簭浣垮緱瀹冧滑鎬绘槸鍑嗗濡傛灉鍥犱负姝婚攣鑰 鍥炴粴涓涓簨鍔″氨閲嶆柊鍙戝嚭涓涓簨鍔°

InnoDB浣跨敤鑷姩琛岀骇閿佸畾銆傚嵆浣垮湪鍙彃鍏ユ垨鍒犻櫎鍗曚釜琛岀殑浜嬪姟鐨勬儏鍐典笅锛屼綘鍙互閬囧埌姝婚攣銆傝繖鏄洜涓鸿繖浜涙搷浣滀笉鏄湡姝g殑“鏋佸皬鐨”锛屽畠浠嚜鍔ㄥ鎻掑叆鎴栧垹闄ょ殑琛岀殑锛堝彲鑳芥槸鏁颁釜锛夌储寮曡褰曡缃攣瀹氥

浣犲彲浠ョ敤涓嬪垪鎶鏈浠樻閿佸噺灏戝畠浠彂鐢熺殑鍙兘鎬э細

·         鐢║se SHOW INNODB STATUS鏉ョ‘瀹氭渶鍚庝竴涓閿佺殑鍘熷洜銆傝繖鏍峰彲浠ュ府鍔╀綘璋冭妭搴旂敤绋嬪簭鏉ラ伩鍏嶆閿併

·         鎬绘槸鍑嗗鐫閲嶆柊鍙戝嚭浜嬪姟锛屽鏋滃畠鍥犱负姝婚攣鑰屽け璐ヤ簡銆傛閿佷笉鍗遍櫓锛屽啀璇曚竴娆°

·         缁忓父鎻愪氦浣犵殑浜嬪姟銆傚皬浜嬪姟鏇村皯鍦板惧悜浜庡啿绐併

·         濡傛灉浣犳浣跨敤閿佸畾璇伙紝锛圫ELECT ... FOR UPDATE鎴 ... LOCK IN SHARE MODE锛夛紝璇曠潃鐢ㄦ洿浣庣殑闅旂绾у埆锛屾瘮濡俁EAD COMMITTED銆

·         浠ュ浐瀹氱殑椤哄簭璁块棶浣犵殑琛ㄥ拰琛屻傚垯浜嬪姟褰㈡垚鑹ソ瀹氫箟鐨勬煡璇㈠苟涓旀病鏈夋閿併

·         娣诲姞绮惧績閫夊畾鐨勭储寮曞埌浣犵殑琛ㄣ傚垯浣犵殑鏌ヨ闇瑕佹壂鎻忔洿灏戠殑绱㈠紩璁板綍骞朵笖鍥犳璁剧疆鏇村皯鐨勯攣瀹氥備娇鐢‥XPLAIN SELECT鏉ョ‘瀹氬浜庝綘鐨勬煡璇,MySQL璁や负鍝釜绱㈠紩鏄渶閫傚綋鐨勩

·         浣跨敤鏇村皯鐨勯攣瀹氥傚鏋滀綘鍙互鎺ュ彈鍏佽涓涓猄ELECT浠庝竴涓棫鐨勫揩鐓ц繑鍥炴暟鎹紝涓嶈缁欏畠娣诲姞FOR UPDATE鎴朙OCK IN SHARE MODE瀛愬彞銆傝繖閲屼娇鐢≧EAD COMMITTED闅旂绾у埆鏄瘮杈冨ソ鐨勶紝鍥犱负姣忎釜鍦ㄥ悓涓浜嬪姟閲岀殑鎸佺画璇讳粠瀹冭嚜宸辨柊椴滅殑蹇収閲岃鍙栥

·         濡傛灉娌℃湁鍒殑鏈夊府鍔╃殑浜嗭紝鐢ㄨ〃绾ч攣瀹氱郴鍒楀寲浣犵殑浜嬪姟銆傜敤LOCK TABLES瀵逛簨鍔″瀷琛紙濡侷nnoDB)鐨勬纭柟娉曟槸璁剧疆AUTOCOMMIT = 0 骞朵笖涓嶈皟鐢║NLOCK TABLES鐩村埌浣犳槑纭湴鎻愪氦浜嗕簨鍔°備緥濡傦紝濡傛灉浣犻渶瑕佸啓琛╰1骞朵粠琛╰璇伙紝浣犲彲浠ユ寜濡備笅鍋氾細

·                SET AUTOCOMMIT=0;
·                LOCK TABLES t1 WRITE, t2 READ, ...;
·                [do something with tables t1 and t2 here];
·                COMMIT;
·                UNLOCK TABLES;

琛ㄧ骇閿佸畾浣垮緱浣犵殑浜嬪姟寰堝ソ鍦版帓闃燂紝骞朵笖姝婚攣琚伩鍏嶄簡銆

·         棰嗕竴涓郴鍒楀寲浜嬪姟鐨勬柟娉曟槸鍒涘缓涓涓緟鍔╃殑“semaphore” 琛紝瀹冨彧鍖呭惈涓涓崟琛屻傝姣忎釜浜嬪姟鍦ㄨ闂叾瀹冭〃涔嬪墠鏇存柊閭d釜琛屻備互杩欑鏂瑰紡锛屾墍鏈変簨鍔′互搴忓垪鐨勬柟寮忓彂鐢熴傛敞鎰忥紝InnoDB鍗虫椂姝婚攣妫娴嬬畻娉曚篃鑳藉湪杩欑鎯呭喌涓嬭捣绉熺敤锛屽洜涓虹郴鍒楀寲閿佸畾鏄绾ч攣瀹氥傝秴鏃舵柟娉曪紝鐢∕ySQL琛ㄧ骇閿佸畾锛屽繀椤昏鐢ㄦ潵瑙e喅姝婚攣銆

·         鍦ㄥ簲鐢ㄧ▼搴忎腑浣跨敤LOCK TABLES鍛戒护锛屽鏋淎UTOCOMMIT=1,MySQL涓嶈瀹欼nnoDB琛ㄩ攣瀹氥

15.2.11InnoDB鎬ц兘璋冭妭鎻愮ず

·         濡傛灉Unix椤跺眰宸ュ叿鎴栬匴indows浠诲姟绠$悊鍣ㄦ樉绀猴紝浣犵殑鏁版嵁搴撶殑宸ヤ綔璐熻嵎鐨凜PU浣跨敤鐜囧皬浜70%锛屽垯浣犵殑宸ヤ綔璐熻嵎鍙兘鏄鐩樼粦瀹氱殑锛屽彲鑳戒綘姝g敓鎴愬お澶氱殑浜嬪姟鍜屾彁浜わ紝鎴栬呯紦鍐叉睜澶皬銆備娇寰楃紦鍐叉睜鏇村ぇ 涓浜涗細鏈夊府鍔╃殑锛屼絾涓嶈璁剧疆缂撳啿姹犵瓑浜庢垨瓒呰繃鐗╃悊鍐呭瓨鐨80%.

·         鎶婃暟涓慨鏀硅9杩涗竴涓簨鍔¢噷銆傚鏋滀簨鍔″鏁版嵁搴撲慨鏀癸紝InnoDB鍦ㄨ浜嬪姟鎻愪氦鏃跺繀椤诲埛鏂版棩蹇楀埌纾佺洏銆傚洜涓虹鐩樻棆杞殑閫熷害鑷冲167杞/绉掞紝濡傛灉纾佺洏娌℃湁楠楁搷浣滅郴缁熺殑璇濓紝杩欏氨闄愬埗鎻愪氦鐨勬暟鐩负鍚屾牱鐨勬瘡绉167娆°

·         濡傛灉浣犲彲浠ユ帴鍙楁崯澶变竴浜涙渶杩戠殑宸叉彁浜や簨鍔★紝浣犲彲浠ヨ缃甿y.cnf鏂囦欢閲岀殑鍙傛暟innodb_flush_log_at_trx_commit涓0銆 鏃犺濡備綍InnoDB璇曠潃姣忕 鍒锋柊涓娆℃棩蹇楋紝灏界鍒锋柊涓嶈璁稿彲銆

·         浣跨敤澶х殑鏃ュ織鏂囦欢锛岃瀹冪敋鑷充笌缂撳啿姹犱竴鏍峰ぇ銆傚綋InnoDB鍐欐弧鏃ュ織鏂囦欢鏃讹紝瀹冧笉寰椾笉鍦ㄤ竴涓鏌ョ偣鎶婄紦鍐叉睜宸蹭慨鏀圭殑鍐呭鍐欒繘纾佺洏銆傚皬鏃ュ織鏂囦欢瀵艰嚧璁稿涓嶅繀瑕佺殑鍚哥洏鍐欐搷浣溿傚ぇ鏃ュ織鏂囦欢鐨勭己鐐规椂鎭㈠鏃堕棿鏇撮暱銆

·         涔熻鏃ュ織缂撳啿鐩稿綋澶э紙涓8MB鐩镐技鐨勬暟閲)銆

·         濡傛灉浣犲瓨鍌ㄥ彉闀垮害瀛楃涓诧紝鎴栬呭垪鍙兘鍖呭惈寰堝NULL鍊硷紝鍒欎娇鐢╒ARCHAR鍒楃被鍨嬭屼笉鏄疌HAR绫诲瀷銆備竴涓狢HAR(N)鍒楁绘槸鍗犳嵁N 瀛楄妭鏉ュ瓨鍌紝鍗充娇瀛楃涓叉洿鐭垨瀛楃涓茬殑鍊兼槸NULL銆傝秺灏忕殑琛ㄨ秺濂藉湴閫傚悎缂撳啿姹犲苟涓斿噺灏戠鐩業/O銆

褰撲娇鐢╮ow_format=compact 锛圡ySQL 5.1涓 榛樿鐨処nnoDB璁板綍鏍煎紡锛夊拰鍙彉闀垮害瀛楃闆嗭紝姣斿UTF-8鎴杝jis锛孋HAR(N)灏嗗崰鎹彲鍙樻暟閲忕殑绌洪棿锛岃嚦灏戜负N 瀛楄妭銆

·         鍦ㄤ竴浜涚増鏈殑GNU/Linux鍜孶nix涓婏紝鐢║nix鐨刦sync()锛圛nnoDB榛樿浣跨敤鐨勶級鎶婃枃浠跺埛鏂板埌纾佺洏锛屽苟涓斿叾浠栫浉浼肩殑鏂规硶鏄儕浜虹殑鎱€傚鏋滀綘涓嶆弧鎰忔暟鎹簱鐨勫啓鎬ц兘锛屼綘鍙互璇曠潃璁剧疆my.cnf閲岀殑innodb_flush_method涓篛_DSYNC锛岃櫧鐒禣_DSYNC鍦ㄥ鏁扮郴缁熶笂鐪嬭捣鏉ユ洿鎱€

·         褰撳湪Solaris 10涓婏紝涓簒86_64鏋舵瀯锛圓MD Opteron)浣跨敤InnoDB瀛樺偍寮曟搸锛岄噸瑕佺殑鏄娇鐢╢orcedirectio閫夐」鏉ュ畨瑁呬换浣曚负瀛樺偍涓嶪nnoDB鐩稿叧鐨勬枃浠惰屼娇鐢ㄧ殑鏁版嵁绯荤粺銆傦紙榛樿鍦⊿olaris 10/x86_64涓婁笉浣跨敤杩欎釜鏂囦欢绯荤粺瀹夎閫夐」 锛夈備娇鐢╢orcedirectio 澶辫触浼氬鑷碔nnoDB鍦ㄨ繖涓钩鍙颁笂鐨勯熷害鍜屾ц兘涓ラ噸閫鍖栥

·         褰撳鍏ユ暟鎹埌InnoDB涓箣鏃讹紝璇风‘淇ySQL娌℃湁鍏佽autocommit妯″紡锛屽洜涓哄厑璁竌utocommit妯″紡浼氶渶瑕佹瘡娆℃彃鍏ラ兘瑕 鍒锋柊鏃ュ織鍒扮鐩樸傝鍦ㄥ鍏ユ搷浣滆绋嬩腑绂佹autocommit妯″紡锛岀敤SET AUTOCOMMIT鍜孋OMMIT璇彞鏉ュ寘浣忓鍏ヨ鍙ワ細

·                SET AUTOCOMMIT=0;
·                /* SQL import statements ... */
·                COMMIT;

濡傛灉浣犱娇鐢mysqldump 閫夐」--opt锛屽嵆浣夸笉鐢⊿ET AUTOCOMMIT鍜孋OMMIT璇彞鏉ュ寘瑁癸紝浣犱篃浣垮緱蹇熺殑杞偍鏂囦欢琚鍏ュ埌InnoDB琛ㄤ腑銆

·         灏忓績澶у畻鎻掑叆鐨勫ぇ鍥炴粴锛欼nnoDB鍦ㄦ彃鍏ヤ腑浣跨敤鎻掑叆缂撳啿鏉ヨ妭绾︾鐩業/O锛 浣嗘槸鍦ㄧ浉搴旂殑鍥炴粴涓病鏈変娇鐢ㄨ繖鏍风殑鏈哄埗銆備竴涓鐩樼粦瀹氱殑鍥炴粴鍙互鐢ㄧ浉搴旀彃鍏ヨ姳璐规椂闂寸殑30鍊嶆潵鎵ц銆傛潃鎺夋暟鎹簱杩涚▼娌℃湁鏄府鍔╃殑锛屽洜涓哄洖婊氬湪鏈嶅姟鍣ㄥ惎鍔ㄦ椂 浼氬啀娆″惎鍔ㄣ傞櫎鎺変竴涓け鎺х殑鍥炴粴鐨勫敮涓鏂规硶鏄澶х紦鍐叉睜浣垮緱鍥炴粴鍙樻垚CPU缁戝畾涓旇窇寰楀揩锛屾垨鑰呬娇鐢ㄤ笓鐢ㄦ楠わ紝璇峰弬闃15.2.8.1鑺傦紝鈥滃己鍒舵仮澶”

·         涔熻灏忓績鍏跺畠澶х殑纾佺洏缁戝畾鎿嶄綔銆傜敤DROP TABLE鎴朇REATE TABLE鏉ユ竻绌轰竴涓〃锛岃屼笉鏄敤DELETE FROM tbl_name

·         濡傛灉浣犻渶瑕佹彃鍏ヨ澶氳锛屽垯浣跨敤澶氳鎻掑叆璇硶鏉ュ噺灏戝鎴风鍜屾湇鍔″櫒涔嬮棿鐨勯氳寮鏀細

·                INSERT INTO yourtable VALUES (1,2), (5,5), ...;

杩欎釜鎻愮ず瀵瑰埌浠讳綍琛ㄧ被鍨嬬殑鎻掑叆閮芥槸鍚堟硶鐨勶紝涓嶄粎浠呮槸瀵笽nnoDB绫诲瀷銆

·         濡傛灉浣犲湪绗簩涓敭涓婃湁UNIQUE绾︽潫锛屼綘鍙互鍦ㄥ鍏ヤ細璇濅腑鏆傛椂鍏抽棴鍞竴鎬ф鏌ヤ互鍔犻熻〃鐨勫鍏ワ細

·                SET UNIQUE_CHECKS=0;

瀵逛簬澶ц〃锛岃繖鑺傜害浜嗗ぇ閲忕鐩業/O锛屽洜涓篒nnoDB鍙互浣跨敤瀹冪殑鎻掑叆缂撳啿鏉ュ湪涓鎵瑰唴鍐欑浜屼釜绱㈠紩璁板綍銆

·         濡傛灉浣犲浣犵殑琛ㄦ湁FOREIGN KEY绾︽潫锛屼綘鍙互鍦ㄥ鍏ヤ細璇濊繃绋嬩腑閫氳繃鍏抽棴澶栭敭妫鏌ユ潵鎻愰熻〃鐨勫鍏ワ細

·                SET FOREIGN_KEY_CHECKS=0;

瀵逛簬澶ц〃锛岃繖鍙互鑺傜害澶ч噺鐨勭鐩業/O銆

·         濡傛灉浣犵粡甯告湁瀵逛笉缁忓父鏇存柊鐨勮〃鐨勯噸鍙戞煡璇紝璇蜂娇鐢ㄦ煡璇㈢紦瀛橈細

·                [mysqld]
·                query_cache_type = ON
·                query_cache_size = 10M

15.2.11.1. SHOW INNODB STATUS鍜 InnoDB Monitors

InnoDB鍖呭惈InnoDB Monitors锛屽畠鎵撳嵃鏈夊叧InnoDB鍐呴儴鐘舵佺殑淇℃伅銆備綘鍙互浣跨敤SQL璇彞SHOW INNODB STATUS鏉ュ彇鏍囧噯InnoDB Monitor鐨勮緭鍑洪佸埌浣犵殑SQL瀹㈡埛绔傝繖涓俊鎭湪鎬ц兘璋冭妭鏃舵湁鐢ㄣ傦紙濡傛灉浣犳浣跨敤mysql浜嬪姟SQL瀹㈡埛绔紝濡傛灉浣犵敤\G鏇夸唬閫氬父鐨勮鍙ョ粓姝㈢鍒嗗彿锛岃緭鍑虹粨鏋滀細鏇村彲璇 锛夈傚叧浜嶪nnoDB閿佸畾妯″紡鐨勮璁猴紝璇峰弬闃15.2.10.1鑺傦紝鈥淚nnoDB閿佸畾妯″紡”

mysql> SHOW INNODB STATUS\G

鍙︿竴涓娇鐢↖nnoDB Monitors鐨勬柟娉曟椂璁╁畠浠笉鏂啓鏁版嵁鍒mysqld鏈嶅姟鍣ㄧ殑鏍囧噯杈撳嚭銆傚湪杩欑鎯呭喌涓嬶紝娌℃湁杈撳嚭琚佸埌瀹㈡埛绔傚綋琚墦寮鐨勬椂鍊欙紝InnoDB Monitors姣15绉掓墦鍗颁竴娆℃暟鎹傛湇鍔″櫒杈撳嚭閫氬父琚畾鍚戝埌MySQL鏁版嵁鐩綍閲岀殑.err鏃ュ織銆傝繖涓暟鎹湪鎬ц兘璋冭妭鏃舵湁鐢ㄣ傚湪Windows涓婏紝濡傛灉浣犳兂瀹氬悜杈撳嚭鍒扮獥鍙 鑰屼笉鏄埌鏃ュ織鏂囦欢锛屼綘蹇呴』浠庢帶鍒跺彴绐楀彛鐨勫懡浠よ浣跨敤--console閫夐」鏉ュ惎鍔ㄦ湇鍔″櫒銆

鐩戣鍣ㄨ緭鍑哄寘鎷笅鍒楃被鍨嬬殑淇℃伅锛

·         琚瘡涓縺娲讳簨鍔℃寔鏈夌殑琛ㄥ拰璁板綍閿佸畾

·         浜嬪姟鐨勯攣瀹氱瓑寰

·         绾跨▼鐨勪俊鍙风瓑寰

·         鏈В鍐崇殑鏂囦欢I/O璇锋眰

·         缂撳啿姹犵粺璁℃暟鎹

·         涓籌nnoDB绾跨▼鐨勫噣鍖栧拰鎻掑叆缂撳啿鍚堝苟娲诲姩

瑕佽鏍囧噯InnoDB Monitor鍐欏埌mysqld鐨勬爣鍑嗚緭鍑猴紝璇蜂娇鐢ㄤ笅鍒桽QL璇彞锛

CREATE TABLE innodb_monitor(a INT) ENGINE=INNODB;

鐩戣鍣ㄥ彲浠ラ氳繃鍙戝嚭涓嬪垪璇彞鏉ヨ鍋滄锛

DROP TABLE innodb_monitor;

CREATE TABLE璇硶姝f槸閫氳繃MySQL鐨凷QL瑙f瀽绋嬪簭寰InnoDB寮曟搸浼犻掑懡浠ょ殑鏂规硶锛氬敮涓鏈夐棶棰樼殑浜嬫儏鏄〃鍚嶅瓧innodb_monitor鍙婂畠鏄竴涓狪nnoDB琛ㄣ傚浜InnoDB Monitor, 琛ㄧ殑缁撴瀯鏍规湰涓嶇浉鍏炽傚鏋滀綘鍦ㄧ洃瑙嗗櫒姝h繍琛屾椂鍏抽棴鏈嶅姟鍣紝骞朵笖浣犳兂瑕佸啀娆″惎鍔ㄧ洃瑙嗗櫒锛屼綘蹇呴』鍦ㄥ彲浠ュ彂鍑烘柊CREATE TABLE璇彞鍚姩鐩戣鍣ㄤ箣鍓嶇Щ闄よ〃銆傝繖涓娉曞湪灏嗘潵鐨勫彂琛岀増鏈腑鍙兘浼氭敼鍙樸

浣犲彲浠ヤ互鐩镐技鐨勬柟寮忎娇鐢╥nnodb_lock_monitor銆傞櫎浜嗗畠涔熸彁渚涘ぇ閲忛攣瀹氫俊鎭箣澶栵紝瀹冧笌innodb_monitor鐩稿悓銆備竴涓垎绂荤殑 innodb_tablespace_monitor鎵撳嵃瀛樺湪浜庤〃绌洪棿涓凡鍒涘缓鏂囦欢鐗囨柇鐨勪竴涓垪琛紝骞朵笖纭琛ㄧ┖闂村垎閰嶆暟鎹粨鏋勩傛澶栵紝鏈塱nnodb_table_monitor锛岀敤瀹冧綘鍙互鎵撳嵃InnoDB鍐呴儴鏁版嵁璇嶅吀鐨勫唴瀹广

InnoDB Monitor杈撳嚭鐨勪竴涓緥瀛愶細

mysql> SHOW INNODB STATUS\G
*************************** 1. row ***************************
Status:
=====================================
030709 13:00:59 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 18 seconds
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 413452, signal count 378357
--Thread 32782 has waited at btr0sea.c line 1477 for 0.00 seconds the semaphore:
X-lock on RW-latch at 41a28668 created in file btr0sea.c line 135
a writer (thread id 32782) has reserved it in mode wait exclusive
number of readers 1, waiters flag 1
Last time read locked in file btr0sea.c line 731
Last time write locked in file btr0sea.c line 1347
Mutex spin waits 0, rounds 0, OS waits 0
RW-shared spins 108462, OS waits 37964; RW-excl spins 681824, OS waits 375485
------------------------
LATEST FOREIGN KEY ERROR
------------------------
030709 13:00:59 Transaction:
TRANSACTION 0 290328284, ACTIVE 0 sec, process no 3195, OS thread id 34831 inser
ting
15 lock struct(s), heap size 2496, undo log entries 9
MySQL thread id 25, query id 4668733 localhost heikki update
insert into ibtest11a (D, B, C) values (5, 'khDk' ,'khDk')
Foreign key constraint fails for table test/ibtest11a:
,
  CONSTRAINT `0_219242` FOREIGN KEY (`A`, `D`) REFERENCES `ibtest11b` (`A`, `D`)
 ON DELETE CASCADE ON UPDATE CASCADE
Trying to add in child table, in index PRIMARY tuple:
 0: len 4; hex 80000101; asc ....;; 1: len 4; hex 80000005; asc ....;; 2: len 4;
 hex 6b68446b; asc khDk;; 3: len 6; hex 0000114e0edc; asc ...N..;; 4: len 7; hex
 00000000c3e0a7; asc .......;; 5: len 4; hex 6b68446b; asc khDk;;
But in parent table test/ibtest11b, in index PRIMARY,
the closest match we can find is record:
RECORD: info bits 0 0: len 4; hex 8000015b; asc ...[;; 1: len 4; hex 80000005; a
sc ....;; 2: len 3; hex 6b6864; asc khd;; 3: len 6; hex 0000111ef3eb; asc ......
;; 4: len 7; hex 800001001e0084; asc .......;; 5: len 3; hex 6b6864; asc khd;;
------------------------
LATEST DETECTED DEADLOCK
------------------------
030709 12:59:58
*** (1) TRANSACTION:
TRANSACTION 0 290252780, ACTIVE 1 sec, process no 3185, OS thread id 30733 inser
ting
LOCK WAIT 3 lock struct(s), heap size 320, undo log entries 146
MySQL thread id 21, query id 4553379 localhost heikki update
INSERT INTO alex1 VALUES(86, 86, 794,'aA35818','bb','c79166','d4766t','e187358f'
,'g84586','h794',date_format('2001-04-03 12:54:22','%Y-%m-%d %H:%i'),7
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 48310 n bits 568 table test/alex1 index symbole
trx id 0 290252780 lock mode S waiting
Record lock, heap no 324 RECORD: info bits 0 0: len 7; hex 61613335383138; asc a
a35818;; 1:
*** (2) TRANSACTION:
TRANSACTION 0 290251546, ACTIVE 2 sec, process no 3190, OS thread id 32782 inser
ting
130 lock struct(s), heap size 11584, undo log entries 437
MySQL thread id 23, query id 4554396 localhost heikki update
REPLACE INTO alex1 VALUES(NULL, 32, NULL,'aa3572','','c3572','d6012t','', NULL,'
h396', NULL, NULL, 7.31,7.31,7.31,200)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 48310 n bits 568 table test/alex1 index symbole
trx id 0 290251546 lock_mode X locks rec but not gap
Record lock, heap no 324 RECORD: info bits 0 0: len 7; hex 61613335383138; asc a
a35818;; 1:
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 48310 n bits 568 table test/alex1 index symbole
trx id 0 290251546 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 82 RECORD: info bits 0 0: len 7; hex 61613335373230; asc aa
35720;; 1:
*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 0 290328385
Purge done for trx's n:o < 0 290315608 undo n:o < 0 17
Total number of lock structs in row lock hash table 70
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, process no 3491, OS thread id 42002
MySQL thread id 32, query id 4668737 localhost heikki
show innodb status
---TRANSACTION 0 290328384, ACTIVE 0 sec, process no 3205, OS thread id 38929 in
serting
1 lock struct(s), heap size 320
MySQL thread id 29, query id 4668736 localhost heikki update
insert into speedc values (1519229,1, 'hgjhjgghggjgjgjgjgjggjgjgjgjgjgggjgjgjlhh
gghggggghhjhghgggggghjhghghghghghhhhghghghjhhjghjghjkghjghjghjghjfhjfh
---TRANSACTION 0 290328383, ACTIVE 0 sec, process no 3180, OS thread id 28684 co
mmitting
1 lock struct(s), heap size 320, undo log entries 1
MySQL thread id 19, query id 4668734 localhost heikki update
insert into speedcm values (1603393,1, 'hgjhjgghggjgjgjgjgjggjgjgjgjgjgggjgjgjlh
hgghggggghhjhghgggggghjhghghghghghhhhghghghjhhjghjghjkghjghjghjghjfhjf
---TRANSACTION 0 290328327, ACTIVE 0 sec, process no 3200, OS thread id 36880 st
arting index read
LOCK WAIT 2 lock struct(s), heap size 320
MySQL thread id 27, query id 4668644 localhost heikki Searching rows for update
update ibtest11a set B = 'kHdkkkk' where A = 89572
------- TRX HAS BEEN WAITING 0 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 65556 n bits 232 table test/ibtest11a index PRIM
ARY trx id 0 290328327 lock_mode X waiting
Record lock, heap no 1 RECORD: info bits 0 0: len 9; hex 73757072656d756d00; asc
 supremum.;;
------------------
---TRANSACTION 0 290328284, ACTIVE 0 sec, process no 3195, OS thread id 34831 ro
llback of SQL statement
ROLLING BACK 14 lock struct(s), heap size 2496, undo log entries 9
MySQL thread id 25, query id 4668733 localhost heikki update
insert into ibtest11a (D, B, C) values (5, 'khDk' ,'khDk')
---TRANSACTION 0 290327208, ACTIVE 1 sec, process no 3190, OS thread id 32782
58 lock struct(s), heap size 5504, undo log entries 159
MySQL thread id 23, query id 4668732 localhost heikki update
REPLACE INTO alex1 VALUES(86, 46, 538,'aa95666','bb','c95666','d9486t','e200498f
','g86814','h538',date_format('2001-04-03 12:54:22','%Y-%m-%d %H:%i'),
---TRANSACTION 0 290323325, ACTIVE 3 sec, process no 3185, OS thread id 30733 in
serting
4 lock struct(s), heap size 1024, undo log entries 165
MySQL thread id 21, query id 4668735 localhost heikki update
INSERT INTO alex1 VALUES(NULL, 49, NULL,'aa42837','','c56319','d1719t','', NULL,
'h321', NULL, NULL, 7.31,7.31,7.31,200)
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (write thread)
Pending normal aio reads: 0, aio writes: 0,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
151671 OS file reads, 94747 OS file writes, 8750 OS fsyncs
25.44 reads/s, 18494 avg bytes/read, 17.55 writes/s, 2.33 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf for space 0: size 1, free list len 19, seg size 21,
85004 inserts, 85004 merged recs, 26669 merges
Hash table size 207619, used cells 14461, node heap has 16 buffer(s)
1877.67 hash searches/s, 5121.10 non-hash searches/s
---
LOG
---
Log sequence number 18 1212842764
Log flushed up to   18 1212665295
Last checkpoint at  18 1135877290
0 pending log writes, 0 pending chkp writes
4341 log i/o's done, 1.22 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 84966343; in additional pool allocated 1402624
Buffer pool size   3200
Free buffers       110
Database pages     3074
Modified db pages  2674
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages read 171380, created 51968, written 194688
28.72 reads/s, 20.72 creates/s, 47.55 writes/s
Buffer pool hit rate 999 / 1000
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
Main thread process no. 3004, id 7176, state: purging
Number of rows inserted 3738558, updated 127415, deleted 33707, read 755779
1586.13 inserts/s, 50.89 updates/s, 28.44 deletes/s, 107.88 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
1 row in set (0.05 sec)

鍏充簬杩欎釜杈撳嚭涓浜涜娉ㄦ剰鐨勶細

·         濡傛灉TRANSACTIONS鑺傛姤鍛婇攣瀹氱瓑寰咃紝浣犵殑搴旂敤绋嬪簭鍙兘鏈夐攣瀹氱珵浜夈傝緭鍑轰篃甯姪璺熻釜浜嬪姟姝婚攣鐨勫師鍥犮

·         SEMAPHORES鑺傛姤鍛婄瓑寰呬竴涓俊鍙风殑绾跨▼鍜屽叧浜庣嚎绋嬪涓涓簰鏂ヤ綋鎴杛w-lock淇″彿闇瑕佸灏戞鍥炴粴鎴栫瓑寰呯殑缁熻鏁版嵁銆傚ぇ閲忕瓑寰呬俊鍙风殑绾跨▼鍙兘鏄鐩業/O鐨勭粨鏋滄垨鑰匢nnoDB鍐呯珵浜夐棶棰樼殑缁撴灉銆傜珵浜夊彲鑳芥槸鍥犱负鏌ヨ鐨勪弗閲嶅苟琛岋紝鎴栬呮搷浣滅郴缁熺嚎绋嬭鍒掔殑闂銆傝缃甶nnodb_thread_concurrency灏忎簬榛樿鍊煎湪杩欑鎯呭喌涓嬫槸鏈夊府鍔╃殑銆

·         BUFFER POOL AND MEMORY鑺傜粰浣犲叧浜庨〉闈㈣鍐欑殑缁熻鏁版嵁銆備綘鍙互浠庤繖浜涙暟璁$畻浣犲綋鍓嶇殑鏌ヨ姝e仛澶氬皯鏂囦欢鏁版嵁I/O鎿嶄綔銆

·         ROW OPERATIONS鑺傛樉绀轰富绾跨▼姝e湪鍋氫粈涔堛

InnoDB鍙戦佽瘖鏂緭鍑哄埌stderr鎴栨枃浠讹紝鑰屼笉鏄埌stdout鎴栬呭浐瀹氬昂瀵稿唴瀛樼紦鍐诧紝浠ラ伩鍏 搴曞眰缂撳啿婧㈠嚭銆備綔涓轰竴涓壇鏁堟灉锛孲HOW INNODB STATUS鐨勮緭鍑烘瘡15绉掗挓鍐欏埌涓涓姸鎬佹枃浠躲傝繖涓枃浠剁殑鍚嶅瓧鏄痠nnodb_status.pid锛屽叾涓pid 鏄湇鍔″櫒杩涚▼ID銆傝繖涓枃浠跺湪MySQL鏁版嵁鐩綍閲屽垱寤恒傛甯稿叧鏈轰箣鏃讹紝InnoDB鍒犻櫎杩欎釜鏂囦欢銆傚鏋滃彂鐢熶笉姝e父鐨勫叧鏈猴紝 杩欎簺鐘舵佹枃浠剁殑瀹炰緥鍙兘琚睍绀猴紝鑰屼笖蹇呴』琚墜鍔ㄥ垹闄ゃ傚湪绉婚櫎瀹冧滑涔嬪墠锛屼綘鍙兘鎯宠妫鏌ュ畠浠潵鐪嬪畠浠槸鍚﹀寘鍚湁鍏充笉姝e父鍏虫満鐨勫師鍥犵殑鏈夌敤淇℃伅銆備粎鍦ㄩ厤缃夐」innodb_status_file=1琚缃箣鏃讹紝innodb_status.pid鏂囦欢琚垱寤恒

15.2.12. 澶氱増鏈殑瀹炴柦

鍥犱负InnoDB鏄鐗堟湰鍖栫殑鏁版嵁搴, 瀹冨繀椤讳繚鎸佸叧浜庤〃绌洪棿涓棫鐗堟湰琛岀殑淇℃伅銆傝繖涓俊鎭瀛樺湪鍚嶄负rollback segment锛堝湪Oracle涓ā鎷熸暟鎹粨鏋勪箣鍚庯級鐨勬暟鎹粨鏋勪腑銆

鍐呴儴鍦帮紝InnoDB寰瀛樺湪鏁版嵁搴撲腑鐨勬瘡涓琛屼腑娣诲姞涓や釜鍩熴備竴涓6瀛楄妭鐨勫煙璇存槑鎻掑叆鎴栨洿鏂拌琛岀殑鏈鍚庝竴涓簨鍔$殑浜嬪姟璇嗗埆绗︺傚悓鏃讹紝涓涓垹闄や篃琚唴閮ㄥ鐞嗕负涓涓洿鏂帮紝鍏朵腑琛屼腑涓涓壒娈婄殑浣嶈璁剧疆鏉ユ爣娉ㄨ琛屼负宸插垹闄ゃ傛瘡涓琛屼篃鍖呭惈涓涓О涓烘粴鍔ㄦ寚閽堢殑7瀛楄妭鍩熴傛粴鍔ㄦ寚閽堟寚鍚戜竴涓鍐欏埌鍥炴粴鐗囨柇鐨勬挙閿鏃ュ織璁板綍銆傚鏋滆琛岃鏇存柊锛屾挙閿鏃ュ織璁板綍鍖呭惈鍦ㄨ琛岃鏇存柊涔嬪墠閲嶅缓璇ヨ鐨勫唴瀹瑰繀闇鐨勭殑淇℃伅銆

InnoDB浣跨敤鍦ㄥ洖婊氱墖鏂腑鐨勪俊鎭潵鎵ц鍦ㄤ簨鍔″洖婊氫腑闇瑕佺殑鎾ら攢鎿嶄綔銆傚畠涔熶娇鐢ㄨ繖涓俊鎭潵涓轰竴涓寔缁鏋勫缓鏇存棭鐗堟湰鐨勮銆

鍦ㄥ洖婊氱墖鏂腑鐨勬挙閿鏃ュ織琚垎涓烘彃鍏ュ拰鏇存柊鎾ら攢鏃ュ織銆傛彃鍏ユ挙閿鏃ュ織浠呭湪浜嬪姟鍥炴粴涓渶瑕侊紝涓斿彧瑕佷簨鍔′竴鎻愪氦灏卞彲浠ヨ涓㈠純銆傛洿鏂版挙閿鏃ュ織涔熻鐢ㄥ湪鎸佺画璇讳腑锛岃屼笖瀹冧滑浠呭湪 褰撳墠娌℃湁琚獻nnoDB鍒嗛厤缁欎竴涓揩鐓х殑浜嬪姟涔嬪悗琚涪寮冿紝杩欎釜蹇収鍦ㄦ寔缁涓彲鑳戒細闇瑕佹洿鏂版挙閿鏃ュ織鐨勪俊鎭潵寤虹珛涓涓暟鎹簱琛岀殑鏃╂湡鐗堟湰銆

浣犲繀椤昏寰楄寰嬪湴鎻愪氦浣犵殑浜嬪姟锛屽寘鎷偅浜涘彧鍙戝竷鎸佺画璇荤殑浜嬪姟銆傚惁鍒, InnoDB涓嶈兘浠庢洿鏂版挙閿鏃ュ織涓㈠純鏁版嵁锛屽苟涓斿洖婊氱墖鏂彲鑳藉彉寰楀お澶э紝濉弧浣犵殑琛ㄧ┖闂淬

鍦ㄤ竴涓洖婊氱墖鏂噷锛屼竴涓挙閿鏃ュ織璁板綍鐨勭墿鐞嗗昂瀵稿吀鍨嬪湴灏忎簬鐩稿簲鐨勫凡鎻掑叆琛屾垨宸叉洿鏂拌銆備綘鍙互鐢ㄨ繖涓俊鎭潵璁$畻鍥炴粴鐗囨柇闇瑕佺殑绌洪棿銆

鍦↖nnoDB澶氱増鏈寲鏂规涓紝褰撲綘鐢⊿QL璇彞鍒犻櫎涓琛屼箣鏃讹紝璇ヨ娌℃湁琚粠鏁版嵁搴撶珛鍗崇墿鐞嗗垹闄ゆ帀銆 鍙湁褰InnoDB鍙互涓㈠純涓哄垹闄よ岃鍐欑殑鏇存柊鎾ら攢鏃ュ織璁板綍鏃讹紝InnoDB涔熺墿鐞嗗湴浠庢暟鎹簱鍒犻櫎鐩稿簲琛屽拰瀹冪殑绱㈠紩璁板綍銆傝繖涓垹闄ゆ搷浣滆鎴愪负绮惧崕锛屽畠杩愯寰楀緢蹇紝閫氬父涓庡仛鍒犻櫎鐨凷QL璇彞鑺辩殑鏃堕棿鍦ㄤ竴涓暟閲忕骇

鍦ㄦ煇涓鎯呮櫙涓嬶紝鍦ㄩ偅閲岋紝鐢ㄦ埛浠ュ嚑涔庣浉鍚岀殑姣旂巼锛屽皬鎵规鍦板湪琛ㄤ腑鎻掑叆鍜屽垹闄よ锛屽噣鍖栫嚎绋嬪紑濮嬫粸鍚 鏄彲鑳界殑锛屽苟涓旇〃鍙樺緱瓒婃潵瓒婂ぇ锛屼娇寰楁瘡鏍蜂簨閮芥槸纾佺洏缁戝畾鐨勮屼笖闈炲父鎱€傚嵆浣胯〃浠呰浇鏈10MB鏈夌敤鐨勬暟鎹紝瀹冨彲鑳藉彉寰楃敤鎵鏈夌殑姝昏鍗犳嵁10GB绌洪棿銆傚湪杩欑鎯呭喌涓嬶紝鑺傛祦鏂版搷浣滐紝骞跺垎閰嶆洿澶氱殑璧勬簮鏉ュ噣鍖栫嚎绋嬪彲鑳芥槸姣旇緝濂界殑銆傚惎鍔ㄩ夐」鍜屽彲璁剧疆鍏ㄧ悆鍙橀噺innodb_max_purge_lag灏辨槸涓鸿繖涓洰鐨勮屽瓨鍦ㄧ殑銆傝鍙傞槄15.2.4鑺傦紝鈥淚nnoDB 鍚姩閫夐」” 浠ヨ幏寰楁洿澶氫俊鎭

15.2.13. 琛ㄥ拰绱㈠紩缁撴瀯

MySQL涓鸿〃鎶婂畠鐨勬暟鎹瘝鍏镐俊鎭互.frm鏂囦欢鐨勫舰寮忓瓨鍦ㄦ暟鎹簱鐩綍閲岋紝杩欏鎵鏈塎ySQL瀛樺偍寮曟搸閮芥槸鐪熺殑銆備絾鏄瘡涓狪nnoDB琛ㄥ湪琛ㄧ┖闂村唴鐨処nnoDB鍐呴儴鏁版嵁璇嶅吀閲屾湁瀹冭嚜宸辩殑鏉$洰銆傚綋MySQL绉婚櫎琛ㄦ垨鏁版嵁搴擄紝瀹冧笉寰椾笉鍒犻櫎.frm鏂囦欢鍜孖nnoDB鏁版嵁璇嶅吀鍐呯殑鐩稿簲鏉$洰銆傝繖灏辨槸涓轰粈涔堜綘涓嶈兘鍦ㄦ暟鎹簱涔嬮棿绠鍗曞湴绉诲姩.frm鏂囦欢鏉ョЩ鍔↖nnoDB琛ㄣ

姣忎釜InnoDB琛ㄦ湁涓撻棬绱㈠紩锛岃绉颁负clustered index锛屽琛岀殑鏁版嵁琚瓨浜庡叾涓傚鏋滀綘瀵逛綘鐨勮〃瀹氫箟涓涓狿RIMARY KEY锛 涓婚敭鐨勭储寮曟槸闆嗘潫绱㈠紩銆

濡傛灉浣犳病鏈変负琛ㄥ畾涔塒RIMARY KEY锛孧ySQL鎷惧彇绗竴涓粎鏈塏OT NULL鍒楃殑UNIQUE绱㈠紩浣滀负涓婚敭锛屽苟涓擨nnoDB鎶婂畠褰撲綔闆嗘潫绱㈠紩鏉ョ敤銆傚鏋滆〃涓病鏈夎繖鏍蜂竴涓储寮曪紝InnoDB鍐呴儴浜х敓涓涓泦鏉熺储寮曪紝鍏朵腑鐢↖nnoDB鍦ㄨ繖鏍蜂竴涓〃鍐呮寚瀹氱粰琛岀殑琛孖D鏉ユ帓搴忚銆傝ID鏄竴涓6瀛楄妭鐨勫煙锛屽畠鍦ㄦ柊琛岃鎻掑叆鐨勬椂鍊欏崟涓鍦板鍔犮傚洜姝よ琛孖D鎺掑簭鐨勮鏄墿鐞嗗湴鎸夌収鎻掑叆椤哄簭鎺掔殑銆

閫氳繃闆嗘潫绱㈠紩璁块棶涓涓鏄緝蹇殑锛屽洜涓鸿鏁版嵁鏄湪绱㈠紩鎼滅储寮曞鐨勫悓涓椤甸潰銆傚鏋滆〃鏄法澶х殑锛屽綋瀵规瘮浜庝紶缁熻В鍐虫柟妗堬紝闆嗘潫绱㈠紩鏋勬灦缁忓父鑺傜害纾佺洏I/O銆傦紙鍦ㄨ澶氭暟鎹簱锛屾暟鎹紶缁熷湴琚瓨鍦ㄤ笌绱㈠紩璁板綍涓嶅悓鐨勯〉锛夈

鍦↖nnoDB涓紝闈為泦鏉熺储寮曢噷鐨勮褰曪紙涔熺О涓虹浜岀储寮曪級鍖呭惈瀵硅鐨勪富閿笺侷nnoDB鐢ㄨ繖涓 涓婚敭鍊兼潵浠庨泦鏉熺储寮曚腑鎼滅储琛屻傛敞鎰忥紝濡傛灉涓婚敭鏄暱鐨勶紝绗簩绱㈠紩浣跨敤鏇村绌洪棿銆

InnoDB姣旇緝CHAR鍜孷ARCHAR瀛楃涓蹭笉鍚岄暱搴︼紝浠ヤ究鍦ㄨ緝鐭瓧绗︿覆涓墿涓嬬殑闀垮害琚鐞嗚涓虹敤绌烘牸琛ヤ笂鐨勩

15.2.13.1. 绱㈠紩鐨勭墿鐞嗙粨鏋

鎵鏈塈nnoDB鐨勭储寮曟槸B鏁帮紝鍏朵腑绱㈠紩璁板綍琚瓨鍌ㄥ湪鏍戠殑鏍戝彾椤点備竴涓储寮曢〉鐨勯粯璁ゅぇ灏忔槸16KB銆傚綋鏂拌褰曡鎻掑叆锛孖nnoDB璇曠潃涓哄皢鏉ョ储寮曡褰曠殑鎻掑叆鍜屾洿鏂扮暀涓嬪崄鍏垎涔嬩竴鐨勭┖鐧介〉銆

濡傛灉绱㈠紩璁板綍浠ヨ繛缁殑椤哄簭琚彃鍏ワ紙鍗囧簭鎴栬呴檷搴忥級锛岀粨鏋滅储寮曢〉澶х害鏄15/16婊°傚鏋滆褰曡浠ラ殢鏈虹殑椤哄簭琚彃鍏ワ紝椤甸潰鏄粠1/2鍒 15/16婊°傚鏋滅储寮曢〉鐨勫~鍏呭洜瀛愰檷鍒颁綆浜1/2锛孖nnoDB璇曠潃鎼滅储绱㈠紩鏍戞潵閲婃斁椤点

15.2.13.2. 缂撳啿鎻掑叆

鍦ㄦ暟鎹簱搴旂敤涓紝涓婚敭鏄竴涓敮涓鐨勮瘑鍒锛屽苟涓旀柊琛岃浠ヤ富閿殑鍗囧簭鏉ユ彃鍏ワ紝杩欐槸涓父瑙佺殑鎯呭喌銆傚洜姝わ紝鍒伴泦鏉熺储寮曠殑鎻掑叆涓嶉渶瑕佷粠涓涓鐩橀殢鏈鸿銆

鍙︿竴鏂归潰锛岀浜岀储寮曢氬父鏄潪鍞竴鐨勶紝鍒扮浜岀储寮曠殑鎻掑叆浠ョ浉瀵归殢鏈烘搴忓彂鐢熴傝繖鍙兘浼氬鑷村ぇ閲忕殑闅忔満纾佺洏I/O鎿嶄綔锛岃屾病鏈変竴涓鐢ㄥ湪InnoDB涓殑涓撶敤鏈哄埗銆

濡傛灉涓涓储寮曡褰曞簲璇ヨ鎻掑叆鍒颁竴涓潪鍞竴绗簩绱㈠紩锛孖nnoDB妫鏌ョ浜岀储寮曢〉鏄惁鍦ㄧ紦鍐叉睜涓傚鏋滄槸锛孖nnoDB鐩存帴鎻掑叆鍒扮储寮曢〉銆傚鏋滅储寮曢〉娌℃湁鍦ㄧ紦鍐叉睜涓鍙戠幇锛孖nnoDB鎻掑叆璁板綍鍒颁竴涓笓闂ㄧ殑鎻掑叆缂撳啿缁撴瀯銆傛彃鍏ョ紦鍐茶淇濇寔寰楀姝ゅ皬浠ヨ嚦浜庡畠瀹屽叏閫傚悎鍦ㄧ紦鍐叉睜锛屽苟涓斿彲浠ラ潪甯稿揩鍦板仛鎻掑叆銆

鎻掑叆缂撳啿鍛ㄦ湡鍦拌鍚堝苟鍒版暟鎹簱涓浜岀储寮曟爲閲屻傛妸鏁颁釜鎻掑叆鍚堝苟鍒扮储寮曟爲鐨勫悓涓椤碉紝鑺傜渷纾佺洏I/O鎿嶄綔锛岀粡甯稿湴杩欐槸鏈夊彲鑳界殑銆傛嵁娴嬮噺锛屾彃鍏ョ紦鍐插彲浠ユ彁楂樺埌琛ㄧ殑鎻掑叆閫熷害杈15鍊嶃

鍦ㄦ彃鍏ヤ簨鍔¤鎻愪氦涔嬪悗锛屾彃鍏ョ紦鍐插悎骞跺彲鑳借繛缁彂鐢熴傚疄闄呬笂锛屾湇鍔″櫒鍏抽棴鍜岄噸鍚箣鍚庯紝杩欎細杩炵画鍙戠敓銆傦紙璇峰弬闃15.2.8.1鑺傦紝鈥滃己鍒舵仮澶”锛夈

褰撹澶氱浜岀储寮曞繀椤昏鏇存柊涔嬫椂锛屽苟涓旇澶氳宸茶鎻掑叆涔嬫椂锛屾彃鍏ョ紦鍐插悎骞跺彲鑳介渶瑕佹暟涓皬鏃躲傚湪杩欎釜鏃堕棿鍐咃紝纾佺洏I/O灏嗕細澧炲姞锛岃繖鏍蜂細瀵艰嚧纾佺洏缁戝畾鏌ヨ鏄庢樉缂撴參銆傚彟涓涓槑鏄剧殑鍚庡彴I/O鎿嶄綔鏄噣鍖栫嚎绋嬶紙璇峰弬闃15.2.12鑺傦紝鈥滃疄鐜板鐗堟湰鍖”锛夈

15.2.13.3. 閫傚簲鐨勫搱甯岀储寮

濡傛灉涓涓〃鍑犱箮瀹屽叏閰嶅悎涓诲唴瀛橈紝鍦ㄥ叾涓婃墽琛屾煡璇㈡渶蹇殑鏂规硶灏辨槸浣跨敤鍝堝笇绱㈠紩銆侷nnoDB鏈変竴涓嚜鍔ㄦ満鍒讹紝瀹冪洃瑙嗗涓轰竴涓〃瀹氫箟鐨勭储寮曠殑绱㈠紩鎼滅储銆傚鏋淚nnoDB娉ㄦ剰鍒版煡璇細浠庡缓绔嬩竴涓搱甯岀储寮曚腑鑾风泭锛屽畠浼氳嚜鍔ㄥ湴杩欎箞鍋氥

娉ㄦ剰锛屽搱甯岀储寮曟绘槸鍩轰簬琛ㄤ笂宸插瓨鍦ㄧ殑B鏍戠储寮曟潵寤虹珛銆傛牴鎹甀nnoDB瀵笲鏍戠储寮曡瀵熺殑鎼滅储鏂瑰紡锛孖nnoDB浼氬湪涓鸿B鏍戝畾涔夌殑浠讳綍闀垮害鐨 閿殑涓涓墠缂涓婂缓绔嬪搱甯岀储寮曘 鍝堝笇绱㈠紩鍙互鏄儴鍒嗙殑锛氬畠涓嶈姹傛暣涓狟鏍戠储寮曡缂撳瓨鍦ㄧ紦鍐叉睜銆侷nnoDB鏍规嵁闇瑕佸琚粡甯歌闂殑绱㈠紩鐨勯偅浜涢〉闈㈠缓绔嬪搱甯岀储寮曘

鍦ㄦ煇绉嶆剰涔変笂锛孖nnoDB閫氳繃閽堝涓板瘜涓诲唴瀛樼殑閫傚簲鐨勫搱甯岀储寮曟満鍒舵潵鍓鑷繁锛屾洿鍔犻潬杩戜富鍐呭瓨鏁版嵁搴撶殑鏋舵瀯銆

15.2.13.4. 鐗╃悊璁板綍缁撴瀯

InnoDB琛ㄤ腑鐨勮褰曟湁濡備笅鐗瑰緛锛

·         InnoDB涓瘡涓储寮曡褰曞寘鍚竴涓6瀛楄妭鐨勫ご銆傝繖涓ご琚敤鏉ュ皢杩炵画鐨勮褰曡繛鎺ュ湪涓璧凤紝骞朵笖涔熺敤鍦╮ow-level閿佸畾涓

·         闆嗘潫绱㈠紩涓殑璁板綍鍖呭惈瀵规墍鏈夎嚜瀹氫箟鍒楃殑鍩熴傛澶栵紝鏈変竴涓6瀛楄妭鐨勫煙缁欎簨鍔D浠ュ強涓涓7瀛楄妭鐨勫煙缁欐粴鍔ㄦ寚閽堛

·         濡傛灉娌℃湁涓轰竴涓〃瀹氫箟鍒濈骇閿紝姣忎釜闆嗘潫绱㈠紩璁板綍涔熷寘鍚竴涓6瀛楄妭鐨勮ID鍩熴

·         姣忎釜绗簩绱㈠紩璁板綍涔熷寘鍚负闆嗘潫绱㈠紩閿畾涔夌殑鎵鏈夊煙銆

·         涓涓褰曚篃鍖呭惈涓涓寚鍚戣璁板綍姣忎竴涓煙鐨勬寚閽堬紝濡傛灉鍦ㄤ竴涓褰曚腑鍩熺殑鎬婚暱搴﹀皬浜128瀛楄妭锛岃鎸囬拡鏃朵竴涓瓧鑺傦紱鍚﹀垯灏辨槸2瀛楄妭銆傝繖浜涙寚閽堢殑闃靛垪琚О涓鸿褰曠洰褰曘傝繖浜涙寚閽堟寚鍚戠殑鍖哄煙琚О涓鸿褰曠殑鏁版嵁閮ㄥ垎銆

·         鍐呴儴鍦帮紝InnoDB浠ュ浐瀹氶暱搴︽牸寮忓瓨鍌ㄥ浐瀹氶暱搴﹀瓧绗﹀垪锛屾瘮濡侰HAR(10)銆侷nnoDB浠嶸ARCHAR鍒楁埅鐭窡韪┖闂淬傛敞鎰忥紝MySQL鍙互鍐呴儴鍦版妸CHAR鍒楄浆鎹负VARCHAR鍒椼傝鍙傞槄13.1.5.1鑺傦紝鈥滃畨闈欑殑鍒楄鏍兼敼鍙”

·         涓涓猄QL鐨凬ULL鍊煎湪璁板綍鐩綍閲屼繚鐣1鎴2瀛楄妭銆傛澶栵紝SQL鐨凬ULL鍊煎鏋滆淇濆瓨鍦ㄥ彲鍙橀暱搴﹀垪锛屽垯鍦ㄨ褰曟暟鎹儴鍒嗕繚鐣欓浂瀛楄妭銆傚湪涓涓浐瀹氶暱搴﹀垪锛屽畠鍦ㄨ褰曠殑鏁版嵁閮ㄥ垎淇濈暀璇ュ垪鐨勫浐瀹氶暱搴︺備负NULL鍊间繚鐣欏浐瀹氱┖闂磋儗鍚庣殑鍔ㄦ満鏄箣鍚庤 鍒椾粠NULL鍊煎埌闈濶ULL鍊肩殑鏇存柊鍙互灏卞湴瀹屾垚锛屼笖涓嶄細瀵艰嚧绱㈠紩椤电殑纰庣墖銆

15.2.14.鏂囦欢绌洪棿绠$悊鍜岀鐩業/O

15.2.14.1. 纾佺洏 I/O

InnoDB浣跨敤妯℃嫙寮傛纾佺洏I/O锛欼nnoDB鍒涘缓璁稿绾跨▼鏉ュ鐞咺/O鎿嶄綔锛屾瘮濡俽ead-ahead.

鍦↖nnoDB涓湁涓や釜read-ahead璇曟帰锛

·         鍦ㄨ繛缁璻ead-ahead涓紝濡傛灉InnoDB娉ㄦ剰鍒板湪琛ㄧ┖闂翠腑瀵逛竴涓墖鏂殑璁块棶鏂瑰紡鏄繛缁殑锛屽畠灏遍鍏堝竷缃竴鎵 鏁版嵁搴撻〉鐨勮 缁橧/O绯荤粺銆

·         鍦ㄩ殢鏈簉ead-ahead涓紝濡傛灉InnoDB娉ㄦ剰鍒拌〃绌洪棿涓殑涓浜涘尯鍩熺湅璧锋潵杩涘叆瀹屽叏璇昏繘缂撳啿姹犱腑鐨勫鐞嗕腑锛屽畠灏卞竷缃墿浣欑殑璇诲埌I/O绯荤粺銆

InnoDB浣跨敤涓涓绉颁负doublewrite鐨勬柊棰栫殑鏂囦欢 鍒锋柊鎶鏈傚畠缁欐搷浣滀綋缁熷穿婧冩垨鎺夌數鍚庣殑鎭㈠娣诲姞浜嗗畨鍏紝骞朵笖閫氳繃鍑忓皯瀵筬sync()鎿嶄綔鐨勯渶瑕侊紝瀹冨湪澶氭暟Unix鍙樼涓婃敼鍠勪簡鎬ц兘銆.

Doublewrite鎰忎负鍦ㄥ悜涓涓暟鎹枃浠跺啓椤典箣鍓嶏紝InnoDB棣栧厛鎶婂畠浠啓鍒颁竴涓瘲閭荤殑琛ㄧ┖闂村尯鍩燂紝绉颁负doublewrite缂撳啿銆備粎鍦ㄥ啓鐒跺悗 鍒锋柊鍒癲oublewrite宸茬粡瀹屾垚涔嬪墠锛孖nnoDB鍐欓〉闈㈠埌瀹冧滑鍦ㄨ〃绌洪棿涓伆褰撶殑浣嶇疆銆傚鏋滄搷浣滅郴缁熷湪鍐欓〉闈㈢殑涓棿宕╂簝锛屽湪鎭㈠杩囩▼涓紝InnoDB鍙互鍦ㄩ殢鍚庝粠doublewrite缂撳啿涓壘鍒伴〉闈㈢殑涓涓壇濂藉鍒躲

15.2.14.2. 涓鸿〃绌洪棿浣跨敤鍘熷璁惧

浣犱篃鍙互浣跨敤鍘熷纾佺洏鍒嗗尯浣滀负琛ㄧ┖闂存暟鎹枃浠躲傞氳繃浣跨敤鍘熷纾佺洏锛屼綘鍙互鍦╓indows鍜屼竴浜沀nix绯荤粺涓婃墽琛宯on-buffered I/O 鑰屾棤椤绘枃浠剁郴缁熷紑鏀紝杩欐牱鍙互鏀瑰杽鎬ц兘

褰撲綘鍒涘缓涓涓柊鏁版嵁鏂囦欢涔嬫椂锛屼綘蹇呴』鍦╥nnodb_data_file_path閲岀揣鎺ョ潃鏁版嵁鏂囦欢灏哄涔嬪悗鏀剧疆 鍏抽敭瀛梟ewraw銆傚垎鍖哄繀椤昏嚦灏戝拰浣犳寚瀹氱殑灏哄涓鏍峰ぇ锛屾敞鎰忥紝鍦↖nnoDB涓紝1MB鏄1024x1024瀛楄妭锛 浣嗘槸鍦ㄧ鐩樿鏍间腑锛1MB閫氬父鎰忎负1,000,000瀛楄妭銆

[mysqld]
innodb_data_home_dir=
innodb_data_file_path=/dev/hdd1:3Gnewraw;/dev/hdd2:2Gnewraw

涓嬫浣犲惎鍔ㄦ湇鍔″櫒涔嬫椂锛孖nnoDB娉ㄦ剰鍒板叧閿瓧newraw骞跺垵濮嬪寲鏂板垎鍖恒備絾鏄粛鐒跺苟涓嶅垱寤烘垨鏀瑰彉浠讳綍InnoDB琛ㄣ傚彟澶栵紝褰撲綘閲嶅惎鏈嶅姟鍣ㄤ箣鏃讹紝InnoDB閲嶆柊鍒濆鍖栧垎鍖猴紝浣犵殑鏀瑰彉浼氫涪澶便傦紙浠3.23.44鍚姩锛屼綔涓轰竴涓畨鍏ㄦ帾鏂斤紝 褰撶敤newraw鎸囧畾浠讳綍鍒嗗尯涔嬫椂锛孖nnoDB闃绘鐢ㄦ埛淇敼鏁版嵁锛夈

InnoDB鍒濆鍖栨柊鍒嗗尯涔嬪悗锛屽仠姝㈡湇鍔″櫒锛屽湪瀵硅鐨勬暟鎹枃浠惰鏍间腑鏀瑰彉newraw锛

[mysqld]
innodb_data_home_dir=
innodb_data_file_path=/dev/hdd1:5Graw;/dev/hdd2:2Graw

鐒跺悗閲嶅惎鍔ㄦ湇鍔″櫒锛屽苟涓擨nnoDB鍏佽鍋氭敼鍙樸

鍦╓indows涓婏紝浣犲彲浠ヨ薄杩欐牱鍒嗛厤纾佺洏鍒嗗尯涓轰竴涓暟鎹枃浠讹細

[mysqld]
innodb_data_home_dir=
innodb_data_file_path=//./D::10Gnewraw

瀵逛簬璁块棶鐗╃悊椹卞姩鍣紝//./ 鐩稿綋浜嶹indows璇硶鐨刓\.\ 銆

褰撲綘浣跨敤鍘熷纾佺洏鍒嗗尯涔嬫椂锛岀‘淇″畠浠湁鍏佽琚敤鏉ヨ繍琛孧ySQL鏈嶅姟鍣ㄧ殑甯愬彿璇诲拰鍐欒闂殑璁稿彲銆

15.2.14.3.鏂囦欢绌洪棿绠$悊

浣犲湪閰嶇疆鏂囦欢涓畾涔夌殑鏁版嵁鏂囦欢褰㈡垚InnoDB鐨勮〃绌洪棿銆傛枃浠惰绠鍗曞湴杩炶捣鏉ヤ互褰㈡垚琛ㄧ┖闂淬傛病鏈夋潯绾瑰湪浣跨敤銆傚綋鍓嶄綘涓嶈兘瀹氫箟浣犵殑琛ㄨ鍒嗛厤鍒拌〃绌洪棿涓殑浣嶇疆銆備絾鏄紝鍦ㄤ竴涓柊鍒涘缓鐨勮〃涓棿涓紝InnoDB 浠庣涓涓暟鎹枃浠跺紑濮嬪垎閰嶇┖闂淬

琛ㄧ┖闂村寘鍚暟鎹簱椤碉紝榛樿澶у皬鏄16KB銆傝繖浜涢〉琚垎缁勬垚64涓繛缁〉鐨勮寖鍥淬傝〃绌洪棿鍐呯殑鏂囦欢鍦↖nnoDB涓绉颁负鐗囨柇銆傛湳璇“rollback segment”鏈変竴浜涙贩娣嗭紝鍥犱负瀹冪‘鍒囧湴鍖呭惈璁稿琛ㄧ┖闂寸墖鏂

鍦↖nnoDB涓紝姣忎釜绱㈠紩鍒嗛厤涓や釜鐗囨柇銆備竴涓槸缁橞鏍戠殑闈炴爲鍙惰妭鐐圭殑锛屽彟涓涓槸缁欐爲鍙惰妭鐐圭殑銆傚湪杩欓噷锛岀悊鎯崇殑鏄负鍖呭惈鏁版嵁鐨勬爲鍙惰妭鐐硅揪鍒版洿濂界殑鏈夊簭鎬с

褰撲竴涓墖鏂湪琛ㄧ┖闂村唴闀垮ぇ锛孖nnoDB鍗曠嫭鍦板垎閰嶆渶鍏堢殑32椤电粰瀹冦傛鍚嶪nnoDB寮濮嬪垎閰嶆暣涓寖鍥寸粰璇ョ墖鏂侷nnoDB鍙互涓娆$粰涓涓ぇ鐗囨柇娣诲姞澶氳揪4涓寖鍥翠互纭繚鏁版嵁鑹ソ鐨勮繛缁с

鍦ㄨ〃绌洪棿涓殑涓浜涢〉鍖呭惈鍏跺畠椤电殑浣嶅浘锛屽洜姝ゅ湪涓涓狪nnoDB琛ㄧ┖闂翠腑鐨勪竴浜涜寖鍥翠笉鑳借鏁翠釜鍦板垎閰嶇粰鐗囨柇锛屽彧鑳戒綔涓哄崟涓〉琚垎閰嶃

褰撲綘鍙戝嚭SHOW TABLE STATUS璇㈤棶琛ㄧ┖闂撮噷鍙敤鐨勮嚜鐢辩┖闂翠箣鏃讹紝InnoDB鎶ュ憡鍦ㄨ〃绌洪棿涓畬鍏ㄨ嚜鐢辩殑鑼冨洿銆侷nnoDB鎬绘槸涓烘壂闄ゅ拰鍏跺畠鍐呴儴鐩殑淇濈暀涓浜涜寖鍥达紝杩欎簺淇濈暀鐨勮寖鍥翠笉鍖呮嫭鍦ㄨ嚜鐢辩┖闂撮噷銆

褰撲綘浠庝竴涓〃涓垹闄ゆ暟鎹箣鏃讹紝InnoDB鑱旂郴鐩稿簲鐨凚鏍戠储寮曘傛槸鍚﹂噴鏀惧崟鐙〉鎴栨槸鑼冨洿鍒拌〃绌洪棿鍙栧喅鍒犻櫎鐨勬柟寮忥紝鍥犳琚噴鏀剧殑绌洪棿鍙樻垚瀵瑰叾瀹冪敤鎴峰彲鐢紝浣嗘槸璁颁綇锛屽凡鍒犻櫎鐨勮浠呭湪璇ヨ涓嶅啀琚簨鍔 鍥炴粴鎴栨寔缁鎵闇瑕佷箣鍚庣殑涓涓紙鑷姩锛夊噣鍖栨搷浣滀腑琚墿鐞嗗垹闄ゃ

15.2.14.4. 鏁寸悊琛ㄧ鐗

濡傛灉鏈夐殢鏈烘彃鍏ュ埌琛ㄧ殑绱㈠紩鎴栦粠琛ㄧ殑绱㈠紩闅忔満鍒犻櫎锛岀储寮曞彲鑳藉彉鎴愮鐗囩殑銆傜鐗囨剰鎬濇槸绱㈠紩椤靛湪纾佺洏涓婄殑鐗╃悊鎺掑簭骞朵笉鎺ヨ繎椤典笂璁板綍鐨勭储寮曟帓搴忥紝鎴栬呭湪鍒嗛厤缁欑储寮曠殑64椤靛潡涓婃湁璁稿娌℃湁琚娇鐢ㄧ殑椤点

纰庣墖鐨勪竴涓“鍚屼箟璇”鏄竴涓〃鍗犳嵁鐨勭┖闂磋秴杩囧畠搴旇鍗犳嵁鐨勭┖闂寸殑澶 灏忋傜‘鍒囨槸澶氬皯锛岃繖鏄緢闅惧幓纭畾鐨勩傛墍鏈塈nnoDB鏁版嵁鍜岀储寮曡瀛樺湪B鏍戜腑锛屽苟涓斿畠浠殑濉厖鍥犲瓙鍙兘浠50%鍒100%銆傜鐗囩殑鍙︿竴涓“鍚 涔夎瘝”鏄竴涓〃鎵弿渚嬪锛

SELECT COUNT(*) FROM t WHERE a_non_indexed_column <> 12345;

鑺变簡瓒呰繃瀹冨簲璇ヨ姳鐨勬椂闂淬傦紙鍦ㄤ笂闈㈢殑鏌ヨ涓垜浠“娆洪獥”SQL浼樺寲鍣ㄦ潵鎵弿闆嗘潫绱㈠紩锛岃屼笉鏄竴涓浜岀储寮 锛夈傚鏁扮鐩樺彲浠ヨ10MB/s鍒50MB/s锛岃繖鍙互琚敤鏉ヨ瘎浼颁竴涓〃鎵弿鍙互澶氬揩鍦拌繍琛屻

濡傛灉浣犲懆鏈熷湴鎵ц“null” ALTER TABLE鎿嶄綔锛屽畠灏卞彲浠ュ姞閫熺储寮曟壂鎻忥細 

ALTER TABLE tbl_name ENGINE=INNODB

杩欏鑷碝ySQL閲嶅缓琛ㄣ傚彟涓涓墽琛岀鐗囨暣鐞嗘搷浣滅殑鍔炴硶鏄娇鐢mysqldump鏉ヨ浆鍌ㄤ竴涓〃鍒颁竴涓枃鏈枃浠讹紝绉婚櫎琛紝骞堕噸鏂颁粠杞偍鏂囦欢閲嶈杞藉畠銆

濡傛灉鍒颁竴涓储寮曠殑鎻掑叆鎬绘槸鍗囧簭鐨勶紝骞朵笖璁板綍浠呬粠鏈熬琚垹闄わ紝InnoDB鏂囦欢绌洪棿绠$悊淇濊瘉鍦ㄧ储寮曚腑鐨勭鐗囦笉浼氬彂鐢熴

15.2.15. InnoDB閿欒澶勭悊

鍦↖nnoDB涓敊璇鐞嗕笉鍍廠QL鏍囧噯涓寚瀹氱殑涓鏍枫傛寜鐓ф爣鍑嗭紝鍦⊿QL璇彞杩囩▼涓殑浠讳綍閿欒搴旇瀵艰嚧璇ヨ鍙ョ殑 鍥炴粴銆侷nnoDB鏈夋椂浠呭洖婊氶儴鍒嗚鍙ワ紝鎴栬呮暣涓簨鍔°備笅鍒楁潯鐩彊杩癐nnoDB濡備綍鎵ц閿欒澶勭悊锛

·         濡傛灉浣犺楀敖琛ㄧ┖闂翠腑鐨勬枃浠剁┖闂达紝浣犱娇寰桵ySQL琛ㄥ畬鍏ㄩ敊璇紝骞朵笖InnoDB杩斿洖SQL璇彞銆

·         涓涓簨鍔℃閿佸鑷碔nnoDB鍥炴粴鏁翠釜浜嬪姟锛屽湪閿佸畾绛夊緟瓒呮椂鐨勬儏鍐碉紝InnoDB浠呭洖婊氭渶杩戠殑SQL璇彞銆

褰撲竴涓簨鍔″洖婊氬洜涓烘閿佹垨閿佸畾绛夊緟瓒呮椂鑰屽彂鐢燂紝瀹冨湪浜嬪姟涓挙閿璇彞鐨勪綔鐢ㄣ備絾鏄鏋滀簨鍔℃槸鐢⊿TART TRANSACTION鎴朆EGIN璇彞寮濮嬬殑锛屽畠灏变笉鎾ら攢璇ヨ鍙ャ傝繘涓姝ワ紝SQL璇彞鍙樻垚浜嬪姟鐨勪竴閮ㄥ垎鐩村埌COMMIT, ROLLBACK鎴栬呭鑷存殫鍦版彁浜ょ殑SQL璇彞鍙戠敓銆

·         濡傛灉浣犳病鏈夊湪璇彞涓寚瀹欼GNORE閫夐」锛岄噸澶嶉敭閿欒鍥炴粴SQL璇彞銆

·         琛屽お闀块敊璇洖婊歋QL璇彞銆

·         鍏跺畠閿欒涓昏琚唬鐮佺殑MySQL灞傦紙鍦↖nnoDB瀛樺偍寮曟搸绾у埆浠ヤ笂锛夋帰娴嬶紝瀹冧滑鍥炴粴鐩稿簲鐨凷QL璇彞銆傚湪鍗曚釜SQL璇彞涓殑鍥炴粴涓攣瀹氫笉琚噴鏀俱

鍦ㄦ殫鍦板洖婊氳繃绋嬩腑锛屽氨鍍忓湪涓涓槑纭殑ROLLBACK SQL鍛戒护鐨勬墽琛岃繃绋嬩腑涓鏍凤紝SHOW PROCESSLIST鍦⊿tate鍒椾负鏈夊叧鐨勮繛鎺ユ樉绀篟olling back銆

15.2.15.1. InnoDB閿欒浠g爜

涓嬮潰鐨勮繖涓笉瀹屽叏鍒楄〃鏄綘鍙兘閬囪鐨勫父瑙佺殑InnoDB涓撴湁閿欒锛屽甫鐫涓轰粈涔堝彂鐢熺殑鍘熷洜浠ュ強濡備綍璇ヨВ鍐抽棶棰樼殑鐩稿叧淇℃伅

·         1005 (ER_CANT_CREATE_TABLE)

涓嶈兘鍒涘缓琛ㄣ傚鏋滈敊璇俊鎭瓧绗︿覆鎸囧悜errno 150锛屽洜涓 澶栭敭绾︽潫琚笉姝g‘鍦板舰鎴愶紝鎵浠ヨ〃鍒涘缓澶辫触銆

·         1016 (ER_CANT_OPEN_FILE)

铏界劧瀵规煇琛ㄧ殑.frm鏂囦欢瀛樺湪锛屼絾涓嶈兘浠嶪nnoDB鏁版嵁鏂囦欢鎵惧埌璇nnoDB琛ㄣ傝鍙傞槄15.2.17.1鑺傦紝鈥淚nnoDB鏁版嵁璇嶅吀鎿嶄綔 鏁呴殰璇婃柇鍜屾帓闄も

·         1114 (ER_RECORD_FILE_FULL)

InnoDB鑰楀敖琛ㄧ┖闂翠腑鐨勫彲鐢ㄧ┖闂达紝浣犲簲璇ラ噸鏂伴厤缃〃绌洪棿鏉ユ坊鍔犱竴涓柊鏁版嵁鏂囦欢銆

·         1205 (ER_LOCK_WAIT_TIMEOUT)

閿佸畾绛夊緟瓒呮椂杩囨湡銆備簨鍔¤鍥炴粴銆

·         1213 (ER_LOCK_DEADLOCK)

浜嬪姟姝婚攣銆備綘搴旇閲嶈繍琛屼簨鍔°

·         1216 (ER_NO_REFERENCED_ROW)

浣犳璇曠潃娣诲姞涓琛岋紝浣嗘病鏈夌埗琛岋紝骞朵笖涓涓閿害鏉熷け璐ャ備綘搴旇鍏堟坊鍔犵埗琛屻

·         1217 (ER_ROW_IS_REFERENCED)

浣犳璇曞浘鍒犻櫎涓涓湁瀛愯鐨勭埗琛岋紝骞朵笖涓涓閿害鏉熷け璐ャ備綘搴旇鍏堝垹闄ゅ瓙琛屻

15.2.15.2. 鎿嶄綔绯荤粺閿欒浠g爜

瑕佹墦鍗颁竴涓搷浣滅郴缁熼敊璇彿鐨勬剰鎬濓紝璇蜂娇鐢∕ySQL鍒嗗彂鐗閲岀殑perror绋嬪簭銆

涓嬮潰琛ㄦ彁渚涗竴浜涘父鐢↙inux绯荤粺閿欒浠g爜銆傛洿瀹屾暣鐨勫垪琛ㄨ鍙傞槄Linux source code

·         1 (EPERM)

鎿嶄綔涓嶈鍏佽

·         2 (ENOENT)

鏃犳鏂囦欢鎴栫洰褰

·         3 (ESRCH)

鏃犳杩涚▼

·         4 (EINTR)

涓柇鐨勭郴缁熻皟鐢

·         5 (EIO)

I/O 閿欒

·         6 (ENXIO)

鏃犳璁惧鎴栧湴鍧

·         7 (E2BIG)

Arg鍒楄〃澶暱

·         8 (ENOEXEC)

Exec鍚堥傞敊璇

·         9 (EBADF)

鍧忔枃浠跺彿

·         10 (ECHILD) 

鏃犲瓙杩涚▼

·         11 (EAGAIN)

鍐嶈瘯涓娆

·         12 (ENOMEM)

鍐呭瓨鑰楀敖

·         13 (EACCES)

璁稿彲琚惁瀹

·         14 (EFAULT)

鍧忓湴鍧

·         15 (ENOTBLK)

闃绘闇姹傜殑璁惧

·         16 (EBUSY)

璁惧鎴栬祫婧愬繖

·         17 (EEXIST)

鏂囦欢瀛樺湪

·         18 (EXDEV)

浜ゅ弶璁惧杩炴帴

·         19 (ENODEV)

鏃犳璁惧

·         20 (ENOTDIR)

涓嶆槸涓涓洰褰

·         21 (EISDIR)

鏄竴涓洰褰曪紵

·         22 (EINVAL)

闈炴硶鍙傞噺

·         23 (ENFILE)

鏂囦欢琛ㄦ孩鍑

·         24 (EMFILE)

鎵撳紑鐨勬枃浠惰繃澶

·         25 (ENOTTY)

璁惧涓嶉傚悎鐨刬octl

·         26 (ETXTBSY)

鏂囨湰鏂囦欢蹇

·         27 (EFBIG)

鏂囦欢澶ぇ

·         28 (ENOSPC)

璁惧涓婃病绌洪棿浜

·         29 (ESPIPE)

闈炴硶鏌ユ壘

·         30 (EROFS)

鍙鏂囦欢绯荤粺

·         31 (EMLINK)

澶杩炴帴

涓嬪垪琛ㄦ彁渚涗竴鍒楀父鐢╓indows绯荤粺閿欒浠g爜銆傚畬鏁村垪琛ㄨ璁块棶Microsoft website

·         1 (ERROR_INVALID_FUNCTION)

涓嶆纭嚱鏁般

·         2 (ERROR_FILE_NOT_FOUND)

绯荤粺涓嶈兘鎵惧埌鎸囧畾鐨勬枃浠躲

·         3 (ERROR_PATH_NOT_FOUND)

绯荤粺涓嶈兘鎵惧埌鎸囧畾鐨勮矾寰勩

·         4 (ERROR_TOO_MANY_OPEN_FILES)

绯荤粺涓嶈兘鎵撳紑鏂囦欢銆

·         5 (ERROR_ACCESS_DENIED)

璁块棶琚嫆缁濄

·         6 (ERROR_INVALID_HANDLE)

鍙ユ焺闈炴硶銆

·         7 (ERROR_ARENA_TRASHED)

瀛樺偍鎺у埗鍧楄鐮村潖銆

·         8 (ERROR_NOT_ENOUGH_MEMORY)

鏃犺冻澶熷瓨鍌ㄦ潵澶勭悊杩欎釜鍛戒护銆

·         9 (ERROR_INVALID_BLOCK)

瀛樺偍鎺у埗鍧楀湴鍧闈炴硶銆

·         10 (ERROR_BAD_ENVIRONMENT)

鐜涓嶆纭

·         11 (ERROR_BAD_FORMAT)

璇曞浘鐢ㄤ笉姝g‘鐨勬牸寮忚杞戒竴涓▼搴忋

·         12 (ERROR_INVALID_ACCESS)

璁块棶浠g爜涓嶅悎娉曘

·         13 (ERROR_INVALID_DATA)

鏁版嵁涓嶅悎娉曘

·         14 (ERROR_OUTOFMEMORY)

鏃犺冻澶熺殑瀛樺偍鏉ュ畬鎴愯繖涓搷浣溿

·         15 (ERROR_INVALID_DRIVE)

绯荤粺涓嶈兘鎵惧埌鎸囧畾鐨勯┍鍔ㄥ櫒銆

·         16 (ERROR_CURRENT_DIRECTORY)

鐩綍涓嶈兘琚垹闄ゃ

·         17 (ERROR_NOT_SAME_DEVICE)

绯荤粺涓嶈兘绉诲姩姝ゆ枃浠跺埌涓涓笉鍚岀殑纾佺洏椹卞姩鍣ㄣ

·         18 (ERROR_NO_MORE_FILES)

娌℃湁鏇村鏂囦欢銆

·         19 (ERROR_WRITE_PROTECT)

濯掕川鍐欎繚鎶ゃ

·         20 (ERROR_BAD_UNIT)

绯荤粺涓嶈兘鎵惧埌鎸囧畾鐨勮澶囥

·         21 (ERROR_NOT_READY)

璁惧鏈噯澶囧ソ銆

·         22 (ERROR_BAD_COMMAND)

璁惧涓嶈兘璇嗗埆姝ゅ懡浠ゃ

·         23 (ERROR_CRC)

鏁版嵁閿欒(寰幆鍐椾綑鐮佹牎楠).

·         24 (ERROR_BAD_LENGTH)

绋嬪簭鍙戝嚭涓涓懡浠わ紝浣嗘槸鍛戒护闀垮害涓嶆纭

·         25 (ERROR_SEEK)

椹卞姩鍣ㄤ笉鑳藉湪纾佺洏涓婂畾浣嶆寚瀹氬尯鍩熸垨纾侀亾銆

·         26 (ERROR_NOT_DOS_DISK)

鎸囧畾鐨勭鐩樻垨杞洏涓嶈兘琚闂

·         27 (ERROR_SECTOR_NOT_FOUND)

椹卞姩鍣ㄤ笉鑳芥壘鍒拌姹傜殑鎵囧尯銆

·         28 (ERROR_OUT_OF_PAPER)

鎵撳嵃鏈虹己绾搞

·         29 (ERROR_WRITE_FAULT)

绯荤粺涓嶈兘鍐欐寚瀹氳澶囥

·         30 (ERROR_READ_FAULT)

绯荤粺涓嶈兘浠庢寚瀹氳澶囪銆

·         31 (ERROR_GEN_FAILURE)

闄勫姞鍒扮郴缁熺殑璁惧涓嶈捣浣滅敤銆

·         32 (ERROR_SHARING_VIOLATION)

杩涚▼涓嶈兘璁块棶鏂囦欢锛屽洜涓哄畠姝h鍙︿竴涓繘绋嬩娇鐢ㄣ

·         33 (ERROR_LOCK_VIOLATION)

杩涚▼涓嶈兘璁块棶鏂囦欢锛屽洜涓哄彟涓涓繘绋嬪凡缁忛攣瀹氭枃浠剁殑涓閮ㄥ垎銆

·         34 (ERROR_WRONG_DISK)

椹卞姩鍣ㄧ殑鐨勭鐩樹笉姝g‘锛岃鎻掑叆 %2 (鍗风郴鍒楀彿: %3) 鍒伴┍鍔ㄥ櫒 %1.

·         36 (ERROR_SHARING_BUFFER_EXCEEDED)

澶鏂囦欢琚墦寮浠ュ叡浜

·         38 (ERROR_HANDLE_EOF)

鍒拌揪鏂囦欢鐨勬湯灏俱

·         39 (ERROR_HANDLE_DISK_FULL)

纾佺洏宸叉弧銆

·         87 (ERROR_INVALID_PARAMETER)

鍙傛暟涓嶆纭傦紙濡傛灉浣犲湪Windows涓緱鍒拌繖涓敊璇紝骞朵笖宸茬粡鍦╩y.cnf鎴杕y.ini鏂囦欢涓缃甶nnodb_file_per_table锛屽垯娣诲姞innodb_flush_method=unbuffered鍒颁綘鐨刴y.cnf鎴杕y.ini鏂囦欢锛夈

·         112 (ERROR_DISK_FULL)

纾佺洏宸叉弧銆

·         123 (ERROR_INVALID_NAME)

鏂囦欢鍚嶏紝鐩綍鍚嶆垨鑰呭嵎鏍囪娉曚笉姝g‘銆

·         1450 (ERROR_NO_SYSTEM_RESOURCES)

瀛樺湪绯荤粺璧勬簮涓嶅瀹屾垚璇锋眰鐨勬湇鍔°

15.2.16瀵笽nnoDB琛ㄧ殑闄愬埗

·         涓涓〃涓嶈兘鍖呭惈瓒呰繃1000鍒椼

·         鍐呴儴鏈澶ч敭闀垮害鏄3500瀛楄妭锛屼絾MySQL鑷繁闄愬埗杩欎釜鍒1024瀛楄妭銆

·         闄や簡VARCHAR, BLOB鍜孴EXT鍒楋紝鏈澶ц闀垮害绋嶅井灏忎簬鏁版嵁搴撻〉鐨勪竴鍗娿傚嵆锛屾渶澶ц闀垮害澶х害8000瀛楄妭銆侺ONGBLOB鍜孡ONGTEXT鍒楀繀椤诲皬浜4GB, 鎬荤殑琛岄暱搴︼紝椤靛寘鎷珺LOB鍜孴EXT鍒楋紝蹇呴』灏忎簬4GB銆侷nnoDB鍦ㄨ涓瓨鍌╒ARCHAR锛孊LOB鎴朤EXT鍒楃殑鍓768瀛楄妭锛屼綑涓嬬殑瀛樺偍鐨勫垎鏁g殑椤甸潰涓

·         铏界劧InnoDB鍐呴儴鍦版敮鎸佽灏哄澶т簬65535锛屼綘涓嶈兘瀹氫箟涓涓寘鍚玍ARCHAR鍒楃殑锛屽悎骞跺昂瀵稿ぇ浜65535鐨勮銆

·                mysql> CREATE TABLE t (a VARCHAR(8000), b VARCHAR(10000),
·                    -> c VARCHAR(10000), d VARCHAR(10000), e VARCHAR(10000),
·                    -> f VARCHAR(10000), g VARCHAR(10000));
·                ERROR 1118 (42000): Row size too large. The maximum row size for the
·                used table type, not counting BLOBs, is 65535. You have to change some
·                columns to TEXT or BLOBs

·         鍦ㄤ竴浜涙洿鑰佺殑鎿嶄綔绯荤粺涓婏紝鏁版嵁鏂囦欢蹇呴』灏忎簬2GB銆

·         InnoDB鏃ュ織鏂囦欢鐨勫悎骞跺昂瀵稿繀椤诲皬浜4GB銆

·         鏈灏忕殑琛ㄧ┖闂村昂瀵告槸10MB銆傛渶澶х殑琛ㄧ┖闂村昂瀵告槸4,000,000,000涓暟鎹簱椤碉紙64TB锛夈傝繖涔熸槸涓涓〃鐨勬渶澶у昂瀵搞

·         InnoDB琛ㄤ笉鏀寔FULLTEXT绱㈠紩銆

·         ANALYZE TABLE 閫氳繃瀵规瘡涓储寮曟爲鍋氬叓娆¢殢鏈烘繁鍏ュ苟鐩稿簲鍦版洿鏂扮储寮曢泦浼板硷紝杩欐牱鏉ヨ鏁伴泦銆傛敞鎰忥紝鍥犱负杩欐槸浠呮湁鐨勪及鍊硷紝鍙嶅杩愯ANALYZE TABLE浼氫骇鐢熶笉鍚屾暟銆傝繖浣垮緱 ANALYZE TABLE 鍦 InnoDB 琛ㄤ笂寰堝揩锛屼笉鏄櫨鍒嗙櫨鍑嗙‘锛屽洜涓哄畠娌℃湁鑰冭檻鎵鏈夌殑琛屻

MySQL 涓嶄粎鍦ㄦ眹鍚堜紭鍖栦腑浣跨敤绱㈠紩闆嗕及鍊笺傚鏋滀竴浜涙眹鍚堟病鏈変互姝g‘鐨勬柟寮忎紭鍖栵紝浣犲彲浠ヨ瘯涓涓 ANALYZE TABLE 銆傚緢灏戞湁鎯呭喌锛孉NALYZE TABLE 娌℃湁浜х敓瀵逛綘鐗瑰畾鐨勮〃瓒冲濂界殑鍊硷紝浣犲彲浠ヤ娇鐢 FORCE INDEX 鍦ㄤ綘鏌ヨ涓潵寮哄埗浣跨敤鐗瑰畾绱㈠紩锛屾垨鑰呰缃 max_seeks_for_key 鏉ョ‘淇滿ySQL鍦琛ㄦ壂鎻忎箣涓婅繍琛岀储寮曟煡鎵俱璇峰弬闃5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺”銆傝鍙傞槄A.6鑺傦紝鈥滀紭鍖栧櫒鐩稿叧鐨勯棶棰”

·         鍦╓indows涓婏紝InnoDB鎬绘槸鍐呴儴鍦扮敤灏忓啓瀛楁瘝瀛樺偍鏁版嵁搴撳拰琛ㄥ悕瀛椼傝鎶婃暟鎹簱浠ヤ簩杩涘埗褰㈠紡浠嶶nix 绉诲埌Windows锛屾垨鑰呬粠Windows绉诲埌Unix锛屼綘搴旇璁╂墍鏈夋暟鎹簱鍜岃〃鐨勫悕瀛楅兘鏄皬鍐欍

·         璀﹀憡: 涓嶈鍦∕ySQL鏁版嵁搴撳唴鐨勬妸MySQL绯荤粺琛ㄤ粠MyISAM杞负InnoDB琛紒杩欐槸涓涓笉琚敮鎸佺殑鎿嶄綔銆傚鏋滀綘杩欎箞鍋氫簡锛孧ySQL鐩村埌浣犱粠澶囦唤鎭㈠鏃х郴缁熻〃锛屾垨鐢mysql_install_db鑴氭湰閲嶅缓绯荤粺琛ㄦ墠閲嶅惎鍔ㄣ

·         InnoDB鍦ㄨ〃鍐涓嶄繚鐣欒鐨勫唴閮ㄨ鏁般傦紙鍥犱负澶氱増鏈寲锛岃繖鍙兘纭疄鏈変簺澶嶆潅 锛夈傝澶勭悊涓涓猄ELECT COUNT(*) FROM t璇彞锛孖nnoDB蹇呴』鎵弿琛ㄧ殑涓涓储寮曪紝濡傛灉杩欎釜绱㈠紩涓嶅湪缂撳啿姹犱腑锛屾壂鎻忛渶瑕佽姳涓浜涙椂闂淬傝鑾峰緱蹇熻鏁帮紝浣犱笉寰椾笉浣跨敤涓涓嚜宸卞垱寤虹殑璁℃暟鍣ㄨ〃锛屽苟璁╀綘鐨勫簲鐢ㄦ寜鐓у畠鍋氱殑鎻掑叆鍜屽垹闄ゆ潵鏇存柊瀹冦傚鏋滀綘鐨勮〃鏍间笉缁忓父鏀瑰彉锛屼娇鐢∕ySQL鏌ヨ缂撳瓨鏃朵竴涓ソ鐨勮В鍐虫柟妗堛傚鏋滃ぇ鑷寸殑琛屾暟灏辫冻澶熶簡锛屽垯SHOW TABLE STATUS涔熷彲琚娇鐢ㄣ傝鍙傞槄15.2.11鑺傦紝鈥淚nnoDB鎬ц兘璋冭妭鎻愮ず”

·         瀵逛簬AUTO_INCREMENT鍒楋紝浣犲繀椤绘绘槸涓鸿〃瀹氫箟涓涓储寮曪紝骞朵笖绱㈠紩蹇呴』鍖呭惈AUTO_INCREMENT鍒椼傚湪MyISAM琛ㄤ腑锛孉UTO_INCREMENT鍒楀彲鑳芥椂澶 鍒楃储寮曠殑涓閮ㄥ垎銆

·         褰撲綘閲嶅惎MySQL鏈嶅姟鍣ㄤ箣鏃讹紝InnoDB鍙兘涓轰竴涓狝UTO_INCREMENT鍒楅噸浣跨敤涓涓棫鍊硷紙鍗筹紝涓涓璧嬬粰涓涓佺殑宸 鍥炴粴鐨勪簨鍔$殑鍊硷級銆

·         褰撲竴涓狝UTO_INCREMENT鍒楃敤瀹屽硷紝InnoDB闄愬埗涓涓狟IGINT鍒帮紞9223372036854775808浠ュ強BIGINT UNSIGNED鍒1銆傚敖绠″姝わ紝BIGINT鍊兼湁鐢64浣嶏紝鎵浠ユ敞鎰忓埌锛屽鏋滀綘瑕佷竴绉掕緭鍏100涓囦釜琛岋紝鍦˙IGINT鍒拌揪瀹冧笂闄愪箣鍓嶏紝鍙兘杩橀渶瑕佸皢杩30涓囧勾銆傜敤鎵鏈夊叾瀹冩暣鏁扮被鍨 鍒楋紝浜х敓涓涓噸澶嶉敭閿欒銆傝繖绫讳技浜嶮yISAM濡備綍宸ヤ綔鐨勶紝鍥犱负瀹冧富瑕佹槸涓鑸琈ySQL琛屼负锛屽苟涓嶇壒鍒叧浜庝换浣曞瓨鍌ㄥ紩鎿庛

·         DELETE FROM tbl_name涓嶉噸鏂扮敓鎴愯〃锛屼絾鍙栬屼唬涔嬪湴鍒犻櫎鎵鏈夎锛屼竴涓帴涓涓湴鍒犻櫎銆

·         TRUNCATE tbl_name涓篒nnoDB鑰岃鏄犲皠鍒癉ELETE FROM tbl_name 骞朵笖涓嶉噸缃瓵UTO_INCREMENT璁℃暟鍣ㄣ

·         SHOW TABLE STATUS涓嶈兘缁欏嚭鍏充簬InnoDB琛ㄥ噯纭殑缁熻鏁版嵁锛岄櫎浜嗚琛ㄤ繚鐣欑殑鐗╃悊灏哄銆傝璁℃暟浠呮槸鍦⊿QL浼樺寲涓矖鐣ョ殑浼拌銆

·         鍦∕ySQL 5.1涓紝濡傛灉innodb_table_locks=1(1鏄粯璁ゅ硷級 MySQL LOCK TABLES鎿嶄綔鍦ㄦ瘡涓涓〃涓婅幏鍙栦袱涓攣瀹氥傞櫎浜嗗湪MySQL灞傜殑琛ㄩ攣瀹氾紝瀹冧篃鑾峰緱涓涓狪nnoDB琛ㄩ攣瀹氥傛棫鐗堢殑MySQL涓嶈幏鍙朓nnoDB琛ㄩ攣瀹氾紝鏃ц涓哄彲浠ラ氳繃璁剧疆innodb_table_locks=0 鏉ラ夋嫨銆傚鏋滄病鏈塈nnoDB琛ㄩ攣瀹氳鑾峰緱锛屽嵆浣胯〃鐨勪竴浜涜褰曡鍏跺畠浜嬪姟閿佸畾锛孡OCK TABLES瀹屾垚銆 

·         鎵鏈夎涓涓簨鍔℃寔鏈夌殑InnoDB閿佸畾鍦ㄨ浜嬪姟琚彁浜ゆ垨涓涔嬫椂琚噴鏀俱傚洜姝ゅ湪AUTOCOMMIT=1妯″紡锛屽湪InnoDB琛ㄤ笂璋冪敤鏄病鏈夊お澶氭剰涔夌殑锛屽洜涓鸿闇姹傜殑InnoDB琛ㄩ攣瀹氬彲鑳戒細琚珛鍗抽噴鏀俱

·         鏈夋椂锛屽湪浜嬪姟鐨勮繃绋嬩腑閿佸畾鏇村鐨勮〃鍙兘鏄湁鐢ㄧ殑銆備笉骞稿湴锛孧ySQL涓殑LOCK TABLES鎵ц涓涓殫鍦扮殑COMMIT鍜孶NLOCK TABLES銆侺OCK TABLES鐨勪竴涓狪nnoDB鍙橀噺宸茬粡琚鍒掞紝 璇ヨ鍒掑湪浜嬪姟鐨勪腑闂磋鎵ц銆

·         涓哄缓绔嬪鍒朵粠鏈嶅姟鍣ㄧ殑LOAD TABLE FROM MASTER璇彞瀵InnoDB琛ㄤ笉璧蜂綔鐢ㄣ備竴涓伐浣滃尯鍦ㄤ富鏈嶅姟鍣ㄤ笂鏇存崲琛ㄤ负MyISAM鐨勶紝鐒跺悗鍋氳礋杞斤紝涔嬪悗鏇存崲涓绘湇鍔″櫒琛ㄥ洖鍒癐nnoDB涓

·         鍦↖nnoDB涓粯璁ゆ暟鎹簱椤电殑澶у皬鏄16KB銆傞氳繃缂栬瘧浠g爜锛屼綘鍙互鍦8KB鍒64KB涔嬮棿鏉ヨ缃繖涓笺備綘涓嶅緱涓嶆洿鏂板湪univ.i婧愭枃浠朵腑鐨刄NIV_PAGE_SIZE鍜孶NIV_PAGE_SIZE_SHIFT鐨勫笺

·         鍦∕ySQL 5.1涓紝瑙﹀彂鍣ㄤ笉琚骇鑱旂殑澶栭敭琛屼负婵娲汇

15.2.17InnoDB鏁呴殰璇婃柇鍜屾帓闄

·         涓涓荤殑瑙勫垯鏄紝褰撲竴涓搷浣滃け璐ユ垨杩欎綘鎬鐤戞湁涓涓己闄枫備綘搴旇鏌ョ湅MySQL鏈嶅姟鍣ㄧ殑閿欒鏃ュ織锛岃鏃ュ織鍏稿瀷鍦版湁涓涓湁浜涜薄hostname.err杩欐牱鐨勫悕瀛楋紝鎴栬呭湪Windows涓婃槸mysql.err杩欐牱鐨勩

·         鏁呴殰璇婃柇涓庢帓闄や箣鏃讹紝閫氬父鏈濂戒粠鍛戒护鎻愮ず绗﹁繍琛孧ySQL鏈嶅姟鍣紝鑰屼笉鏄粠mysqld_safe鍖呰繍琛岋紝鎴栦笉浣滀负涓涓猈indows鏈嶅姟鏉ヨ繍琛屻備綘鍙互鐪mysqld鎵撳嵃鍒版帶鍒跺彴涓婄殑鍐呭锛屽洜姝ゆ洿濂芥帉鎻″彂鐢熶簡浠涔堛傚湪Windows涓婏紝浣犲繀椤荤敤--console閫夐」鍚姩鏈嶅姟鍣ㄥ皢杈撳嚭瀹氬悜鍒版帶鍒跺彴绐楀彛

·         浣跨敤InnoDB Monitors鑾峰彇鍏充簬鏌愰棶棰樼殑淇℃伅銆傚鏋滈棶棰樻槸鎬ц兘鐩稿叧鐨勶紝鎴栬呬綘鐨勬湇鍔″櫒鐪嬭捣鏉ヨ鎸傝捣锛屼綘搴旇浣跨敤innodb_monitor鏉ユ墦鍗癐nnoDB鍐呴儴鐘舵佺殑淇℃伅锛屽鏋滈棶棰樻槸鍏充簬閿佸畾锛屽垯浣跨敤innodb_lock_monitor銆傚鏋滈棶棰樻槸鍦ㄨ〃鐨勫垱寤烘垨鍏跺畠鏁版嵁璇嶅吀鎿嶄綔锛屼娇鐢╥nnodb_table_monitor鏉ユ墦鍗癐nnoDB鍐呴儴鏁版嵁璇嶅吀鐨勫唴瀹广

·         濡傛灉浣犵寽娴嬩竴涓〃琚牬鍧忥紝鍒欏湪璇ヨ〃涓婅繍琛孋HECK TABLE銆

15.2.17.1. InnoDB鏁版嵁璇嶅吀鎿嶄綔閿欒璇婃柇鍜屾帓闄

琛ㄧ殑涓涓壒娈婇棶棰樻槸MySQL鏈嶅姟鍣ㄤ互.frm鏂囦欢鏉ヤ繚瀛樻暟鎹瘝鍏镐俊鎭紝瀹冭鏀惧湪鏁版嵁搴撶洰褰曪紝鐒惰孖nnoDB涔熷瓨鍌ㄤ俊鎭埌琛ㄧ┖闂存枃浠堕噷瀹冭嚜宸辩殑鏁版嵁璇嶅吀閲屻傚鏋滀綘鎶.frm鏂囦欢绉绘潵绉诲幓 锛涙垨鑰咃紝濡傛灉鏈嶅姟鍣ㄥ湪鏁版嵁璇嶅吀鎿嶄綔鐨勪腑闂村穿婧冿紝.frm鏂囦欢鍙兘缁撴潫涓嶪nnoDB鍐呴儴鏁版嵁璇嶅吀鐨勫悓姝ャ

涓涓笉鍚屾鐨勬暟鎹瘝鍏哥殑鐥囩姸鏄疌REATE TABLE璇彞澶辫触銆傚鏋滃彂鐢熻繖绉嶆儏鍐碉紝浣犲簲璇ユ煡鐪嬫湇鍔″櫒鐨勯敊璇棩蹇椼傚鏋滄棩蹇楄琛ㄥ凡缁忓瓨鍦ㄤ簬InnoDB鍐呴儴鏁版嵁璇嶅吀褰撲腑锛屼綘鍦↖nnoDB琛ㄧ┖闂存枃浠跺唴鏈変竴涓琛紝瀹冩病鏈夊搴旂殑.frm鏂囦欢銆傞敊璇俊鎭湅璧锋潵璞″涓嬬殑锛

InnoDB: Error: table test/parent already exists in InnoDB internal
InnoDB: data dictionary. Have you deleted the .frm file
InnoDB: and not used DROP TABLE? Have you used DROP DATABASE
InnoDB: for InnoDB tables in MySQL version <= 3.23.43?
InnoDB: See the Restrictions section of the InnoDB manual.
InnoDB: You can drop the orphaned table inside InnoDB by
InnoDB: creating an InnoDB table with the same name in another
InnoDB: database and moving the .frm file to the current database.
InnoDB: Then MySQL thinks the table exists, and DROP TABLE will
InnoDB: succeed.

浣犲彲浠ユ寜鐓ч敊璇棩蹇楅噷缁欑殑鎸囩ず绉婚櫎涓涓琛ㄣ傚鏋滆繕鏄笉鑳芥垚鍔熷湴浣跨敤DROP TABLE锛岄棶棰樺彲鑳芥槸鍥犱负鍦mysql瀹㈡埛绔噷鐨勫悕瀛楀畬鎴愩傝瑙e喅杩欎釜闂锛岀敤--disable-auto-rehash閫夐」鏉ュ惎鍔mysql瀹㈡埛绔苟鍐嶆灏濊瘯DROP TABLE 銆傦紙鏈夊悕瀛楀畬鎴愭墦寮鐫锛mysql璇曠潃鏋勫缓涓〃鍚嶅瓧鐨勫垪琛紝褰撲竴涓濡傛弿杩扮殑闂瀛樺湪涔嬫椂锛岃繖涓垪琛ㄥ氨涓嶈捣浣滅敤锛夈

涓嶅悓姝ユ暟鎹瘝鍏哥殑鍙︿竴涓“鍚屼箟璇”鏄疢ySQL鎵撳嵃涓涓笉鑳芥墦寮.InnoDB鏂囦欢鐨勯敊璇細

ERROR 1016: Can't open file: 'child2.InnoDB'. (errno: 1)

鍦ㄩ敊璇棩蹇椾綘鍙互鍙戠幇涓涓被浼间簬姝ょ殑淇℃伅锛

InnoDB: Cannot find table test/child2 from the internal data dictionary
InnoDB: of InnoDB though the .frm file for the table exists. Maybe you
InnoDB: have deleted and recreated InnoDB data files but have forgotten
InnoDB: to delete the corresponding .frm files of InnoDB tables?

杩欐剰鍛宠繖鏈変竴涓鍗曠殑.frm鏂囦欢锛屽湪InnoDB鍐呮病鏈夌浉瀵瑰簲鐨勮〃銆備綘鍙互閫氳繃鎵嬪姩鍒犻櫎鏉ョЩ闄よ繖涓鍗曠殑.frm鏂囦欢銆

濡傛灉MySQL鍦ㄤ竴涓 ALTER TABLE鎿嶄綔鐨勪腑闂村穿婧冿紝浣犲彲浠ョ敤InnoDB琛ㄧ┖闂村唴涓存椂瀛よ〃鏉ョ粨鏉熴備綘鍙互鐢╥nnodb_table_monitor鐪嬩竴涓垪鍑虹殑琛紝鍚嶄负#sql-...銆傚鏋滀綘鎶婅〃鐨勫悕瀛楀寘鍦`(backticks)閲岋紝浣犲彲浠ュ湪鍚嶅瓧鍖呭惈“#”瀛楃鐨勮〃涓婃墽琛孲QL璇彞銆傚洜姝わ紝浣犲彲浠ョ敤鍓嶈堪鐨勭殑鏂规硶璞$Щ闄ゅ叾瀹冨琛ㄤ竴鏍风Щ闄よ繖鏍蜂竴涓琛ㄣ傛敞鎰忥紝瑕佸湪Unix澶栧3閲屽鍒舵垨閲嶅懡鍚嶄竴涓枃浠讹紝濡傛灉鏂囦欢鍚嶅寘鍚"#"瀛楃锛屼綘闇瑕佹妸鏂囦欢鍚嶆斁鍦ㄥ弻寮曞彿閲屻

15.3. MERGE瀛樺偍寮曟搸

MERGE瀛樺偍寮曟搸锛屼篃琚璇嗕负MRG_MyISAM寮曟搸锛屾槸涓涓浉鍚岀殑鍙互琚綋浣滀竴涓潵鐢ㄧ殑MyISAM琛ㄧ殑闆嗗悎銆“鐩稿悓”鎰忓懗鐫鎵鏈夎〃鍚屾牱鐨 鍒楀拰绱㈠紩淇℃伅銆備綘涓嶈兘鍚堝苟鍒楄浠ヤ笉鍚岄『搴忓垪浜庡叾涓殑琛紝娌℃湁鎭板ソ鍚屾牱鍒楃殑琛紝鎴栨湁涓嶅悓椤哄簭绱㈠紩鐨勮〃銆傝屼笖锛屼换浣曟垨鑰呮墍鏈夌殑琛ㄥ彲浠ョ敤myisampack鏉ュ帇缂┿傝鍙傞槄8.2鑺傦紝鈥渕yisampack — 鐢熸垚鍘嬬缉鐨勫彧璇籑yISAM琛”銆傝〃閫夐」鐨勫樊寮傦紝姣斿AVG_ROW_LENGTH, MAX_ROWS鎴朠ACK_KEYS閮戒笉閲嶈銆

褰撲綘鍒涘缓涓涓狹ERGE琛ㄤ箣鏃讹紝MySQL鍦ㄧ鐩樹笂鍒涘缓涓や釜鏂囦欢銆傛枃浠跺悕浠ヨ〃鐨勫悕瀛楀紑濮嬶紝骞朵笖鏈変竴涓墿灞曞悕鏉ユ寚鏄庢枃浠剁被鍨嬨備竴涓.frm鏂囦欢瀛樺偍琛ㄥ畾涔夛紝涓涓.MRG鏂囦欢鍖呭惈琚綋浣滀竴涓潵鐢ㄧ殑琛ㄧ殑鍚嶅瓧銆傝繖浜涜〃浣滀负MERGE琛ㄨ嚜韬紝涓嶅繀瑕佸湪鍚屼竴涓暟鎹簱涓

浣犲彲浠ュ琛ㄧ殑闆嗗悎鐢⊿ELECT, DELETE, UPDATE鍜孖NSERT銆備綘蹇呴』瀵逛綘鏄犲皠鍒颁竴涓狹ERGE琛ㄧ殑杩欎簺琛ㄦ湁SELECT, UPDATE鍜孌ELETE 鐨勬潈闄愩

濡傛灉浣燚ROP MERGE琛紝浣犱粎鍦ㄧЩ闄ERGE瑙勬牸銆傚簳灞傝〃娌℃湁鍙楀奖鍝嶃

褰撲綘鍒涘缓涓涓狹ERGE琛ㄤ箣鏃讹紝浣犲繀椤绘寚瀹氫竴涓猆NION=(list-of-tables)瀛愬彞锛屽畠璇存槑浣犺鎶婂摢浜涜〃褰撲綔涓涓潵鐢ㄣ傚鏋滀綘鎯宠瀵筂ERGE琛ㄧ殑鎻掑叆鍙戠敓鍦║NION鍒楄〃涓殑绗竴涓垨鏈鍚庝竴涓〃涓婏紝浣犲彲浠ラ夋嫨鍦版寚瀹氫竴涓狪NSERT_METHOD閫夐」銆備娇鐢‵IRST鎴朙AST鍊间娇寰楁彃鍏ヨ鐩稿簲鍦板仛鍦ㄧ涓鎴栨渶鍚庝竴涓〃涓娿傚鏋滀綘娌℃湁鎸囧畾INSERT_METHOD閫夐」锛屾垨浣犵敤涓涓狽O鍊兼寚瀹氳閫夐」銆傚線MERGE琛ㄦ彃鍏ヨ褰曠殑璇曞浘瀵艰嚧閿欒銆

涓嬮潰渚嬪瓙璇存槑濡備綍鍒涘缓涓涓狹ERGE琛細

mysql> CREATE TABLE t1 (
    ->    a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->    message CHAR(20));
mysql> CREATE TABLE t2 (
    ->    a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->    message CHAR(20));
mysql> INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
mysql> INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');
mysql> CREATE TABLE total (
    ->    a INT NOT NULL AUTO_INCREMENT,
    ->    message CHAR(20), INDEX(a))
    ->    TYPE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;

娉ㄦ剰锛屼竴涓垪鍦∕ERGEN琛ㄤ腑琚储寮曪紝浣嗘病鏈夎瀹e憡涓轰竴涓狿RIMARY KEY锛屽洜涓哄畠鏄湪鏇撮噸瑕佺殑MyISAM琛ㄤ腑銆傝繖鏄繀瑕佺殑锛屽洜涓篗ERGE琛ㄥ湪鏇撮噸瑕佺殑琛ㄤ腑鐨勮缃笂寮哄埗闈炲敮涓鎬с

鍒涘缓MERGE琛ㄤ箣鍚庯紝浣犲彲浠ュ彂鍑烘妸涓缁勮〃褰撲綔涓浣撴潵鎿嶄綔鐨勬煡璇細

mysql> SELECT * FROM total;
+---+---------+
| a | message |
+---+---------+
| 1 | Testing |
| 2 | table   |
| 3 | t1      |
| 1 | Testing |
| 2 | table   |
| 3 | t2      |
+---+---------+

娉ㄦ剰锛屼綘涔熷彲浠ョ洿鎺ヤ粠MySQL涔嬪鐩存帴鎿嶄綔.MRG鏂囦欢锛

shell> cd /mysql-data-directory/current-database
shell> ls -1 t1 t2 > total.MRG
shell> mysqladmin flush-tables

瑕侀噸鏄犲皠涓涓狹ERGE琛ㄥ埌涓涓笉鍚岀殑MyISAM琛ㄩ泦锛屼綘鍙互鎵ц涓嬪垪涔嬩竴锛

·         DROP MERGE琛ㄥ苟涓旈噸寤哄畠銆

·         浣跨敤ALTER TABLE tbl_name UNION=(...)鏉ユ敼鍙樺簳灞傝〃鐨勫垪琛ㄣ

·         鏀瑰彉.MRG鏂囦欢锛屽苟瀵筂ERGE琛ㄦ垨鑰呮墍鏈夊簳灞傝〃鍙戝嚭涓涓狥LUSH TABLE璇彞鏉ュ己鍒跺瓨鍌ㄥ紩鎿庡幓璇绘柊鐨勫畾涔夋枃浠躲

MERGE琛ㄥ彲浠ュ府浣犺В鍐充互涓嬮棶棰橈細

·         瀹规槗鍦扮鐞嗕竴濂楁棩蹇楄〃銆傛瘮濡傦紝浣犲彲浠ユ妸涓嶅悓鏈堢殑鏁版嵁鏀捐繘鍒嗙鐨勮〃涓紝鐢myisampack鍘嬬缉鍏朵腑鐨勪竴浜涳紝骞堕殢鍚庡垱寤轰竴涓狹ERGE琛ㄦ潵鎶婂畠浠綋浣滀竴涓潵浣跨敤銆

·         鑾峰緱鏇村揩鐨勯熷害銆備綘鍙互浠ヤ竴浜涙爣鍑嗘潵鍒嗗壊澶х殑鍙琛紝鐒跺悗鏀捐繘涓嶅悓纾佺洏涓婄殑鍗曚釜琛ㄤ腑銆傚熀浜庢鐨勪竴涓狹ERGE琛ㄥ彲姣斾娇鐢ㄥぇ琛ㄨ蹇緱澶氥

·         鎵ц鏇存湁鏁堢殑鎼滅储銆傚鏋滀綘纭垏鐭ラ亾瑕佹悳绱粈涔堬紝瀵逛竴浜涙煡璇綘鍙互鍙湪琚垎鍓茬殑琛ㄧ殑鍏朵腑涔嬩竴鏉ユ悳绱紝骞朵笖瀵瑰叾瀹冧娇鐢∕ERGE銆備綘鐢氳嚦鏈夎澶氫笉鍚岀殑MERGE琛紝瀹冧滑浣跨敤鏈夐噸鍙犵殑琛ㄥ銆

·         鎵ц鏇存湁鏁堢殑淇ˉ銆備慨琛ヨ鏄犲皠鍒颁竴涓狹ERGE琛ㄤ腑鐨勫崟涓〃姣斾慨琛ュ崟涓ぇ鍨嬭〃瑕佹洿杞绘澗銆

·         鍗冲埢鏄犲皠璁稿琛ㄦ垚涓涓侻ERGE琛ㄤ笉闇瑕佺淮鎶ゅ畠鑷繁鐨勭储寮曪紝鍥犱负瀹冧娇鐢ㄥぇ鍝ヨ〃鐨勬墍鐢ㄣ傚洜姝わ紝MERGE琛ㄩ泦鍚堟槸闈炲父鍧楀湴鍒涘缓鎴栭噸鏄犲皠銆傦紙娉ㄦ剰锛屽綋浣犲垱寤轰竴涓狹ERGE琛ㄤ箣鏃讹紝鍗充娇娌℃湁绱㈠紩琚垱寤猴紝浣犲繀椤讳粛鐒舵寚瀹氱储寮曞畾涔夛級銆

·         濡傛灉鏍规嵁闇瑕佹垨鎸夌収鎵规锛屼綘鏈変竴缁勮鍚堣捣鏉ヤ綔涓轰竴涓ぇ琛ㄧ殑琛紝浣犲簲璇ユ牴鎹渶瑕佸瀹冧滑鍒涘缓涓涓狹ERGE琛ㄦ潵鏇夸唬澶ц〃銆傝繖鏍疯蹇緱澶氳屼笖鑺傜害澶ч噺鐨勭鐩樼┖闂淬

·         瓒呰繃鎿嶄綔绯荤粺鐨勬枃浠跺昂瀵搁檺鍒躲傛瘡涓狹yISAM琛ㄩ兘鍙楀埗浜庤繖涓檺鍒讹紝浣嗘槸涓涓狹yISAM琛ㄧ殑闆嗗悎鍒欎笉鐒躲

·         浣犲彲浠ラ氳繃瀹氫箟涓涓槧灏勫埌鍗曚釜琛ㄧ殑MERGE琛ㄦ潵涓轰竴涓狹yISAM琛ㄥ垱寤轰竴涓埆鍚嶆垨“鍚岀墿寮傚悕”銆傝繖鏍峰仛搴旇娌℃湁鐪熷疄鐨勫彲瀵熻鐨勬ц兘褰卞搷 锛堝姣忎釜璇诲彧鏈変竴浜涢棿鎺ヨ皟鐢ㄥ拰memcpy()璋冪敤锛夈

MERGE琛ㄧ殑缂虹偣锛

·         浣犲彲浠ュMERGE琛ㄤ娇鐢ㄤ粎鐩稿悓鐨凪yISAM琛ㄣ

·         浣犱笉鑳藉湪MERGE琛ㄤ腑浣跨敤寰堝MyISAM鍔熻兘銆傛瘮濡傦紝浣犱笉鑳藉湪MERGE琛ㄤ笂鍒涘缓FULLTEXT绱㈠紩銆傦紙褰撶劧锛屼綘鍙互鍦ㄥ簳灞侻ERGE 琛ㄤ笂鍒涘缓FULLTEXT绱㈠紩锛屼絾鏄綘涓嶈兘鐢ㄥ叏鏂囨悳绱㈡潵鎼滅储MERGE琛級銆

·         濡傛灉MERGE琛ㄦ槸闈炰复鏃剁殑锛屾墍鏈夊簳灞侻yISAM琛ㄤ篃蹇呴』鏄案涔呯殑銆傚鏋淢ERGE琛ㄦ槸涓存椂鐨勶紝MyISAM琛ㄥ彲浠ユ槸浠讳綍涓存椂锛嗛潪涓存椂鐨勬贩鍚堛

·         MERGE琛ㄤ娇鐢ㄦ洿澶氱殑鏂囦欢鎻忚堪绗︺傚鏋滄槸涓鎴风姝d娇鐢ㄤ竴涓槧灏勫埌10涓〃鐨凪ERGE琛紝鏈嶅姟鍣ㄤ娇鐢(10*10)+10涓枃浠舵弿杩扮銆傦紙10涓暟鎹枃浠舵弿杩扮缁10涓鎴风姣忎汉涓涓紝骞朵笖鍦ㄥ鎴风涔嬮棿鍏变韩10涓储寮曟枃浠舵弿杩扮锛夈

· 閿浼鏇存參銆傚綋浣犺涓涓敭鐨勬椂鍊欙紝MERGE瀛樺偍寮曟搸闇瑕佸湪鎵鏈 搴曞眰琛ㄤ笂鍙戝嚭涓涓浠ユ鏌ュ摢涓涓帴杩戝尮閰嶇粰瀹氱殑閿傚鏋滀綘闅忓悗鍋氫簡涓涓猺ead-next锛孧ERGE瀛樺偍寮曟搸闇瑕佹悳绱㈣缂撳啿鏉ユ壘鍑轰笅涓涓敭銆傚彧鏈夊綋涓涓敭缂撳啿琚楀敖锛屽瓨鍌ㄥ紩鎿庢墠闇瑕佽涓嬩竴涓 閿潡銆傝繖浣垮緱MERGE閿湪eq_ref鎼滅储涓潪甯告參锛屼絾鍦╮ef鎼滅储涓笉鏄お鎱€傝鍙傞槄7.2.1鑺傦紝鈥淓XPLAIN 璇硶(鑾峰彇SELECT鐩稿叧淇℃伅)” 浠ヨ幏寰楁洿澶氬叧浜巈q_ref鍜宺ef鐨勪俊鎭

15.3.1MERGE琛 鏂归潰鐨勯棶棰

涓嬪垪鏄凡鐭ュ叧浜嶮ERGE琛ㄧ殑闂锛

·         濡傛灉浣犱娇鐢ˋLTER TABLE 鏉ユ妸MERGE琛ㄥ彉涓哄叾瀹冭〃绫诲瀷锛屽埌 搴曞眰琛ㄧ殑鏄犲皠灏辫涓㈠け浜嗐傚彇鑰屼唬涔嬬殑锛屾潵鑷簳灞侻yISAM琛ㄧ殑琛岃澶嶅埗鍒板凡鏇存崲鐨勮〃涓紝璇ヨ〃闅忓悗琚寚瀹氭柊绫诲瀷銆

·         REPLACE涓嶈捣浣滅敤銆

·         娌℃湁WHERE瀛愬彞锛屾垨鑰呭湪浠讳綍琚槧灏勫埌涓涓墦寮鐨凪ERGE琛ㄤ笂鐨勪换浣曚竴涓〃涓婄殑REPAIR TABLE锛孴RUNCATE TABLE, OPTIMIZE TABLE鎴朅NALYZE TABLE锛屼綘涓嶈兘浣跨敤DROP TABLE, ALTER TABLE, DELETE FROM銆傚鏋滀綘杩欎箞鍋氫簡锛孧ERGE琛ㄥ皢浠嶆棫鎸囧悜鍘熷琛紝杩欐牱浜х敓鎰忓缁撴灉銆傝В鍐宠繖涓笉瓒虫渶绠鍗曠殑鍔炴硶鏄湪鎵ц浠讳綍涓涓繖浜涙搷浣滀箣鍓嶅彂鍑轰竴涓狥LUSH TABLES璇彞鏉ョ‘淇濇病鏈塎ERGE琛ㄤ粛鏃т繚鎸佹墦寮銆

·         涓涓狹ERGE琛ㄤ笉鑳藉湪鏁翠釜琛ㄤ笂缁存寔UNIQUE绾︽潫銆傚綋浣犳墽琛屼竴涓狪NSERT, 鏁版嵁杩涘叆绗竴涓垨鑰呮渶鍚庝竴涓狹yISAM琛紙鍙栧喅浜嶪NSERT_METHOD閫夐」鐨勫硷級銆侻ySQL纭繚鍞竴 閿煎湪閭d釜MyISAM琛ㄩ噷淇濇寔鍞竴锛屼絾涓嶆槸璺ㄩ泦鍚堥噷鎵鏈夌殑琛ㄣ

·         褰撲綘鍒涘缓涓涓狹ERGE琛ㄤ箣鏃讹紝娌℃湁妫鏌ュ幓纭繚搴曞眰琛ㄧ殑瀛樺湪浠ュ強鏈夌浉鍚岀殑鏈烘瀯銆傚綋MERGE琛ㄨ浣跨敤涔嬫椂锛孧ySQL妫鏌ユ瘡涓鏄犲皠鐨勮〃鐨勮褰曢暱搴︽槸鍚︾浉绛夛紝浣嗚繖骞朵笉鍗佸垎鍙潬銆傚鏋滀綘浠庝笉鐩镐技鐨凪yISAM琛ㄥ垱寤轰竴涓狹ERGE琛紝浣犻潪甯告湁鍙兘鎾炶濂囨殑闂銆

·         鍦∕ERGE琛ㄤ腑鐨勭储寮曠殑椤哄簭鍜屽畠鐨 搴曞眰琛ㄤ腑鐨勭储寮曞簲璇ヤ竴鏍枫傚鏋滀綘浣跨敤ALTER TABLE缁欎竴涓鐢ㄥ湪MERGE琛ㄤ腑鐨勮〃娣诲姞涓涓猆NIQUE绱㈠紩锛岀劧鍚庝娇鐢ˋLTER TABLE鍦∕ERGE琛ㄤ笂娣诲姞涓涓潪鍞竴绱㈠紩锛屽鏋滃湪 搴曞眰琛ㄤ笂宸茬粡鏈変竴涓潪鍞竴绱㈠紩锛屽琛ㄧ殑绱㈠紩鎺掑簭鏄笉鍚岀殑銆傦紙杩欐槸鍥犱负ALTER TABLE鎶奤NIQUE绱㈠紩鏀惧湪闈炲敮涓绱㈠紩涔嬪墠浠ュ埄浜庨噸澶嶉敭鐨勫揩閫熸娴 锛夈傚洜姝ゅ浣跨敤杩欐牱绱㈠紩鐨勮〃鐨勬煡璇㈠彲鑳借繑鍥炰笉鏈熸湜鐨勭粨鏋溿

·         鍦╓indows涓紝鍦ㄤ竴涓MERGE琛ㄤ娇鐢ㄧ殑琛ㄤ笂DROP TABLE涓嶈捣浣滅敤锛屽洜涓MERGE寮曟搸鐨勮〃鏄犲皠瀵筂ySQL鐨勬洿涓婂眰闅愯棌銆傚洜涓篧indows涓嶅厑璁稿凡鎵撳紑鏂囦欢鐨勫垹闄わ紝浣犻鍏堝繀椤 鍒锋柊鎵鏈塎ERGE琛紙浣跨敤FLUSH TABLES锛夋垨鍦ㄧЩ闄よ琛ㄤ箣鍓嶇Щ闄ERGE琛ㄣ

瀵逛簬MERGE瀛樺偍寮曟搸锛屽湪http://forums.mysql.com/list.php?93涓婃湁涓涓笓闂ㄧ殑璁哄潧銆

15.4. MEMORY (HEAP)瀛樺偍寮曟搸

MEMORY瀛樺偍寮曟搸鐢ㄥ瓨鍦ㄥ唴瀛樹腑鐨勫唴瀹规潵鍒涘缓琛ㄣ傝繖浜涘湪浠ュ墠琚璇嗕负HEAP琛ㄣ侻EMORY鏄竴涓閫夌殑鏈锛岃櫧鐒朵负鍚戜笅鍏煎锛孒EAP渚濇棫琚敮鎸併

姣忎釜MEMORY琛ㄥ拰涓涓鐩樻枃浠跺叧鑱旇捣鏉ャ傛枃浠跺悕鐢辫〃鐨勫悕瀛楀紑濮嬶紝骞朵笖鐢变竴涓.frm鐨勬墿灞曞悕鏉ユ寚鏄庡畠瀛樺偍鐨勮〃瀹氫箟銆

瑕佹槑纭寚鍑轰綘鎯宠涓涓狹EMORY琛紝鍙娇鐢‥NGINE閫夐」鏉ユ寚瀹氾細

CREATE TABLE t (i INT) ENGINE = MEMORY;

濡傚畠浠悕瀛楁墍鎸囨槑鐨勶紝MEMORY琛ㄨ瀛樺偍鍦ㄥ唴瀛樹腑锛屼笖榛樿浣跨敤鍝堝笇绱㈠紩銆傝繖浣垮緱瀹冧滑闈炲父蹇紝骞朵笖瀵瑰垱寤轰复鏃惰〃闈炲父鏈夌敤銆傚彲鏄紝褰撴湇鍔″櫒鍏抽棴涔嬫椂锛屾墍鏈夊瓨鍌ㄥ湪MEMORY琛ㄩ噷鐨勬暟鎹涓㈠け銆傚洜涓鸿〃鐨勫畾涔夎瀛樺湪纾佺洏涓婄殑.frm鏂囦欢涓紝鎵浠ヨ〃鑷韩缁х画瀛樺湪锛屽湪鏈嶅姟鍣ㄩ噸鍚姩鏃跺畠浠槸绌虹殑銆

杩欎釜渚嬪瓙鏄剧ず浣犲浣曞彲浠ュ垱寤猴紝浣跨敤骞跺垹闄や竴涓狹EMORY琛細

mysql> CREATE TABLE test ENGINE=MEMORY
    ->     SELECT ip,SUM(downloads) AS down
    ->     FROM log_table GROUP BY ip;
mysql> SELECT COUNT(ip),AVG(down) FROM test;
mysql> DROP TABLE test;

MEMORY琛ㄦ湁涓嬪垪鐗瑰緛锛

·         缁橫EMORY琛ㄧ殑绌洪棿琚互灏忓潡鏉ュ垎閰嶃傝〃瀵规彃鍏ヤ娇鐢100%鍔ㄦ佸搱甯屾潵銆備笉闇瑕佹孩鍑哄尯鎴栭澶栭敭绌洪棿銆傝嚜鐢卞垪琛ㄦ棤棰濆鐨勭┖闂撮渶姹傘傚凡鍒犻櫎鐨勮琚斁鍦ㄤ竴涓互閾炬帴鐨勫垪琛ㄩ噷锛屽苟涓斿湪浣犲線琛ㄩ噷鎻掑叆鏂版暟鎹箣鏃惰閲嶆柊浣跨敤銆侻EMORY琛ㄤ篃娌℃湁閫氬父涓庡湪鍝堝笇琛ㄤ腑鍒犻櫎鍔犳彃鍏ョ浉鍏崇殑闂銆

·         MEMORY琛ㄥ彲浠ユ湁澶氳揪姣忎釜琛32涓储寮曪紝姣忎釜绱㈠紩16鍒楋紝浠ュ強500瀛楄妭鐨勬渶澶 閿暱搴︺

·         MEMORY瀛樺偍寮曟搸鎵цHASH鍜孊TREE绱㈠紩銆備綘鍙互閫氳繃娣诲姞涓涓涓嬫墍绀虹殑USING瀛愬彞涓虹粰瀹氱殑绱㈠紩鎸囧畾涓涓垨鍙︿竴涓細

·                CREATE TABLE lookup
·                    (id INT, INDEX USING HASH (id))
·                    ENGINE = MEMORY;
·                CREATE TABLE lookup
·                    (id INT, INDEX USING BTREE (id))
·                    ENGINE = MEMORY;

B鏍戠殑涓鑸壒寰佸拰鍝堝笇绱㈠紩鍦7.4.5鑺傦紝鈥淢ySQL濡備綍浣跨敤绱㈠紩”閲屾弿杩般

·         浣犲彲浠ュ湪涓涓狹EMORY琛ㄤ腑鏈夐潪鍞竴閿傦紙瀵瑰搱甯岀储寮曠殑瀹炵幇锛岃繖鏄竴涓笉甯哥敤鐨勫姛鑳斤級銆

·         浣犻〉鍙互瀵筂EMORY琛ㄤ娇鐢↖NSERT DELAYED銆傝鍙傞槄13.2.4.2鑺傦紝鈥淚NSERT DELAYED璇硶”

·         濡傛灉浣犲湪涓涓湁楂樺害閿噸澶嶇殑锛堣澶氱储寮曟潯鐩寘鍚悓涓涓硷級MEMORY琛ㄤ笂鏈変竴涓搱甯岀储寮曪紝瀵瑰奖鍝嶉敭鍊肩殑琛ㄧ殑鏇存柊鍙婃墍鏈夊垹闄ら兘鏄槑鏄惧湴鎱㈢殑銆傝繖涓彉鎱㈢殑绋嬪害姣斾緥浜庨噸澶嶇殑绋嬪害锛堟垨鑰呭弽姣斾簬绱㈠紩cardinality锛夈備綘鍙互浣跨敤涓涓狟鏍戠储寮曟潵閬垮厤杩欎釜闂銆

·         MEMORY琛ㄤ娇鐢ㄤ竴涓浐瀹氱殑璁板綍闀垮害鏍煎紡銆

·         MEMORY涓嶆敮鎸BLOB鎴朤EXT鍒椼

·         MEMORY鏀寔AUTO_INCREMENT鍒楀拰瀵瑰彲鍖呭惈NULL鍊肩殑 鍒楃殑绱㈠紩銆

·         MEMORY琛ㄥ湪鎵鏈夊鎴风涔嬮棿鍏变韩锛堝氨鍍忓叾瀹冧换浣曢潪TEMPORARY琛級銆

·         MEMORY琛ㄥ唴瀹硅瀛樺湪鍐呭瓨涓紝鍐呭瓨鏄疢EMORY琛ㄥ拰鏈嶅姟鍣ㄥ湪鏌ヨ澶勭悊涔嬫椂鐨勭┖闂蹭腑鍒涘缓鐨勫唴閮ㄨ〃鍏变韩銆傚彲鏄紝涓や釜绫诲瀷鐨勮〃涓嶅悓鍦ㄤ簬MEMORY琛ㄤ笉浼氶亣鍒板瓨鍌ㄨ浆鎹紝鑰屽唴閮ㄨ〃鏄細

-        濡傛灉涓涓唴閮ㄨ〃鍙樺緱澶ぇ锛屾湇鍔″櫒鑷姩鎶婂畠杞崲涓轰竴涓鐩樿〃銆傚昂瀵搁檺鍒剁敱tmp_table_size绯荤粺鍙橀噺鐨勫兼潵纭畾銆

-        MEMORY琛ㄥ喅涓嶄細杞崲鎴愮鐩樿〃銆傝纭繚浣犱笉浼氬伓灏斿仛鐐瑰偦浜嬶紝浣犲彲浠ヨ缃max_heap_table_size绯荤粺鍙橀噺缁橫EMORY琛ㄥ姞浠ユ渶澶у昂瀵搞傚浜庡崟涓殑琛紝浣犱篃鍙互鍦–REATE TABLE璇彞涓寚瀹氫竴涓狹AX_ROWS琛ㄩ夐」銆

·         鏈嶅姟鍣ㄩ渶瑕佽冻澶熷唴瀛樻潵缁存寔鎵鏈夊湪鍚屼竴鏃堕棿浣跨敤鐨凪EMORY琛ㄣ

·         褰撲綘涓嶅啀闇瑕丮EMORY琛ㄧ殑鍐呭涔嬫椂锛岃閲婃斁琚玀EMORY琛ㄤ娇鐢ㄧ殑鍐呭瓨锛屼綘搴旇鎵цDELETE FROM鎴朤RUNCATE TABLE锛屾垨鑰呮暣涓湴鍒犻櫎琛紙浣跨敤DROP TABLE锛夈

·         褰揗ySQL鏈嶅姟鍣ㄥ惎鍔ㄦ椂锛屽鏋滀綘鎯冲紑鎷揗EMORY琛紝浣犲彲浠ヤ娇鐢--init-file閫夐」銆備緥濡傦紝浣犲彲浠ユ妸INSERT INTO ... SELECT 鎴朙OAD DATA INFILE杩欐牱鐨勮鍙ユ斁鍏ヨ繖涓枃浠朵腑浠ヤ究浠庢寔涔呯ǔ鍥虹殑鐨勬暟鎹簮瑁呰浇琛ㄣ傝鍙傞槄5.3.1鑺傦紝鈥mysqld 鍛戒护琛岄夐」” and 13.2.5鑺傦紝鈥淟OAD DATA INFILE 璇硶”

·         濡傛灉浣犳浣跨敤澶嶅埗锛屽綋涓绘湇鍔″櫒琚叧闂笖閲嶅惎鍔ㄤ箣鏃讹紝涓绘湇鍔″櫒鐨凪EMORY琛ㄥ彉绌恒傚彲鏄粠鏈嶅姟鍣ㄦ剰璇嗕笉鍒拌繖浜涜〃宸茬粡鍙樼┖锛屾墍浠ュ鏋滀綘浠庡畠浠夋嫨鏁版嵁锛屽畠灏辫繑鍥炶繃鏃剁殑鍐呭銆傝嚜浠庢湇鍔″櫒鍚姩鍚庯紝褰撲竴涓狹EMORY琛ㄥ湪涓绘湇鍔″櫒涓婄涓娆¤浣跨敤涔嬫椂锛屼竴涓狣ELETE FROM璇彞琚嚜鍔ㄥ啓杩涗富鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇楋紝鍥犳鍐嶆璁╀粠鏈嶅姟鍣ㄤ笌涓绘湇鍔″櫒鍚屾銆傛敞鎰忥紝鍗充娇浣跨敤杩欎釜绛栫暐锛屽湪涓绘湇鍔″櫒鐨勯噸鍚拰瀹冪涓娆′娇鐢ㄨ琛ㄤ箣闂寸殑闂撮殧涓紝浠庢湇鍔″櫒浠嶆棫鍦ㄨ〃涓湁杩囨椂鏁版嵁銆傚彲鏄紝濡傛灉浣犱娇鐢--init-file閫夐」浜庝富鏈嶅姟鍣ㄥ惎鍔ㄤ箣鏃跺湪鍏朵笂鎺ㄨMEMORY琛ㄣ傚畠纭繚杩欎釜鏃堕棿闂撮殧涓洪浂銆

·         鍦∕EMORY琛ㄤ腑锛屼竴琛岄渶瑕佺殑鍐呭瓨浣跨敤涓嬪垪琛ㄨ揪寮忔潵璁$畻锛

·                SUM_OVER_ALL_BTREE_KEYS(max_length_of_key + sizeof(char*) * 4)
·                + SUM_OVER_ALL_HASH_KEYS(sizeof(char*) * 2)
·                + ALIGN(length_of_row+1, sizeof(char*))

ALIGN()浠h〃round-up鍥犲瓙锛屽畠浣垮緱琛岀殑闀垮害涓篶har鎸囬拡澶у皬鐨勭‘鍒囧嶆暟銆俿izeof(char*)鍦32浣嶆満鍣ㄤ笂鏄4锛屽湪64浣嶆満鍣ㄤ笂鏄8銆

瀵逛簬MEMORY瀛樺偍寮曟搸锛屽湪http://forums.mysql.com/list.php?92涓婃湁涓涓笓闂ㄧ殑璁哄潧銆

15.5. BDB (BerkeleyDB)瀛樺偍寮曟搸

Sleepycat Software缁橫ySQL鎻愪緵Berkeley DB浜嬪姟鎬у瓨鍌ㄥ紩鎿庛傝繖涓瓨鍌ㄥ紩鎿庡吀鍨嬭绠绉颁负BDB銆傚BDB瀛樺偍寮曟搸鐨勬敮鎸佸寘鎷湪MySQL婧愮爜鍒嗗彂鐗閲岋紝鍦∕ySQL-Max浜岃繘鍒鍒嗗彂鐗閲岃婵娲汇

BDB琛ㄥ彲鑳芥湁涓涓洿澶х殑宕╂簝骞稿瓨鏈轰細锛屽苟涓斾篃鍏锋湁瀵逛簨鍔OMMIT鍜孯OLLBACK鎿嶄綔鐨勮兘鍔涖侻ySQL婧愮爜鍒嗗彂鐗鍜岃琛ヤ竵杩囧彲浠ヤ笌MySQL涓璧峰伐浣滅殑BDB鍒嗗彂鐗涓璧锋彁渚涖備綘涓嶈兘浣跨敤涓涓湭琛ヤ竵杩囩殑BDB鐗堟湰涓嶮ySQL涓璧峰伐浣溿傘

鎴戜滑鍦∕ySQL AB涓婁笌Sleepycat绱у瘑鍚堜綔宸ヤ綔浠ヤ繚鎸丮ySQL/BDB鎺ュ彛鐨勮川閲忓湪楂樻按骞炽傦紙鍗充娇Berkeley DB鍏舵湰韬槸闈炲父鑳界粡鍙楄冮獙鍜岄潪甯稿彲闈犵殑銆侻ySQL鎺ュ彛浠嶇劧琚涓烘槸绗笁绛夎川閲忕殑銆傛垜浠皢缁х画鏀瑰杽鍜屼紭鍖栧畠锛夈

褰撳畠杈惧埌瀵规墍鏈夋秹鍙夿DB琛ㄧ殑闂鐨勬敮鎸佷箣鏃讹紝鎴戜滑绛斿簲璐熻矗甯姪鎴戜滑鐨勭敤鎴峰畾浣嶉棶棰樺苟鍒涘缓鍙噸澶嶄骇鐢熺殑娴嬭瘯妗堜緥銆備换浣曡繖鏍风殑娴嬭瘯妗堜緥琚浆浜ょ粰Sleepycat锛屽畠鍙嶈繃鏉ュ府鍔╂垜浠壘鍒板苟瑙e喅闂銆傚洜涓鸿繖鏄竴涓簩闃舵鐨勬搷浣滐紝浠讳綍鍏充簬BDB琛ㄧ殑闂鎴戜滑鍙兘瑕佽姳姣斿鍏跺畠瀛樺偍寮曟搸绋嶅井鏇撮暱涓鐐圭殑鏃堕棿鏉ヨВ鍐冲畠銆傚彲鏄紝鎴戜滑鏈熸湜杩欎釜杩囩▼娌℃湁鏄捐憲鐨勫洶闅撅紝鍥犱负Berkeley DB 浠g爜鏈韩琚敤鍦∕ySQL涔嬪璁稿鐨勫簲鐢ㄤ腑銆

瑕佽幏寰楀叧浜嶣erkeley DB鐨勪竴鑸俊鎭紝璇疯闂甋leepycat缃戠珯锛http://www.sleepycat.com/

15.5.1BDB鏀寔鐨勬搷浣滅郴缁

褰撳墠锛屾垜浠煡閬揃DB瀛樺偍寮曟搸鏀寔涓嬪垪鎿嶄綔绯荤粺锛

·         Linux 2.x Intel

·         Sun Solaris (SPARC and x86)

·         FreeBSD 4.x/5.x (x86, sparc64)

·         IBM AIX 4.3.x

·         SCO OpenServer

·         SCO UnixWare 7.1.x

·         Windows NT/2000/XP

BDB涓嶆敮鎸佷笅鍒楁搷浣滅郴缁燂細

·         Linux 2.x Alpha

·         Linux 2.x AMD64

·         Linux 2.x IA-64

·         Linux 2.x s390

·         Mac OS X

娉ㄩ噴锛鍓嶄竴涓垪琛ㄨ繕涓嶅畬鍏紝鎴戜滑鏀跺埌鏇村淇℃伅鏃舵垜浠細鏇存柊瀹冦

濡傛灉浣犱粠鏀寔BDB琛ㄧ殑婧愮爜寤虹珛鐨凪ySQL锛屼絾鏄紝褰撲綘鍚姩mysqld涔嬫椂锛屽彂鐢熶笅鍒楅敊璇紝杩欐剰鍛崇潃瀵逛綘鐨勬灦鏋凚DB涓嶈鏀寔锛

bdb: architecture lacks fast mutexes: applications cannot be threaded
Can't init databases

鍦ㄨ繖绉嶆儏鍐典笅锛屼綘蹇呴』閲嶅缓MySQL涓嶅甫BDB琛ㄦ敮鎸佹垨鐢--skip-bdb閫夐」鍚姩鏈嶅姟鍣ㄣ

15.5.2. 瀹夎BDB

濡傛灉浣犲凡缁忎笅杞戒竴涓寘鎷Berkeley DB鏀寔鐨勭殑浜岃繘鍒剁増鐨凪ySQL锛 鍙绠鍗曞湴鎸夌収閫氬父鐨勪簩杩涘埗鍒嗗彂鐗瀹夎鎸囦护銆傦紙MySQL-Max 鍒嗗彂鐗堝寘鎷珺DB鏀寔锛夈

濡傛灉浣犱粠婧愮爜寤虹珛MySQL锛屼綘鍙互鍦ㄥ叾瀹冧换浣曞父鐢ㄩ夐」涔嬪鐢--with-berkeley-db閫夐」杩愯configure鏉ュ厑璁告敮鎸丅DB銆備笅杞戒竴涓狹ySQL 5.1鍒嗗彂鐗堬紝鏀瑰彉浣嶇疆鍒板畠鐨勬渶椤跺眰鐩綍锛岀劧鍚庤繍琛屼笅闈㈠懡浠わ細

shell> ./configure --with-berkeley-db [other-options]

鏇村淇℃伅锛岃鍙傞槄2.7鑺傦紝鈥滃湪鍏跺畠绫讳技Unix绯荤粺涓婂畨瑁匨ySQL”, 5.1.2鑺傦紝鈥mysqld-max鎵╁睍MySQL鏈嶅姟鍣”, 鍜2.8鑺傦紝鈥滀娇鐢ㄦ簮鐮佸垎鍙戠増鐗堝畨瑁匨ySQL”

15.5.3BDB鍚姩閫夐」

涓嬪垪瀵mysqld鐨勯夐」鍙鐢ㄦ潵鏀瑰彉BDB瀛樺偍寮曟搸鐨勮涓猴細

·         --bdb-home=path

BDB琛ㄧ殑鍩虹鐩綍銆傝繖搴旇鍜屼綘涓--datadir浣跨敤鐨勭洰褰曠浉鍚屻

·         --bdb-lock-detect=method

BDB 閿佸畾妫娴嬫柟寮忋傞夐」鍊煎簲璇ヤ负DEFAULT, OLDEST, RANDOM鎴朰OUNGEST銆

·         --bdb-logdir=path

BDB鏃ュ織鏂囦欢鐩綍銆

·         --bdb-no-recover

涓嶅湪鎭㈠妯″紡鍚姩Berkeley DB銆

·         --bdb-no-sync

涓嶅悓姝ュ埛鏂癇DB鏃ュ織銆傝繖涓夐」涓嶈璧炴垚锛屽彇鑰屼唬涔嬪湴浣跨敤--skip-sync-bdb-logs锛堣鍙傞槄瀵--sync-bdb-logs鐨勬弿杩)銆

·         --bdb-shared-data

浠ュ澶勭悊妯″紡鍚姩Berkeley DB銆傦紙鍒濆鍖朆erkeley DB涔嬫椂锛屼笉瑕佷娇鐢―B_PRIVATE锛夈

·         --bdb-tmpdir=path

BDB涓存椂鏂囦欢鐩綍銆

·         --skip-bdb

绂佹BDB瀛樺偍寮曟搸銆

·         --sync-bdb-logs

鍚屾鍒锋柊BDB鏃ュ織銆傝繖涓夐」榛樿琚厑璁革紝璇蜂娇鐢--skip-sync-bdb-logs鏉ョ姝㈠畠銆

璇峰弬闃5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」”

濡傛灉浣犱娇鐢--skip-bdb閫夐」锛孧ySQL涓嶅垵濮嬪寲Berkeley DB搴擄紝鑰屼笖杩欐牱鑺傜渷澶ч噺鐨勫唴瀛樸傚敖绠″姝わ紝濡傛灉浣犱娇鐢ㄨ繖涓夐」锛屼綘涓嶈兘浣跨敤BDB琛ㄣ傚鏋滀綘璇曠潃鍒涘缓涓涓狟DB琛紝MySQL鍙栬屼唬涔嬪湴鍒涘缓涓涓狹yISAM銆

閫氬父锛屽鏋滀綘璞′娇鐢˙DB琛紝浣犲簲璇ヤ笉鐢--bdb-no-recover閫夐」鍚姩mysqld銆傚彲鏄紝濡傛灉BDB鏃ュ織琚牬鍧忓垯褰撲綘璇曠潃鍚姩mysqld鏃讹紝涓婅堪鍔炴硶鍚姩鏈嶅姟鍣ㄥ彲鑳藉鑷撮棶棰樸傝鍙傞槄2.9.2.3鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄧ殑鍚姩鍜屾晠闅滆瘖鏂帓闄”

浣跨敤bdb_max_lock 鍙橀噺锛屼綘鍙互鎸囧畾鍦˙DB琛ㄤ笂琚縺娲荤殑閿佸畾鐨勬渶澶ф暟鐩傞粯璁ゅ兼槸10,000銆傚綋浣犳墽琛岄暱浜嬪姟鎴栧綋mysqld涓嶅緱涓嶆鏌ヨ澶氳鏉ユ墽琛屼竴涓煡璇箣鏃讹紝濡傛灉鍙戠敓濡備笅閿欒锛屼綘搴旇澧炲姞杩欎釜鏁扮洰锛

bdb: Lock table is out of available locks
Got error 12 from ...

濡傛灉浣犳浣跨敤澶у瀷澶氳鍙ヤ簨鍔★紝浣犲彲鑳戒篃鎯虫敼鍙榖inlog_cache_size鍜宮ax_binlog_cache_size鍙橀噺銆傝鍙傞槄5.11.3鑺傦紝鈥滀簩杩涘埗鏃ュ織”

涔熻鍙傞槄5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺”

15.5.4BDB琛ㄧ殑鐗瑰緛

姣忎釜BDB琛ㄧ敤涓や釜鏂囦欢琚瓨鍦ㄧ鐩樹笂銆傛枃浠剁殑鍚嶅瓧鐢ㄨ〃鐨勫悕瀛楀仛寮澶达紝骞朵笖鏈変竴涓墿灞曞悕鏉ユ寚鏄庢枃浠剁被鍨嬨備竴涓.frm鏂囦欢瀛樺偍琛ㄥ畾涔夛紝涓涓.db鏂囦欢鍖呭惈琛ㄦ暟鎹拰绱㈠紩銆

瑕佹槑纭寚鍑轰綘鎯宠涓涓狟DB琛紝鐢‥NGINE鎴朤YPE琛ㄩ夐」鏉ユ寚鏄庯細

CREATE TABLE t (i INT) ENGINE = BDB;
CREATE TABLE t (i INT) TYPE = BDB;

BerkeleyDB鏄敤ENGINE鎴栬匱YPE閫夐」鐨凚DB瀛樺偍寮曟搸鐨“鍚屼箟璇”銆

BDB瀛樺偍寮曟搸鎻愪緵浜嬪姟鎬ц〃锛屼綘浣跨敤杩欎簺琛ㄧ殑鏂规硶鍙栧喅浜巃utocommit妯″紡锛

·         濡傛灉浣犳杩愯鐫锛屽悓鏃堕殢鐫autocommit鐨勮鍏佽锛堣繖鏄粯璁ょ殑锛夛紝瀵笲DB琛ㄧ殑鏀瑰彉琚珛鍗虫彁浜ゅ苟涓斾笉鑳借 鍥炴粴銆

·         濡傛灉浣犳杩愯鐫锛屽悓鏃堕殢鐫autocommit鐨勮绂佹锛屾敼鍙樹笉鍙樻垚姘镐箙鐨勭洿鍒颁綘鎵ц涓涓狢OMMIT璇彞銆備綔涓烘彁浜ょ殑鏇夸唬锛屼綘鍙互鎵цROLLBACK鏉ュ繕璁版敼鍙樸

浣犲彲浠ョ敤BEGIN WORK璇彞寮濮嬩竴涓簨鍔℃潵鎸傝捣autocommit锛屾垨鑰呯敤SET AUTOCOMMIT=0鏉ユ槑纭姝utocommit銆

璇峰弬闃13.4.1鑺傦紝鈥淪TART TRANSACTION, COMMIT鍜孯OLLBACK璇硶”

BDB瀛樺偍寮曟搸鏈変笅鍒楃壒寰侊細

·         BDB琛ㄥ彲浠ユ湁澶氳揪姣忚〃31涓储寮曪紝姣忎釜绱㈠紩16鍒楋紝骞朵笖1024瀛楄妭鐨勬渶澶 閿昂瀵搞

·         MySQL鍦ㄦ瘡涓狟DB琛ㄤ腑闇瑕佷竴涓狿RIMARY KEY浠ヤ究姣忎竴琛屽彲浠ヨ鍞竴鍦拌瘑鍒傚鏋滀綘涓嶆槑纭垱寤轰竴涓紝MySQL涓轰綘鍒涘缓骞剁淮鎸佷竴涓殣钘忕殑PRIMARY KEY銆傞殣钘忕殑 閿湁涓涓5瀛楄妭鐨勯暱搴︼紝骞朵笖涓烘瘡涓彃鍏ョ殑浼佸浘鑰岃澧炲姞銆傝繖涓敭涓嶅嚭鐜板湪SHOW CREATE TABLE鎴朌ESCRIBE鐨勮緭鍑轰箣涓

·         PRIMARY KEY姣斾换浣曞叾瀹冪储寮曢兘瑕佸揩锛屽洜涓篜RIMARY KEY琚笌琛岀殑鏁版嵁涓璧峰瓨鍌ㄣ傚叾瀹冪储寮曡瀛樺偍涓洪敭鏁版嵁锛婸RIMARY KEY锛屾墍浠ヤ繚鎸丳RIMARY KEY灏藉彲鑳藉湴鐭互鑺傜害纾佺洏绌洪棿骞惰幏寰楁洿濂介熷害鏄噸瑕佺殑銆

杩欎釜琛屼负绫讳技浜嶪nnoDB鐨勶紝鍦ㄥ叾涓緝鐭殑primary keys涓嶄粎鍦ㄤ富绱㈠紩涔熷湪绗簩绱㈠紩鑺傜害绌洪棿 銆

·         濡傛灉鍦˙DB琛ㄤ腑锛屼綘璁块棶鐨勬墍鏈夊垪鏄悓涓绱㈠紩鐨勪竴閮ㄥ垎鎴栦富閿殑涓閮ㄥ垎锛孧ySQL鍙互鎵ц鏌ヨ鑰屼笉璁块棶纭疄鐨勮銆傚湪涓涓狹yISAM琛ㄤ腑锛屽彧鏈 鍒楁槸鍚屼竴绱㈠紩鐨勪竴閮ㄥ垎涔嬫椂锛屾墠鍙互杩欎箞鍋氥

·         杩炵画鎵弿姣斿MyISAM琛ㄧ殑鎵弿鏇存參锛屽洜涓哄湪BDB琛ㄤ腑鐨勬暟鎹瀛樺偍鍦˙鏍戣屼笉鏄湪鍒嗙鐨勬暟鎹枃浠朵腑銆

·        閿鍊艰薄MyISAM琛ㄤ腑鐨 閿间竴鏍蜂笉鏄墠缂鎴栧悗缂鍘嬬缉鐨勩傛崲鍙ヨ瘽璇达紝鍦˙DB琛ㄤ腑閿俊鎭彧姣斿湪MyISAM琛ㄤ腑绋嶅井澶氬崰鎹竴鐐圭┖闂淬

·         鍦˙DB琛ㄤ腑缁忓父鏈夋礊鍏佽浣犲湪绱㈠紩鏍戠殑涓棿鎻掑叆鏂拌銆傝繖涓娇寰桞DB琛ㄦ瘮MyISAM琛ㄧ◢寰ぇ涓浜

·         SELECT COUNT(*) FROM tbl_name瀵笲DB琛ㄥ緢鎱紝鍥犱负鍦ㄨ琛ㄤ腑娌℃湁琛岃鏁拌缁存寔銆

·         浼樺寲鍣ㄩ渶瑕佺煡閬撹〃涓殑澶ф琛屾暟銆侻ySQL閫氳繃璁℃暟鎻掑叆浠ュ強鍦ㄦ瘡涓狟DB琛ㄤ腑鐨勫崟鐙墖鏂唴缁存寔瀹冩潵瑙e喅浜嗛棶棰樸傚鏋滀綘涓嶅彂鍑哄ぇ閲忕殑DELETE鎴朢OLLBACK璇彞锛岃繖涓暟瀵筂ySQL浼樺寲鍣ㄦ潵璇存槸瓒冲绮剧‘浜嗐傚彲鏄紝MySQL浠呭湪鍏抽棴鐨勬椂鍊欐墠瀛樺偍杩欎釜鏁帮紝鎵浠ワ紝濡傛灉鏈嶅姟鍣ㄨ鎰忓鍦扮粓姝紝杩欎釜鏁板彲鑳芥槸涓嶆纭殑銆傚嵆浣挎暟涓嶆槸100%姝g‘锛屽畠涓嶆槸 鏄庣‘鐨勩備綘鍙互浣跨敤ANALYZE TABLE鎴栬 OPTIMIZE TABLE璇彞鏉ユ洿鏂拌璁℃暟銆傝鍙傞槄13.5.2.1鑺傦紝鈥淎NALYZE TABLE璇硶”13.5.2.5鑺傦紝鈥淥PTIMIZE TABLE璇硶”

·         BDB琛ㄤ笂鐨勫唴閮ㄩ攣瀹氬湪椤甸潰绾у埆涓婂仛銆

·         LOCK TABLES鍦˙DB琛ㄤ笂灏辨兂鍦ㄥ叾瀹冭〃涓婁竴鏍峰伐浣溿傚鏋滀綘涓嶄娇鐢↙OCK TABLES锛孧ySQL瀵硅琛ㄥ彂鍑轰竴涓唴閮ㄥ閲嶅啓閿佸畾 锛堜竴涓笉闃绘鍏跺畠浣滆呯殑閿佸畾锛夋潵纭繚鍗充娇鍙︿竴涓嚎绋嬪彂鍑轰竴涓〃閿佸畾锛岃琛ㄤ篃琚伆褰撳湴閿佸畾浜嗐

·         瑕佽兘澶熷洖婊氫竴涓簨鍔★紝BDB瀛樺偍寮曟搸缁存姢鏃ュ織鏂囦欢銆傚瀹炵幇鏈楂樻ц兘锛屼綘鍙互浣跨敤--bdb-logdir閫夐」鏉ユ妸BDB鏃ュ織鏀惧湪涓嶅悓鐨勭鐩樹笂锛岃屼笉鏄斁鍦ㄦ暟鎹簱琚斁缃殑閭d釜纾佺洏銆

·         姣忔鏂癇DB鏃ュ織鏂囦欢琚惎鍔ㄤ箣鏃讹紝MySQL鎵ц涓涓鏌ョ偣锛屽苟涓斿垹鎺夊綋鍓嶄簨鍔′笉闇瑕佺殑浠讳綍BDB鏃ュ織鏂囦欢銆備綘涔熷彲浠ュ湪浠讳綍鏃跺欎娇鐢‵LUSH LOGS鏉ョ粰Berkeley DB琛ㄨ缃鏌ョ偣銆

瀵圭伨闅炬仮澶嶏紝浣犲簲璇ヤ娇鐢ㄨ〃澶囦唤鍔燤ySQL鐨勪簩杩涘埗鏃ュ織锛岃鍙傞槄5.9.1鑺傦紝鈥滄暟鎹簱澶囦唤”

璀﹀憡锛濡傛灉浣犲垹闄や粛鍦ㄤ娇鐢ㄤ腑鐨勬棫鏃ュ織鏂囦欢锛孊DB鏍规湰涓嶈兘鍋氭仮澶嶏紝骞朵笖濡傛灉鏈変簺浜嬩笉瀵癸紝浣犲彲鑳戒細涓㈠け鏁版嵁銆

·         搴旂敤绋嬪簭蹇呴』鎬绘槸琚噯澶囨潵澶勭悊鎯呭喌锛屽嵆BDB琛ㄤ换浣曠殑鏀瑰彉鍙兘瀵艰嚧涓涓嚜鍔ㄥ洖婊氬苟涓斾换浣曡鍙兘浼氬甫鐫涓涓閿侀敊璇屽け璐ャ

·         濡傛灉浣犲湪BDB琛ㄥ唴閬囧埌纾佺洏婊★紝浣犲緱鍒颁竴涓敊璇紙鍙兘鏄敊璇28锛夛紝骞朵笖浜嬪姟搴旇鍥炴粴銆傝繖涓嶮yISAM琛ㄧ浉鍙嶏紝瀵逛簬MyISAM 琛紝mysqld鍦ㄧ户缁箣鍓嶇瓑寰呰冻澶熺殑鑷敱纾佺洏绌洪棿銆

15.5.5. 淇敼BDB鎵闇鐨 浜嬪疁

·         鍚屾椂鎵撳紑璁稿BDB琛ㄥ彲鑳芥槸闈炲父鎱㈢殑銆傚鏋滀綘瑕佷娇鐢˙DB琛紝浣犱笉搴斾娇鐢ㄩ潪甯稿ぇ鐨勮〃缂撳瓨锛堜緥濡傦紝澶т簬256锛夊苟涓斿綋浣犱娇鐢mysql瀹㈡埛绔箣鏃讹紝浣犲簲璇ヤ娇鐢--no-auto-rehash閫夐」銆

·         SHOW TABLE STATUS 涓嶄负BDB琛ㄦ彁渚涗竴浜涗俊鎭細

·                mysql> SHOW TABLE STATUS LIKE 'bdbtest'\G
·                *************************** 1. row ***************************
·                           Name: bdbtest
·                         Engine: BerkeleyDB
·                        Version: 10
·                     Row_format: Dynamic
·                           Rows: 154
·                 Avg_row_length: 0
·                    Data_length: 0
·                Max_data_length: 0
·                   Index_length: 0
·                      Data_free: 0
·                 Auto_increment: NULL
·                    Create_time: NULL
·                    Update_time: NULL
·                     Check_time: NULL
·                      Collation: latin1_swedish_ci
·                       Checksum: NULL
·                 Create_options:
·                        Comment:

·         浼樺寲鎬ц兘銆

·         鏀瑰彉涓哄琛ㄦ壂鎻忔搷浣滀娇鐢ㄦ棤椤甸潰閿佸畾銆

15.5.6瀵笲DB琛ㄧ殑闄愬埗

涓嬪垪琛ㄨ鏄庝娇鐢˙DB琛ㄤ箣鏃朵綘蹇呴』瑕侀伒浠庣殑闄愬埗锛

·         姣忎釜BDB琛ㄥ湪.db鏂囦欢閲屽瓨鍌ㄦ枃浠惰鍒涘缓涔嬫椂鍒拌鏂囦欢鐨勮矾寰勩傝繖涓鍋氭潵鍏佽鍦ㄦ敮鎸乻ymlinks鐨勫鐢ㄦ埛鐜閲屾娴嬮攣瀹氥傚洜姝わ紝浠庝竴涓暟鎹簱鐩綍寰鍙︿竴涓洰褰曠Щ鍔˙DB琛ㄦ槸涓嶈兘鐨勩

·         褰撳埗浣淏DB琛ㄧ殑澶囦唤涔嬫椂锛屼綘蹇呴』瑕佷箞浣跨敤mysqldump瑕佷箞鍋氫竴涓寘鍚姣忎釜BDB琛ㄧ殑鏂囦欢锛.frm鍜.db鏂囦欢锛夊強BDB鏃ュ織鏂囦欢鐨勫浠姐侭DB瀛樺偍寮曟搸鍦ㄥ畠鐨勬棩蹇楁枃浠跺瓨鍌ㄦ湭瀹屾垚鐨勪簨鍔′互鍙婅姹傚畠浠湪mysqld鍚姩鐨勬椂鍊欒鍛堝嚭鏉ャ侭DB鏃ュ織鍦ㄦ暟鎹洰褰曢噷锛屽叿鏈塴og.XXXXXXXXXX锛10浣嶆暟瀛楋級褰㈠紡鍚嶅瓧鐨勬枃浠躲

·         濡傛灉鍏佽NULL鍊肩殑鍒楁湁鍞竴鐨勭储寮曪紝鍙湁鍗曚釜NULL鍊兼槸琚厑璁哥殑銆傝繖涓嶅悓浜庡叾瀹冨瓨鍌ㄥ紩鎿庛

15.5.7. 浣跨敤BDB琛ㄦ椂鍙兘鍙戠敓鐨勯敊璇

·         濡傛灉浣犲崌绾т箣鍚庡惎鍔mysqld鏃跺彂鐢熶笅鍒楅敊璇紝瀹冩剰鍛崇潃鏂癇DB鐗堟湰涓嶆敮鎸佹棫鏃ュ織鏂囦欢鏍煎紡锛

·                bdb:  Ignoring log file: .../log.XXXXXXXXXX:
·                unsupported log version #

鍦ㄨ繖绉嶆儏鍐典笅锛屼綘蹇呴』鍒犻櫎浠庢暟鎹洰褰曟墍鏈塀DB鏃ュ織锛堝悕瀛椾负log.XXXXXXXXXX杩欐牱鏍煎紡鐨勬枃浠讹級骞堕噸鏂板惎鍔mysqld銆傛垜浠篃鎺ㄨ崘浣犻殢鍚庣敤mysqldump --opt鏉ヨ浆鍌ㄤ綘鐨凚DB琛紝绉婚櫎琛紝骞朵笖浠庤浆鍌ㄦ枃浠舵仮澶嶅畠浠

·         濡傛灉autocommit妯″紡琚姝紝浣犵Щ闄ゅ湪鍙︿竴涓簨鍔′腑琚弬鑰冪殑BDB琛紝浣犱細浠庝綘鐨凪ySQL閿欒鏃ュ織寰楀埌濡備笅鐨勯敊璇俊鎭細

·                001119 23:43:56  bdb:  Missing log fileid entry
·                001119 23:43:56  bdb:  txn_abort: Log undo failed for LSN:
·                                       1 3644744: Invalid

杩欎笉鏄竴涓嚧鍛介敊璇紝浣嗘槸鐭ラ亾闂琚В鍐筹紝鎴戜滑鎺ㄨ崘浣犱笉瑕佺Щ闄DB琛紝闄や簡autocommit妯″紡琚厑璁镐箣鏃躲傦紙淇涓嶆槸寰笉瓒抽亾鐨勶級銆

15.6. EXAMPLE瀛樺偍寮曟搸

EXAMPLE瀛樺偍寮曟搸鏄竴涓笉鍋氫换浣曚簨鎯呯殑瀛樻牴寮曟搸銆傚畠鐨勭洰鐨勬槸浣滀负MySQL婧愪唬鐮佷腑鐨勪竴涓緥瀛愶紝鐢ㄦ潵婕旂ず濡備綍寮濮嬬紪鍐欎竴涓柊瀛樺偍寮曟搸銆 鍚屾牱锛屽畠鐨勪富瑕佸叴瓒f槸瀵瑰紑鍙戣呫

瑕佸EXAMPLE寮曟搸妫鏌ユ簮 鐮侊紝璇锋煡鐪婱ySQL婧愮爜鍒嗗彂鐗堢殑sql/examples鐩綍銆

瑕佸厑璁歌繖涓瓨鍌ㄥ紩鎿庯紝浣跨敤鍦ㄥ缓绔婱ySQL涔嬫椂浣跨敤--with-example-storage-engine閫夐」鏉configure

褰撲綘鍒涘缓涓涓狤XAMPLE涔嬫椂锛屾湇鍔″櫒鍒涘缓鍦ㄦ暟鎹簱鐩綍鍒涘缓涓涓〃瀹氫箟鏂囦欢銆傛枃浠剁敱琛ㄥ悕瀛楀紑濮嬶紝骞剁敱涓涓.frm鐨勬墿灞曞悕銆傛病鏈夊叾瀹冩枃浠惰鍒涘缓銆傛病鏈夋暟鎹瀛樺偍杩涜〃鎴栬呬粠涓彇鍥炪

mysql> CREATE TABLE test (i INT) ENGINE = EXAMPLE;
Query OK, 0 rows affected (0.78 sec)
 
mysql> INSERT INTO test VALUES(1),(2),(3);
ERROR 1031 (HY000): Table storage engine for 'test' doesn't have this option
 
mysql> SELECT * FROM test;
Empty set (0.31 sec)

EXAMPLE瀛樺偍寮曟搸涓嶆敮鎸佺紪绱㈠紩銆

15.7. FEDERATED瀛樺偍寮曟搸

FEDERATED瀛樺偍寮曟搸璁块棶鍦ㄨ繙绋嬫暟鎹簱鐨勮〃涓殑鏁版嵁锛岃屼笉鏄湰鍦扮殑琛ㄣ

FEDERATED瀛樺偍寮曟搸浠呭湪锛峂AX鐗堢殑MySQL涓婂彲鐢ㄣ

瑕佷负FEDERATED寮曟搸妫鏌 婧愮爜锛岃鏌ョ湅MySQL婧愮爜鍒嗗彂鐗堢殑sql鐩綍銆

瀵逛簬FEDERATED瀛樺偍寮曟搸锛屽湪http://forums.mysql.com/list.php?105涓婃湁涓涓笓闂ㄧ殑璁哄潧銆

15.7.1. 瀹夎FEDERATED瀛樺偍寮曟搸

瑕佸厑璁歌繖涓瓨鍌ㄥ紩鎿庯紝褰撲綘鏋勫缓MySQL鏃惰浣跨敤--with-federated-storage-engine鏉configure

15.7.2FEDERATED瀛樺偍寮曟搸鐨勬弿杩

褰撲綘鍒涘缓涓涓狥EDERATED琛ㄧ殑鏃跺欙紝鏈嶅姟鍣ㄥ湪鏁版嵁搴撶洰褰曞垱寤轰竴涓〃瀹氫箟鏂囦欢銆傛枃浠剁敱琛ㄧ殑鍚嶅瓧寮濮嬶紝骞舵湁涓涓.frm鎵╁睍鍚嶃傛棤鍏跺畠琛ㄨ鍒涘缓锛屽洜涓哄疄闄呯殑鏁版嵁鍦ㄤ竴涓繙绋嬫暟鎹簱涓娿傝繖涓嶅悓浜庝负鏈湴琛ㄥ伐浣滅殑瀛樺偍寮曟搸鐨勬柟寮忋

瀵逛簬鏈湴鐨勬暟鎹綍琛紝鏁版嵁鏂囦欢鏄湰鍦扮殑銆備緥濡傦紝濡傛灉浣犲垱寤轰竴涓悕涓簎ser鐨凪yISAM琛紝MyISAM澶勭悊鍣ㄥ垱寤轰竴涓悕涓簎sers.MYD鐨勬暟鎹枃浠躲傚鏈湴琛ㄨ锛屾彃鍏ワ紝鍒犻櫎鍜屾洿鏂板湪鏈湴鏁版嵁鏂囦欢閲岀殑鏁版嵁鐨勫鐞嗗櫒鍜岃褰曡浠ュ澶勭悊鍣ㄧ殑鐗瑰畾鏍煎紡瀛樺偍銆 涓轰簡璇昏褰曪紝澶勭悊鍣ㄥ繀椤绘妸鏁版嵁鍒嗚В杩涘垪銆備负浜嗗啓璁板綍锛屽垪鍊煎繀椤昏杞崲鍒拌澶勭悊鍣ㄤ娇鐢ㄧ殑琛屾牸寮忓苟涓旇鍐欒繘鏈湴鐨勬暟鎹枃浠躲

浣跨敤MySQL FEDERATED瀛樺偍寮曟搸锛屾病鏈夊琛ㄧ殑鏈湴鏁版嵁鏂囦欢锛堟瘮濡傦紝娌℃湁.MYD鏂囦欢锛夈傚彇鑰屼唬涔嬪湴锛屼竴涓繙绋嬫暟鎹簱瀛樺偍閭d簺姝e父鍦板簲璇ュ湪琛ㄤ腑鐨勬暟鎹傝繖浣垮緱MySQL瀹㈡埛绔疉PI鏉ヨ锛屽垹闄わ紝鏇存柊鍜屾彃鍏ユ暟鎹殑浣跨敤鎴愪负蹇呰銆傛暟鎹彇鍥炶閫氳繃SELECT * FROM tbl_name SQL璇彞鏉ュ垵濮嬪寲銆傝璇昏繖涓粨鏋滐紝閫氳繃浣跨敤mysql_fetch_row() C API鍑芥暟锛岃琚竴娆″彇涓涓紝鐒跺悗浠嶴ELECT缁撴灉鍖呬腑鐨勫垪杞崲鎴怓EDERATED澶勭悊鍣ㄦ湡鏈涚殑鏍煎紡銆

鍩烘湰娴佺▼濡備笅锛

1.    SQL璋冪敤琚湰鍦板彂甯

2.    MySQL澶勭悊鍣ˋPI 锛堟暟鎹互澶勭悊鍣ㄦ牸寮忥級

3.    MySQL瀹㈡埛绔疉PI 锛堟暟鎹杞崲鎴怱QL璋冪敤锛

4.    杩滅▼鏁版嵁搴-> MySQL瀹㈡埛绔疉PI

5.    杞崲缁撴灉鍖咃紙濡傛灉鏈夌殑璇濓級鍒板鐞嗗櫒鏍煎紡

6.    澶勭悊鍣 API -> 缁撴灉琛屾垨鍙楄褰卞搷鐨勫鏈湴鐨勮鏁

15.7.3. 濡備綍浣跨敤FEDERATED

浣跨敤FEDERATED琛ㄧ殑姝ラ鏄潪甯哥畝鍗曠殑銆傞氬父锛屼綘杩愯涓や釜鏈嶅姟鍣紝瑕佷箞鍦ㄥ悓涓涓富鏈轰笂锛岃涔堝湪涓嶅悓涓绘満涓娿傦紙涓涓狥EDERATED琛ㄤ娇鐢ㄥ叾瀹冭鍚屼竴鏈嶅姟鍣ㄧ鐞嗙殑琛ㄤ篃鏄彲鑳界殑銆傝櫧鐒跺彧鏈夋瀬灏戠殑鐐硅杩欎箞鍋氾級銆

棣栧厛锛屼綘蹇呴』鍦ㄤ綘鎯宠鐢‵EDERATED琛ㄨ闂殑杩滅▼鏈嶅姟鍣ㄤ笂鏈変竴涓〃銆傚亣璁撅紝杩滅▼鐨勮〃鍦‵EDERATED鏁版嵁搴撲腑骞朵笖琚涓嬪畾涔夛細

CREATE TABLE test_table (
    id     int(20) NOT NULL auto_increment,
    name   varchar(32) NOT NULL default '',
    other  int(20) NOT NULL default '0',
    PRIMARY KEY  (id),
    KEY name (name),
    KEY other_key (other)
)
ENGINE=MyISAM
DEFAULT CHARSET=latin1;

ENGINE琛ㄩ夐」鍙兘鍛藉悕浠讳綍瀛樺偍寮曟搸锛岃琛ㄩ渶瑕佷笉鏄竴涓狹yISAM琛ㄣ

鎺ョ潃锛屽湪鏈湴鏈嶅姟鍣ㄤ笂涓鸿闂繙绋嬭〃鍒涘缓涓涓狥EDERATED琛細

CREATE TABLE federated_table (
    id     int(20) NOT NULL auto_increment,
    name   varchar(32) NOT NULL default '',
    other  int(20) NOT NULL default '0',
    PRIMARY KEY  (id),
    KEY name (name),
    KEY other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://root@remote_host:9306/federated/test_table';

娉ㄦ剰: CONNECTION 鏇夸唬 鐢ㄥ湪鍏堝墠鐗堟湰鐨凪ySQL閲岀殑COMMENT锛夈

闄や簡ENGINE琛ㄩ夐」搴旇鏄疐EDERATED锛屽苟涓擟ONNECTION琛ㄩ夐」鏄粰FEDERATED鎸囨槑濡備綍杩炴帴鍒拌繙绋嬫湇鍔″櫒涓婄殑杩炴帴瀛楃涓蹭箣澶栵紝 杩欎釜琛ㄧ殑缁撴瀯蹇呴』瀹屽叏涓庤繙绋嬭〃鐨勭粨鏋勭浉鍚屻

FEDERATED寮曟搸浠呭垱寤哄湪宸茶仈鐩熸暟鎹簱涓殑test_table.frm鏂囦欢銆

杩滅▼涓绘満淇℃伅鎸囨槑鏈湴鏈嶅姟鍣ㄨ杩炴帴鍒扮殑杩滅▼鏈嶅姟鍣紝鏁版嵁搴撳拰琛ㄤ俊鎭寚鏄庡摢涓涓繙绋嬭〃瑕佽浣滀负鏁版嵁鏂囦欢鏉ョ敤銆傚湪杩欎釜渚嬪瓙涓傝繙绋嬫湇鍔″櫒琚寚瀹氭潵浣滀负杩滅▼涓绘満鍦9306绔彛涓婅繍琛岋紝鎵浠ヤ綘瑕佸惎鍔ㄦ湇鍔″櫒锛岃瀹冪洃鍚9306绔彛銆

鍦–ONNECTION閫夐」涓殑杩炴帴瀛楃涓茬殑涓鑸舰寮忓涓嬶細

scheme://user_name[:password]@host_name[:port_num]/db_name/tbl_name

鍙湁mysql鍦ㄨ繖涓鐐硅鏀寔涓scheme锛屽瘑鐮佸拰绔彛鍙锋椂鍙夌殑銆

杩欓噷鏈変竴浜涜繛鎺ュ瓧绗︿覆鐨勪緥瀛愶細

CONNECTION='mysql://username:password@hostname:port/database/tablename'
CONNECTION='mysql://username@hostname/database/tablename'
CONNECTION='mysql://username:password@hostname/database/tablename'

涓烘寚瀹氳繛鎺ュ瓧绗︿覆浣跨敤CONNECTION鏄潪鍙夛紝骞朵笖鍦ㄥ皢鏉ュ彲鑳戒細鏀瑰彉銆傚綋浣犱娇鐢‵EDERATED琛ㄧ殑鏃跺欙紝瑕佽寰楄繖涓紝鍥犱负杩欐剰鍛崇潃褰撳皢鏉ュ彂鐢熼偅绉嶆敼鍙樹箣鏃讹紝鍙兘琚姹傘

鍥犱负浠讳綍琚敤鐨勫瘑鐮佷綔涓虹函鏂囨湰琚瓨鍦ㄨ繛鎺ュ瓧绗︿覆涓紝瀹冨彲浠ヨ浠讳綍浣垮FEDERATED琛ㄤ娇鐢⊿HOW CREATE TABLE鎴朣HOW TABLE STATUS鐨勭敤鎴凤紝鎴栬呭湪INFORMATION_SCHEMA鏁版嵁搴撲腑鏌ヨTABLES琛ㄧ殑鐢ㄦ埛鐪嬭銆

瀵逛簬FEDERATED瀛樺偍寮曟搸锛屽湪http://forums.mysql.com/list.php?105涓婃湁涓涓笓闂ㄧ殑璁哄潧銆

15.7.4FEDERATED瀛樺偍寮曟搸鐨勫眬闄 鎬

FEDERATED鏀寔鍙婁笉鏀寔鐨勫涓嬶細

·         鍦ㄧ涓涓増鏈腑锛岃繙绋嬫湇鍔″櫒蹇呴』鏄竴涓狹ySQL鏈嶅姟鍣ㄣ侳EDERATED瀵瑰叾瀹冩暟鎹簱寮曟搸鐨勬敮鎸佸彲鑳戒細鍦ㄥ皢鏉ヨ娣诲姞銆

·         FEDERATED琛ㄦ寚鍚戠殑杩滅▼琛ㄥ湪浣犻氳繃FEDERATED琛ㄨ闂畠涔嬪墠蹇呴』瀛樺湪銆

·         涓涓狥EDERATED琛ㄦ寚鍚戝彟涓涓狥EDERATED琛ㄦ槸鍙兘鐨勶紝浣嗘槸浣犲繀椤诲皬蹇冧笉瑕佸垱寤轰竴涓惊鐜

·         娌℃湁瀵逛簨鍔$殑鏀寔銆

·         濡傛灉杩滅▼琛ㄥ凡缁忔敼鍙橈紝瀵笷EDERATED寮曟搸鑰岃█鏄病鏈夊姙娉曠煡閬撶殑銆傝繖涓殑鍘熷洜鏄洜涓鸿繖涓〃蹇呴』璞℃暟鎹枃浠朵竴鏍峰伐浣滐紝闄や簡鏁版嵁搴撳叾瀹冧换浣曢兘涓嶄細琚啓鍏ャ傚鏋滄湁浠讳綍瀵硅繙绋嬫暟鎹簱鐨勬敼鍙橈紝鏈湴琛ㄤ腑鏁版嵁鐨勫畬鏁存у彲鑳戒細琚牬鍧忋

·         FEDERATED瀛樺偍寮曟搸鏀寔SELECT, INSERT, UPDATE, DELETE鍜岀储寮曘傚畠涓嶆敮鎸丄LTER TABLE, DROP TABLE鎴栦换浣曞叾瀹冪殑鏁版嵁瀹氫箟璇█璇彞銆傚綋鍓嶇殑瀹炵幇涓嶄娇鐢ㄩ鍏堝噯澶囧ソ鐨勮鍙ャ

·         鎵ц浣跨敤SELECT, INSERT, UPDATE鍜孌ELETE锛屼絾涓嶇敤HANDLER銆

·         FEDERATED琛ㄤ笉鑳藉鏌ヨ缂撳瓨涓嶈捣浣滅敤銆

杩欎簺闄愬埗涓殑涓浜涘湪FEDERATED澶勭悊鏈虹殑灏嗘潵鐗堟湰鍙兘琚秷闄ゃ

15.8. ARCHIVE瀛樺偍寮曟搸

ARCHIVE瀛樺偍寮曟搸琚敤鏉ヤ互闈炲父灏忕殑瑕嗙洊鍖哄瓨鍌ㄥぇ閲忔棤绱㈠紩鏁版嵁銆 

瑕佸厑璁歌繖涓瓨鍌ㄥ紩鎿庯紝鍦ㄥ缓绔婱ySQL涔嬫椂浣跨敤--with-archive-storage-engine閫夐」鏉configure銆傚鏋滆繖涓瓨鍌ㄥ紩鎿庡彲甯﹁繖涓鍙ヤ娇鐢紝浣犲彲浠ョ湅瑙侊細

mysql> SHOW VARIABLES LIKE 'have_archive';

褰撳勾鍒涘缓涓涓狝RCHIVE琛紝鏈嶅姟鍣ㄥ湪鏁版嵁搴撶洰褰曞垱寤轰竴涓〃瀹氫箟鏂囦欢銆傛枃浠剁敱琛ㄧ殑鍚嶅瓧寮濮嬶紝骞剁敱涓涓.frm鐨勬墿灞曞悕銆傚瓨鍌ㄥ紩鎿庡垱寤哄叾瀹冩枃浠讹紝鎵鏈夐兘鏈夌敱琛ㄥ悕瀛楀紑澶寸殑鍚嶅瓧銆傛暟鎹拰鍏冩暟鎹枃浠舵湁鎵╁睍鍚.ARZ鍜.ARM銆備竴涓.ARN鏂囦欢鍙兘鍦ㄤ紭鍖栨搷浣滀腑鍑虹幇銆

ARCHIVE寮曟搸浠呮敮鎸両NSERT鍜孲ELEC锛堟棤鍒犻櫎锛屾浛鎹㈡垨鏇存柊锛夈傚畠鏀寔ORDER BY鎿嶄綔锛孊LOB 鍩燂紝浠ュ強鍩烘湰鍦版墍鏈夋暟鎹被鍨嬶紝闄や簡鍑犱綍鏁版嵁绫诲瀷锛堣鍙傞槄19.4.1鑺傦紝鈥淢ySQL 绌洪棿鏁版嵁绫诲瀷”锛夈侫RCHIVE寮曟搸浣跨敤琛岀骇閿佸畾銆

瀛樺偍: 褰撹褰曡鎻掑叆鏃讹紝瀹冧滑琚帇缂┿侫RCHIVE寮曟搸浣跨敤zlib鏃犳崯鏁版嵁鍘嬬缉銆侽PTIMIZE TABLE鐨勪娇鐢ㄥ彲浠ュ垎鏋愯〃锛屽苟鎶婂畠鎵撳寘涓烘洿灏忕殑鏍煎紡锛 浣跨敤OPTIMIZE TABLE鐨勫師鍥狅紝寰涓嬬湅锛夈傚紩鎿庨〉鏀寔CHECK TABLE銆傛湁鏁扮琚娇鐢ㄧ殑鎻掑叆绫诲瀷锛

·         鐩存帴鎻掑叆INSERT涔嬫椂鎶婁竴琛屾帹浠呭帇缂╃紦鍐诧紝骞朵笖缂撳啿鍦ㄥ畠闇瑕佺殑鏃跺欏埛鏂般傚埌缂撳啿鐨勬彃鍏ヨ閿佸畾淇濇姢銆備竴涓猄ELECT寮哄埗涓涓 鍒锋柊鍙戠敓锛岄櫎闈炶繘鏉ョ殑鍞竴鎻掑叆鏄疘NSERT DELAYED锛堥偅浜涘埛鏂板鍚岄渶瑕佷竴鏍凤級銆傝鍙傞槄13.2.4.2鑺傦紝鈥淚NSERT DELAYED 璇硶”

·         鍧楁彃鍏ユ棬鍦ㄥ畠瀹屾垚鍚庡彲瑙侊紝闄ら潪鍏跺畠鎻掑叆鍦ㄥ悓涓鏃堕棿鍙戠敓锛屽湪杩欑鎯呭喌涓嬶紝鍧楁彃鍏ュ彲浠ヨ閮ㄥ垎鐪嬭銆備竴涓猄ELECT涓嶄細瀵艰嚧涓涓潡鎻掑叆鐨 鍒锋柊锛岄櫎闈濻ELECT鍦ㄨ瑁呰浇鏃跺彂鐢熶竴涓甯告彃鍏ャ

鍙栧洖锛 鍦ㄥ彇鍥炴椂锛岃褰曟牴鎹渶瑕佽瑙e帇缂╋紝娌℃湁琛岀紦瀛樸備竴涓猄ELECT鎿嶄綔鎵ц瀹屽叏琛ㄦ牸鎵弿锛氬綋涓涓SELECT鍙戠敓涔嬫椂锛屽畠鎵惧嚭褰撳墠鏈夊灏戣鍙敤锛屽苟璇昏鐨勬暟閲忋SELECT琚綋浣滄寔缁鏉ユ墽琛屻傛敞鎰忥紝璁稿SELECT璇彞鍦ㄦ彃鍏ヨ繃绋嬩腑浼氭崯鍧忓帇缂╋紝闄ら潪鍧楁彃鍏ユ垨鑰呭欢杩熺殑鎻掑叆琚娇鐢ㄣ傝淇浠讳綍宸插彂鐢熷帇缂╅棶棰橈紝浣犲彲浠ユ绘槸鍋歄PTIMIZE TABLE锛堜篃鏀寔REPAIR TABLE锛夈傝SHOW TABLE STATUS鎶ュ憡鐨勮鏁版绘槸姝g‘鐨勩傝鍙傞槄13.5.2.6鑺傦紝鈥淩EPAIR TABLE璇硶”13.5.2.5鑺傦紝鈥淥PTIMIZE TABLE璇硶”13.5.4.18鑺 锛“SHOW TABLE STATUS 璇硶”

瀵逛簬ARCHIVE瀛樺偍寮曟搸锛屽湪 http://forums.mysql.com/list.php?112 涓婃湁涓撻棬璁哄潧銆

15.9. CSV瀛樺偍寮曟搸

CSV瀛樺偍寮曟搸浣跨敤閫楀彿鍒嗛殧鍊兼牸寮忕殑鏂囨湰鏂囦欢瀛樺偍鏁版嵁銆

瑕佸厑璁镐娇鐢ㄨ繖涓瓨鍌ㄥ紩鎿庯紝褰撲綘寤虹珛MySQL涔嬫椂锛屼娇鐢--with-csv-storage-engine閫夐」鏉configure

褰撲綘鍒涘缓涓涓狢SV琛ㄤ箣鏃讹紝鏈嶅姟鍣ㄥ湪鏁版嵁搴撶洰褰曞垱寤轰竴涓〃瀹氫箟鏂囦欢銆傛枃浠剁敱琛ㄧ殑鍚嶅瓧寮濮嬶紝骞朵笖鐢变竴涓.frm鐨勬墿灞曞悕銆傚瓨鍌ㄥ紩鎿庝篃鍒涘缓涓涓暟鎹枃浠躲傚畠鐨勫悕瀛楃敱琛ㄧ殑鍚嶅瓧寮濮嬶紝骞朵笖鏈変竴涓.CSV鐨勬墿灞曞悕銆傛暟鎹枃浠舵槸鏃犳牸寮忔枃鏈枃浠躲傚綋浣犳妸鏁版嵁瀛樺偍杩涜〃鏃讹紝瀛樺偍寮曟搸鐢–SV鏍煎紡鎶婂畠瀛樿繘鏁版嵁鏂囦欢銆

mysql> CREATE TABLE test(i INT, c CHAR(10)) ENGINE = CSV;
Query OK, 0 rows affected (0.12 sec)
 
mysql> INSERT INTO test VALUES(1,'record one'),(2,'record two');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
 
mysql> SELECT * FROM test;
+------+------------+
| i    | c          |
+------+------------+
|    1 | record one |
|    2 | record two |
+------+------------+
2 rows in set (0.00 sec)

濡傛灉浣犳鏌ュ湪鎵ц鍓嶈堪璇彞鎵鍒涘缓鐨勬暟鎹簱鐩綍閲岀殑test.CSV鏂囦欢锛屽畠鐨勫唴瀹瑰簲璇ョ湅璧锋潵璞″涓嬫墍绀猴細

"1","record one"
"2","record two"

CSV瀛樺偍寮曟搸涓嶆敮鎸佺储寮曘

15.10. BLACKHOLE瀛樺偍寮曟搸

BLACKHOLE瀛樺偍寮曟搸灏卞儚“榛戞礊”涓鏍凤紝瀹冩帴鏀舵暟鎹絾涓㈠純瀹冭屼笉鏄瓨鍌ㄥ畠銆傚彇鍥炴绘槸杩斿洖绌洪泦锛

mysql> CREATE TABLE test(i INT, c CHAR(10)) ENGINE = BLACKHOLE;
Query OK, 0 rows affected (0.03 sec)
 
mysql> INSERT INTO test VALUES(1,'record one'),(2,'record two');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
 
mysql> SELECT * FROM test;
Empty set (0.00 sec)

褰撲綘鍒涘缓涓涓狟LACKHOLE琛ㄧ殑鏃跺欙紝鏈嶅姟鍣ㄥ湪鏁版嵁搴撶洰褰曞垱寤轰竴涓〃瀹氫箟鏂囦欢銆傛枃浠剁敤琛ㄧ殑鍚嶅瓧寮澶达紝骞朵笖鏈変竴涓.frm鎵╁睍鍚嶃傛病鏈夊叾瀹冩枃浠跺叧鑱斿埌杩欎釜琛ㄦ牸銆

BLACKHOLE瀛樺偍寮曟搸鏀寔鎵鏈夌绫荤殑绱㈠紩銆

瑕佸厑璁歌繖涓瓨鍌ㄥ紩鎿庯紝鍦ㄤ綘寤虹珛MySQL涔嬫椂浣跨敤--with-blackhole-storage-engine閫夐」鏉configure銆侭LACKHOLE瀛樺偍寮曟搸鍦∕ySQ渚涘簲鐨勬湇鍔″櫒浜岃繘鍒剁増閲屽彲浠ユ壘鍒帮紱閫氳繃鏌ョ湅SHOW ENGINES鎴朣HOW VARIABLES LIKE 'have%'鐨勮緭鍑猴紝浣犲彲浠ョ‘瀹氫綘鐨勭増鏈槸鍚︽敮鎸佽繖涓紩鎿庛

鍒癇LACKHOLE琛ㄧ殑鎻掑叆涓嶅瓨鍌ㄤ换浣曟暟鎹紝浣嗗鏋滀簩杩涘埗鏃ュ織琚厑璁革紝SQL璇彞琚啓鍏ユ棩蹇楋紙骞惰澶嶅埗鍒颁粠鏈嶅姟鍣級銆傝繖鍙互鏈夌敤甯姪鍦颁綔涓洪噸澶嶅櫒鎴栬繃婊ゅ櫒鏈哄埗銆備緥濡傦紝鍋囪浣犵殑搴旂敤闇瑕佷粠鏈嶅姟鍣ㄤ晶鐨勮繃婊よ鍒欙紝浣嗕紶杈撴墍鏈変簩杩涘埗鏃ュ織鏁版嵁鍒颁粠鏈嶅姟鍣ㄩ鍏堝鑷磋繃澶氫氦閫氱殑缁撴灉銆傚湪杩欑鎯呭喌涓嬶紝鍦ㄤ富鏈嶅姟鍣ㄤ富鏈轰笂寤虹珛涓涓吉浠庢湇鍔″櫒杩涚▼锛屽畠鐨勫瓨鍌ㄥ紩鎿庢槸BLACKHOLE锛屾弿杩板涓嬶細

Replication using BLACKHOLE
        for filtering

涓绘湇鍔″櫒鍐欏畠鐨勪簩杩涘埗鏃ュ織銆備吉mysqld杩涚▼浣滀负浠庢湇鍔″櫒锛搴旂敤鏈熸湜鐨剅eplicate-do鍜宺eplicate-ignore瑙勫垯鐨勫悎骞讹紝骞朵笖鍐欎竴涓柊鐨勶紝琚繃婊ょ殑灞炰簬瀹冭嚜宸辩殑浜岃繘鍒舵棩蹇 銆傦紙璇峰弬闃6.8鑺傦紝鈥滃鍒跺惎鍔ㄩ夐」”锛夈傝繖涓凡杩囨护鏃ュ織琚彁渚涚粰浠庢湇鍔″櫒銆

鏃㈢劧浼繘绋嬩笉纭疄鍦板瓨鍌ㄤ换浣曟暟鎹紝鍙湁寰堝皬鐨勭敱鍦ㄥ鍒朵富鏈嶅姟鍣ㄤ富鏈轰笂棰濆鐨刴ysqld杩涚▼鎷涜嚧鐨勫鐞嗗紑鏀傝繖涓被鍨嬬殑寤虹珛鍙互鐢ㄩ澶栧鍒朵粠鏈嶅姟鍣ㄦ潵閲嶅銆

鍏跺畠鍙兘瀵笲LACKHOLE瀛樺偍寮曟搸鐨勪娇鐢ㄥ寘鎷細

·         杞偍鏂囦欢璇硶鐨勯獙璇併

·         鏉ヨ嚜浜岃繘鍒舵棩蹇楄褰曠殑寮閿娴嬮噺锛岄氳繃姣旇緝鍏佽浜岃繘鍒舵棩蹇楀姛鑳界殑BLACKHOLE鐨勬ц兘涓庣姝簩杩涘埗鏃ュ織鍔熻兘鐨凚LACKHOLE鐨勬ц兘銆

·         鍥犱负BLACKHOLE鏈川涓婃槸涓涓“no-op” 瀛樺偍寮曟搸锛屽畠鍙兘琚敤鏉ユ煡鎵句笌瀛樺偍寮曟搸鑷韩涓嶇浉鍏崇殑鎬ц兘鐡堕銆


杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆