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

Chapter聽7.聽Optimization - MySQL 5.1参考手册中文版

绗7绔狅細浼樺寲

鐩綍

7.1. 浼樺寲姒傝堪
7.1.1. MySQL璁捐灞闄愪笌鎶樿》
7.1.2. 涓哄彲绉绘鎬ц璁″簲鐢ㄧ▼搴
7.1.3. 鎴戜滑宸插皢MySQL鐢ㄥ湪浣曞锛
7.1.4. MySQL鍩哄噯濂椾欢
7.1.5. 浣跨敤鑷繁鐨勫熀鍑
7.2. 浼樺寲SELECT璇彞鍜屽叾瀹冩煡璇
7.2.1. EXPLAIN璇硶锛堣幏鍙朣ELECT鐩稿叧淇℃伅锛
7.2.2. 浼拌鏌ヨ鎬ц兘
7.2.3. SELECT鏌ヨ鐨勯熷害
7.2.4. MySQL鎬庢牱浼樺寲WHERE瀛愬彞
7.2.5. 鑼冨洿浼樺寲
7.2.6. 绱㈠紩鍚堝苟浼樺寲
7.2.7. MySQL濡備綍浼樺寲IS NULL
7.2.8. MySQL濡備綍浼樺寲DISTINCT
7.2.9. MySQL濡備綍浼樺寲LEFT JOIN鍜孯IGHT JOIN
7.2.10. MySQL濡備綍浼樺寲宓屽Join
7.2.11. MySQL濡備綍绠鍖栧閮ㄨ仈鍚
7.2.12. MySQL濡備綍浼樺寲ORDER BY
7.2.13. MySQL濡備綍浼樺寲GROUP BY
7.2.14. MySQL濡備綍浼樺寲LIMIT
7.2.15. 濡備綍閬垮厤琛ㄦ壂鎻
7.2.16. INSERT璇彞鐨勯熷害
7.2.17. UPDATE璇彞鐨勯熷害
7.2.18. DELETE璇彞鐨勯熷害
7.2.19. 鍏跺畠浼樺寲鎶宸
7.3. 閿佸畾浜嬪疁
7.3.1. 閿佸畾鏂规硶
7.3.2. 琛ㄩ攣瀹氫簨瀹
7.4. 浼樺寲鏁版嵁搴撶粨鏋
7.4.1. 璁捐閫夋嫨
7.4.2. 浣夸綘鐨勬暟鎹敖鍙兘灏
7.4.3. 鍒楃储寮
7.4.4. 澶氬垪绱㈠紩
7.4.5. MySQL濡備綍浣跨敤绱㈠紩
7.4.6. MyISAM閿珮閫熺紦鍐
7.4.7. MyISAM绱㈠紩缁熻闆嗗悎
7.4.8. MySQL濡備綍璁$畻鎵撳紑鐨勮〃
7.4.9. MySQL濡備綍鎵撳紑鍜屽叧闂〃
7.4.10. 鍦ㄥ悓涓涓暟鎹簱涓垱寤哄涓〃鐨勭己闄
7.5. 浼樺寲MySQL鏈嶅姟鍣
7.5.1. 绯荤粺鍥犵礌鍜屽惎鍔ㄥ弬鏁扮殑璋冭妭
7.5.2. 璋冭妭鏈嶅姟鍣ㄥ弬鏁
7.5.3. 鎺у埗鏌ヨ浼樺寲鍣ㄧ殑鎬ц兘
7.5.4. 缂栬瘧鍜岄摼鎺ユ庢牱褰卞搷MySQL鐨勯熷害
7.5.5. MySQL濡備綍浣跨敤鍐呭瓨
7.5.6. MySQL濡備綍浣跨敤DNS
7.6. 纾佺洏浜嬪疁
7.6.1. 浣跨敤绗﹀彿閾炬帴

浼樺寲鏄竴涓鏉傜殑浠诲姟锛屽洜涓烘渶缁堣姹備簡瑙f暣涓緟浼樺寲鐨勭郴缁熴傚敖绠″彲浠ヨ繘琛屽眬閮ㄤ紭鍖栬屼笉闇瑕佷簡瑙g郴缁熸垨搴旂敤绋嬪簭锛屼负浜嗕紭鍖栧緱鏇村ソ锛屼綘蹇呴』鐭ラ亾鏇村鐨勪俊鎭

鏈珷瑙i噴骞剁粰鍑轰笉鍚岀殑浼樺寲MySQL鐨勬柟娉曠ず渚嬨備絾瑕佽浣忔绘湁涓浜涘叾瀹冩柟娉曚娇绯荤粺鏇村揩锛屽敖绠¢渶瑕佹洿澶氱殑宸ヤ綔銆

7.1.聽浼樺寲姒傝堪

浣夸竴涓郴缁熸洿蹇殑鏈閲嶈鍥犵礌褰撶劧鏄熀鏈璁°傛澶栵紝杩橀渶瑕佺煡閬撶郴缁熸鍋氫粈涔堟牱鐨勪簨鎯咃紝浠ュ強鐡堕鏄粈涔堛

鏈甯歌鐨勭郴缁熺摱棰堟槸锛

  • 纾佺洏鎼滅储銆傞渶瑕佽姳鏃堕棿浠庣鐩樹笂鎵惧埌涓涓暟鎹紝鐢ㄥ湪鐜颁唬纾佺洏鐨勫钩鍧囨椂闂撮氬父灏忎簬10ms锛屽洜姝ょ悊璁轰笂鎴戜滑鑳藉姣忕澶х害鎼滅储1000娆°傝繖涓椂闂村湪鏂扮鐩樹笂鎻愰珮涓嶅ぇ骞朵笖寰堥毦涓轰竴涓〃杩涜浼樺寲銆備紭鍖栧畠鐨勬柟娉曟槸灏嗘暟鎹垎甯冨湪澶氫釜纾佺洏涓娿
  • 纾佺洏璇/鍐欍傚綋纾佺洏鏀惧叆姝g‘浣嶇疆鍚庯紝鎴戜滑闇瑕佷粠涓鍙栨暟鎹傚浜庣幇浠g殑纾佺洏锛屼竴涓鐩樿嚦灏戜紶杈10-20Mb/s鐨勫悶鍚愩傝繖姣旀悳绱㈣瀹规槗浼樺寲锛屽洜涓轰綘鑳戒粠澶氫釜纾佺洏骞惰鍦拌銆
  • CPU鍛ㄦ湡銆傛垜浠皢鏁版嵁璇诲叆鍐呭瓨鍚庯紝闇瑕佸瀹冭繘琛屽鐞嗕互鑾峰緱鎴戜滑闇瑕佺殑缁撴灉銆傝〃鐩稿浜庡唴瀛樿緝灏忔槸鏈甯歌鐨勯檺鍒跺洜绱犮備絾鏄浜庡皬琛紝閫熷害閫氬父涓嶆垚闂銆

         鍐呭瓨甯﹀銆傚綋CPU闇瑕佺殑鏁版嵁瓒呭嚭CPU缂撳瓨鏃讹紝涓荤紦瀛樺甫瀹藉氨鎴愪负鍐呭瓨鐨勪竴涓摱棰堛傝繖鍦ㄥぇ澶氭暟绯荤粺姝f槸涓涓笉甯歌鐨勭摱棰堜絾鏄綘搴旇鐭ラ亾瀹冦

7.1.1. MySQL璁捐灞闄愪笌鎶樿》

褰撲娇鐢MyISAM瀛樺偍寮曟搸鏃讹紝MySQL浣跨敤鏋佸揩閫熺殑琛ㄩ攣瀹氾紝浠ヤ究鍏佽澶氭璇绘垨涓娆″啓銆備娇鐢ㄨ瀛樺偍寮曟搸鐨勬渶澶ч棶棰樺嚭鐜板湪鍚屼竴涓〃涓繘琛屾贩鍚堢ǔ瀹氭暟鎹祦鏇存柊涓庢參閫熼夋嫨銆傚鏋滆繖鍙槸鏌愪簺琛ㄧ殑闂锛屼綘鍙互浣跨敤鍙︿竴涓瓨鍌ㄥ紩鎿庛傚弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

MySQL鍙互浣跨敤浜嬪姟琛ㄥ拰闈炰簨鍔¤〃銆備负浜嗘洿瀹规槗鍦拌闈炰簨鍔¤〃椤哄埄宸ヤ綔(濡傛灉鍑虹幇闂涓嶈兘鍥炴粴)MySQL閲囩敤涓嬭堪瑙勫垯銆傝娉ㄦ剰杩欎簺瑙勫垯鍙傜敤浜涓嶈繍琛屽湪涓ユ牸妯″紡涓嬫垨涓INSERTUPDATE浣跨敤IGNORE瑙勫畾绋嬪簭鏃躲

         鎵鏈夊垪鏈夐粯璁ゅ笺傝娉ㄦ剰褰撹繍琛屽湪涓ユ牸SQL妯″紡(鍖呮嫭TRADITIONAL SQL妯″紡)鏃讹紝蹇呴』涓NOT NULL鍒楁寚瀹氶粯璁ゅ笺

         濡傛灉鍚戝垪鍐呮彃鍏ヤ笉鍚堥傜殑鎴栬秴鍑鸿寖鍥寸殑鍊硷紝MySQL灏嗚鍒楄瀹氫负鈥滄渶濂界殑鍙兘鐨鈥濓紝鑰屼笉鏄姤鍛婇敊璇傚浜庢暟瀛楀硷紝涓0銆佸彲鑳界殑鏈灏忓兼垨鏈澶у笺傚浜庡瓧绗︿覆锛屼负绌哄瓧绗︿覆鎴栧垪鍐呭彲浠ヤ繚瀛樼殑瀛楃涓层傝娉ㄦ剰褰撹繍琛屽湪涓ユ牸妯″紡鎴TRADITIONAL SQL妯″紡鏃惰琛屼负涓 閫傜敤銆

         鎵鏈夎〃杈惧紡鐨勮绠楃粨鏋滆繑鍥炰竴涓〃绀洪敊璇姸鍐电殑淇″彿銆備緥濡傦紝1/0杩斿洖NULL(浣跨敤ERROR_FOR_DIVISION_BY_ZERO SQL妯″紡鍙互鏇存敼璇ヨ涓)

濡傛灉姝d娇鐢ㄩ潪浜嬪姟琛紝涓嶅簲璇ヤ娇鐢MySQL鏉ユ鏌ュ垪鐨勫唴瀹广備竴鑸儏鍐碉紝鏈瀹夊叏鐨(閫氬父鏄渶蹇殑)鏂规硶寰勬槸璁╁簲鐢ㄧ▼搴忕‘淇濆彧鍚戞暟鎹簱浼犻掑悎娉曞笺

鐩稿叧璇︾粏淇℃伅鍙傝1.8.6鑺傦紝鈥淢ySQL澶勭悊绾︽潫鐨勬柟寮忊13.2.4鑺傦紝鈥淚NSERT璇硶鈥5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

7.1.2. 涓哄彲绉绘鎬ц璁″簲鐢ㄧ▼搴

鍥犱负涓嶅悓SQL鏈嶅姟鍣ㄥ疄鐜颁簡鏍囧噯SQL鐨勪笉鍚岄儴鍒嗭紝闇瑕佽姳鍔熷か鏉ョ紪鍐欏彲绉绘鐨SQL搴旂敤绋嬪簭銆傚寰堢畝鍗曠殑閫夋嫨/鎻掑叆锛屽緢瀹规槗瀹炵幇绉绘锛屼絾鏄渶瑕佺殑鍔熻兘瓒婂鍒欒秺鍥伴毦銆傚鏋滄兂瑕佸簲鐢ㄧ▼搴忓寰堝鏁版嵁搴撶郴缁熼兘蹇紝瀹冨彉寰楁洿闅撅紒

涓轰簡浣夸竴涓鏉傚簲鐢ㄧ▼搴忓彲绉绘锛屼綘闇瑕侀夋嫨瀹冨簲璇ュ伐浣滅殑SQL鏈嶅姟鍣紝骞剁‘瀹氳繖浜涙湇鍔″櫒鏀寔浠涔堢壒鎬с

鎵鏈夋暟鎹簱閮芥湁涓浜涘急鐐广傝繖灏辨槸瀹冧滑涓嶅悓鐨勮璁℃姌琛峰鑷寸殑涓嶅悓琛屼负銆

鍙互浣跨敤MySQLcrash-me绋嬪簭鏉ユ壘鍑鸿兘鐢ㄤ簬鏁版嵁搴撴湇鍔″櫒閫夋嫨鐨勫嚱鏁般佺被鍨嬪拰闄愬埗銆crash-me骞朵笉鑳芥壘鍑烘墍鏈夌殑鐗规э紝浣嗘槸鍏跺箍搴︿粛鐒跺緢鍚堢悊锛屽彲浠ヨ繘琛屽ぇ绾450涓祴璇曘

crash-me鍙互鎻愪緵鐨勪竴绉嶇被鍨嬬殑淇℃伅鐨勪緥瀛愶細濡傛灉鎯宠浣跨敤InformixDB2锛屼笉搴旇浣跨敤瓒呰繃18涓瓧绗︾殑鍒楀悕銆

crash-me绋嬪簭鍜MySQL鍩哄噯绋嬪簭鏄嫭绔嬩簬鏁版嵁搴撶殑銆傞氳繃瑙傚療瀹冧滑鏄浣曠紪鍐欑殑锛岀紪鍙互鐭ラ亾蹇呴』涓虹紪鍐欑嫭绔嬩簬鏁版嵁搴撶殑搴旂敤绋嬪簭鍋氫粈涔堛傚熀鍑嗘湰韬彲鍦MySQL婧愮爜鍒嗗彂鐨鈥渟ql-bench鈥鐩綍涓嬫壘鍒般傚畠浠敤DBI鏁版嵁搴撴帴鍙d互Perl鍐欐垚銆備娇鐢DBI鏈韩鍗冲彲浠ヨВ鍐抽儴鍒嗙Щ妞嶆ч棶棰橈紝鍥犱负瀹冩彁渚涗笌鏁版嵁搴撴棤鍏崇殑鐨勫瓨鍙栨柟娉曘

鍏充簬crash-me缁撴灉锛岃闂http://dev.mysql.com/tech-resources/crash-me.php銆傚埌http://dev.mysql.com/tech-resources/benchmarks/鐪嬭繖涓熀鍑嗙殑缁撴灉銆

濡傛灉浣犱负鏁版嵁搴撶殑鐙珛鎬ц屽姫鍔涳紝闇瑕佸緢濂藉湴浜嗚В姣忎釜SQL鏈嶅姟鍣ㄧ殑鐡堕銆備緥濡傦紝MySQL鍦ㄦ绱㈠拰鏇存柊MyISAM琛ㄨ褰曟柟闈㈠緢蹇紝浣嗘槸鍦ㄥ悓涓涓〃涓婃贩鍚堟參閫熻鑰呭拰鍐欒呮柟闈㈡湁涓涓棶棰樸傚彟涓鏂归潰锛屽綋浣犺瘯鍥捐闂渶杩戞洿鏂颁簡(鐩村埌瀹冧滑琚埛鏂板埌纾佺洏涓)鐨勮鏃讹紝鍦Oracle涓湁涓涓緢澶х殑闂銆備簨鍔℃暟鎹簱鎬荤殑鏉ヨ鍦ㄤ粠璁板綍鏂囦欢琛ㄤ腑鐢熸垚鎬荤粨琛ㄦ柟闈笉鏄緢濂斤紝鍥犱负鍦ㄨ繖绉嶆儏鍐典笅锛岃閿佸畾鍑犱箮娌℃湁鐢ㄣ

涓轰簡浣垮簲鐢ㄧ▼搴忊滅‘瀹炩濈嫭绔嬩簬鏁版嵁搴擄紝闇瑕佸畾涔変竴涓鏄撴墿灞曠殑鎺ュ彛锛岀敤瀹冨彲鎿嶇旱鏁版嵁銆傚洜涓C++鍦ㄥぇ澶氭暟绯荤粺涓婂彲浠ラ傜敤锛屼娇鐢ㄦ暟鎹簱鐨勪竴涓C++ 绫绘帴鍙f槸鏈夋剰涔夌殑銆

濡傛灉浣犱娇鐢ㄦ煇涓暟鎹簱鐗瑰畾鐨勫姛鑳(渚嬪MySQL涓撶敤鐨REPLACE璇彞)锛屽簲璇ヤ负SQL鏈嶅姟鍣ㄧ紪鐮佷竴涓柟娉曚互瀹炵幇鍚屾牱鐨勫姛鑳姐傚敖绠℃參浜涳紝浣嗙‘鍏佽鍏跺畠鏈嶅姟鍣ㄦ墽琛屽悓鏍风殑浠诲姟銆

MySQL锛屽彲浠ヤ娇鐢/*! */璇硶鎶MySQL鐗瑰畾鐨勫叧閿瘝鍔犲埌鏌ヨ涓傚湪/**/涓殑浠g爜灏嗚鍏跺畠澶у鏁SQL鏈嶅姟鍣ㄨ涓烘敞閲(骞惰蹇界暐)

濡傛灉楂樻ц兘鐪熺殑姣斿噯纭ф洿閲嶈锛屽氨鍍忓湪涓浜web搴旂敤绋嬪簭閭f牱锛屼竴绉嶅彲琛岀殑鏂规硶鏄垱寤轰竴涓簲鐢ㄥ眰锛岀紦瀛樻墍鏈夌殑缁撴灉浠ヤ究寰楀埌鏇撮珮鐨勬ц兘銆傞氳繃鍙槸璁╂棫鐨勭粨鏋滃湪鐭椂闂村悗鈥樿繃鏈熲欙紝鑳戒繚鎸佺紦瀛樺悎鐞嗗湴鍒锋柊銆傝繖鍦ㄦ瀬楂樿礋杞界殑鎯呭喌涓嬫槸鐩稿綋涓嶉敊鐨勶紝鍦ㄦ鎯呭喌涓嬶紝鑳藉姩鎬佸湴澧炲姞缂撳瓨骞朵笖璁惧畾杈冮珮鐨勮繃鏈熸椂闄愮洿鍒颁竴鍒囨仮澶嶆甯搞

鍦ㄨ繖绉嶆儏鍐典笅锛岃〃鍒涘缓淇℃伅搴旇鍖呭惈缂撳瓨鍒濆澶у皬鍜岃〃鍒锋柊棰戠巼绛変俊鎭

瀹炴柦搴旂敤绋嬪簭缂撳瓨鐨勪竴绉嶆柟娉曟槸浣跨敤MySQL鏌ヨ缂撳瓨銆傚惎鐢ㄦ煡璇㈢紦瀛樺悗锛屾湇鍔″櫒鍙互纭畾鏄惁鍙互閲嶆柊浣跨敤鏌ヨ缁撴灉銆傝繖鏍风畝鍖栦簡浣犵殑搴旂敤绋嬪簭銆傚弬瑙5.13鑺傦紝鈥淢ySQL鏌ヨ楂橀熺紦鍐测

7.1.3. 鎴戜滑宸插皢MySQL鐢ㄥ湪浣曞锛

璇ヨ妭鎻忚堪浜Mysql鐨勬棭鏈熷簲鐢ㄧ▼搴忋

MySQL鏈鍒濆紑鍙戞湡闂达紝MySQL鐨勫姛鑳介傚悎澶у鏁板鎴枫MySQL涓虹憺鍏哥殑涓浜涙渶澶х殑闆跺敭鍟嗗鐞嗘暟鎹粨搴撱

鎴戜滑浠庢墍鏈夊晢搴楀緱鍒版墍鏈夌孩鍒╁崱浜ゆ槗鐨勬瘡鍛ㄦ荤粨锛屽苟涓旀垜浠湡鏈涗负鎵鏈夊簵涓绘彁渚涙湁鐢ㄧ殑淇℃伅浠ュ府鍔╀粬浠緱鍑轰粬浠殑骞垮憡鎴樺浣曞奖鍝嶄粬浠殑椤惧銆

鏁版嵁鏄浉褰撳法閲忕殑(澶х害姣忔湀7鐧句竾瀹椾氦鏄撴荤粨)骞朵笖鎴戜滑淇濆瓨4-10骞存潵鐨勬暟鎹渶瑕佸憟鐜扮粰鐢ㄦ埛銆傛垜浠瘡鍛ㄤ粠椤惧閭i噷寰楀埌璇锋眰锛屼粬浠兂瑕佲滅珛鍒烩濊闂潵鑷鏁版嵁鐨勬柊鎶ュ憡銆

鎴戜滑閫氳繃姣忔湀灏嗘墍鏈変俊鎭瓨鍌ㄥ湪鍘嬬缉鐨勨滀氦鏄撯濊〃涓潵瑙e喅瀹冦傛垜浠湁涓濂楃畝鍗曠殑瀹/鑴氭湰鐢ㄦ潵鐢熸垚鏉ヨ嚜浜ゆ槗琛ㄧ殑涓嶅悓鏉′欢( 浜у搧缁勩侀【瀹id锛屽晢搴...)鐨勬荤粨琛ㄣ傛姤鍛婃槸鐢变竴涓繘琛岃娉曞垎鏋愮綉椤电殑灏perl鑴氭湰鍔ㄦ佺敓鎴愮殑缃戦〉锛屽湪鑴氭湰涓墽琛SQL璇彞骞朵笖鎻掑叆缁撴灉銆傛垜浠緢鎯充娇鐢PHPmod_perl锛屼絾鏄偅鏃跺畠浠繕涓嶅彲鐢ㄣ

瀵瑰浘褰㈡暟鎹紝鎴戜滑鐢C璇█缂栧啓浜嗕竴涓畝鍗曠殑宸ュ叿锛屽畠鑳藉熀浜庨偅浜涚粨鏋滃鐞SQL鏌ヨ缁撴灉骞剁敓鎴GIF鍥惧舰銆傝宸ュ叿涔熶粠鍒嗘瀽Web缃戦〉鐨perl鑴氭湰涓姩鎬佸湴鎵ц銆

鍦ㄥぇ澶氭暟鎯呭喌涓嬶紝涓涓柊鐨勬姤鍛婇氳繃绠鍗曞湴澶嶅埗涓涓幇鏈夎剼鏈苟涓斾慨鏀瑰叾涓殑SQL鏌ヨ鏉ュ畬鎴愩傚湪涓浜涙儏鍐典笅锛屾垜浠皢闇瑕佹妸鏇村鐨勫垪鍔犲埌涓涓幇鏈夌殑鎬荤粨琛ㄤ腑鎴栦骇鐢熶竴涓柊鐨勶紝浣嗘槸杩欎篃鐩稿綋绠鍗曪紝鍥犱负鎴戜滑鍦ㄧ鐩樹笂淇濆瓨鎵鏈変氦鏄撹〃銆傦紙鐩墠鎴戜滑澶х害鏈50G鐨勪氦鏄撹〃鍜200G鐨勫叾瀹冮【瀹㈡暟鎹)

鎴戜滑涔熻鎴戜滑鐨勯【瀹㈢洿鎺ョ敤ODBC璁块棶鎬荤粨琛ㄤ互渚块珮绾х敤鎴疯兘鑷繁鐢ㄨ繖浜涙暟鎹繘琛岃瘯楠屻

璇ョ郴缁熷伐浣滃緱寰堝ソ锛屾垜浠彲浠ユ鏃犻棶棰樺湴鐢ㄥ緢閫傚害鐨Sun Ultra SPARC宸ヤ綔绔欑‖浠(2x200MHz)鏉ュ鐞嗘暟鎹傝绯荤粺琚愭绉绘鍒颁簡Linux涓

7.1.4. MySQL鍩哄噯濂椾欢

鏈妭搴旇鍖呭惈MySQL鍩哄噯濂椾欢(crash-me)鐨勬妧鏈弿杩帮紝浣嗘槸璇ユ弿杩拌繕娌″啓鎴愩傜洰鍓嶏紝浣犲彲浠ラ氳繃鍦MySQL婧愮爜鍒嗗彂涓殑鈥渟ql-bench鈥鐩綍涓嬬殑浠g爜鍜岀粨鏋滀簡瑙e熀鍑嗗浠舵槸濡備綍宸ヤ綔鐨勩

閫氳繃鍩哄噯鐢ㄦ埛鍙互浜嗚В涓涓粰瀹氱殑SQL瀹炵幇鍦ㄥ摢鏂归潰鎵ц寰楀緢濂芥垨寰堢碂绯曘

娉ㄦ剰锛岃繖涓熀鍑嗘槸鍗曠嚎绋嬬殑锛屽畠鍙互娴嬮噺鎿嶄綔鎵ц鐨勬渶灏忔椂闂淬傛垜浠鍒掑皢鏉ュ湪鍩哄噯濂椾欢涓坊鍔犲绾跨▼娴嬭瘯銆

瑕佷娇鐢ㄥ熀鍑嗗浠讹紝蹇呴』婊¤冻涓嬮潰鐨勮姹傦細

         鍩哄噯濂椾欢闅MySQL婧愮爜鍒嗗彂鎻愪緵銆傚彲浠ヤ粠http://dev.mysql.com/downloads/涓嬭浇鍒嗗彂锛屾垨鑰呬娇鐢ㄥ綋鍓嶇殑寮鍙戞簮鐮佹爲(鍙傝2.8.3鑺傦紝鈥滀粠寮鍙戞簮鐮佹爲瀹夎鈥)

         鍩哄噯鑴氭湰鐢Perl缂栧啓鑰屾垚锛屼娇鐢Perl DBI妯″潡璁块棶鏁版嵁搴撴湇鍔″櫒锛屽洜姝ゅ繀椤诲畨瑁DBI銆傝繕闇瑕佷负姣忎釜寰呮祴璇曠殑鏈嶅姟鍣ㄦ彁渚涙湇鍔″櫒涓撶敤DBD椹卞姩绋嬪簭銆備緥濡傦紝瑕佹祴璇MySQLPostgreSQLDB2锛屽繀椤诲畨瑁DBD::mysqlDBD::PgDBD::DB2妯″潡銆傚弬瑙2.13鑺傦紝鈥淧erl瀹夎娉ㄦ剰浜嬮」鈥

鑾峰緱MySQL婧愮爜鍒嗗彂鍚庯紝鍙互鍦sql-bench鐩綍鎵惧埌鍩哄噯濂椾欢銆傝杩愯鍩哄噯娴嬭瘯锛屽簲鏋勫缓MySQL锛岀劧鍚庤繘鍏sql-bench鐩綍骞舵墽琛run-all-tests鑴氭湰锛

shell> cd sql-bench

shell> perl run-all-tests --server=server_name

server_name鏄竴涓敮鎸佺殑鏈嶅姟鍣ㄣ傝鑾峰緱鎵鏈夐夐」鍜屾敮鎸佺殑鏈嶅姟鍣紝璋冪敤鍛戒护锛

shell> perl run-all-tests --help

crash-me鑴氭湰涔熶綅浜sql-bench鐩綍銆crash-me灏濊瘯閫氳繃瀹為檯杩愯鏌ヨ纭畾鏁版嵁搴撴敮鎸佺殑鐗规т互鍙婂叾鍔熻兘鍜岄檺鍒躲備緥濡傦紝瀹冪‘瀹氾細

         鏀寔浠涔堝垪绫诲瀷

         鏀寔澶氬皯绱㈠紩

         鏀寔浠涔堝嚱鏁

         鏌ヨ鍙互澶氬ぇ

         VARCHAR鍒楀彲浠ュ澶

鍙互浠http://dev.mysql.com/tech-resources/crash-me.php鍙戠幇璁稿涓嶅悓鏁版嵁搴撴湇鍔″櫒鐨crash-me鐨勭粨鏋溿傚叧浜庡熀鍑嗘祴璇曠粨鏋滅殑璇︾粏淇℃伅锛岃闂http://dev.mysql.com/tech-resources/benchmarks/

7.1.5. 浣跨敤鑷繁鐨勫熀鍑

涓瀹氳娴嬭瘯搴旂敤绋嬪簭鍜屾暟鎹簱锛屼互鍙戠幇鐡堕鍦ㄥ摢鍎裤傞氳繃淇瀹(鎴栭氳繃鐢ㄤ竴涓滃搼妯″潡鈥濅唬鏇跨摱棰)锛屽彲浠ュ緢瀹规槗鍦扮‘瀹氫笅涓涓摱棰堛傚嵆浣夸綘鐨勫簲鐢ㄧ▼搴忕殑鏁翠綋鎬ц兘鐩墠鍙互鎺ュ彈锛岃嚦灏戝簲璇ュ姣忎釜鐡堕鍋氫竴涓鍒掞紝濡傛灉鏌愬ぉ纭疄闇瑕佹洿濂界殑鎬ц兘锛屽簲鐭ラ亾濡備綍瑙e喅瀹冦

鍏充簬涓浜涘彲绉绘鐨勫熀鍑嗙▼搴忕殑渚嬪瓙锛屽弬瑙MySQL鍩哄噯濂椾欢銆傝鍙傝7.1.4鑺傦紝鈥淢ySQL鍩哄噯濂椾欢鈥銆傚彲浠ュ埄鐢ㄨ繖涓浠剁殑浠讳綍绋嬪簭骞朵笖鏍规嵁浣犵殑闇瑕佷慨鏀瑰畠銆傞氳繃杩欐牱鍋氾紝鍙互灏濊瘯涓嶅悓鐨勯棶棰樼殑瑙e喅鏂规骞舵祴璇曞摢涓涓槸鏈濂界殑瑙e喅鏂规銆

鍙︿竴涓厤璐瑰熀鍑嗗浠舵槸寮鏀炬簮鐮佹暟鎹簱鍩哄噯濂椾欢锛屽弬瑙http://osdb.sourceforge.net/

鍦ㄧ郴缁熻礋杞界箒閲嶆椂鍑虹幇涓浜涢棶棰樻槸寰堟櫘閬嶇殑锛屽苟涓斿緢澶氬鎴峰凡缁忎笌鎴戜滑鑱旂郴浜嗭紝浠栦滑鍦ㄧ敓浜х郴缁熶腑鏈変竴涓(娴嬭瘯)绯荤粺骞朵笖鏈夎礋杞介棶棰樸傚ぇ澶氭暟鎯呭喌涓嬶紝鎬ц兘闂缁忚瘉鏄庢槸涓庡熀鏈暟鎹簱璁捐鏈夊叧鐨勯棶棰(渚嬪锛岃〃鎵弿鍦ㄩ珮璐熻浇鏃惰〃鐜颁笉濂)鎴栨搷浣滅郴缁熸垨搴撻棶棰樸傚鏋滅郴缁熷凡缁忎笉鍦ㄧ敓浜х郴缁熶腑锛屽畠浠ぇ澶氭暟灏瀹规槗淇銆

涓轰簡閬垮厤杩欐牱鐨勯棶棰橈紝搴旇鎶婂伐浣滈噸鐐规斁鍦ㄥ湪鍙兘鏈鍧忕殑璐熻浇涓嬫祴璇曚綘鐨勬暣涓簲鐢ㄧ▼搴忋備綘鍙互浣跨敤Super Smack銆傝宸ュ叿鍙互浠http://jeremy.zawodny.com/mysql/super-smack/鑾峰緱銆傛濡傚畠鐨勫悕瀛楁墍寤鸿锛屽畠鍙互鏍规嵁浣犵殑闇瑕佹彁渚涘悎鐞嗙殑绯荤粺锛屽洜姝ょ‘淇濆彧鐢ㄤ簬浣犵殑寮鍙戠郴缁熴

7.2. 浼樺寲SELECT璇彞鍜屽叾瀹冩煡璇

棣栧厛锛屽奖鍝嶆墍鏈夎鍙ョ殑涓涓洜绱犳槸锛氫綘鐨勮鍙缃緱瓒婂鏉傦紝鎵闇瑕佺殑寮閿瓒婂銆

鎵цGRANT璇彞鏃朵娇鐢ㄧ畝鍗曠殑璁稿彲锛屽綋瀹㈡埛鎵ц璇彞鏃讹紝鍙互浣MySQL闄嶄綆璁稿彲妫鏌ュ紑閿銆備緥濡傦紝濡傛灉鏈巿浜堜换浣曡〃绾ф垨鍒楃骇鏉冮檺锛屾湇鍔″櫒涓嶉渶瑕佹鏌tables_privcolumns_priv琛ㄧ殑鍐呭銆傚悓鏍峰湴锛屽鏋滀笉瀵逛换浣 璐︽埛杩涜闄愬埗锛屾湇鍔″櫒涓嶉渶瑕佸璧勬簮杩涜缁熻銆傚鏋滄煡璇㈤噺寰堥珮锛屽彲浠ヨ姳涓浜涙椂闂翠娇鐢ㄧ畝鍖栫殑鎺堟潈缁撴瀯鏉ラ檷浣庤鍙鏌ュ紑閿銆

濡傛灉浣犵殑闂鏄笌鍏蜂綋MySQL琛ㄨ揪寮忔垨鍑芥暟鏈夊叧锛屽彲浠ヤ娇鐢mysql瀹㈡埛绋嬪簭鎵甯︾殑BENCHMARK()鍑芥暟鎵ц瀹氭椂娴嬭瘯銆傚叾璇硶涓BENCHMARK(loop_count,expression)銆備緥濡傦細

mysql> SELECT BENCHMARK(1000000,1+1)
+------------------------+
| BENCHMARK(1000000,1+1) |
+------------------------+
|                      0 |
+------------------------+
1 row in set (0.32 sec)

涓婇潰缁撴灉鍦PentiumII 400MHz绯荤粺涓婅幏寰椼傚畠鏄剧ずMySQL鍦ㄨ绯荤粺涓婂湪0.32绉掑唴鍙互鎵ц1,000,000涓畝鍗曠殑+琛ㄨ揪寮忚繍绠椼

鎵鏈MySQL鍑芥暟搴旇琚珮搴︿紭鍖栵紝浣嗘槸鎬绘湁鍙兘鏈変竴浜涗緥澶栥BENCHMARK()鏄竴涓壘鍑烘槸鍚︽煡璇㈡湁闂鐨勪紭绉鐨勫伐鍏枫

7.2.1.聽EXPLAIN璇硶锛堣幏鍙朣ELECT鐩稿叧淇℃伅锛

EXPLAIN tbl_name

鎴栵細

EXPLAIN [EXTENDED] SELECT select_options

EXPLAIN璇彞鍙互鐢ㄤ綔DESCRIBE鐨勪竴涓悓涔夎瘝锛屾垨鑾峰緱鍏充簬MySQL濡備綍鎵цSELECT璇彞鐨勪俊鎭細

         EXPLAIN tbl_nameDESCRIBE tbl_nameSHOW COLUMNS FROM tbl_name鐨勪竴涓悓涔夎瘝銆

         濡傛灉鍦SELECT璇彞鍓嶆斁涓婂叧閿瘝EXPLAINMySQL灏嗚В閲婂畠濡備綍澶勭悊SELECT锛屾彁渚涙湁鍏宠〃濡備綍鑱旀帴鍜岃仈鎺ョ殑娆″簭銆

璇ヨ妭瑙i噴EXPLAIN鐨勭2涓敤娉曘

鍊熷姪浜EXPLAIN锛屽彲浠ョ煡閬撲粈涔堟椂鍊欏繀椤讳负琛ㄥ姞鍏ョ储寮曚互寰楀埌涓涓娇鐢ㄧ储寮曟潵瀵绘壘璁板綍鐨勬洿蹇殑SELECT

濡傛灉鐢变簬浣跨敤涓嶆纭殑绱㈠紩鍑虹幇浜嗛棶棰橈紝搴旇繍琛ANALYZE TABLE鏇存柊琛ㄧ殑缁熻锛堜緥濡傚叧閿瓧闆嗙殑鍔匡級锛岃繖鏍蜂細褰卞搷浼樺寲鍣ㄨ繘琛岀殑閫夋嫨銆傚弬瑙13.5.2.1鑺傦紝鈥淎NALYZE TABLE璇硶鈥

杩樺彲浠ョ煡閬撲紭鍖栧櫒鏄惁浠ヤ竴涓渶浣虫搴忚仈鎺ヨ〃銆備负浜嗗己鍒朵紭鍖栧櫒璁╀竴涓SELECT璇彞鎸夌収琛ㄥ懡鍚嶉『搴忕殑鑱旀帴娆″簭锛岃鍙ュ簲浠STRAIGHT_JOIN鑰屼笉鍙槸SELECT寮澶淬

EXPLAIN涓虹敤浜SELECT璇彞涓殑姣忎釜琛ㄨ繑鍥炰竴琛屼俊鎭傝〃浠ュ畠浠湪澶勭悊鏌ヨ杩囩▼涓皢琚MySQL璇诲叆鐨勯『搴忚鍒楀嚭銆MySQL鐢ㄤ竴閬嶆壂鎻忓娆¤仈鎺ワ紙single-sweep multi-join锛夌殑鏂瑰紡瑙e喅鎵鏈夎仈鎺ャ傝繖鎰忓懗鐫MySQL浠庣涓涓〃涓涓琛岋紝鐒跺悗鎵惧埌鍦ㄧ浜屼釜琛ㄤ腑鐨勪竴涓尮閰嶈锛岀劧鍚庡湪绗3涓〃涓瓑绛夈傚綋鎵鏈夌殑琛ㄥ鐞嗗畬鍚庯紝瀹冭緭鍑洪変腑鐨勫垪骞朵笖杩斿洖琛ㄦ竻鍗曠洿鍒版壘鍒颁竴涓湁鏇村鐨勫尮閰嶈鐨勮〃銆備粠璇ヨ〃璇诲叆涓嬩竴琛屽苟缁х画澶勭悊涓嬩竴涓〃銆

褰撲娇鐢EXTENDED鍏抽敭瀛楁椂锛EXPLAIN浜х敓闄勫姞淇℃伅锛屽彲浠ョ敤SHOW WARNINGS娴忚銆傝淇℃伅鏄剧ず浼樺寲鍣ㄩ檺瀹SELECT璇彞涓殑琛ㄥ拰鍒楀悕锛岄噸鍐欏苟涓旀墽琛屼紭鍖栬鍒欏悗SELECT璇彞鏄粈涔堟牱瀛愶紝骞朵笖杩樺彲鑳藉寘鎷紭鍖栬繃绋嬬殑鍏跺畠娉ㄨВ銆

EXPLAIN鐨勬瘡涓緭鍑鸿鎻愪緵涓涓〃鐨勭浉鍏充俊鎭紝骞朵笖姣忎釜琛屽寘鎷笅闈㈢殑鍒楋細

         id

SELECT璇嗗埆绗︺傝繖鏄SELECT鐨勬煡璇㈠簭鍒楀彿銆

         select_type

SELECT绫诲瀷锛屽彲浠ヤ负浠ヤ笅浠讳綍涓绉嶏細

o        SIMPLE

绠鍗SELECT(涓嶄娇鐢UNION鎴栧瓙鏌ヨ)

o        PRIMARY

鏈澶栭潰鐨SELECT

o        UNION

UNION涓殑绗簩涓垨鍚庨潰鐨SELECT璇彞

o        DEPENDENT UNION

UNION涓殑绗簩涓垨鍚庨潰鐨SELECT璇彞锛屽彇鍐充簬澶栭潰鐨勬煡璇

o        UNION RESULT

UNION鐨勭粨鏋溿

o        SUBQUERY

瀛愭煡璇腑鐨勭涓涓SELECT

o        DEPENDENT SUBQUERY

瀛愭煡璇腑鐨勭涓涓SELECT锛屽彇鍐充簬澶栭潰鐨勬煡璇

o        DERIVED

瀵煎嚭琛ㄧ殑SELECT(FROM瀛愬彞鐨勫瓙鏌ヨ)

         table

杈撳嚭鐨勮鎵寮曠敤鐨勮〃銆

         type

鑱旀帴绫诲瀷銆備笅闈㈢粰鍑哄悇绉嶈仈鎺ョ被鍨嬶紝鎸夌収浠庢渶浣崇被鍨嬪埌鏈鍧忕被鍨嬭繘琛屾帓搴忥細

o        system

琛ㄤ粎鏈変竴琛(=绯荤粺琛)銆傝繖鏄const鑱旀帴绫诲瀷鐨勪竴涓壒渚嬨

o        const

琛ㄦ渶澶氭湁涓涓尮閰嶈锛屽畠灏嗗湪鏌ヨ寮濮嬫椂琚鍙栥傚洜涓轰粎鏈変竴琛岋紝鍦ㄨ繖琛岀殑鍒楀煎彲琚紭鍖栧櫒鍓╀綑閮ㄥ垎璁や负鏄父鏁般const琛ㄥ緢蹇紝鍥犱负瀹冧滑鍙鍙栦竴娆★紒

const鐢ㄤ簬鐢ㄥ父鏁板兼瘮杈PRIMARY KEYUNIQUE绱㈠紩鐨勬墍鏈夐儴鍒嗘椂銆傚湪涓嬮潰鐨勬煡璇腑锛tbl_name鍙互鐢ㄤ簬const琛細

SELECT * from tbl_name WHERE primary_key=1
 
SELECT * from tbl_name
WHERE primary_key_part1=1primary_key_part2=2

o        eq_ref

瀵逛簬姣忎釜鏉ヨ嚜浜庡墠闈㈢殑琛ㄧ殑琛岀粍鍚堬紝浠庤琛ㄤ腑璇诲彇涓琛屻傝繖鍙兘鏄渶濂界殑鑱旀帴绫诲瀷锛岄櫎浜const绫诲瀷銆傚畠鐢ㄥ湪涓涓储寮曠殑鎵鏈夐儴鍒嗚鑱旀帴浣跨敤骞朵笖绱㈠紩鏄UNIQUEPRIMARY KEY

eq_ref鍙互鐢ㄤ簬浣跨敤= 鎿嶄綔绗︽瘮杈冪殑甯︾储寮曠殑鍒椼傛瘮杈冨煎彲浠ヤ负甯搁噺鎴栦竴涓娇鐢ㄥ湪璇ヨ〃鍓嶉潰鎵璇诲彇鐨勮〃鐨勫垪鐨勮〃杈惧紡銆

鍦ㄤ笅闈㈢殑渚嬪瓙涓紝MySQL鍙互浣跨敤eq_ref鑱旀帴鏉ュ鐞ref_tables

SELECT * FROM ref_table,other_table
  WHERE ref_table.key_column=other_table.column;
 
SELECT * FROM ref_table,other_table
  WHERE ref_table.key_column_part1=other_table.column
    AND ref_table.key_column_part2=1;

o        ref

瀵逛簬姣忎釜鏉ヨ嚜浜庡墠闈㈢殑琛ㄧ殑琛岀粍鍚堬紝鎵鏈夋湁鍖归厤绱㈠紩鍊肩殑琛屽皢浠庤繖寮犺〃涓鍙栥傚鏋滆仈鎺ュ彧浣跨敤閿殑鏈宸﹁竟鐨勫墠缂锛屾垨濡傛灉閿笉鏄UNIQUEPRIMARY KEY锛堟崲鍙ヨ瘽璇达紝濡傛灉鑱旀帴涓嶈兘鍩轰簬鍏抽敭瀛楅夋嫨鍗曚釜琛岀殑璇濓級锛屽垯浣跨敤ref銆傚鏋滀娇鐢ㄧ殑閿粎浠呭尮閰嶅皯閲忚锛岃鑱旀帴绫诲瀷鏄笉閿欑殑銆

ref鍙互鐢ㄤ簬浣跨敤=<=>鎿嶄綔绗︾殑甯︾储寮曠殑鍒椼

鍦ㄤ笅闈㈢殑渚嬪瓙涓紝MySQL鍙互浣跨敤ref鑱旀帴鏉ュ鐞ref_tables

SELECT * FROM ref_table WHERE key_column=expr;
 
SELECT * FROM ref_table,other_table
  WHERE ref_table.key_column=other_table.column;
 
SELECT * FROM ref_table,other_table
  WHERE ref_table.key_column_part1=other_table.column
    AND ref_table.key_column_part2=1;

o        ref_or_null

璇ヨ仈鎺ョ被鍨嬪鍚ref锛屼絾鏄坊鍔犱簡MySQL鍙互涓撻棬鎼滅储鍖呭惈NULL鍊肩殑琛屻傚湪瑙e喅瀛愭煡璇腑缁忓父浣跨敤璇ヨ仈鎺ョ被鍨嬬殑浼樺寲銆

鍦ㄤ笅闈㈢殑渚嬪瓙涓紝MySQL鍙互浣跨敤ref_or_null鑱旀帴鏉ュ鐞ref_tables

SELECT * FROM ref_table
WHERE key_column=expr OR key_column IS NULL;

鍙傝7.2.7鑺傦紝鈥淢ySQL濡備綍浼樺寲IS NULL

o        index_merge

璇ヨ仈鎺ョ被鍨嬭〃绀轰娇鐢ㄤ簡绱㈠紩鍚堝苟浼樺寲鏂规硶銆傚湪杩欑鎯呭喌涓嬶紝key鍒楀寘鍚簡浣跨敤鐨勭储寮曠殑娓呭崟锛key_len鍖呭惈浜嗕娇鐢ㄧ殑绱㈠紩鐨勬渶闀跨殑鍏抽敭鍏冪礌銆傝缁嗕俊鎭弬瑙7.2.6鑺傦紝鈥滅储寮曞悎骞朵紭鍖栤

o        unique_subquery

璇ョ被鍨嬫浛鎹簡涓嬮潰褰㈠紡鐨IN瀛愭煡璇㈢殑ref

value IN (SELECT primary_key FROM single_table WHERE some_expr)

unique_subquery鏄竴涓储寮曟煡鎵惧嚱鏁帮紝鍙互瀹屽叏鏇挎崲瀛愭煡璇紝鏁堢巼鏇撮珮銆

o        index_subquery

璇ヨ仈鎺ョ被鍨嬬被浼间簬unique_subquery銆傚彲浠ユ浛鎹IN瀛愭煡璇紝浣嗗彧閫傚悎涓嬪垪褰㈠紡鐨勫瓙鏌ヨ涓殑闈炲敮涓绱㈠紩锛

value IN (SELECT key_column FROM single_table WHERE some_expr)

o        range

鍙绱㈢粰瀹氳寖鍥寸殑琛岋紝浣跨敤涓涓储寮曟潵閫夋嫨琛屻key鍒楁樉绀轰娇鐢ㄤ簡鍝釜绱㈠紩銆key_len鍖呭惈鎵浣跨敤绱㈠紩鐨勬渶闀垮叧閿厓绱犮傚湪璇ョ被鍨嬩腑ref鍒椾负NULL

褰撲娇鐢=<>>>=<<=IS NULL<=>BETWEEN鎴栬IN鎿嶄綔绗︼紝鐢ㄥ父閲忔瘮杈冨叧閿瓧鍒楁椂锛屽彲浠ヤ娇鐢range

SELECT * FROM tbl_name
WHERE key_column = 10;
 
SELECT * FROM tbl_name
WHERE key_column BETWEEN 10 and 20;
 
SELECT * FROM tbl_name
WHERE key_column IN (10,20,30);
 
SELECT * FROM tbl_name
WHERE key_part1= 10 AND key_part2 IN (10,20,30);

o        index

璇ヨ仈鎺ョ被鍨嬩笌ALL鐩稿悓锛岄櫎浜嗗彧鏈夌储寮曟爲琚壂鎻忋傝繖閫氬父姣ALL蹇紝鍥犱负绱㈠紩鏂囦欢閫氬父姣旀暟鎹枃浠跺皬銆

褰撴煡璇㈠彧浣跨敤浣滀负鍗曠储寮曚竴閮ㄥ垎鐨勫垪鏃讹紝MySQL鍙互浣跨敤璇ヨ仈鎺ョ被鍨嬨

o        ALL

瀵逛簬姣忎釜鏉ヨ嚜浜庡厛鍓嶇殑琛ㄧ殑琛岀粍鍚堬紝杩涜瀹屾暣鐨勮〃鎵弿銆傚鏋滆〃鏄涓涓病鏍囪const鐨勮〃锛岃繖閫氬父涓嶅ソ锛屽苟涓旈氬父鍦ㄥ畠鎯呭喌涓宸傞氬父鍙互澧炲姞鏇村鐨勭储寮曡屼笉瑕佷娇鐢ALL锛屼娇寰楄鑳藉熀浜庡墠闈㈢殑琛ㄤ腑鐨勫父鏁板兼垨鍒楀艰妫绱㈠嚭銆

         possible_keys

possible_keys鍒楁寚鍑MySQL鑳戒娇鐢ㄥ摢涓储寮曞湪璇ヨ〃涓壘鍒拌銆傛敞鎰忥紝璇ュ垪瀹屽叏鐙珛浜EXPLAIN杈撳嚭鎵绀虹殑琛ㄧ殑娆″簭銆傝繖鎰忓懗鐫鍦possible_keys涓殑鏌愪簺閿疄闄呬笂涓嶈兘鎸夌敓鎴愮殑琛ㄦ搴忎娇鐢ㄣ

濡傛灉璇ュ垪鏄NULL锛屽垯娌℃湁鐩稿叧鐨勭储寮曘傚湪杩欑鎯呭喌涓嬶紝鍙互閫氳繃妫鏌WHERE瀛愬彞鐪嬫槸鍚﹀畠寮曠敤鏌愪簺鍒楁垨閫傚悎绱㈠紩鐨勫垪鏉ユ彁楂樹綘鐨勬煡璇㈡ц兘銆傚鏋滄槸杩欐牱锛屽垱閫犱竴涓傚綋鐨勭储寮曞苟涓斿啀娆$敤EXPLAIN妫鏌ユ煡璇€傚弬瑙13.1.2鑺傦紝鈥淎LTER TABLE璇硶鈥

涓轰簡鐪嬫竻涓寮犺〃鏈変粈涔堢储寮曪紝浣跨敤SHOW INDEX FROM tbl_name

         key

key鍒楁樉绀MySQL瀹為檯鍐冲畾浣跨敤鐨勯敭锛堢储寮曪級銆傚鏋滄病鏈夐夋嫨绱㈠紩锛岄敭鏄NULL銆傝鎯冲己鍒MySQL浣跨敤鎴栧拷瑙possible_keys鍒椾腑鐨勭储寮曪紝鍦ㄦ煡璇腑浣跨敤FORCE INDEXUSE INDEX鎴栬IGNORE INDEX銆傚弬瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥

瀵逛簬MyISAMBDB琛紝杩愯ANALYZE TABLE鍙互甯姪浼樺寲鍣ㄩ夋嫨鏇村ソ鐨勭储寮曘傚浜MyISAM琛紝鍙互浣跨敤myisamchk --analyze銆傚弬瑙13.5.2.1鑺傦紝鈥淎NALYZE TABLE璇硶鈥5.9.4鑺傦紝鈥滆〃缁存姢鍜屽穿婧冩仮澶嶁

         key_len

key_len鍒楁樉绀MySQL鍐冲畾浣跨敤鐨勯敭闀垮害銆傚鏋滈敭鏄NULL锛屽垯闀垮害涓NULL銆傛敞鎰忛氳繃key_len鍊兼垜浠彲浠ョ‘瀹MySQL灏嗗疄闄呬娇鐢ㄤ竴涓閮ㄥ叧閿瓧鐨勫嚑涓儴鍒嗐

         ref

ref鍒楁樉绀轰娇鐢ㄥ摢涓垪鎴栧父鏁颁笌key涓璧蜂粠琛ㄤ腑閫夋嫨琛屻

         rows

rows鍒楁樉绀MySQL璁や负瀹冩墽琛屾煡璇㈡椂蹇呴』妫鏌ョ殑琛屾暟銆

         Extra

璇ュ垪鍖呭惈MySQL瑙e喅鏌ヨ鐨勮缁嗕俊鎭備笅闈㈣В閲婁簡璇ュ垪鍙互鏄剧ず鐨勪笉鍚岀殑鏂囨湰瀛楃涓诧細

o        Distinct

MySQL鍙戠幇绗1涓尮閰嶈鍚庯紝鍋滄涓哄綋鍓嶇殑琛岀粍鍚堟悳绱㈡洿澶氱殑琛屻

o        Not exists

MySQL鑳藉瀵规煡璇㈣繘琛LEFT JOIN浼樺寲锛屽彂鐜1涓尮閰LEFT JOIN鏍囧噯鐨勮鍚庯紝涓嶅啀涓哄墠闈㈢殑鐨勮缁勫悎鍦ㄨ琛ㄥ唴妫鏌ユ洿澶氱殑琛屻

涓嬮潰鏄竴涓彲浠ヨ繖鏍蜂紭鍖栫殑鏌ヨ绫诲瀷鐨勪緥瀛愶細

SELECT * t1 LEFT JOIN t2 ON t1.id=t2.id
  WHERE t2.id IS NULL

鍋囧畾t2.id瀹氫箟涓NOT NULL銆傚湪杩欑鎯呭喌涓嬶紝MySQL浣跨敤t1.id鐨勫兼壂鎻t1骞舵煡鎵t2涓殑琛屻傚鏋MySQLt2涓彂鐜颁竴涓尮閰嶇殑琛岋紝瀹冪煡閬t2.id缁濅笉浼氫负NULL锛屽苟涓斾笉鍐嶆壂鎻t2鍐呮湁鐩稿悓鐨id鍊肩殑琛屻傛崲鍙ヨ瘽璇达紝瀵逛簬t1鐨勬瘡涓锛MySQL鍙渶瑕佸湪t2涓煡鎵句竴娆★紝鏃犺t2鍐呭疄闄呮湁澶氬皯鍖归厤鐨勮銆

o        range checked for each record (index map: #)

MySQL娌℃湁鍙戠幇濂界殑鍙互浣跨敤鐨勭储寮曪紝浣嗗彂鐜板鏋滄潵鑷墠闈㈢殑琛ㄧ殑鍒楀煎凡鐭ワ紝鍙兘閮ㄥ垎绱㈠紩鍙互浣跨敤銆傚鍓嶉潰鐨勮〃鐨勬瘡涓缁勫悎锛MySQL妫鏌ユ槸鍚﹀彲浠ヤ娇鐢rangeindex_merge璁块棶鏂规硶鏉ョ储鍙栬銆傚叧浜庨傜敤鎬ф爣鍑嗙殑鎻忚堪鍙傝7.2.5鑺傦紝鈥滆寖鍥翠紭鍖栤7.2.6鑺傦紝鈥滅储寮曞悎骞朵紭鍖栤锛屼笉鍚岀殑鏄墠闈㈣〃鐨勬墍鏈夊垪鍊煎凡鐭ュ苟涓旇涓烘槸甯搁噺銆

杩欏苟涓嶅緢蹇紝浣嗘瘮鎵ц娌℃湁绱㈠紩鐨勮仈鎺ヨ蹇緱澶氥

o        Using filesort

MySQL闇瑕侀澶栫殑涓娆′紶閫掞紝浠ユ壘鍑哄浣曟寜鎺掑簭椤哄簭妫绱㈣銆傞氳繃鏍规嵁鑱旀帴绫诲瀷娴忚鎵鏈夎骞朵负鎵鏈夊尮閰WHERE瀛愬彞鐨勮淇濆瓨鎺掑簭鍏抽敭瀛楀拰琛岀殑鎸囬拡鏉ュ畬鎴愭帓搴忋傜劧鍚庡叧閿瓧琚帓搴忥紝骞舵寜鎺掑簭椤哄簭妫绱㈣銆傚弬瑙7.2.12鑺傦紝鈥淢ySQL濡備綍浼樺寲ORDER BY

o        Using index

浠庡彧浣跨敤绱㈠紩鏍戜腑鐨勪俊鎭屼笉闇瑕佽繘涓姝ユ悳绱㈣鍙栧疄闄呯殑琛屾潵妫绱㈣〃涓殑鍒椾俊鎭傚綋鏌ヨ鍙娇鐢ㄤ綔涓哄崟涓绱㈠紩涓閮ㄥ垎鐨勫垪鏃讹紝鍙互浣跨敤璇ョ瓥鐣ャ

o        Using temporary

涓轰簡瑙e喅鏌ヨ锛MySQL闇瑕佸垱寤轰竴涓复鏃惰〃鏉ュ绾崇粨鏋溿傚吀鍨嬫儏鍐靛鏌ヨ鍖呭惈鍙互鎸変笉鍚屾儏鍐靛垪鍑哄垪鐨GROUP BYORDER BY瀛愬彞鏃躲

o        Using where

WHERE瀛愬彞鐢ㄤ簬闄愬埗鍝竴涓鍖归厤涓嬩竴涓〃鎴栧彂閫佸埌瀹㈡埛銆傞櫎闈炰綘涓撻棬浠庤〃涓储鍙栨垨妫鏌ユ墍鏈夎锛屽鏋Extra鍊间笉涓Using where骞朵笖琛ㄨ仈鎺ョ被鍨嬩负ALLindex锛屾煡璇㈠彲鑳戒細鏈変竴浜涢敊璇

濡傛灉鎯宠浣挎煡璇㈠敖鍙兘蹇紝搴旀壘鍑Using filesort Using temporaryExtra鍊笺

o        Using sort_union(...), Using union(...), Using intersect(...)

杩欎簺鍑芥暟璇存槑濡備綍涓index_merge鑱旀帴绫诲瀷鍚堝苟绱㈠紩鎵弿銆傝缁嗕俊鎭弬瑙7.2.6鑺傦紝鈥滅储寮曞悎骞朵紭鍖栤

o        Using index for group-by

绫讳技浜庤闂〃鐨Using index鏂瑰紡锛Using index for group-by琛ㄧずMySQL鍙戠幇浜嗕竴涓储寮曪紝鍙互鐢ㄦ潵鏌ヨGROUP BYDISTINCT鏌ヨ鐨勬墍鏈夊垪锛岃屼笉瑕侀澶栨悳绱㈢‖鐩樿闂疄闄呯殑琛ㄣ傚苟涓旓紝鎸夋渶鏈夋晥鐨勬柟寮忎娇鐢ㄧ储寮曪紝浠ヤ究瀵逛簬姣忎釜缁勶紝鍙鍙栧皯閲忕储寮曟潯鐩傝鎯呭弬瑙7.2.13鑺傦紝鈥淢ySQL濡備綍浼樺寲GROUP BY

閫氳繃鐩镐箻EXPLAIN杈撳嚭鐨rows鍒楃殑鎵鏈夊硷紝浣犺兘寰楀埌涓涓叧浜庝竴涓仈鎺ュ浣曠殑鎻愮ず銆傝繖搴旇绮楃暐鍦板憡璇変綘MySQL蹇呴』妫鏌ュ灏戣浠ユ墽琛屾煡璇€傚綋浣犱娇鐢max_join_size鍙橀噺闄愬埗鏌ヨ鏃讹紝涔熺敤杩欎釜涔樼Н鏉ョ‘瀹氭墽琛屽摢涓琛SELECT璇彞銆傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

涓嬪垪渚嬪瓙鏄剧ず鍑轰竴涓琛JOIN濡備綍鑳戒娇鐢EXPLAIN鎻愪緵鐨勪俊鎭愭琚紭鍖栥

鍋囧畾浣犳湁涓嬮潰鎵绀虹殑SELECT璇彞锛岃鍒掍娇鐢EXPLAIN鏉ユ鏌ュ畠锛

EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,
               tt.ProjectReference, tt.EstimatedShipDate,
               tt.ActualShipDate, tt.ClientID,
               tt.ServiceCodes, tt.RepetitiveID,
               tt.CurrentProcess, tt.CurrentDPPerson,
               tt.RecordVolume, tt.DPPrinted, et.COUNTRY,
               et_1.COUNTRY, do.CUSTNAME
        FROM tt, et, et AS et_1, do
        WHERE tt.SubmitTime IS NULL
          AND tt.ActualPC = et.EMPLOYID
          AND tt.AssignedPC = et_1.EMPLOYID
          AND tt.ClientID = do.CUSTNMBR;

瀵逛簬杩欎釜渚嬪瓙锛屽亣瀹氾細

         琚瘮杈冪殑鍒楀0鏄庡涓嬶細

鍒楃被鍨

tt

ActualPC

CHAR(10)

tt

AssignedPC

CHAR(10)

tt

ClientID

CHAR(10)

et

EMPLOYID

CHAR(15)

do

CUSTNMBR

CHAR(15)

         琛ㄦ湁涓嬮潰鐨勭储寮曪細

绱㈠紩

tt

ActualPC

tt

AssignedPC

tt

ClientID

et

EMPLOYID(涓婚敭)

do

CUSTNMBR(涓婚敭)

         tt.ActualPC鍊间笉鏄潎鍖鍒嗗竷鐨勩

寮濮嬶紝鍦ㄨ繘琛屼紭鍖栧墠锛EXPLAIN璇彞浜х敓涓嬪垪淇℃伅锛

 

table type possible_keys key  key_len ref  rows  Extra
et    ALL  PRIMARY       NULL NULL    NULL 74
do    ALL  PRIMARY       NULL NULL    NULL 2135
et_1  ALL  PRIMARY       NULL NULL    NULL 74
tt    ALL  AssignedPC,   NULL NULL    NULL 3872
           ClientID,
           ActualPC
      range checked for each record (key map: 35)
 

鍥犱负type瀵规瘡寮犺〃鏄ALL锛岃繖涓緭鍑烘樉绀MySQL姝e湪瀵规墍鏈夎〃浜х敓涓涓瑳鍗″皵涔樼Н锛涘嵆姣忎竴涓鐨勭粍鍚堬紒杩欏皢鑺辩浉褰撻暱鐨勬椂闂达紝鍥犱负蹇呴』妫鏌ユ瘡寮犺〃鐨勮鏁扮殑涔樼Н锛佸浜庝竴涓疄渚嬶紝杩欐槸74 * 2135 * 74 * 3872 = 45,268,558,720琛屻傚鏋滆〃鏇村ぇ锛屼綘鍙兘鎯宠薄瀹冨皢鑺卞闀挎椂闂粹︹

杩欓噷鐨勪竴涓棶棰樻槸MySQL鑳芥洿楂樻晥鍦板湪澹版槑鍏锋湁鐩稿悓绫诲瀷鍜屽昂瀵哥殑鍒椾笂浣跨敤绱㈠紩銆傚湪鏈枃涓紝VARCHARCHAR鏄浉鍚岀殑锛岄櫎闈炲畠浠0鏄庝负涓嶅悓鐨勯暱搴︺傚洜涓tt.ActualPC琚0鏄庝负CHAR(10)骞朵笖et.EMPLOYID琚0鏄庝负CHAR(15)锛岄暱搴︿笉鍖归厤銆

涓轰簡淇鍦ㄥ垪闀垮害涓婄殑涓嶅悓锛屼娇鐢ALTER TABLEActualPC鐨勯暱搴︿粠10涓瓧绗﹀彉涓15涓瓧绗︼細

mysql> ALTER TABLE tt MODIFY ActualPC VARCHAR(15);

鐜板湪tt.ActualPCet.EMPLOYID閮芥槸VARCHAR(15)锛屽啀鎵цEXPLAIN璇彞浜х敓杩欎釜缁撴灉锛

 

table type   possible_keys key     key_len ref         rows    Extra
tt    ALL    AssignedPC,   NULL    NULL    NULL        3872    Using
             ClientID,                                         where
             ActualPC
do    ALL    PRIMARY       NULL    NULL    NULL        2135
      range checked for each record (key map: 1)
et_1  ALL    PRIMARY       NULL    NULL    NULL        74
      range checked for each record (key map: 1)
et    eq_ref PRIMARY       PRIMARY 15      tt.ActualPC 1
 

杩欎笉鏄畬缇庣殑锛屼絾鏄ソ涓浜涗簡锛rows鍊肩殑涔樼Н灏戜簡涓涓洜瀛74銆傝繖涓増鏈湪鍑犵鍐呮墽琛屽畬銆

2绉嶆柟娉曡兘娑堥櫎tt.AssignedPC = et_1.EMPLOYIDtt.ClientID = do.CUSTNMBR姣旇緝鐨勫垪鐨勯暱搴﹀け閰嶉棶棰橈細

mysql> ALTER TABLE tt MODIFY AssignedPC VARCHAR(15),
    ->                MODIFY ClientID   VARCHAR(15);

EXPLAIN浜х敓鐨勮緭鍑烘樉绀哄湪涓嬮潰锛

table type   possible_keys key      key_len ref           rows Extra
et    ALL    PRIMARY       NULL     NULL    NULL          74
tt    ref    AssignedPC,   ActualPC 15      et.EMPLOYID   52   Using
             ClientID,                                         where
             ActualPC
et_1  eq_ref PRIMARY       PRIMARY  15      tt.AssignedPC 1
do    eq_ref PRIMARY       PRIMARY  15      tt.ClientID   1
 

杩欏嚑涔庡緢濂戒簡銆

鍓╀笅鐨勯棶棰樻槸锛岄粯璁ゆ儏鍐碉紝MySQL鍋囪鍦tt.ActualPC鍒楃殑鍊兼槸鍧囧寑鍒嗗竷鐨勶紝骞朵笖瀵tt琛ㄤ笉鏄繖鏍枫傚垢濂斤紝寰堝鏄撳憡璇MySQL鏉ュ垎鏋愬叧閿瓧鍒嗗竷锛

mysql> ANALYZE TABLE tt

鐜板湪鑱旀帴鏄滃畬缇庘濈殑浜嗭紝鑰屼笖EXPLAIN浜х敓杩欎釜缁撴灉锛

table type   possible_keys key     key_len ref           rows Extra
tt    ALL    AssignedPC    NULL    NULL    NULL          3872 Using
             ClientID,                                        where
             ActualPC
et    eq_ref PRIMARY       PRIMARY 15      tt.ActualPC   1
et_1  eq_ref PRIMARY       PRIMARY 15      tt.AssignedPC 1
do    eq_ref PRIMARY       PRIMARY 15      tt.ClientID   1

娉ㄦ剰鍦ㄤ粠EXPLAIN杈撳嚭鐨rows鍒楁槸涓涓潵鑷MySQL鑱旀帴浼樺寲鍣ㄧ殑鈥滄暀鑲茬寽娴嬧濄備綘搴旇妫鏌ユ暟瀛楁槸鍚︽帴杩戜簨瀹炪傚鏋滀笉鏄紝鍙互閫氳繃鍦SELECT璇彞閲岄潰浣跨敤STRAIGHT_JOIN骞朵笖璇曠潃鍦FROM瀛愬彞浠ヤ笉鍚岀殑娆″簭鍒楀嚭琛紝鍙兘寰楀埌鏇村ソ鐨勬ц兘銆

7.2.2. 浼拌鏌ヨ鎬ц兘

鍦ㄥぇ澶氭暟鎯呭喌涓嬶紝鍙互閫氳繃璁$畻纾佺洏鎼滅储鏉ヤ及璁℃ц兘銆傚灏忕殑琛紝閫氬父鑳藉湪1娆$鐩樻悳绱腑鎵惧埌琛(鍥犱负绱㈠紩鍙兘琚紦瀛)銆傚鏇村ぇ鐨勮〃锛屽彲浠ヤ娇鐢B锛嶆爲绱㈠紩杩涜浼拌锛屽皢闇瑕log(row_count)/log(index_block_length/3 * 2/(index_length + data_pointer_length))+1娆℃悳绱㈡墠鑳芥壘鍒拌銆

MySQL涓紝绱㈠紩鍧楅氬父鏄1024涓瓧鑺傦紝鏁版嵁鎸囬拡閫氬父鏄4涓瓧鑺傦紝杩欏浜庢湁涓涓暱搴︿负3(涓瓑鏁存暟)鐨勭储寮曠殑500,000琛岀殑琛紝閫氳繃鍏紡鍙互璁$畻鍑log(500,000)/log(1024/3*2/(3+4))+1= 4娆℃悳绱€

涓婇潰鐨勭储寮曢渶瑕佸ぇ绾500,000 * 7 * 3/2 = 5.2MB(鍋囪鍏稿瀷鎯呭喌涓嬬储寮曠紦瀛樺尯濉厖鐜囦负2/3)锛屽彲浠ュ皢澶ч儴鍒嗙储寮曚繚瀛樺湪鍐呭瓨涓紝浠呴渶瑕1-2璋冪敤浠OS璇绘暟鎹潵鎵惧嚭琛屻

鐒惰屽浜庡啓锛屽皢闇瑕4娆℃悳绱㈣姹(濡備笂)鏉ユ壘鍒板湪鍝効瀛樻斁鏂扮储寮曪紝骞朵笖閫氬父闇瑕2娆℃悳绱㈡潵鏇存柊杩欎釜绱㈠紩骞朵笖鍐欏叆琛屻

娉ㄦ剰锛屼笂杩拌璁哄苟涓嶆剰鍛崇潃搴旂敤绋嬪簭鐨勬ц兘灏嗙紦鎱㈠湴浠logN 閫鍖栵紒褰撹〃鏍煎彉寰楁洿澶ф椂锛屾墍鏈夊唴瀹圭紦瀛樺埌OSSQL鏈嶅姟鍣ㄥ悗锛屽皢浠呬粎鎴栧鎴栧皯鍦版洿鎱€傚湪鏁版嵁鍙樺緱澶ぇ涓嶈兘缂撳瓨鍚庯紝灏嗛愭笎鍙樺緱鏇存參锛岀洿鍒板簲鐢ㄧ▼搴忓彧鑳借繘琛岀鐩樻悳绱(logN澧炲姞)銆備负浜嗛伩鍏嶈繖涓棶棰橈紝闅忔暟鎹鍔犺屽鍔 閿珮閫熺紦鍐插尯澶у皬銆傚浜MyISAM, key_buffer_size绯荤粺鍙橀噺鎺у埗 閿珮閫熺紦鍐插尯澶у皬銆傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

7.2.3. SELECT鏌ヨ鐨勯熷害

鎬荤殑鏉ヨ锛岃鎯充娇涓涓緝鎱㈤SELECT ... WHERE鏇村揩锛屽簲棣栧厛妫鏌ユ槸鍚﹁兘澧炲姞涓涓储寮曘備笉鍚岃〃涔嬮棿鐨勫紩鐢ㄩ氬父閫氳繃绱㈠紩鏉ュ畬鎴愩備綘鍙互浣跨敤EXPLAIN璇彞鏉ョ‘瀹SELECT璇彞浣跨敤鍝簺绱㈠紩銆傚弬瑙7.4.5鑺傦紝鈥淢ySQL濡備綍浣跨敤绱㈠紩鈥7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級

涓嬮潰鏄竴浜涘姞閫熷MyISAM琛ㄧ殑鏌ヨ鐨勪竴鑸缓璁細

         涓轰簡甯姪MySQL鏇村ソ鍦颁紭鍖栨煡璇紝鍦ㄤ竴涓杞芥暟鎹悗鐨勮〃涓婅繍琛ANALYZE TABLEmyisamchk --analyze銆傝繖鏍蜂负姣忎竴涓储寮曟洿鏂版寚鍑烘湁鐩稿悓鍊肩殑琛岀殑骞冲潎琛屾暟鐨勫硷紙褰撶劧锛屽鏋滃彧鏈変竴涓储寮曪紝杩欐绘槸1銆傦級MySQL浣跨敤璇ユ柟娉曟潵鍐冲畾褰撲綘鑱旀帴涓や釜鍩轰簬闈炲父閲忚〃杈惧紡鐨勮〃鏃堕夋嫨鍝釜绱㈠紩銆備綘鍙互浣跨敤SHOW INDEX FROM tbl_name骞舵鏌Cardinality鍊兼潵妫鏌ヨ〃鍒嗘瀽缁撴灉銆myisamchk --description --verbose鍙互鏄剧ず绱㈠紩鍒嗗竷淇℃伅銆

         瑕佹兂鏍规嵁涓涓储寮曟帓搴忎竴涓储寮曞拰鏁版嵁锛屼娇鐢myisamchk --sort-index --sort-records=1锛堝鏋滀綘鎯宠鍦ㄧ储寮1涓婃帓搴忥級銆傚鏋滃彧鏈変竴涓储寮曪紝鎯宠鏍规嵁璇ョ储寮曠殑娆″簭璇诲彇鎵鏈夌殑璁板綍锛岃繖鏄娇鏌ヨ鏇村揩鐨勪竴涓ソ鏂规硶銆備絾鏄娉ㄦ剰锛岀涓娆″涓涓ぇ琛ㄦ寜鐓ц繖绉嶆柟娉曟帓搴忔椂灏嗚姳寰堥暱鏃堕棿锛

7.2.4. MySQL鎬庢牱浼樺寲WHERE瀛愬彞

璇ヨ妭璁ㄨ涓哄鐞WHERE瀛愬彞鑰岃繘琛岀殑浼樺寲銆備緥瀛愪腑浣跨敤浜SELECT璇彞锛屼絾鐩稿悓鐨勪紭鍖栦篃閫傜敤DELETEUPDATE璇彞涓殑WHERE瀛愬彞銆

璇锋敞鎰忓MySQL浼樺寲鍣ㄧ殑宸ヤ綔鍦ㄤ笉鏂繘琛屼腑锛屽洜姝よ鑺傚苟涓嶅畬鍠勩MySQL鎵ц浜嗗ぇ閲忕殑浼樺寲锛屾湰鏂囦腑鎵鍒楃殑骞朵笉璇﹀敖銆

涓嬮潰鍒楀嚭浜MySQL鎵ц鐨勯儴鍒嗕紭鍖栵細

         鍘婚櫎涓嶅繀瑕佺殑鎷彿锛

                        ((a AND b) AND c OR (((a AND b) AND (c AND d))))
                -> (a AND b AND c) OR (a AND b AND c AND d)

         甯搁噺閲嶅彔锛

                   (a<b AND b=c) AND a=5
                -> b>5 AND b=c AND a=5

         鍘婚櫎甯搁噺鏉′欢(鐢变簬甯搁噺閲嶅彔闇瑕)

                   (B>=5 AND B=5) OR (B=6 AND 5=5) OR (B=7 AND 5=6)
                -> B=5 OR B=6

         绱㈠紩浣跨敤鐨勫父鏁拌〃杈惧紡浠呰绠椾竴娆°

  • 瀵逛簬MyISAMHEAP琛紝鍦ㄤ竴涓崟涓〃涓婄殑娌℃湁涓涓WHERECOUNT(*)鐩存帴浠庤〃涓绱俊鎭傚綋浠呬娇鐢ㄤ竴涓〃鏃讹紝瀵NOT NULL琛ㄨ揪寮忎篃杩欐牱鍋氥
  • 鏃犳晥甯告暟琛ㄨ揪寮忕殑鏃╂湡妫娴嬨MySQL蹇熸娴嬫煇浜SELECT璇彞鏄笉鍙兘鐨勫苟涓斾笉杩斿洖琛屻
  • 濡傛灉涓嶄娇鐢GROUP BY鎴栧垎缁勫嚱鏁(COUNT()MIN()鈥︹)HAVINGWHERE鍚堝苟銆
  • 瀵逛簬鑱旀帴鍐呯殑姣忎釜琛紝鏋勯犱竴涓洿绠鍗曠殑WHERE浠ヤ究鏇村揩鍦板琛ㄨ繘琛WHERE璁$畻骞朵笖涔熷敖蹇烦杩囪褰曘
  • 鎵鏈夊父鏁扮殑琛ㄥ湪鏌ヨ涓瘮鍏跺畠琛ㄥ厛璇诲嚭銆傚父鏁拌〃涓猴細
    • 绌鸿〃鎴栧彧鏈1琛岀殑琛ㄣ
    • 涓庡湪涓涓PRIMARY KEYUNIQUE绱㈠紩鐨WHERE瀛愬彞涓璧蜂娇鐢ㄧ殑琛紝杩欓噷鎵鏈夌殑绱㈠紩閮ㄥ垎浣跨敤甯告暟琛ㄨ揪寮忓苟涓旂储寮曢儴鍒嗚瀹氫箟涓NOT NULL

涓嬪垪鐨勬墍鏈夎〃鐢ㄤ綔甯告暟琛細

mysql> SELECT * FROM t WHERE primary_key=1;
mysql> SELECT * FROM t1,t2
           WHERE t1.primary_key=1 AND t2.primary_key=t1.id;
  • 灏濊瘯鎵鏈夊彲鑳芥т究鍙互鎵惧埌琛ㄨ仈鎺ョ殑鏈濂借仈鎺ョ粍鍚堛傚鏋滄墍鏈夊湪ORDER BYGROUP BY鐨勫垪鏉ヨ嚜鍚屼竴涓〃锛岄偅涔堝綋鑱旀帴鏃讹紝璇ヨ〃棣栧厛琚変腑銆
  • 濡傛灉鏈変竴涓ORDER BY瀛愬彞鍜屼笉鍚岀殑GROUP BY瀛愬彞锛屾垨濡傛灉ORDER BYGROUP BY鍖呭惈鑱旀帴闃熷垪涓殑绗竴涓〃涔嬪鐨勫叾瀹冭〃鐨勫垪锛屽垯鍒涘缓涓涓复鏃惰〃銆
  • 濡傛灉浣跨敤SQL_SMALL_RESULTMySQL浣跨敤鍐呭瓨涓殑涓涓复鏃惰〃銆
  • 姣忎釜琛ㄧ殑绱㈠紩琚煡璇紝骞朵笖浣跨敤鏈濂界殑绱㈠紩锛岄櫎闈炰紭鍖栧櫒璁や负浣跨敤琛ㄦ壂鎻忔洿鏈夋晥銆傛槸鍚︿娇鐢ㄦ壂鎻忓彇鍐充簬鏄惁鏈濂界殑绱㈠紩璺ㄨ秺瓒呰繃30%鐨勮〃銆備紭鍖栧櫒鏇村姞澶嶆潅锛屽叾浼拌鍩轰簬鍏跺畠鍥犵礌锛屼緥濡傝〃澶у皬銆佽鏁板拰I/O鍧楀ぇ灏忥紝鍥犳鍥哄畾姣斾緥涓嶅啀鍐冲畾閫夋嫨浣跨敤绱㈠紩杩樻槸鎵弿銆
  • 鍦ㄤ竴浜涙儏鍐典笅锛MySQL鑳戒粠绱㈠紩涓鍑鸿锛岀敋鑷充笉鏌ヨ鏁版嵁鏂囦欢銆傚鏋滅储寮曚娇鐢ㄧ殑鎵鏈夊垪鏄暟鍊肩被锛岄偅涔堝彧浣跨敤绱㈠紩鏍戞潵杩涜鏌ヨ銆
  • 杈撳嚭姣忎釜璁板綍鍓嶏紝璺宠繃涓嶅尮閰HAVING瀛愬彞鐨勮銆

涓嬮潰鏄竴浜涘揩閫熸煡璇㈢殑渚嬪瓙锛

SELECT COUNT(*) FROM tbl_name;
 
SELECT MIN(key_part1),MAX(key_part1) FROM tbl_name;
 
SELECT MAX(key_part2) FROM tbl_name
    WHERE key_part1=constant;
 
SELECT ... FROM tbl_name
    ORDER BY key_part1,key_part2,... LIMIT 10;
 
SELECT ... FROM tbl_name
    ORDER BY key_part1 DESC, key_part2 DESC, ... LIMIT 10;

涓嬪垪鏌ヨ浠呬娇鐢ㄧ储寮曟爲灏卞彲浠ヨВ鍐(鍋囪绱㈠紩鐨勫垪涓烘暟鍊煎瀷)

SELECT key_part1,key_part2 FROM tbl_name WHERE key_part1=val;
 
SELECT COUNT(*) FROM tbl_name
    WHERE key_part1=val1 AND key_part2=val2;
 
SELECT key_part2 FROM tbl_name GROUP BY key_part1;

涓嬪垪鏌ヨ浣跨敤绱㈠紩鎸夋帓搴忛『搴忔绱㈣锛屼笉鐢ㄥ彟澶栫殑鎺掑簭锛

SELECT ... FROM tbl_name
    ORDER BY key_part1,key_part2,... ;
 
SELECT ... FROM tbl_name
    ORDER BY key_part1 DESC, key_part2 DESC, ... ;

7.2.5. 鑼冨洿浼樺寲

range璁块棶鏂规硶浣跨敤鍗曚竴绱㈠紩鏉ユ悳绱㈠寘鍚湪涓涓垨鍑犱釜绱㈠紩鍊艰窛绂诲唴鐨勮〃璁板綍鐨勫瓙闆嗐傚彲浠ョ敤浜庡崟閮ㄥ垎鎴栧鍏冪礌绱㈠紩銆傚悗闈㈢殑绔犺妭灏嗚缁嗘弿杩板浣曚粠WHERE瀛愬彞鎻愬彇鍖洪棿銆

7.2.5.1. 鍗曞厓绱犵储寮曠殑鑼冨洿璁块棶鏂规硶

瀵逛簬鍗曞厓绱犵储寮曪紝鍙互鐢WHERE瀛愬彞涓殑鐩稿簲鏉′欢寰堟柟渚垮湴琛ㄧず绱㈠紩鍊煎尯闂达紝鍥犳鎴戜滑绉颁负鑼冨洿鏉′欢鑰屼笉鏄滃尯闂粹濄

鍗曞厓绱犵储寮曡寖鍥存潯浠剁殑瀹氫箟濡備笅锛

         瀵逛簬BTREEHASH绱㈠紩锛屽綋浣跨敤=<=>INIS NULL鎴栬IS NOT NULL鎿嶄綔绗︽椂锛屽叧閿厓绱犱笌甯搁噺鍊肩殑姣旇緝鍏崇郴瀵瑰簲涓涓寖鍥存潯浠躲

         瀵逛簬BTREE绱㈠紩锛屽綋浣跨敤><>=<=BETWEEN!=鎴栬<>锛屾垨鑰LIKE 'pattern'(鍏朵腑 'pattern'涓嶄互閫氶厤绗﹀紑濮)鎿嶄綔绗︽椂锛屽叧閿厓绱犱笌甯搁噺鍊肩殑姣旇緝鍏崇郴瀵瑰簲涓涓寖鍥存潯浠躲

         瀵逛簬鎵鏈夌被鍨嬬殑绱㈠紩锛屽涓寖鍥存潯浠剁粨鍚ORAND鍒欎骇鐢熶竴涓寖鍥存潯浠躲

鍓嶉潰鎻忚堪鐨勨閲忓尖濈郴鎸囷細

         鏌ヨ瀛楃涓蹭腑鐨勫父閲

         鍚屼竴鑱旀帴涓殑constsystem琛ㄤ腑鐨勫垪

         鏃犲叧鑱斿瓙鏌ヨ鐨勭粨鏋

         瀹屽叏浠庡墠闈㈢被鍨嬬殑瀛愯〃杈惧紡缁勬垚鐨勮〃杈惧紡

涓嬮潰鏄竴浜WHERE瀛愬彞涓湁鑼冨洿鏉′欢鐨勬煡璇㈢殑渚嬪瓙锛

SELECT * FROM t1 
    WHERE key_col > 1 
    AND key_col < 10;
 
SELECT * FROM t1 
    WHERE key_col = 1 
    OR key_col IN (15,18,20);
 
SELECT * FROM t1 
    WHERE key_col LIKE 'ab%' 
    OR key_col BETWEEN 'bar' AND 'foo';
 

璇锋敞鎰忓湪甯搁噺浼犳挱闃舵閮ㄥ垎闈炲父閲忓煎彲浠ヨ浆鎹负甯告暟銆

MySQL灏濊瘯涓烘瘡涓彲鑳界殑绱㈠紩浠WHERE瀛愬彞鎻愬彇鑼冨洿鏉′欢銆傚湪鎻愬彇杩囩▼涓紝涓嶈兘鐢ㄤ簬鏋勬垚鑼冨洿鏉′欢鐨勬潯浠惰鏀惧純锛屼骇鐢熼噸鍙犺寖鍥寸殑鏉′欢缁勫悎鍒颁竴璧凤紝骞朵笖浜х敓绌鸿寖鍥寸殑鏉′欢琚垹闄ゃ

渚嬪锛岃冭檻涓嬮潰鐨勮鍙ワ紝鍏朵腑key1鏄湁绱㈠紩鐨勫垪锛nonkey娌℃湁绱㈠紩锛

SELECT * FROM t1 WHERE
   (key1 < 'abc' AND (key1 LIKE 'abcde%' OR key1 LIKE '%b')) OR
   (key1 < 'bar' AND nonkey = 4) OR
   (key1 < 'uux' AND key1 > 'z');

key1鐨勬彁鍙栬繃绋嬪涓嬶細

1.    鐢ㄥ師濮WHERE瀛愬彞寮濮嬶細

2.    (key1 < 'abc' AND (key1 LIKE 'abcde%' OR key1 LIKE '%b')) OR

3.     (key1 < 'bar' AND nonkey = 4) OR

4.     (key1 < 'uux' AND key1 > 'z')

5.    鍒犻櫎nonkey = 4key1 LIKE '%b'锛屽洜涓哄畠浠笉鑳界敤浜庤寖鍥存壂鎻忋傚垹闄ゅ畠浠殑姝g‘閫斿緞鏄敤TRUE鏇挎崲瀹冧滑锛屼互渚胯繘琛岃寖鍥存壂鎻忔椂涓嶄細涓㈠け鍖归厤鐨勮褰曘傜敤TRUE鏇挎崲瀹冧滑鍚庯紝鍙互寰楀埌锛

6.            (key1 < 'abc' AND (key1 LIKE 'abcde%' OR TRUE)) OR
7.            (key1 < 'bar' AND TRUE) OR
8.            (key1 < 'uux' AND key1 > 'z')

9.    鍙栨秷鎬绘槸涓truefalse鐨勬潯浠讹細

         (key1 LIKE 'abcde%' OR TRUE)鎬绘槸true

         (key1 < 'uux' AND key1 > 'z')鎬绘槸false

鐢ㄥ父閲忔浛鎹㈣繖浜涙潯浠讹紝鎴戜滑寰楀埌锛

(key1 < 'abc' AND TRUE) OR (key1 < 'bar' AND TRUE) OR (FALSE)

鍒犻櫎涓嶅繀瑕佺殑TRUEFALSE甯搁噺锛屾垜浠緱鍒

(key1 < 'abc') OR (key1 < 'bar')

10.灏嗛噸鍙犲尯闂寸粍鍚堟垚涓涓骇鐢熺敤浜庤寖鍥存壂鎻忕殑鏈缁堟潯浠讹細

11.        (key1 < 'bar')

鎬荤殑鏉ヨ(濡傚墠闈㈢殑渚嬪瓙鎵杩)锛岀敤浜庤寖鍥存壂鎻忕殑鏉′欢姣WHERE瀛愬彞闄愬埗灏戙MySQL鍐嶆墽琛屾鏌ヤ互杩囨护鎺夋弧瓒宠寖鍥存潯浠朵絾涓嶅畬鍏ㄦ弧瓒WHERE瀛愬彞鐨勮銆

鑼冨洿鏉′欢鎻愬彇绠楁硶鍙互澶勭悊宓屽鐨勪换鎰忔繁搴︾殑AND/OR缁撴瀯锛屽苟涓斿叾杈撳嚭涓嶄緷璧栨潯浠跺湪WHERE瀛愬彞涓嚭鐜扮殑椤哄簭銆

7.2.5.2. 澶氬厓绱犵储寮曠殑鑼冨洿璁块棶鏂规硶

澶氬厓绱犵储寮曠殑鑼冨洿鏉′欢鏄崟鍏冪礌绱㈠紩鐨勮寖鍥存潯浠剁殑鎵╁睍銆傚鍏冪礌绱㈠紩鐨勮寖鍥存潯浠跺皢绱㈠紩璁板綍闄愬埗鍒颁竴涓垨鍑犱釜鍏抽敭鍏冪粍鍐呫備娇鐢ㄧ储寮曠殑椤哄簭锛岄氳繃涓绯诲垪鍏抽敭鍏冪粍鏉ュ畾涔夊叧閿厓缁勫尯闂淬

渚嬪锛岃冭檻瀹氫箟涓key1(key_part1, key_part2, key_part3)鐨勫鍏冪礌绱㈠紩锛屼互鍙婁笅闈㈢殑鎸夊叧閿瓧椤哄簭鎵鍒楃殑鍏抽敭鍏冪粍锛

key_part1  key_part2  key_part3
  NULL       1          'abc'
  NULL       1          'xyz'
  NULL       2          'foo'
   1         1          'abc'
   1         1          'xyz'
   1         2          'abc'
   2         1          'aaa'
 

鏉′欢key_part1 = 1瀹氫箟浜嗕笅闈㈢殑鑼冨洿锛

(1-inf-inf) <= (key_part1key_part2key_part3) < (1+inf+inf)

鑼冨洿鍖呮嫭鍓嶉潰鏁版嵁闆嗕腑鐨勭456涓厓缁勶紝鍙互鐢ㄤ簬鑼冨洿璁块棶鏂规硶銆

閫氳繃瀵规瘮锛屾潯浠key_part3 = 'abc'涓嶅畾涔夊崟涓鐨勫尯闂达紝涓嶈兘鐢ㄤ簬鑼冨洿璁块棶鏂规硶銆

涓嬮潰鏇村姞璇︾粏鍦版弿杩颁簡鑼冨洿鏉′欢濡備綍鐢ㄤ簬澶氬厓绱犵储寮曚腑銆

         瀵逛簬HASH绱㈠紩锛屽彲浠ヤ娇鐢ㄥ寘鍚浉鍚屽肩殑姣忎釜鍖洪棿銆傝繖璇存槑鍖洪棿鍙兘鐢变笅闈㈠舰寮忕殑鏉′欢浜х敓锛

                     key_part1 cmp const1
                 AND key_part2 cmp const2
                 AND ...
                AND key_partN cmp constN;

杩欓噷锛const1const2...涓哄父閲忥紝cmp=<=>鎴栬IS NULL姣旇緝鎿嶄綔绗︿箣涓锛屾潯浠跺寘鎷墍鏈夌储寮曢儴鍒嗐(涔熷氨鏄锛屾湁N 涓潯浠讹紝姣忎竴涓搴N-鍏冪礌绱㈠紩鐨勬瘡涓儴鍒嗭級

鍏充簬甯搁噺鐨勫畾涔夛紝鍙傝7.2.5.1鑺傦紝鈥滃崟鍏冪礌绱㈠紩鐨勮寖鍥磋闂柟娉曗

渚嬪锛屼笅闈负涓夊厓绱HASH绱㈠紩鐨勮寖鍥存潯浠讹細

key_part1 = 1 AND key_part2 IS NULL AND key_part3 = 'foo'

         瀵逛簬BTREE绱㈠紩锛屽尯闂村彲浠ュ缁撳悎AND鐨勬潯浠舵湁鐢紝鍏朵腑姣忎釜鏉′欢鐢ㄤ竴涓父閲忓奸氳繃=<=>IS NULL><>=<=!=<>BETWEEN鎴栬LIKE 'pattern' (鍏朵腑'pattern'涓嶄互閫氶厤绗﹀紑澶)姣旇緝涓涓叧閿厓绱犮傚尯闂村彲浠ヨ冻澶熼暱浠ョ‘瀹氫竴涓寘鍚墍鏈夊尮閰嶆潯浠(鎴栧鏋滀娇鐢<>!=锛屼负涓や釜鍖洪棿)鐨勮褰曠殑鍗曚竴鐨勫叧閿厓缁勩備緥濡傦紝瀵逛簬鏉′欢锛

                  key_part1 = 'foo' AND key_part2 >= 10 AND key_part3 > 10

鍗曚竴鍖洪棿涓猴細

('foo'1010)
   < (key_part1key_part2key_part3)
      < ('foo'+inf+inf)

鍒涘缓鐨勫尯闂村彲浠ユ瘮鍘熸潯浠跺寘鍚洿澶氱殑璁板綍銆備緥濡傦紝鍓嶉潰鐨勫尯闂村寘鎷('foo'110)锛屼笉婊¤冻鍘熸潯浠躲

         濡傛灉鍖呭惈鍖洪棿鍐呯殑涓绯诲垪璁板綍鐨勬潯浠剁粨鍚堜娇鐢OR锛屽垯褰㈡垚鍖呮嫭涓绯诲垪鍖呭惈鍦ㄥ尯闂村苟闆嗙殑璁板綍鐨勪竴涓潯浠躲傚鏋滄潯浠剁粨鍚堜娇鐢ㄤ簡AND锛屽垯褰㈡垚鍖呮嫭涓绯诲垪鍖呭惈鍦ㄥ尯闂翠氦闆嗗唴鐨勮褰曠殑涓涓潯浠躲備緥濡傦紝瀵逛簬涓ら儴鍒嗙储寮曠殑鏉′欢锛

                (key_part1 = 1 AND key_part2 < 2)
                OR (key_part1 > 5)

鍖洪棿涓猴細

(1, -inf) < (key_part1, key_part2) < (1, 2)

(5, -inf) < (key_part1, key_part2)

鍦ㄨ渚嬪瓙涓紝绗1琛岀殑鍖洪棿宸︿晶鐨勭害鏉熶娇鐢ㄤ簡涓涓叧閿厓绱狅紝鍙充晶绾︽潫浣跨敤浜嗕袱涓叧閿厓绱犮傜2琛岀殑鍖洪棿鍙娇鐢ㄤ簡涓涓叧閿厓绱犮EXPLAIN杈撳嚭鐨key_len鍒楄〃绀烘墍浣跨敤鍏抽敭瀛楀墠缂鐨勬渶澶ч暱搴︺

鍦ㄦ煇浜涙儏鍐典腑锛key_len鍙互琛ㄧず浣跨敤鐨勫叧閿厓绱狅紝浣嗗彲鑳戒笉鏄綘鎵鏈熸湜鐨勩傚亣瀹key_part1key_part2鍙互涓NULL銆傚垯key_len鍒楁樉绀轰笅闈㈡潯浠剁殑涓や釜鍏抽敭鍏冪礌鐨勯暱搴︼細

key_part1 >= 1 AND key_part2 < 2

浣嗗疄闄呬笂锛岃鏉′欢鍙互鍙樻崲涓猴細

key_part1 >= 1 AND key_part2 IS NOT NULL

7.2.5.1鑺傦紝鈥滃崟鍏冪礌绱㈠紩鐨勮寖鍥磋闂柟娉曗鎻忚堪浜嗗浣曡繘琛屼紭鍖栦互缁撳悎鎴栧垹闄ゅ崟鍏冪礌绱㈠紩鑼冨洿鏉′欢鐨勫尯闂淬傚鍏冪礌绱㈠紩鑼冨洿鏉′欢鐨勫尯闂寸殑姝ラ绫讳技銆

7.2.6. 绱㈠紩鍚堝苟浼樺寲

绱㈠紩鍚堝苟鏂规硶鐢ㄤ簬閫氳繃range鎵弿鎼滅储琛屽苟灏嗙粨鏋滃悎鎴愪竴涓傚悎骞朵細浜х敓骞堕泦銆佷氦闆嗘垨鑰呮鍦ㄨ繘琛岀殑鎵弿鐨勪氦闆嗙殑骞堕泦銆

EXPLAIN杈撳嚭涓紝璇ユ柟娉曡〃鐜颁负type鍒楀唴鐨index_merge銆傚湪杩欑鎯呭喌涓嬶紝key鍒楀寘鍚竴鍒椾娇鐢ㄧ殑绱㈠紩锛key_len鍖呭惈杩欎簺绱㈠紩鐨勬渶闀跨殑鍏抽敭鍏冪礌銆

渚嬪锛

SELECT * FROM tbl_name WHERE key_part1 = 10 OR key_part2 = 20;

 

SELECT * FROM tbl_name

    WHERE (key_part1 = 10 OR key_part2 = 20) AND non_key_part=30;

 

SELECT * FROM t1, t2

    WHERE (t1.key1 IN (1,2) OR t1.key2 LIKE 'value%')

    AND t2.key1=t1.some_col;

 

SELECT * FROM t1, t2

    WHERE t1.key1=1

    AND (t2.key1=t1.some_col OR t2.key2=t1.some_col2);

 

绱㈠紩鍚堝苟鏂规硶鏈夊嚑绉嶈闂畻娉 (鍙傝EXPLAIN杈撳嚭鐨Extra瀛楁)

         浜ら泦

         鑱斿悎

         鎺掑簭骞堕泦

鍚庨潰鍑犺妭鏇村姞璇︾粏鍦版弿杩颁簡杩欎簺鏂规硶銆

娉ㄩ噴锛绱㈠紩鍚堝苟浼樺寲绠楁硶鍏锋湁浠ヤ笅鍑犱釜宸茬煡缂洪櫡锛

         濡傛灉鍙互瀵规煇浜涘叧閿瓧杩涜鑼冨洿鎵弿锛屽垯涓嶈冭檻绱㈠紩鍚堝苟銆備緥濡傦紝涓嬮潰鐨勬煡璇細

                SELECT * FROM t1 WHERE (goodkey1 < 10 OR goodkey2 < 20) AND badkey < 30;

瀵逛簬璇ユ煡璇紝鍙互鏈変袱涓柟妗堬細

1.    浣跨敤(goodkey1 < 10 OR goodkey2 < 20)鏉′欢杩涜绱㈠紩鍚堝苟鎵弿銆

2.    浣跨敤badkey < 30鏉′欢杩涜鑼冨洿鎵弿銆

鐒惰岋紝浼樺寲鍣ㄥ彧鑰冭檻绗2涓柟妗堛傚鏋滆繖涓嶆槸浣犳兂瑕佺殑锛屼綘鍙互閫氳繃浣跨敤IGNORE INDEXFORCE INDEX璁╀紭鍖栧櫒鑰冭檻index_merge銆備笅闈㈢殑鏌ヨ浣跨敤绱㈠紩鍚堝苟鎵ц锛

SELECT * FROM t1 FORCE INDEX(goodkey1,goodkey2)

WHERE (goodkey1 < 10 OR goodkey2 < 20) AND badkey < 30;

 

SELECT * FROM t1 IGNORE INDEX(badkey)

WHERE (goodkey1 < 10 OR goodkey2 < 20) AND badkey < 30;

         濡傛灉鏌ヨ鏈変竴涓鏉傜殑WHERE瀛愬彞锛屾湁杈冩繁鐨AND/OR宓屽鍏崇郴锛MySQL涓嶉夋嫨璇ヤ紭閫夋柟妗堬紝閫氳繃涓嬮潰鐨勮瘑鍒硶鍒欏皾璇曞垎甯冨悇鏉′欢锛

                (x AND y) OR z = (x OR z) AND (y OR z)
                (x OR y) AND z = (x AND z) OR (y AND z)

index_merge璁块棶鏂规硶鐨勪笉鍚屽彉閲忎箣闂寸殑閫夋嫨鍜屽叾瀹冭闂柟娉曞熀浜庡悇閫傜敤閫夐」鐨勬垚鏈及璁°

7.2.6.1. 绱㈠紩鍚堝苟浜ら泦璁块棶绠楁硶

璇ヨ闂畻娉曞彲浠ョ敤浜庡綋WHERE瀛愬彞缁撳悎AND琚浆鎹负涓嶅悓鐨勫叧閿瓧鐨勫嚑涓寖鍥存潯浠讹紝姣忎釜鏉′欢涓轰笅闈箣涓锛

         浠ヨ繖绉嶅舰寮忥紝鍗崇储寮曟湁纭垏鐨N閮ㄥ垎(鍗冲寘鎷簡鎵鏈夌储寮曢儴鍒)

                key_part1=const1 AND key_part2=const2 ... AND key_partN=constN

         浠讳綍InnoDBBDB琛ㄧ殑涓婚敭鐨勮寖鍥存潯浠躲

涓嬮潰鏄竴浜涗緥瀛愶細

SELECT * FROM innodb_table WHERE primary_key < 10 AND key_col1=20;

 

SELECT * FROM tbl_name

WHERE (key1_part1=1 AND key1_part2=2) AND key2=2;

绱㈠紩鍚堝苟浜ら泦绠楁硶鍚屾椂瀵规墍鏈変娇鐢ㄧ殑绱㈠紩杩涜鎵弿锛屽苟浜х敓浠庡悎骞剁殑绱㈠紩鎵弿鎺ユ敹鐨勮搴忓垪鐨勪氦闆嗐

濡傛灉浣跨敤鐨勭储寮曞寘鎷煡璇腑浣跨敤鐨勬墍鏈夊垪锛屾墍鏈夎〃璁板綍鍧囦笉鎼滅储锛屽苟涓斿湪杩欑鎯呭喌涓EXPLAIN鐨勮緭鍑哄寘鍚Extra瀛楁涓殑Using index銆備笅闈㈡槸涓涓绫绘煡璇㈢殑渚嬪瓙锛

SELECT COUNT(*) FROM t1 WHERE key1=1 AND key2=1;

濡傛灉浣跨敤鐨勭储寮曟湭鍖呮嫭鏌ヨ涓娇鐢ㄧ殑鎵鏈夊垪锛屽彧鏈夋弧瓒虫墍鏈変娇鐢ㄧ殑鍏抽敭瀛楃殑鑼冨洿鏉′欢鎵嶆悳绱㈡墍鏈夎褰曘

濡傛灉鏌愪釜鍚堝苟鏉′欢鏄InnoDBBDB琛ㄧ殑涓婚敭鐨勪竴涓潯浠讹紝涓嶇敤浜庤褰曟煡璇紝浣嗙敤浜庤繃婊や娇鐢ㄥ叾瀹冩潯浠舵悳绱㈢殑璁板綍銆

7.2.6.2. 绱㈠紩鍚堝苟骞堕泦璁块棶绠楁硶

璇ョ畻娉曠殑閫傜敤鏍囧噯绫讳技浜庣储寮曞悎骞舵柟娉曚氦闆嗙畻娉曠殑鏍囧噯銆傜畻娉曞彲浠ョ敤浜庡綋WHERE瀛愬彞缁撳悎OR琚浆鎹负涓嶅悓鐨勫叧閿瓧鐨勫嚑涓寖鍥存潯浠剁殑鏃跺欙紝姣忎釜鏉′欢涓轰笅闈箣涓锛

         浠ヨ繖绉嶅舰寮忥紝鍗崇储寮曟湁纭垏鐨N閮ㄥ垎(鍗冲寘鎷簡鎵鏈夌储寮曢儴鍒)

                key_part1=const1 AND key_part2=const2 ... AND key_partN=constN

         浠讳綍InnoDBBDB琛ㄧ殑涓婚敭鐨勮寖鍥存潯浠躲

         绱㈠紩鍚堝苟鏂规硶浜ら泦绠楁硶閫傜敤鐨勪竴涓潯浠躲

涓嬮潰鏄竴浜涗緥瀛愶細

SELECT * FROM t1 WHERE key1=1 OR key2=2 OR key3=3;
 
SELECT * FROM innodb_table WHERE (key1=1 AND key2=2) OR
  (key3='foo' AND key4='bar') AND key5=5;

7.2.6.3. 绱㈠紩鍚堝苟鎺掑簭骞堕泦璁块棶绠楁硶

璇ヨ闂畻娉曞彲浠ョ敤浜庡綋WHERE瀛愬彞缁撳悎OR琚浆鎹负涓嶅悓鐨勫叧閿瓧鐨勫嚑涓寖鍥存潯浠讹紝浣嗙储寮曞悎骞舵柟娉曡仈鍚堢畻娉曞苟涓嶉傜敤鐨勬椂鍊欍

涓嬮潰鏄竴浜涗緥瀛愶細

SELECT * FROM tbl_name WHERE key_col1 < 10 OR key_col2 < 20;
 
SELECT * FROM tbl_name
     WHERE (key_col1 > 10 OR key_col2 = 20) AND nonkey_col=30;

鎺掑簭鑱斿悎绠楁硶鍜岃仈鍚堢畻娉曠殑鍖哄埆鏄帓搴忚仈鍚堢畻娉曞繀椤诲厛绱㈠彇鎵鏈夎褰曠殑琛ID锛岀劧鍚庡湪杩斿洖璁板綍鍓嶅瀹冧滑杩涜鎺掑簭銆

7.2.7. MySQL濡備綍浼樺寲IS NULL

MySQL鍙互瀵瑰彲浠ョ粨鍚col_name = constant_value浣跨敤鐨col_name IS NULL杩涜鐩稿悓鐨勪紭鍖栥備緥濡傦紝MySQL鍙互浣跨敤绱㈠紩鍜岃寖鍥寸敤IS NULL鎼滅储NULL

SELECT * FROM tbl_name WHERE key_col IS NULL;
 
SELECT * FROM tbl_name WHERE key_col <=> NULL;
 
SELECT * FROM tbl_name
    WHERE key_col=const1 OR key_col=const2 OR key_col IS NULL;

濡傛灉WHERE瀛愬彞鍖呮嫭澹版槑涓NOT NULL鐨勫垪鐨col_name IS NULL鏉′欢锛岃〃杈惧紡鍒欎紭鍖栥傚綋鍒椾細浜х敓NULL鏃讹紝涓嶄細杩涜浼樺寲锛涗緥濡傦紝濡傛灉鏉ヨ嚜LEFT JOIN鍙充晶鐨勮〃銆

MySQL涔熷彲浠ヤ紭鍖栫粍鍚col_name = expr AND col_name IS NULL锛岃繖鏄В鍐冲瓙鏌ヨ鐨勪竴绉嶅父鐢ㄥ舰寮忋傚綋浣跨敤浼樺寲鏃EXPLAIN鏄剧ずref_or_null

璇ヤ紭鍖栧彲浠ヤ负浠讳綍鍏抽敭鍏冪礌澶勭悊IS NULL

涓嬮潰鏄竴浜涗紭鍖栫殑鏌ヨ渚嬪瓙锛屽亣瀹氳〃t2鐨勫垪ab鏈変竴涓储寮曪細

SELECT * FROM t1 WHERE t1.a=expr OR t1.a IS NULL;
 
SELECT * FROM t1, t2 WHERE t1.a=t2.a OR t2.a IS NULL;
 
SELECT * FROM t1, t2
    WHERE (t1.a=t2.a OR t2.a IS NULL) AND t2.b=t1.b;
 
SELECT * FROM t1, t2
    WHERE t1.a=t2.a AND (t2.b=t1.b OR t2.b IS NULL);
 
SELECT * FROM t1, t2
    WHERE (t1.a=t2.a AND t2.a IS NULL AND ...)
    OR (t1.a=t2.a AND t2.a IS NULL AND ...);

ref_or_null棣栧厛璇诲彇鍙傝冨叧閿瓧锛岀劧鍚庡崟鐙悳绱NULL鍏抽敭瀛楃殑琛屻

璇锋敞鎰忚浼樺寲鍙彲浠ュ鐞嗕竴涓IS NULL銆傚湪鍚庨潰鐨勬煡璇腑锛MySQL鍙琛ㄨ揪寮(t1.a=t2.a AND t2.a IS NULL)浣跨敤鍏抽敭瀛楁煡璇紝涓嶈兘浣跨敤b鐨勫叧閿厓绱狅細

SELECT * FROM t1, t2
     WHERE (t1.a=t2.a AND t2.a IS NULL)
     OR (t1.b=t2.b AND t2.b IS NULL);

7.2.8. MySQL濡備綍浼樺寲DISTINCT

鍦ㄨ澶氭儏鍐典笅缁撳悎ORDER BYDISTINCT闇瑕佷竴涓复鏃惰〃銆

璇锋敞鎰忓洜涓DISTINCT鍙兘浣跨敤GROUP BY锛屽繀椤绘竻妤MySQL濡備綍浣跨敤鎵閫夊畾鍒楃殑涓閮ㄥ垎鐨ORDER BYHAVING瀛愬彞涓殑鍒椼傚弬瑙12.10.3鑺傦紝鈥滃叿鏈夐殣鍚瓧娈电殑GROUP BY鈥

鍦ㄥぇ澶氭暟鎯呭喌涓嬶紝DISTINCT瀛愬彞鍙互瑙嗕负GROUP BY鐨勭壒娈婃儏鍐点備緥濡傦紝涓嬮潰鐨勪袱涓煡璇㈡槸绛夋晥鐨勶細

SELECT DISTINCT c1, c2, c3 FROM t1 WHERE c1 > const;
 
SELECT c1, c2, c3 FROM t1 WHERE c1 > const GROUP BY c1, c2, c3;

鐢变簬杩欎釜绛夋晥鎬э紝閫傜敤浜GROUP BY鏌ヨ鐨勪紭鍖栦篃閫傜敤浜庢湁DISTINCT瀛愬彞鐨勬煡璇€傝繖鏍凤紝鍏充簬DISTINCT鏌ヨ鐨勪紭鍖栫殑鏇磋缁嗙殑鎯呭喌锛屽弬瑙7.2.13鑺傦紝鈥淢ySQL濡備綍浼樺寲GROUP BY

缁撳悎LIMIT row_countDISTINCT鍚庯紝MySQL鍙戠幇鍞竴鐨row_count琛屽悗绔嬪嵆鍋滄銆

濡傛灉涓嶄娇鐢ㄦ煡璇腑鍛藉悕鐨勬墍鏈夎〃鐨勫垪锛MySQL鍙戠幇绗1涓尮閰嶅悗绔嬪嵆鍋滄鎵弿鏈娇鐢ㄧ殑琛ㄣ傚湪涓嬮潰鐨勬儏鍐典腑锛屽亣瀹t1t2涔嬪墠浣跨敤(鍙互鐢EXPLAIN妫鏌)锛屽彂鐜t2涓殑绗1琛屽悗锛MySQL涓嶅啀(t1涓殑浠讳綍琛)t2

SELECT DISTINCT t1.a FROM t1, t2 where t1.a=t2.a;

7.2.9. MySQL濡備綍浼樺寲LEFT JOIN鍜孯IGHT JOIN

MySQL涓紝A LEFT JOIN B join_condition鎵ц杩囩▼濡備笅锛

         鏍规嵁琛AA渚濊禆鐨勬墍鏈夎〃璁剧疆琛B

         鏍规嵁LEFT JOIN鏉′欢涓娇鐢ㄧ殑鎵鏈夎〃(闄や簡B)璁剧疆琛A

         LEFT JOIN鏉′欢鐢ㄤ簬纭畾濡備綍浠庤〃B鎼滅储琛屻(鎹㈠彞璇濊锛屼笉浣跨敤WHERE瀛愬彞涓殑浠讳綍鏉′欢锛

         鍙互瀵规墍鏈夋爣鍑嗚仈鎺ヨ繘琛屼紭鍖栵紝鍙槸鍙湁浠庡畠鎵渚濊禆鐨勬墍鏈夎〃璇诲彇鐨勮〃渚嬪銆傚鏋滃嚭鐜板惊鐜緷璧栧叧绯伙紝MySQL鎻愮ず鍑虹幇涓涓敊璇

         杩涜鎵鏈夋爣鍑WHERE浼樺寲銆

         濡傛灉A涓湁涓琛屽尮閰WHERE瀛愬彞锛屼絾B涓病鏈変竴琛屽尮閰ON鏉′欢锛屽垯鐢熸垚鍙︿竴涓B琛岋紝鍏朵腑鎵鏈夊垪璁剧疆涓NULL

         濡傛灉浣跨敤LEFT JOIN鎵惧嚭鍦ㄦ煇浜涜〃涓笉瀛樺湪鐨勮锛屽苟涓旇繘琛屼簡涓嬮潰鐨勬祴璇曪細WHERE閮ㄥ垎鐨col_name IS NULL锛屽叾涓col_name鏄竴涓0鏄庝负 NOT NULL鐨勫垪锛MySQL鎵惧埌鍖归厤LEFT JOIN鏉′欢鐨勪竴涓鍚庡仠姝(涓哄叿浣撶殑鍏抽敭瀛楃粍鍚)鎼滅储鍏跺畠琛屻

RIGHT JOIN鐨勬墽琛岀被浼LEFT JOIN锛屽彧鏄〃鐨勮鑹插弽杩囨潵銆

鑱旀帴浼樺寲鍣ㄨ绠楄〃搴旇仈鎺ョ殑椤哄簭銆LEFT JOINSTRAIGHT_JOIN寮哄埗鐨勮〃璇婚『搴忓彲浠ュ府鍔╄仈鎺ヤ紭鍖栧櫒鏇村揩鍦板伐浣滐紝鍥犱负妫鏌ョ殑琛ㄤ氦鎹㈡洿灏戙傝娉ㄦ剰杩欒鏄庡鏋滄墽琛屼笅闈㈢被鍨嬬殑鏌ヨ锛MySQL杩涜鍏ㄦ壂鎻b锛屽洜涓LEFT JOIN寮哄埗瀹冨湪d涔嬪墠璇诲彇锛

SELECT *
    FROM a,b LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key)
    WHERE b.key=d.key;

鍦ㄨ繖绉嶆儏鍐典笅淇鏃剁敤a鐨勭浉鍙嶉『搴忥紝b鍒椾簬FROM瀛愬彞涓細

SELECT *
    FROM b,a LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key)
    WHERE b.key=d.key;

MySQL鍙互杩涜涓嬮潰鐨LEFT JOIN浼樺寲锛氬鏋滃浜庝骇鐢熺殑NULL琛岋紝WHERE鏉′欢鎬讳负鍋囷紝LEFT JOIN鍙樹负鏅氳仈鎺ャ

渚嬪锛屽湪涓嬮潰鐨勬煡璇腑濡傛灉t2.column1NULLWHERE 瀛愬彞灏嗕负false

SELECT * FROM t1 LEFT JOIN t2 ON (column1) WHERE t2.column2=5;

鍥犳锛屽彲浠ュ畨鍏ㄥ湴灏嗘煡璇㈣浆鎹负鏅氳仈鎺ワ細

SELECT * FROM t1, t2 WHERE t2.column2=5 AND t1.column1=t2.column1;

杩欐牱鍙互鏇村揩锛屽洜涓哄鏋滃彲浠ヤ娇鏌ヨ鏇翠匠锛MySQL鍙互鍦ㄨ〃t1涔嬪墠浣跨敤琛t2銆備负浜嗗己鍒朵娇鐢ㄨ〃椤哄簭锛屼娇鐢STRAIGHT_JOIN

7.2.10. MySQL濡備綍浼樺寲宓屽Join

琛ㄧず鑱旀帴鐨勮娉曞厑璁稿祵濂楄仈鎺ャ備笅闈㈢殑璁ㄨ寮曠敤浜13.2.7.1鑺傦紝鈥淛OIN璇硶鈥涓弿杩扮殑鑱旀帴璇硶銆

SQL鏍囧噯姣旇緝锛table_factor璇硶宸茬粡鎵╁睍浜嗐傚悗鑰呭彧鎺ュ彈table_reference锛岃屼笉鏄嫭鍙峰唴鎵鍒楃殑銆

table_reference椤瑰垪琛ㄥ唴鐨勬瘡涓楀彿绛変环浜庡唴閮ㄨ仈鎺ワ紝杩欐槸涓涓繚鐣欐墿灞曞悕銆備緥濡傦細

SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
                 ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

绛変环浜庯細

SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4)
                 ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

MySQL涓紝CROSS JOIN璇硶涓婄瓑浠蜂簬INNER JOIN (瀹冧滑鍙互褰兼浠f浛銆傚湪鏍囧噯SQL涓紝瀹冧滑涓嶇瓑浠枫INNER JOIN缁撳悎ON瀛愬彞浣跨敤锛CROSS JOIN 鐢ㄤ簬鍏跺畠鍦版柟銆

鎬荤殑鏉ヨ锛屽湪鍙寘鍚唴閮ㄨ仈鎺ユ搷浣滅殑鑱旀帴琛ㄨ揪寮忎腑鍙互蹇界暐鎷彿銆傚垹闄ゆ嫭鍙峰苟灏嗘搷浣滅粍鍚堝埌宸︿晶鍚庯紝鑱旀帴琛ㄨ揪寮忥細

t1 LEFT JOIN (t2 LEFT JOIN t3 ON t2.b=t3.b OR t2.b IS NULL)
   ON t1.a=t2.a

杞崲涓鸿〃杈惧紡锛

(t1 LEFT JOIN t2 ON t1.a=t2.a) LEFT JOIN t3
    ON t2.b=t3.b OR t2.b IS NULL

浣嗘槸杩欎袱涓〃杈惧紡涓嶇瓑鏁堛傝璇存槑杩欑偣锛屽亣瀹氳〃t1t2t3鏈変笅闈㈢殑鐘舵侊細

         t1鍖呭惈琛{1}{2}

         t2鍖呭惈琛{1,101}

         t3鍖呭惈琛{101}

鍦ㄨ繖绉嶆儏鍐典笅锛岀1涓〃杈惧紡杩斿洖鍖呮嫭琛{1,1,101,101}{2,NULL,NULL,NULL}鐨勭粨鏋滐紝绗2涓〃杈惧紡杩斿洖琛{1,1,101,101}{2,NULL,NULL,101}

mysql> SELECT *
    -> FROM t1
    ->      LEFT JOIN
    ->      (t2 LEFT JOIN t3 ON t2.b=t3.b OR t2.b IS NULL)
    ->      ON t1.a=t2.a;
+------+------+------+------+
| a    | a    | b    | b    |
+------+------+------+------+
|    1 |    1 |  101 |  101 |
|    2 | NULL | NULL | NULL |
+------+------+------+------+
 
mysql> SELECT *
    -> FROM (t1 LEFT JOIN t2 ON t1.a=t2.a)
    ->      LEFT JOIN t3
    ->      ON t2.b=t3.b OR t2.b IS NULL;
+------+------+------+------+
| a    | a    | b    | b    |
+------+------+------+------+
|    1 |    1 |  101 |  101 |
|    2 | NULL | NULL |  101 |
+------+------+------+------+

鍦ㄤ笅闈㈢殑渚嬪瓙涓紝澶栭潰鐨勮仈鎺ユ搷浣滅粨鍚堝唴閮ㄨ仈鎺ユ搷浣滀娇鐢細

t1 LEFT JOIN (t2t3) ON t1.a=t2.a

璇ヨ〃杈惧紡涓嶈兘杞崲涓轰笅闈㈢殑琛ㄨ揪寮忥細

t1 LEFT JOIN t2 ON t1.a=t2.at3.

瀵逛簬缁欏畾鐨勮〃鐘舵侊紝绗1涓〃杈惧紡杩斿洖琛{1,1,101,101}{2,NULL,NULL,NULL}锛岀2涓〃杈惧紡杩斿洖琛{1,1,101,101}{2,NULL,NULL,101}

mysql> SELECT *
    -> FROM t1 LEFT JOIN (t2, t3) ON t1.a=t2.a;
+------+------+------+------+
| a    | a    | b    | b    |
+------+------+------+------+
|    1 |    1 |  101 |  101 |
|    2 | NULL | NULL | NULL |
+------+------+------+------+
 
mysql> SELECT *
    -> FROM t1 LEFT JOIN t2 ON t1.a=t2.a, t3;
+------+------+------+------+
| a    | a    | b    | b    |
+------+------+------+------+
|    1 |    1 |  101 |  101 |
|    2 | NULL | NULL |  101 |
+------+------+------+------+

鍥犳锛屽鏋滄垜浠拷鐣ヨ仈鎺ヨ〃杈惧紡涓殑鎷彿杩炲悓澶栭潰鐨勮仈鎺ユ搷浣滅锛屾垜浠細鏀瑰彉鍘熻〃杈惧紡鐨勭粨鏋溿

鏇寸‘鍒囧湴璇达紝鎴戜滑涓嶈兘蹇借宸﹀鑱旀帴鎿嶄綔鐨勫彸鎿嶄綔鏁板拰鍙宠仈鎺ユ搷浣滅殑宸︽搷浣滄暟涓殑鎷彿銆傛崲鍙ヨ瘽璇达紝鎴戜滑涓嶈兘蹇借澶栬仈鎺ユ搷浣滀腑鐨勫唴琛ㄨ揪寮忎腑鐨勬嫭鍙枫傚彲浠ュ拷瑙嗗叾瀹冩搷浣滄暟涓殑鎷彿(澶栭儴琛ㄧ殑鎿嶄綔鏁)

瀵逛簬浠讳綍琛t1t2t3鍜屽睘鎬t2.bt3.b鐨勪换浣曟潯浠P锛屼笅闈㈢殑琛ㄨ揪寮忥細

(t1,t2) LEFT JOIN t3 ON P(t2.b,t3.b)

绛変环浜庤〃杈惧紡

t1t2 LEFT JOIN t3 ON P(t2.b,t3.b)

濡傛灉鑱旀帴琛ㄨ揪寮(join_table)涓殑鑱旀帴鎿嶄綔鐨勬墽琛岄『搴忎笉鏄粠宸﹀埌鍙筹紝鎴戜滑鍒欏簲璁ㄨ宓屽鐨勮仈鎺ャ傝繖鏍凤紝涓嬮潰鐨勬煡璇細

SELECT * FROM t1 LEFT JOIN (t2 LEFT JOIN t3 ON t2.b=t3.b) ON t1.a=t2.a
  WHERE t1.a > 1
 
SELECT * FROM t1 LEFT JOIN (t2, t3) ON t1.a=t2.a
  WHERE (t2.b=t3.b OR t2.b IS NULL) AND t1.a > 1

鑱旀帴琛細

t2 LEFT JOIN t3 ON t2.b=t3.b
t2, t3

璁や负鏄祵濂楃殑銆傜1涓煡璇㈢粨鍚堝乏鑱旀帴鎿嶄綔鍒欏舰鎴愬祵濂楃殑鑱旀帴锛岃屽湪绗簩涓煡璇腑缁撳悎鍐呰仈鎺ユ搷浣滃舰鎴愬祵濂楄仈鎺ャ

鍦ㄧ1涓煡璇腑锛屾嫭鍙峰彲浠ュ拷鐣ワ細鑱旀帴琛ㄨ揪寮忕殑璇硶缁撴瀯涓庤仈鎺ユ搷浣滅殑鎵ц椤哄簭鐩稿悓銆備絾瀵逛簬绗2涓煡璇紝鎷彿涓嶈兘鐪佺暐锛屽敖绠″鏋滄病鏈夋嫭鍙凤紝杩欓噷鐨勮仈鎺ヨ〃杈惧紡瑙i噴涓嶆竻妤氥(鍦ㄥ閮ㄦ墿灞曡娉曚腑锛岄渶瑕佺2涓煡璇㈢殑(t2t3)鐨勬嫭鍙凤紝灏界浠庣悊璁轰笂瀵规煡璇㈠垎鏋愭椂涓嶉渶瑕佹嫭鍙凤細杩欎簺鏌ヨ鐨勮娉曠粨鏋勫皢浠嶇劧涓嶆竻妤氾紝鍥犱负LEFT JOINON灏嗗厖褰撹〃杈惧紡(t2,t3)鐨勫乏銆佸彸鐣屽畾绗︾殑瑙掕壊)

鍓嶉潰鐨勪緥瀛愯鏄庝簡杩欎簺鐐癸細

         瀵逛簬鍙寘鍚唴鑱旀帴(鑰岄潪澶栬仈鎺)鐨勮仈鎺ヨ〃杈惧紡锛屽彲浠ュ垹闄ゆ嫭鍙枫備綘鍙互绉婚櫎鎷彿骞朵粠宸﹀埌鍙宠瘎浼(鎴栧疄闄呬笂锛屼綘鍙互鎸変换浣曢『搴忚瘎浼拌〃)

         鎬荤殑鏉ヨ锛屽澶栬仈鎺ュ嵈涓嶆槸杩欐牱銆傚幓闄ゆ嫭鍙峰彲鑳戒細鏇存敼缁撴灉銆

         鎬荤殑鏉ヨ锛屽澶栬仈鎺ュ拰鍐呰仈鎺ョ殑缁撳悎锛屼篃涓嶆槸杩欐牱銆傚幓闄ゆ嫭鍙峰彲鑳戒細鏇存敼缁撴灉銆

鍚祵濂楀鑱旀帴鐨勬煡璇㈡寜鍚唴鑱旀帴鐨勬煡璇㈢殑鐩稿悓鐨勭閬撴柟寮忔墽琛屻傛洿纭垏鍦拌锛屽埄鐢ㄤ簡宓屽鐜仈鎺ョ畻娉曘傝鎴戜滑鍥炲繂宓屽鐜仈鎺ユ墽琛屾煡璇㈡椂閲囩敤浠涔堢畻娉曘

鍋囧畾鎴戜滑鏈変竴涓涓嬪舰寮忕殑琛T1T2T3鐨勮仈鎺ユ煡璇細

SELECT * FROM T1 INNER JOIN T2 ON P1(T1,T2)
                 INNER JOIN T3 ON P2(T2,T3)
  WHERE P(T1,T2,T3).

杩欓噷锛P1(T1,T2)P2(T3,T3)鏄竴浜涜仈鎺ユ潯浠(琛ㄨ揪寮)锛屽叾涓P(t1,t2,t3)鏄〃T1T2T3鐨勫垪鐨勪竴涓潯浠躲

宓屽鐜仈鎺ョ畻娉曞皢鎸変笅闈㈢殑鏂瑰紡鎵ц璇ユ煡璇細

 

FOR each row t1 in T1 {
  FOR each row t2 in T2 such that P1(t1,t2) {
    FOR each row t3 in T3 such that P2(t2,t3) {
      IF P(t1,t2,t3) {
         t:=t1||t2||t3; OUTPUT t;
      }
    }
  }
}

绗﹀彿t1||t2||t3琛ㄧず鈥杩炴帴琛t1t2t3鐨勫垪缁勬垚鐨勮鈥濄傚湪涓嬮潰鐨勪竴浜涗緥瀛愪腑锛屽嚭鐜拌鍚嶇殑NULL琛ㄧずNULL鐢ㄤ簬琛岀殑姣忎釜鍒椼備緥濡傦紝t1||t2||NULL琛ㄧず鈥杩炴帴琛t1t2鐨勫垪浠ュ強t3鐨勬瘡涓垪鐨NULL缁勬垚鐨勮鈥濄

鐜板湪璁╂垜浠冭檻甯﹀祵濂楃殑澶栬仈鎺ョ殑鏌ヨ锛

SELECT * FROM T1 LEFT JOIN
              (T2 LEFT JOIN T3 ON P2(T2,T3))
              ON P1(T1,T2)
  WHERE P(T1,T2,T3)

瀵逛簬璇ユ煡璇㈡垜浠慨鏀瑰祵濂楃幆妯″紡鍙互寰楀埌锛

FOR each row t1 in T1 {
  BOOL f1:=FALSE;
  FOR each row t2 in T2 such that P1(t1,t2) {
    BOOL f2:=FALSE;
    FOR each row t3 in T3 such that P2(t2,t3) {
      IF P(t1,t2,t3) {
        t:=t1||t2||t3; OUTPUT t;
      }
      f2=TRUE;
      f1=TRUE;
    }
    IF (!f2) {
      IF P(t1,t2,NULL) {
        t:=t1||t2||NULL; OUTPUT t;
      }
      f1=TRUE;
    }
  }
  IF (!f1) {
    IF P(t1,NULL,NULL) {
      t:=t1||NULL||NULL; OUTPUT t;
    }
  }
}
 

鎬荤殑鏉ヨ锛屽浜庡鑱旀帴鎿嶄綔涓殑绗竴涓唴琛ㄧ殑宓屽鐜紝寮曞叆浜嗕竴涓爣蹇楋紝鍦ㄧ幆涔嬪墠鍏抽棴骞朵笖鍦ㄧ幆涔嬪悗鎵撳紑銆傚鏋滃浜庡閮ㄨ〃鐨勫綋鍓嶈锛屽鏋滃尮閰嶈〃绀哄唴鎿嶄綔鏁扮殑琛紝鍒欐爣蹇楁墦寮銆傚鏋滃湪寰幆缁撳熬澶勬爣蹇椾粛鐒跺叧闂紝鍒欏浜庡閮ㄨ〃鐨勫綋鍓嶈锛屾病鏈夊彂鐜板尮閰嶃傚湪杩欑鎯呭喌涓嬶紝瀵逛簬鍐呰〃鐨勫垪锛屽簲浣跨敤NULL鍊艰ˉ鍏呰銆傜粨鏋滆琚紶閫掑埌杈撳嚭杩涜鏈缁堟鏌ユ垨浼犻掑埌涓嬩竴涓祵濂楃幆锛屼絾鍙兘鍦ㄨ婊¤冻鎵鏈夊祵鍏ュ紡澶栬仈鎺ョ殑鑱旀帴鏉′欢鏃躲

鍦ㄦ垜浠殑渚嬪瓙涓紝宓屽叆浜嗕笅闈㈣〃杈惧紡琛ㄧず鐨勫鑱旀帴琛細

(T2 LEFT JOIN T3 ON P2(T2,T3))

璇锋敞鎰忓浜庢湁鍐呰仈鎺ョ殑鏌ヨ锛屼紭鍖栧櫒鍙互閫夋嫨涓嶅悓鐨勫祵濂楃幆椤哄簭锛屼緥濡傦細

FOR each row t3 in T3 {
  FOR each row t2 in T2 such that P2(t2,t3) {
    FOR each row t1 in T1 such that P1(t1,t2) {
      IF P(t1,t2,t3) {
         t:=t1||t2||t3; OUTPUT t;
      }
    }
  }
}

瀵逛簬鏈夊鑱旀帴鐨勬煡璇紝浼樺寲鍣ㄥ彲浠ュ彧閫夋嫨杩欐牱鐨勯『搴忥細澶栬〃鐨勭幆浼樺厛浜庡唴琛ㄧ殑鐜傝繖鏍凤紝瀵逛簬鏈夊鑱旀帴鐨勬煡璇紝鍙彲鑳芥湁涓绉嶅祵濂楅『搴忋傚湪涓嬮潰鐨勬煡璇腑锛屼紭鍖栧櫒灏嗚瘎浼颁袱涓笉鍚岀殑宓屽锛

SELECT * T1 LEFT JOIN (T2,T3) ON P1(T1,T2) AND P2(T1,T3)
  WHERE P(T1,T2,T3)

宓屽涓猴細

FOR each row t1 in T1 {
  BOOL f1:=FALSE;
  FOR each row t2 in T2 such that P1(t1,t2) {
    FOR each row t3 in T3 such that P2(t1,t3) {
      IF P(t1,t2,t3) {
        t:=t1||t2||t3; OUTPUT t;
      }
      f1:=TRUE
    }
  }
  IF (!f1) {
    IF P(t1,NULL,NULL) {
      t:=t1||NULL||NULL; OUTPUT t;
    }
  }
}

FOR each row t1 in T1 {
  BOOL f1:=FALSE;
  FOR each row t3 in T3 such that P2(t1,t3) {
    FOR each row t2 in T2 such that P1(t1,t2) {
      IF P(t1,t2,t3) {
        t:=t1||t2||t3; OUTPUT t;
      }
      f1:=TRUE
    }
  }
  IF (!f1) {
    IF P(t1,NULL,NULL) {
      t:=t1||NULL||NULL; OUTPUT t;
    }
  }
}

鍦ㄤ袱涓祵濂椾腑锛屽繀椤诲湪澶栫幆涓鐞T1锛屽洜涓哄畠鐢ㄤ簬澶栬仈鎺ヤ腑銆T2T3鐢ㄤ簬鍐呰仈鎺ヤ腑锛屽洜姝よ仈鎺ュ繀椤诲湪鍐呯幆涓鐞嗐備絾鏄紝鍥犱负璇ヨ仈鎺ユ槸涓涓唴鑱旀帴锛T2T3鍙互浠ヤ换浣曢『搴忓鐞嗐

褰撹璁哄唴鑱旀帴宓屽鐜殑绠楁硶鏃讹紝鎴戜滑蹇界暐浜嗛儴鍒嗚鎯咃紝鍙兘瀵规煡璇㈡墽琛岀殑鎬ц兘鐨勫奖鍝嶄細寰堝ぇ銆傛垜浠病鏈夋彁鍙婃墍璋撶殑鈥滀笅鎺ㄢ濇潯浠躲傚亣瀹氬彲浠ョ敤杩炴帴鍏紡琛ㄧず鎴戜滑鐨WHERE鏉′欢P(T1,T2,T3)

P(T1,T2,T2) = C1(T1) AND C2(T2) AND C3(T3)

鍦ㄨ繖绉嶆儏鍐典笅锛MySQL瀹為檯浣跨敤浜嗕笅闈㈢殑宓屽鐜柟妗堟潵鎵ц甯﹀唴鑱旀帴寰楀埌鏌ヨ锛

FOR each row t1 in T1 such that C1(t1) {
  FOR each row t2 in T2 such that P1(t1,t2) AND C2(t2)  {
    FOR each row t3 in T3 such that P2(t2,t3) AND C3(t3) {
      IF P(t1,t2,t3) {
         t:=t1||t2||t3; OUTPUT t;
      }
    }
  }
}

浣犱細鐪嬭姣忎釜杩炴帴 C1(T1)C2(T2)C3(T3)琚粠鏈鍐呴儴鐨勭幆鍐呮帹鍑哄埌鍙互瀵瑰畠杩涜璇勪及鐨勬渶澶栫殑鐜腑銆傚鏋C1(T1)鏄竴涓檺鍒舵у緢寮虹殑鏉′欢锛屼笅鎺ㄦ潯浠跺彲浠ュぇ澶ч檷浣庝粠琛T1浼犻掑埌鍐呯幆鐨勮鏁般傜粨鏋滄槸鏌ヨ澶уぇ鍔犻熴

瀵逛簬鏈夊鑱旀帴鐨勬煡璇紝鍙湁鏌ュ嚭澶栬〃鐨勫綋鍓嶇殑琛屽彲浠ュ尮閰嶅唴琛ㄥ悗锛屾墠鍙互妫鏌WHERE鏉′欢銆傝繖鏍凤紝瀵瑰唴宓屽鐜笅鎺ㄧ殑鏉′欢涓嶈兘鐩存帴鐢ㄤ簬甯﹀鑱旀帴鐨勬煡璇€傝繖閲屾垜浠繀椤诲紩鍏ユ湁鏉′欢涓嬫帹鍓嶆彁锛岀敱閬囧埌鍖归厤鍚庢墦寮鐨勬爣蹇椾繚鎶ゃ

瀵逛簬甯︿笅闈㈢殑澶栬仈鎺ョ殑渚嬪瓙

P(T1,T2,T3)=C1(T1) AND C(T2) AND C3(T3)

浣跨敤鍙椾繚鎶ょ殑涓嬫帹鏉′欢鐨勫祵濂楃幆鏂规鐪嬭捣鏉ュ簲涓猴細

FOR each row t1 in T1 such that C1(t1) {
  BOOL f1:=FALSE;
  FOR each row t2 in T2
      such that P1(t1,t2) AND (f1?C2(t2):TRUE) {
    BOOL f2:=FALSE;
    FOR each row t3 in T3
        such that P2(t2,t3) AND (f1&&f2?C3(t3):TRUE) {
      IF (f1&&f2?TRUE:(C2(t2) AND C3(t3))) {
        t:=t1||t2||t3; OUTPUT t;
      }
      f2=TRUE;
      f1=TRUE;
    }
    IF (!f2) {
      IF (f1?TRUE:C2(t2) && P(t1,t2,NULL)) {
        t:=t1||t2||NULL; OUTPUT t;
      }
      f1=TRUE;
    }
  }
  IF (!f1 && P(t1,NULL,NULL)) {
      t:=t1||NULL||NULL; OUTPUT t;
  }
}

鎬荤殑鏉ヨ锛屽彲浠ヤ粠鑱旀帴鏉′欢锛堜緥濡P1(T1,T2)P(T2,T3)锛夋彁鍙栦笅鎺ㄥ墠鎻愩傚湪杩欑鎯呭喌涓嬶紝涓嬫帹鍓嶆彁涔熷彈涓涓爣蹇椾繚鎶わ紝闃叉妫鏌ョ敱鐩稿簲澶栬仈鎺ユ搷浣滄墍浜х敓鐨NULL-琛ュ厖鐨勮鐨勬柇瑷銆

璇锋敞鎰忓鏋滀粠鍒ゆ柇寮忕殑WHERE鏉′欢鎺ㄥ鍑猴紝鏍规嵁浠庝竴涓唴琛ㄥ埌鐩稿悓宓屽鑱旀帴鐨勫彟涓涓〃鐨勫叧閿瓧杩涜鐨勮闂绂佹銆(鍦ㄨ繖绉嶆儏鍐典笅锛屾垜浠彲浠ヤ娇鐢ㄦ湁鏉′欢鍏抽敭瀛楄闂紝浣嗘槸璇ユ妧鏈繕鏈敤浜MySQL 5.1涓級

7.2.11. MySQL濡備綍绠鍖栧閮ㄨ仈鍚

鍦ㄨ澶氭儏鍐典笅锛屼竴涓煡璇㈢殑FROM瀛愬彞鐨勮〃鐨勮〃杈惧紡鍙互绠鍖栥

鍦ㄥ垎鏋愰樁娈碉紝甯﹀彸澶栬仈鎺ユ搷浣滅殑鏌ヨ琚浆鎹负鍙寘鍚乏鑱旀帴鎿嶄綔鐨勭瓑鏁堟煡璇€傛荤殑鏉ヨ锛屾牴鎹互涓嬪師鍒欒繘琛岃浆鎹細

(T1, ...) RIGHT JOIN (T2,...) ON P(T1,...,T2,...) =
(T2, ...) LEFT JOIN (T1,...) ON P(T1,...,T2,...)

鎵鏈T1 INNER JOIN T2 ON P(T1,T2)褰㈠紡鐨勫唴鑱旀帴琛ㄨ揪寮忚鏇挎崲涓T1,T2P(T1,T2)骞舵牴鎹WHERE鏉′欢(鎴栧祵鍏ヨ繛鎺ョ殑鑱旀帴鏉′欢锛屽鏋滄湁)鑱旀帴涓轰竴涓繛鎺ャ

褰撲紭鍖栧櫒涓虹敤澶栬仈鎺ユ搷浣滅殑鑱旀帴鏌ヨ璇勪及鏂规鏃讹紝瀹冨彧鑰冭檻鍦ㄨ闂唴琛ㄤ箣鍓嶈闂琛ㄧ殑鎿嶄綔鐨勬柟妗堛備紭鍖栧櫒閫夐」鍙楀埌闄愬埗锛屽洜涓哄彧鏈夎繖鏍风殑鏂规鍏佽鎴戜滑鐢ㄥ祵濂楃幆鏈哄埗鎵ц甯﹀鑱旀帴鎿嶄綔鐨勬煡璇€

鍋囧畾鎴戜滑鏈変竴涓笅鍒楀舰寮忕殑鏌ヨ锛

SELECT * T1 LEFT JOIN T2 ON P1(T1,T2)

  WHERE P(T1,T2) AND R(T2)

R(T2)澶уぇ鍑忓皯浜嗚〃T2涓尮閰嶇殑琛屾暟銆傚鏋滄垜浠繖鏍锋墽琛屾煡璇紝浼樺寲鍣ㄥ皢涓嶄細鏈夊叾瀹冮夋嫨锛屽彧鑳藉湪璁块棶琛T2涔嬪墠璁块棶琛T1锛屼粠鑰屽鑷存墽琛屾柟妗堥潪甯镐綆銆

骞歌繍鐨勬槸锛屽鏋WHERE鏉′欢鎷掔粷nullMySQL鍙互灏嗘绫绘煡璇㈣浆鎹负娌℃湁澶栬仈鎺ユ搷浣滅殑鏌ヨ銆傚鏋滀负璇ユ搷浣滄瀯寤虹殑NULL琛ュ厖鐨勮璇勪及涓FALSEUNKNOWN锛屽垯璇ユ潯浠剁О涓哄浜庢煇涓鑱旀帴鎿嶄綔鎷掔粷null

鍥犳锛屽浜庤澶栬仈鎺ワ細

T1 LEFT JOIN T2 ON T1.A=T2.A

绫讳技涓嬮潰鐨勬潯浠朵负鎷掔粷null

T2.B IS NOT NULL,
T2.B > 3,
T2.C <= T1.C,
T2.B < 2 OR T2.C > 1

绫讳技涓嬮潰鐨勬潯浠朵笉涓烘嫆缁null

T2.B IS NULL,
T1.B < 3 OR T2.B IS NOT NULL,
T1.B < 3 OR T2.B > 3

妫鏌ヤ竴涓鑱旀帴鎿嶄綔鐨勬潯浠舵槸鍚︽嫆缁null鐨勬诲師鍒欏緢绠鍗曘備互涓嬫儏鍐典笅涓烘嫆缁null鐨勬潯浠讹細

         褰㈠紡涓A IS NOT NULL锛屽叾涓A鏄换浣曞唴琛ㄧ殑涓涓睘鎬

         鍖呭惈鍐呰〃寮曠敤鐨勫垽鏂紡锛屽綋鏌愪釜鍙傞噺涓NULL鏃惰瘎浼颁负UNKNOWN

         鍖呭惈鐢ㄤ簬杩炴帴鐨勬嫆缁null鐨勬潯浠剁殑鑱斿悎

         鎷掔粷null鐨勬潯浠剁殑閫昏緫鍜

涓涓潯浠跺彲浠ュ浜庝竴涓煡璇腑鐨勪竴涓鑱旀帴鎿嶄綔涓烘嫆缁null鐨勮屽浜庡彟涓涓笉涓烘嫆缁null鐨勩傚湪涓嬮潰鐨勬煡璇腑锛

SELECT * FROM T1 LEFT JOIN T2 ON T2.A=T1.A

                 LEFT JOIN T3 ON T3.B=T1.B

  WHERE T3.C > 0

WHERE鏉′欢瀵逛簬绗2涓鑱旀帴鎿嶄綔涓烘嫆缁null鐨勪絾瀵逛簬绗1涓笉涓烘嫆缁null鐨勩

濡傛灉WHERE鏉′欢瀵逛簬涓涓煡璇腑鐨勪竴涓鑱旀帴鎿嶄綔涓烘嫆缁null鐨勶紝澶栬仈鎺ユ搷浣滆涓涓唴鑱旀帴鎿嶄綔浠f浛銆

渚嬪锛屽墠闈㈢殑鏌ヨ琚笅闈㈢殑鏌ヨ浠f浛锛

SELECT * FROM T1 LEFT JOIN T2 ON T2.A=T1.A

                 INNER JOIN T3 ON T3.B=T1.B

  WHERE T3.C > 0

瀵逛簬鍘熸潵鐨勬煡璇紝浼樺寲鍣ㄥ皢璇勪及鍙笌涓涓闂『搴T1T2T3鍏煎鐨勬柟妗堛傚湪鏇挎崲鐨勬煡璇腑锛岃繕鑰冭檻浜嗚闂『搴T3T1T2

涓涓鑱旀帴鎿嶄綔鐨勮浆鍖栧彲浠ヨЕ鍙戝彟涓涓殑杞寲銆傝繖鏍凤紝鏌ヨ锛

SELECT * FROM T1 LEFT JOIN T2 ON T2.A=T1.A

                 LEFT JOIN T3 ON T3.B=T2.B

  WHERE T3.C > 0

灏嗛鍏堣浆鎹负鏌ヨ锛

SELECT * FROM T1 LEFT JOIN T2 ON T2.A=T1.A

                 INNER JOIN T3 ON T3.B=T2.B

  WHERE T3.C > 0

璇ユ煡璇㈢瓑鏁堜簬鏌ヨ锛

SELECT * FROM (T1 LEFT JOIN T2 ON T2.A=T1.A), T3

  WHERE T3.C > 0 AND T3.B=T2.B

鐜板湪鍓╀綑鐨勫鑱旀帴鎿嶄綔涔熷彲浠ヨ涓涓唴鑱旀帴鏇挎崲锛屽洜涓烘潯浠T3.B=T2.B涓烘嫆缁null鐨勶紝鎴戜滑鍙互寰楀埌涓涓牴鏈病鏈夊鑱旀帴鐨勬煡璇細

SELECT * FROM (T1 INNER JOIN T2 ON T2.A=T1.A), T3

  WHERE T3.C > 0 AND T3.B=T2.B

鏈夋椂鎴戜滑鍙互鎴愬姛鏇挎崲宓屽叆鐨勫鑱旀帴鎿嶄綔锛屼絾涓嶈兘杞崲宓屽叆鐨勫鑱旀帴銆備笅闈㈢殑鏌ヨ锛

SELECT * FROM T1 LEFT JOIN

              (T2 LEFT JOIN T3 ON T3.B=T2.B)

              ON T2.A=T1.A

  WHERE T3.C > 0

琚浆鎹负锛

SELECT * FROM T1 LEFT JOIN

              (T2 INNER JOIN T3 ON T3.B=T2.B)

              ON T2.A=T1.A

  WHERE T3.C > 0

鍙兘閲嶆柊鍐欎负浠嶇劧鍖呭惈宓屽叆寮忓鑱旀帴鎿嶄綔鐨勫舰寮忥細

SELECT * FROM T1 LEFT JOIN

              (T2,T3)

              ON (T2.A=T1.A AND T3.B=T2.B)

  WHERE T3.C > 0

濡傛灉璇曞浘杞崲涓涓煡璇腑鐨勫祵鍏ュ紡澶栬仈鎺ユ搷浣滐紝鎴戜滑蹇呴』鑰冭檻宓屽叆寮忓鑱旀帴鐨勮仈鎺ユ潯浠跺拰WHERE鏉′欢銆傚湪涓嬮潰鐨勬煡璇腑锛

SELECT * FROM T1 LEFT JOIN
              (T2 LEFT JOIN T3 ON T3.B=T2.B)
              ON T2.A=T1.A AND T3.C=T1.C
  WHERE T3.D > 0 OR T1.D > 0
WHERE鏉′欢瀵逛簬宓屽叆寮忓鑱旀帴涓嶄负鎷掔粷null鐨勶紝浣嗗祵鍏ュ紡澶栬仈鎺T2.A=T1.A AND T3.C=T1.C鐨勮仈鎺ユ潯浠朵负鎷掔粷null鍥犳璇ユ煡璇㈠彲浠ヨ浆鎹负

SELECT * FROM T1 LEFT JOIN

              (T2, T3)

              ON T2.A=T1.A AND T3.C=T1.C AND T3.B=T2.B

  WHERE T3.D > 0 OR T1.D > 0

7.2.12. MySQL濡備綍浼樺寲ORDER BY

鍦ㄦ煇浜涙儏鍐典腑锛MySQL鍙互浣跨敤涓涓储寮曟潵婊¤冻ORDER BY瀛愬彞锛岃屼笉闇瑕侀澶栫殑鎺掑簭銆

鍗充娇ORDER BY涓嶇‘鍒囧尮閰嶇储寮曪紝鍙WHERE瀛愬彞涓殑鎵鏈夋湭浣跨敤鐨勭储寮曢儴鍒嗗拰鎵鏈夐澶栫殑ORDER BY 鍒椾负甯告暟锛屽氨鍙互浣跨敤绱㈠紩銆備笅闈㈢殑鏌ヨ浣跨敤绱㈠紩鏉ヨВ鍐ORDER BY閮ㄥ垎锛

SELECT * FROM t1

    ORDER BY key_part1,key_part2,... ;

   

SELECT * FROM t1

    WHERE key_part1=constant

    ORDER BY key_part2;

   

SELECT * FROM t1

    ORDER BY key_part1 DESC, key_part2 DESC;

   

SELECT * FROM t1

    WHERE key_part1=1

    ORDER BY key_part1 DESC, key_part2 DESC;

鍦ㄦ煇浜涙儏鍐典笅锛MySQL涓嶈兘浣跨敤绱㈠紩鏉ヨВ鍐ORDER BY锛屽敖绠″畠浠嶇劧浣跨敤绱㈠紩鏉ユ壘鍒板尮閰WHERE瀛愬彞鐨勮銆傝繖浜涙儏鍐靛寘鎷細

         瀵逛笉鍚岀殑鍏抽敭瀛椾娇鐢ORDER BY

                SELECT * FROM t1 ORDER BY key1, key2

         瀵瑰叧閿瓧鐨勯潪杩炵画鍏冪礌浣跨敤ORDER BY

                SELECT * FROM t1 WHERE key2=constant ORDER BY key_part2

         娣峰悎ASCDESC

                SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC

         鐢ㄤ簬鏌ヨ琛岀殑鍏抽敭瀛椾笌ORDER BY涓墍浣跨敤鐨勪笉鐩稿悓锛

                SELECT * FROM t1 WHERE key2=constant ORDER BY key1

         浣犳鑱旀帴璁稿琛紝骞朵笖ORDER BY涓殑鍒楀苟涓嶆槸鍏ㄩ儴鏉ヨ嚜绗1涓敤浜庢悳绱㈣鐨勯潪甯搁噺琛ㄣ(杩欐槸EXPLAIN杈撳嚭涓殑娌℃湁const鑱旀帴绫诲瀷鐨勭1涓〃锛

         鏈変笉鍚岀殑ORDER BYGROUP BY琛ㄨ揪寮忋

         浣跨敤鐨勮〃绱㈠紩鐨勭被鍨嬩笉鑳芥寜椤哄簭淇濆瓨琛屻備緥濡傦紝瀵逛簬HEAP琛ㄧ殑HASH绱㈠紩鎯呭喌鍗冲姝ゃ

閫氳繃EXPLAIN SELECT ...ORDER BY锛屽彲浠ユ鏌MySQL鏄惁鍙互浣跨敤绱㈠紩鏉ヨВ鍐虫煡璇€傚鏋Extra鍒楀唴鏈Using filesort锛屽垯涓嶈兘瑙e喅鏌ヨ銆傚弬瑙7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級

鏂囦欢鎺掑簭浼樺寲涓嶄粎鐢ㄤ簬璁板綍鎺掑簭鍏抽敭瀛楀拰琛岀殑浣嶇疆锛屽苟涓旇繕璁板綍鏌ヨ闇瑕佺殑鍒椼傝繖鏍峰彲浠ラ伩鍏嶄袱娆¤鍙栬銆傛枃浠舵帓搴忕畻娉曠殑宸ヤ綔璞¤繖鏍凤細

1.    璇昏鍖归厤WHERE瀛愬彞鐨勮锛屽鍓嶉潰鎵绀恒

2.    瀵逛簬姣忎釜琛岋紝璁板綍鏋勬垚鎺掑簭鍏抽敭瀛楀拰琛屼綅缃殑涓绯诲垪鍊硷紝骞朵笖璁板綍鏌ヨ闇瑕佺殑鍒椼

3.    鏍规嵁鎺掑簭鍏抽敭瀛楁帓搴忓厓缁

4.    鎸夋帓搴忕殑椤哄簭妫绱㈣锛屼絾鐩存帴浠庢帓搴忕殑鍏冪粍璇诲彇闇瑕佺殑鍒楋紝鑰屼笉鏄啀涓娆¤闂〃銆

璇ョ畻娉曟瘮浠ュ墠鐗堟湰鐨Mysql鏈夊緢澶х殑鏀硅繘銆

涓轰簡閬垮厤閫熷害鍙樻參锛岃浼樺寲鍙敤浜庢帓搴忓厓缁勪腑鐨extra鍒楃殑鎬诲ぇ灏忎笉瓒呰繃max_length_for_sort_data绯荤粺鍙橀噺鍊肩殑鏃跺欍(灏嗚鍙橀噺璁剧疆寰楀お楂樼殑鐨勮抗璞℃槸灏嗙湅鍒扮‖鐩樻椿鍔ㄥお棰戠箒鑰CPU娲诲姩杈冧綆锛

濡傛灉鎯宠澧炲姞ORDER BY鐨勯熷害锛岄鍏堢湅鏄惁鍙互璁MySQL浣跨敤绱㈠紩鑰屼笉鏄澶栫殑鎺掑簭闃舵銆傚鏋滀笉鑳斤紝鍙互灏濊瘯涓嬮潰鐨勭瓥鐣ワ細

         澧炲姞sort_buffer_size鍙橀噺鐨勫ぇ灏忋

         澧炲姞read_rnd_buffer_size鍙橀噺鐨勫ぇ灏忋

         鏇存敼tmpdir鎸囧悜鍏锋湁澶ч噺绌洪棽绌洪棿鐨勪笓鐢ㄦ枃浠剁郴缁熴傝閫夐」鎺ュ彈鍑犱釜浣跨敤round-robin(寰幆)妯″紡鐨勮矾寰勩傚湪Unix涓矾寰勫簲鐢ㄥ啋鍙(:)鍖洪棿寮锛屽湪WindowsNetWareOS/2涓敤鍒嗗彿()銆傚彲浠ヤ娇鐢ㄨ鐗规у皢璐熻浇鍧囧垎鍒板嚑涓洰褰曚腑銆娉ㄩ噴锛璺緞搴斾负浣嶄簬涓嶅悓鐗╃悊纭洏涓婄殑鏂囦欢绯荤粺鐨勭洰褰曪紝鑰屼笉鏄悓涓纭洏鐨勪笉鍚岀殑鍒嗗尯銆

榛樿鎯呭喌涓嬶紝MySQL鎺掑簭鎵鏈GROUP BY col1col2...鏌ヨ鐨勬柟娉曞鍚屽湪鏌ヨ涓寚瀹ORDER BY col1col2...銆傚鏋滄樉寮忓寘鎷竴涓寘鍚浉鍚岀殑鍒楃殑ORDER BY瀛愬彞锛MySQL鍙互姣笉鍑忛熷湴瀵瑰畠杩涜浼樺寲锛屽敖绠′粛鐒惰繘琛屾帓搴忋傚鏋滄煡璇㈠寘鎷GROUP BY浣嗕綘鎯宠閬垮厤鎺掑簭缁撴灉鐨勬秷鑰楋紝浣犲彲浠ユ寚瀹ORDER BY NULL绂佹鎺掑簭銆備緥濡傦細

INSERT INTO foo

SELECT a, COUNT(*) FROM bar GROUP BY a ORDER BY NULL;

7.2.13. MySQL濡備綍浼樺寲GROUP BY

婊¤冻GROUP BY瀛愬彞鐨勬渶涓鑸殑鏂规硶鏄壂鎻忔暣涓〃骞跺垱寤轰竴涓柊鐨勪复鏃惰〃锛岃〃涓瘡涓粍鐨勬墍鏈夎搴斾负杩炵画鐨勶紝鐒跺悗浣跨敤璇ヤ复鏃惰〃鏉ユ壘鍒扮粍骞跺簲鐢ㄧ疮绉嚱鏁(濡傛灉鏈)銆傚湪鏌愪簺鎯呭喌涓紝MySQL鑳藉鍋氬緱鏇村ソ锛岄氳繃绱㈠紩璁块棶鑰屼笉鐢ㄥ垱寤轰复鏃惰〃銆

GROUP BY浣跨敤绱㈠紩鐨勬渶閲嶈鐨勫墠鎻愭潯浠舵槸 鎵鏈GROUP BY鍒楀紩鐢ㄥ悓涓绱㈠紩鐨勫睘鎬э紝骞朵笖绱㈠紩鎸夐『搴忎繚瀛樺叾鍏抽敭瀛(渚嬪锛岃繖鏄B-鏍戠储寮曪紝鑰屼笉鏄HASH绱㈠紩)銆傛槸鍚︾敤绱㈠紩璁块棶鏉ヤ唬鏇夸复鏃惰〃鐨勪娇鐢ㄨ繕鍙栧喅浜庡湪鏌ヨ涓娇鐢ㄤ簡鍝儴鍒嗙储寮曘佷负璇ラ儴鍒嗘寚瀹氱殑鏉′欢锛屼互鍙婇夋嫨鐨勭疮绉嚱鏁般

鏈変袱绉嶆柟娉曢氳繃绱㈠紩璁块棶鎵цGROUP BY鏌ヨ锛屽涓嬮潰鐨勭珷鑺傛墍鎻忚堪銆傚湪绗1涓柟娉曚腑锛岀粍鍚堟搷浣滅粨鍚堟墍鏈夎寖鍥村垽鏂紡浣跨敤(濡傛灉鏈)銆傜2涓柟娉曢鍏堟墽琛岃寖鍥存壂鎻忥紝鐒跺悗缁勫悎缁撴灉鍏冪粍銆

7.2.13.1. 鏉炬暎绱㈠紩鎵弿

浣跨敤绱㈠紩鏃舵渶鏈夋晥鐨勯斿緞鏄洿鎺ユ悳绱㈢粍鍩熴傞氳繃璇ヨ闂柟娉曪紝MySQL浣跨敤鏌愪簺鍏抽敭瀛楁帓搴忕殑绱㈠紩绫诲瀷(渚嬪锛B-)鐨勫睘鎬с傝灞炴у厑璁镐娇鐢 绱㈠紩涓殑鏌ユ壘缁勮屼笉闇瑕佽冭檻婊¤冻鎵鏈WHERE鏉′欢鐨勭储寮曚腑鐨勬墍鏈夊叧閿瓧銆傛棦鐒惰璁块棶鏂规硶鍙冭檻绱㈠紩涓殑鍏抽敭瀛楃殑涓灏忛儴鍒嗭紝瀹冭绉颁负鏉炬暎绱㈠紩鎵弿銆傚鏋滄病鏈WHERE瀛愬彞锛 鏉炬暎绱㈠紩鎵弿璇诲彇鐨勫叧閿瓧鏁伴噺涓庣粍鏁伴噺涓鏍峰锛屽彲浠ユ瘮鎵鏈夊叧閿瓧鏁板皬寰楀銆傚鏋WHERE瀛愬彞鍖呭惈鑼冨洿鍒ゆ柇寮(鍏充簬range鑱旀帴绫诲瀷鐨勮璁哄弬瑙7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級)锛 鏉炬暎绱㈠紩鎵弿鏌ユ壘婊¤冻鑼冨洿鏉′欢鐨勬瘡涓粍鐨勭1涓叧閿瓧锛屽苟涓斿啀娆¤鍙栧敖鍙兘鏈灏戞暟閲忕殑鍏抽敭瀛椼傚湪涓嬮潰鐨勬潯浠朵笅鏄彲浠ョ殑锛

         鏌ヨ閽堝涓涓崟琛ㄣ

         GROUP BY鍖呮嫭绱㈠紩鐨勭1涓繛缁儴鍒(濡傛灉瀵逛簬GROUP BY锛屾煡璇㈡湁涓涓DISTINCT瀛愬彞锛屽垯鎵鏈夋樉寮忓睘鎬ф寚鍚戠储寮曞紑澶)

         鍙娇鐢ㄧ疮绉嚱鏁(濡傛灉鏈)MIN()MAX()锛屽苟涓斿畠浠潎鎸囧悜鐩稿悓鐨勫垪銆

         绱㈠紩鐨勪换浣曞叾瀹冮儴鍒嗭紙闄や簡閭d簺鏉ヨ嚜鏌ヨ涓紩鐢ㄧ殑GROUP BY锛夊繀椤讳负甯告暟(涔熷氨鏄锛屽繀椤绘寜甯搁噺鏁伴噺鏉ュ紩鐢ㄥ畠浠)锛屼絾MIN()MAX() 鍑芥暟鐨勫弬鏁颁緥澶栥

姝ょ被鏌ヨ鐨EXPLAIN杈撳嚭鏄剧ずExtra鍒楃殑Using indexforgroup-by

涓嬮潰鐨勬煡璇㈡彁渚涜绫荤殑鍑犱釜渚嬪瓙锛屽亣瀹氳〃t1(c1,c2,c3,c4)鏈変竴涓储寮idx(c1c2c3)

SELECT c1, c2 FROM t1 GROUP BY c1, c2;

SELECT DISTINCT c1, c2 FROM t1;

SELECT c1, MIN(c2) FROM t1 GROUP BY c1;

SELECT c1, c2 FROM t1 WHERE c1 < const GROUP BY c1, c2;

SELECT MAX(c3), MIN(c3), c1, c2 FROM t1 WHERE c2 > const GROUP BY c1, c2;

SELECT c2 FROM t1 WHERE c1 < const GROUP BY c1, c2;

SELECT c1, c2 FROM t1 WHERE c3 = const GROUP BY c1, c2

鐢变簬涓婅堪鍘熷洜锛屼笉鑳界敤璇ュ揩閫熼夋嫨鏂规硶鎵ц涓嬮潰鐨勬煡璇細

1.      闄や簡MIN()MAX()杩樻湁鍏跺畠绱Н鍑芥暟锛屼緥濡傦細

     SELECT c1, SUM(c2) FROM t1 GROUP BY c1;

2.      GROUP BY瀛愬彞涓殑鍩熶笉寮曠敤绱㈠紩寮澶达紝濡備笅鎵绀猴細

     SELECT c1,c2 FROM t1 GROUP BY c2, c3;

3.      鏌ヨ寮曠敤浜GROUP BY閮ㄥ垎鍚庨潰鐨勫叧閿瓧鐨勪竴閮ㄥ垎锛屽苟涓旀病鏈夌瓑浜庡父閲忕殑绛夊紡锛屼緥濡傦細

     SELECT c1,c3 FROM t1 GROUP BY c1, c2

7.2.13.2. 绱у噾绱㈠紩鎵弿

绱у噾寮忕储寮曟壂鎻忓彲浠ヤ负绱㈠紩鎵弿鎴栦竴涓寖鍥寸储寮曟壂鎻忥紝鍙栧喅浜庢煡璇㈡潯浠躲

濡傛灉涓嶆弧瓒虫澗鏁g储寮曟壂鎻忔潯浠讹紝GROUP BY鏌ヨ浠嶇劧鍙互涓嶇敤鍒涘缓涓存椂琛ㄣ傚鏋WHERE瀛愬彞涓湁鑼冨洿鏉′欢锛岃鏂规硶鍙鍙栨弧瓒宠繖浜涙潯浠剁殑鍏抽敭瀛椼傚惁鍒欙紝杩涜绱㈠紩鎵弿銆傝鏂规硶璇诲彇鐢WHERE瀛愬彞瀹氫箟鐨勬瘡涓寖鍥寸殑鎵鏈夊叧閿瓧锛屾垨娌℃湁鑼冨洿鏉′欢寮忔壂鎻忔暣涓储寮曪紝鎴戜滑灏嗗畠瀹氫箟涓虹揣鍑戝紡绱㈠紩鎵弿銆傝娉ㄦ剰瀵逛簬绱у噾寮忕储寮曟壂鎻忥紝鍙湁鎵惧埌浜嗘弧瓒宠寖鍥存潯浠剁殑鎵鏈夊叧閿瓧鍚庢墠杩涜缁勫悎鎿嶄綔銆

瑕佹兂璁╄鏂规硶宸ヤ綔锛屽浜庡紩鐢GROUP BY鍏抽敭瀛楀厓绱犵殑鍓嶉潰銆佷腑闂村叧閿瓧鍏冪礌鐨勬煡璇腑鐨勬墍鏈夊垪锛屾湁涓涓父閲忕瓑寮忔潯浠跺嵆瓒冲浜嗐傜瓑寮忔潯浠朵腑鐨勫父閲忓~鍏呬簡鎼滅储鍏抽敭瀛椾腑鐨勨滃樊璺濃濓紝鍙互褰㈡垚瀹屾暣鐨勭储寮曞墠缂銆傝繖浜涚储寮曞墠缂鍙互鐢ㄤ簬绱㈠紩鏌ユ壘銆傚鏋滈渶瑕佹帓搴GROUP BY缁撴灉锛屽苟涓旇兘澶熷舰鎴愮储寮曞墠缂鐨勬悳绱㈠叧閿瓧锛MySQL杩樺彲浠ラ伩鍏嶉澶栫殑鎺掑簭鎿嶄綔锛屽洜涓轰娇鐢ㄦ湁椤哄簭鐨勭储寮曠殑鍓嶇紑杩涜鎼滅储宸茬粡鎸夐『搴忔绱㈠埌浜嗘墍鏈夊叧閿瓧銆

涓婅堪鐨勭涓绉嶆柟娉曚笉閫傚悎涓嬮潰鐨勬煡璇紝浣嗙2绉嶇储寮曡闂柟娉曞彲浠ュ伐浣(鍋囧畾鎴戜滑宸茬粡鎻愬強浜嗚〃t1鐨勭储寮idx)

         GROUP BY涓湁涓涓樊璺濓紝浣嗗凡缁忕敱鏉′欢c2 = 'a'瑕嗙洊銆

     SELECT c1c2c3 FROM t1 WHERE c2 = 'a' GROUP BY c1c3;

         GROUP BY涓嶄互鍏抽敭瀛楃殑绗1涓厓绱犲紑濮嬶紝浣嗘槸鏈変竴涓潯浠舵彁渚涜鍏冪礌鐨勫父閲忥細

     SELECT c1c2c3 FROM t1 WHERE c1 = 'a' GROUP BY c2c3;

7.2.14. MySQL濡備綍浼樺寲LIMIT

鍦ㄤ竴浜涙儏鍐典腑锛屽綋浣犱娇鐢LIMIT row_count鑰屼笉浣跨敤HAVING鏃讹紝MySQL灏嗕互涓嶅悓鏂瑰紡澶勭悊鏌ヨ銆

         濡傛灉浣犵敤LIMIT鍙夋嫨涓浜涜锛屽綋MySQL閫夋嫨鍋氬畬鏁寸殑琛ㄦ壂鎻忔椂锛屽畠灏嗗湪涓浜涙儏鍐典笅浣跨敤绱㈠紩銆

         濡傛灉浣犱娇鐢LIMIT row_countORDER BYMySQL涓鏃︽壘鍒颁簡鎺掑簭缁撴灉鐨勭涓涓row_count琛岋紝灏嗙粨鏉熸帓搴忚屼笉鏄帓搴忔暣涓〃銆傚鏋滀娇鐢ㄧ储寮曪紝灏嗗緢蹇傚鏋滃繀椤昏繘琛屾枃浠舵帓搴忥紙filesort锛夛紝蹇呴』閫夋嫨鎵鏈夊尮閰嶆煡璇㈡病鏈LIMIT瀛愬彞鐨勮锛屽苟涓斿湪纭畾宸茬粡鎵惧埌绗1row_count琛屽墠锛屽繀椤诲瀹冧滑鐨勫ぇ閮ㄥ垎杩涜鎺掑簭銆傚湪浠讳綍涓绉嶆儏鍐典笅锛屼竴鏃︽壘鍒颁簡琛岋紝鍒欎笉闇瑕佸啀鎺掑簭缁撴灉鐨勫叾瀹冮儴鍒嗭紝骞朵笖MySQL涓嶅啀杩涜鎺掑簭銆

         褰撶粨鍚LIMIT row_countDISTINCT鏃讹紝MySQL涓鏃︽壘鍒row_count涓敮涓鐨勮锛屽畠灏嗗仠姝€

         鍦ㄤ竴浜涙儏鍐典笅锛GROUP BY鑳介氳繃椤哄簭璇诲彇閿(鎴栧湪閿笂鍋氭帓搴)鏉ヨВ鍐筹紝鐒跺悗璁$畻鎽樿鐩村埌鍏抽敭瀛楃殑鍊兼敼鍙樸傚湪杩欑鎯呭喌涓嬶紝LIMIT row_count灏嗕笉璁$畻浠讳綍涓嶅繀瑕佺殑GROUP BY鍊笺

         鍙MySQL宸茬粡鍙戦佷簡闇瑕佺殑琛屾暟鍒板鎴凤紝瀹冨皢鏀惧純鏌ヨ锛岄櫎闈炰綘姝d娇鐢SQL_CALC_FOUND_ROWS

         LIMIT 0灏嗘绘槸蹇熻繑鍥炰竴涓┖闆嗗悎銆傝繖瀵规鏌ユ煡璇㈢殑鏈夋晥鎬ф槸鏈夌敤鐨勩傚綋浣跨敤MySQL API鏃讹紝瀹冧篃鍙互鐢ㄦ潵寰楀埌缁撴灉鍒楃殑鍒楃被鍨嬨(璇ユ妧宸у湪MySQL Monitor涓笉宸ヤ綔锛屽彧鏄剧ずEmpty set锛涘簲浣跨敤SHOW COLUMNSDESCRIBE

         褰撴湇鍔″櫒浣跨敤涓存椂琛ㄦ潵杩涜鏌ヨ鏃讹紝浣跨敤LIMIT row_count瀛愬彞鏉ヨ绠楅渶瑕佸灏戠┖闂淬

7.2.15. 濡備綍閬垮厤琛ㄦ壂鎻

EXPLAIN鐨勮緭鍑烘樉绀轰簡褰MySQL浣跨敤琛ㄦ壂鎻忔潵瑙e喅鏌ヨ鏃朵娇鐢ㄧ殑鎵鏈夌被鍨嬪垪銆傝繖閫氬父鍦ㄥ涓嬫潯浠朵笅鍙戠敓锛

         琛ㄥ緢灏忥紝鎵弿琛ㄦ瘮鏌ユ壘鍏抽敭瀛楅熷害蹇傝繖瀵逛簬灏戜簬10琛屽苟涓旇杈冪煭鐨勮〃姣旇緝鏅亶銆

         ONWHERE瀛愬彞涓病鏈夐傜敤鐨勭储寮曞垪鐨勭害鏉熴

         姝g敤甯搁噺鍊兼瘮杈冪储寮曞垪锛屽苟涓MySQL宸茬粡璁$畻鍒(鍩轰簬绱㈠紩鏍)甯告暟瑕嗙洊浜嗚〃鐨勫緢澶ч儴鍒嗗苟涓旇〃鎵弿灏嗕細姣旇緝蹇傚弬瑙7.2.4鑺傦紝鈥淢ySQL鎬庢牱浼樺寲WHERE瀛愬彞

         浣犳閫氳繃鍙︿竴涓垪浣跨敤涓涓綆鐨勯泦鐨勫娍鐨勫叧閿瓧(璁稿琛屽尮閰嶅叧閿瓧)銆傚湪杩欑鎯呭喌涓嬶紝MySQL鍋囪閫氳繃浣跨敤鍏抽敭瀛楀畠鍙兘浼氳繘琛岃澶氬叧閿瓧鏌ユ壘锛岃〃鎵弿灏嗕細鏇村揩銆

瀵逛簬灏忚〃锛岃〃鎵弿閫氬父鍚堥傘傚浜庡ぇ琛紝灏濊瘯涓嬮潰鐨勬妧宸т互閬垮厤浼樺寲鍣ㄩ敊閫変簡琛ㄦ壂鎻忥細

         浣跨敤ANALYZE TABLE tbl_name涓烘壂鎻忕殑琛ㄦ洿鏂板叧閿瓧鍒嗗竷銆傚弬瑙13.5.2.1鑺傦紝鈥淎NALYZE TABLE璇硶鈥

         瀵规壂鎻忕殑琛ㄤ娇鐢FORCE INDEX鍛婄煡MySQL锛岀浉瀵逛簬浣跨敤缁欏畾鐨勭储寮曡〃鎵弿灏嗛潪甯歌楁椂銆傚弬瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥

                SELECT * FROM t1, t2 FORCE INDEX (index_for_column)
                    WHERE t1.col_name=t2.col_name

         --max-seeks-for-key=1000閫夐」鍚姩mysqld鎴栦娇鐢SET max_seeks_for_key=1000鍛婄煡浼樺寲鍣ㄥ亣璁惧叧閿瓧鎵弿涓嶄細瓒呰繃1,000娆″叧閿瓧鎼滅储銆傚弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

7.2.16. INSERT璇彞鐨勯熷害

鎻掑叆涓涓褰曢渶瑕佺殑鏃堕棿鐢变笅鍒楀洜绱犵粍鎴锛屽叾涓殑鏁板瓧琛ㄧず澶х害姣斾緥锛

  • 杩炴帴锛(3)
  • 鍙戦佹煡璇㈢粰鏈嶅姟鍣細(2)
  • 鍒嗘瀽鏌ヨ锛(2)
  • 鎻掑叆璁板綍锛氾紙1x璁板綍澶у皬锛
  • 鎻掑叆绱㈠紩锛氾紙1x绱㈠紩锛
  • 鍏抽棴锛(1)

杩欎笉鑰冭檻鎵撳紑琛ㄧ殑鍒濆寮閿锛屾瘡涓苟鍙戣繍琛岀殑鏌ヨ鎵撳紑

琛ㄧ殑澶у皬浠logN (B)鐨勯熷害鍑忔參绱㈠紩鐨勬彃鍏ャ

鍔犲揩鎻掑叆鐨勪竴浜涙柟娉曪細

         濡傛灉鍚屾椂浠庡悓涓涓鎴风鎻掑叆寰堝琛岋紝浣跨敤鍚涓VALUEINSERT璇彞鍚屾椂鎻掑叆鍑犺銆傝繖姣斾娇鐢ㄥ崟琛INSERT璇彞蹇(鍦ㄦ煇浜涙儏鍐典笅蹇嚑鍊)銆傚鏋滀綘姝e悜涓涓潪绌鸿〃娣诲姞鏁版嵁锛屽彲浠ヨ皟鑺bulk_insert_buffer_size鍙橀噺锛屼娇鏁版嵁鎻掑叆鏇村揩銆傚弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

         濡傛灉浣犱粠涓嶅悓鐨勫鎴风鎻掑叆寰堝琛岋紝鑳介氳繃INSERT DELAYED璇彞鍔犲揩閫熷害銆傚弬瑙13.2.4鑺傦紝鈥淚NSERT璇硶鈥

         MyISAM锛屽鏋滃湪琛ㄤ腑娌℃湁鍒犻櫎鐨勮锛岃兘鍦SELECT璇彞姝e湪杩愯鐨勫悓鏃舵彃鍏ヨ銆

         褰撲粠涓涓枃鏈枃浠惰杞戒竴涓〃鏃讹紝浣跨敤LOAD DATA INFILE銆傝繖閫氬父姣斾娇鐢ㄥ緢澶INSERT璇彞蹇20鍊嶃傚弬瑙13.2.5鑺傦紝鈥淟OAD DATA INFILE璇硶鈥

         褰撹〃鏈夊緢澶氱储寮曟椂锛屾湁鍙兘瑕佸鍋氫簺宸ヤ綔浣垮緱LOAD DATA INFILE鏇村揩浜涖備娇鐢ㄤ笅鍒楄繃绋嬶細

    1. 鏈夐夋嫨鍦扮敤CREATE TABLE鍒涘缓琛ㄣ
    2. 鎵цFLUSH TABLES璇彞鎴栧懡浠mysqladmin flush-tables
    3. 浣跨敤myisamchk --keys-used=0 -rq /path/to/db/tbl_name銆傝繖灏嗕粠琛ㄤ腑鍙栨秷鎵鏈夌储寮曠殑浣跨敤銆
    4. LOAD DATA INFILE鎶婃暟鎹彃鍏ュ埌琛ㄤ腑锛屽洜涓轰笉鏇存柊浠讳綍绱㈠紩锛屽洜姝ゅ緢蹇
    5. 濡傛灉鍙兂鍦ㄤ互鍚庤鍙栬〃锛屼娇鐢myisampack鍘嬬缉瀹冦傚弬瑙15.1.3.3鑺傦紝鈥滃帇缂╄〃鐗规р
    6. myisamchk -r -q /path/to/db/tbl_name閲嶆柊鍒涘缓绱㈠紩銆傝繖灏嗗湪鍐欏叆纾佺洏鍓嶅湪鍐呭瓨涓垱寤虹储寮曟爲锛屽苟涓斿畠鏇村揩锛屽洜涓洪伩鍏嶄簡澶ч噺纾佺洏鎼滅储銆傜粨鏋滅储寮曟爲涔熻瀹岀編鍦板钩琛°
    7. 鎵цFLUSH TABLES璇彞鎴mysqladmin flush-tables鍛戒护銆

璇锋敞鎰忓鏋滄彃鍏ヤ竴涓┖MyISAM琛紝LOAD DATA INFILE涔熷彲浠ユ墽琛屽墠闈㈢殑浼樺寲锛涗富瑕佷笉鍚屽鏄彲浠ヨmyisamchk涓哄垱寤虹储寮曞垎閰嶆洿澶氱殑涓存椂鍐呭瓨锛屾瘮鎵цLOAD DATA INFILE璇彞鏃朵负鏈嶅姟鍣ㄩ噸鏂板垱寤虹储寮曞垎閰嶅緱瑕佸銆

涔熷彲浠ヤ娇鐢ALTER TABLE tbl_name DISABLE KEYS浠f浛myisamchk --keys-used=0 -rq /path/to/db/tbl_name锛屼娇鐢ALTER TABLE tbl_name ENABLE KEYS浠f浛myisamchk -r -q /path/to/db/tbl_name銆備娇鐢ㄨ繖绉嶆柟寮忥紝杩樺彲浠ヨ烦杩FLUSH TABLES

         閿佸畾琛ㄥ彲浠ュ姞閫熺敤澶氫釜璇彞鎵ц鐨INSERT鎿嶄綔锛

  • LOCK TABLES a WRITE;
  • INSERT INTO a VALUES (1,23),(2,34),(4,33);
  • INSERT INTO a VALUES (8,26),(6,29);
  • UNLOCK TABLES;

杩欐牱鎬ц兘浼氭彁楂橈紝鍥犱负绱㈠紩缂撳瓨鍖轰粎鍦ㄦ墍鏈INSERT璇彞瀹屾垚鍚庡埛鏂板埌纾佺洏涓婁竴娆°備竴鑸湁澶氬皯INSERT璇彞鍗虫湁澶氬皯绱㈠紩缂撳瓨鍖哄埛鏂般傚鏋滆兘鐢ㄤ竴涓鍙ユ彃鍏ユ墍鏈夌殑琛岋紝灏变笉闇瑕侀攣瀹氥

瀵逛簬浜嬪姟琛紝搴斾娇鐢BEGINCOMMIT浠f浛LOCK TABLES鏉ュ姞蹇彃鍏ャ

閿佸畾涔熷皢闄嶄綆澶氳繛鎺ユ祴璇曠殑鏁翠綋鏃堕棿锛屽敖绠″洜涓哄畠浠瓑鍊欓攣瀹氭渶澶х瓑寰呮椂闂村皢涓婂崌銆備緥濡傦細

Connection 1 does 1000 inserts

Connections 2, 3, and 4 do 1 insert

Connection 5 does 1000 inserts

濡傛灉涓嶄娇鐢ㄩ攣瀹氾紝234灏嗗湪15鍓嶅畬鎴愩傚鏋滀娇鐢ㄩ攣瀹氾紝234灏嗗彲鑳戒笉鍦15鍓嶅畬鎴愶紝浣嗘槸鏁翠綋鏃堕棿搴旇蹇ぇ绾40%

INSERTUPDATEDELETE鎿嶄綔鍦MySQL涓槸寰堝揩鐨勶紝閫氳繃涓哄湪涓琛屼腑澶氫簬澶х害5娆¤繛缁笉鏂湴鎻掑叆鎴栨洿鏂扮殑鎿嶄綔鍔犻攣锛屽彲浠ヨ幏寰楁洿濂界殑鏁翠綋鎬ц兘銆傚鏋滃湪涓琛屼腑杩涜澶氭鎻掑叆锛屽彲浠ユ墽琛LOCK TABLES锛岄殢鍚庣珛鍗虫墽琛UNLOCK TABLES(澶х害姣1000)浠ュ厑璁稿叾瀹冪殑绾跨▼璁块棶琛ㄣ傝繖涔熶細鑾峰緱濂界殑鎬ц兘銆

INSERT瑁呰浇鏁版嵁姣LOAD DATA INFILE瑕佹參寰楀锛屽嵆浣挎槸浣跨敤涓婅堪鐨勭瓥鐣ャ

         涓轰簡瀵LOAD DATA INFILEINSERTMyISAM琛ㄥ緱鍒版洿蹇殑閫熷害锛岄氳繃澧炲姞key_buffer_size绯荤粺鍙橀噺鏉ユ墿澶 閿珮閫熺紦鍐插尯銆傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

 

7.2.17. UPDATE璇彞鐨勯熷害

鏇存柊鏌ヨ鐨勪紭鍖栧悓SELECT鏌ヨ涓鏍凤紝闇瑕侀澶栫殑鍐欏紑閿銆傚啓閫熷害渚濊禆浜庢洿鏂扮殑鏁版嵁澶у皬鍜屾洿鏂扮殑绱㈠紩鐨勬暟閲忋傛病鏈夋洿鏀圭殑绱㈠紩涓嶈鏇存柊銆

浣挎洿鏀规洿蹇殑鍙︿竴涓柟娉曟槸鎺ㄨ繜鏇存敼鐒跺悗鍦ㄤ竴琛屽唴杩涜澶氭鏇存柊銆傚鏋滈攣瀹氳〃锛屽悓鏃跺仛澶氫釜鏇存柊姣斾竴娆″仛涓涓揩寰楀銆

璇锋敞鎰忓浣跨敤鍔ㄦ佽褰曟牸寮忕殑MyISAM琛紝鏇存柊涓涓緝闀挎婚暱鐨勮褰曞彲鑳戒細鍒囧垎璁板綍銆傚鏋滅粡甯歌繖鏍疯锛屽伓灏斾娇鐢OPTIMIZE TABLE寰堥噸瑕併傚弬瑙13.5.2.5鑺傦紝鈥淥PTIMIZE TABLE璇硶鈥

7.2.18. DELETE璇彞鐨勯熷害

鍒犻櫎涓涓褰曠殑鏃堕棿涓庣储寮曟暟閲忕‘鍒囨垚姝f瘮銆備负浜嗘洿蹇熷湴鍒犻櫎璁板綍锛屽彲浠ュ鍔犻敭楂橀熺紦鍐茬殑澶у皬銆傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

濡傛灉鎯宠鍒犻櫎涓涓〃鐨勬墍鏈夎锛屼娇鐢TRUNCATE TABLE tbl_name 鑰屼笉瑕佺敤DELETE FROM tbl_name銆傚弬瑙13.2.9鑺傦紝鈥淭RUNCATE璇硶鈥

7.2.19. 鍏跺畠浼樺寲鎶宸

璇ヨ妭鍒楀嚭浜嗘彁楂樻煡璇㈤熷害鐨勫悇绉嶆妧宸э細

         浣跨敤鎸佷箙鐨勮繛鎺ユ暟鎹簱浠ラ伩鍏嶈繛鎺ュ紑閿銆傚鏋滀笉鑳戒娇鐢ㄦ寔涔呯殑杩炴帴骞朵笖浣犳鍚姩璁稿鏂扮殑涓庢暟鎹簱鐨勮繛鎺ワ紝鍙兘瑕佹洿鏀thread_cache_size鍙橀噺鐨勫笺傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

         鎬绘槸妫鏌ユ墍鏈夋煡璇㈢‘瀹炰娇鐢ㄥ凡缁忓湪琛ㄤ腑鍒涘缓浜嗙殑绱㈠紩銆傚湪MySQL涓紝鍙互鐢EXPLAIN鍛戒护鍋氬埌銆傚弬瑙7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級

         灏濊瘯閬垮厤鍦ㄩ绻佹洿鏂扮殑琛ㄤ笂鎵ц澶嶆潅鐨SELECT鏌ヨ锛屼互閬垮厤涓庨攣瀹氳〃鏈夊叧鐨勭敱浜庤銆佸啓鍐茬獊鍙戠敓鐨勯棶棰樸

         瀵逛簬娌℃湁鍒犻櫎鐨勮鐨MyISAM琛紝鍙互鍦ㄥ彟涓涓煡璇㈡浠庤〃涓鍙栫殑鍚屾椂鍦ㄦ湯灏炬彃鍏ヨ銆傚鏋滆繖寰堥噸瑕侊紝搴旇冭檻鎸夌収閬垮厤鍒犻櫎琛岀殑鏂瑰紡浣跨敤琛ㄣ傚彟涓涓彲鑳芥ф槸鍦ㄥ垹闄ゅぇ閲忚鍚庤繍琛OPTIMIZE TABLE銆傚弬瑙15.1鑺傦紝鈥淢yISAM瀛樺偍寮曟搸鈥

         瑕佷慨澶嶄换浣ARCHIVE琛ㄥ彲浠ュ彂鐢熺殑鍘嬬缉闂锛屽彲浠ユ墽琛OPTIMIZE TABLE銆傚弬瑙15.8鑺傦紝鈥淎RCHIVE瀛樺偍寮曟搸鈥

         濡傛灉浣犱富瑕佹寜expr1expr2...椤哄簭妫绱㈣锛屼娇鐢ALTER TABLE ... ORDER BY expr1, expr2, ...銆傚琛ㄥぇ閲忔洿鏀瑰悗浣跨敤璇ラ夐」锛屽彲浠ヨ幏寰楁洿濂界殑鎬ц兘銆

         鍦ㄤ竴浜涙儏鍐典笅锛屼娇寰楀熀浜庢潵鑷叾瀹冭〃鐨勫垪鐨勪俊鎭紩鍏ヤ竴涓 鍝堝笇鈥濈殑鍒楁湁鎰忎箟銆傚鏋滆鍒楄緝鐭苟涓旀湁鍚堢悊鐨勫敮涓鍊硷紝瀹冨彲浠ユ瘮鍦ㄨ澶氬垪涓婄殑涓涓ぇ绱㈠紩蹇簺銆傚湪MySQL涓紝寰堝鏄撲娇鐢ㄨ繖涓澶栧垪锛

                SELECT * FROM tbl_name
                     WHERE hash_col=MD5(CONCAT(col1,col2))
                     AND col1='constant' AND col2='constant';

         瀵逛簬棰戠箒鏇存敼鐨MyISAM琛紝搴旇瘯鍥鹃伩鍏嶆墍鏈夊彉闀垮垪(VARCHARBLOBTEXT)銆傚鏋滆〃鍖呮嫭鍗曚竴鐨勫彉闀垮垪鍒欎娇鐢ㄥ姩鎬佽褰曟牸寮忋傚弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

         鍙槸鍥犱负琛屽お澶э紝灏嗕竴寮犺〃鍒嗗壊涓轰笉鍚岀殑琛ㄤ竴鑸病鏈変粈涔堢敤澶勩備负浜嗚闂锛屾渶澶х殑鎬ц兘鍐插嚮鏄鐩樻悳绱互鎵惧埌琛岀殑绗竴涓瓧鑺傘傚湪鎵惧埌鏁版嵁鍚庯紝澶у鏁版柊鍨嬬鐩樺澶у鏁板簲鐢ㄧ▼搴忔潵璇磋冻澶熷揩锛岃兘璇诲叆鏁翠釜琛屻傜‘瀹炴湁蹇呰鍒嗗壊鐨勫敮涓鎯呭舰鏄鏋滃畠鏄娇鐢ㄥ姩鎬佽褰曟牸寮忎娇涔嬪彉涓哄浐瀹氱殑璁板綍澶у皬鐨MyISAM(瑙佷笂杩)锛屾垨濡傛灉浣犻渶瑕佸緢棰戠箒鍦版壂鎻忚〃鑰屼笉闇瑕佸ぇ澶氭暟鍒椼傚弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

         濡傛灉浣犻渶瑕佸緢缁忓父鍦拌绠楃粨鏋滐紝渚嬪鍩轰簬鏉ヨ嚜寰堝琛岀殑淇℃伅鐨勮鏁帮紝寮曞叆涓涓柊琛ㄥ苟瀹炴椂鏇存柊璁℃暟鍣ㄥ彲鑳芥洿濂戒竴浜涖備笅闈㈠舰寮忕殑鏇存柊浼氭洿蹇竴浜涳細

UPDATE tbl_name SET count_col=count_col+1 WHERE key_col=constant;

褰撲綘浣跨敤璞MyISAM閭f牱鐨勫彧鏈夎〃绾ч攣瀹氱殑MySQL瀛樺偍寮曟搸(澶氶噸璇/鍗曚釜鍐)鏃讹紝杩欑‘瀹炲緢閲嶈銆傝繖涔熺粰澶у鏁版暟鎹簱杈冨ソ鐨勬ц兘锛屽洜涓鸿閿佸畾绠$悊鍣ㄥ湪杩欑鎯呭喌涓嬫湁杈冨皯鐨勪簨鎯呰鍋氥

         濡傛灉浣犻渶瑕佷粠澶х殑璁板綍鏂囦欢琛ㄤ腑鏀堕泦缁熻淇℃伅锛屼娇鐢ㄦ荤粨鎬х殑琛ㄨ屼笉鏄壂鎻忔暣涓〃銆傜淮鎶ゆ荤粨搴旇姣斿皾璇曞仛鈥滃疄鏃垛濈粺璁¤蹇簺銆傚綋鏈夊彉鍖栨椂浠庢棩蹇楅噸鏂扮敓鎴愭柊鐨勬荤粨琛ㄦ瘮鏀瑰彉杩愯鐨勫簲鐢(鍙栧喅浜庝笟鍔″喅绛)瑕佸揩寰楀銆

  • 濡傛灉鍙兘锛屽簲璇ュ皢鎶ュ憡鍒嗙被涓衡滃疄鏃垛濇垨鈥滅粺璁♀濓紝杩欓噷缁熻鎶ュ憡鎵闇鐨勬暟鎹粎浠呭熀浜庝粠瀹為檯鏁版嵁瀹氭湡浜х敓鐨勬荤粨琛ㄤ腑浜х敓銆
  • 鍏呭垎鍒╃敤鍒楁湁榛樿鍊肩殑浜嬪疄銆傚彧鏈夊綋鎻掑叆鐨勫间笉鍚屼簬榛樿鍊兼椂锛屾墠鏄庣‘鍦版彃鍏ュ笺傝繖鍑忓皯MySQL闇瑕佸仛鐨勮娉曞垎鏋愪粠鑰屾彁楂樻彃鍏ラ熷害銆
  • 鍦ㄤ竴浜涙儏鍐典笅锛屽寘瑁呭苟瀛樺偍鏁版嵁鍒颁竴涓BLOB鍒椾腑鏄緢鏂逛究鐨勩傚湪杩欑鎯呭喌涓嬶紝蹇呴』鍦ㄤ綘鐨勫簲鐢ㄤ腑澧炲姞棰濆鐨勪唬鐮佹潵鎵撳寘/瑙e寘淇℃伅锛屼絾鏄繖绉嶆柟娉曞彲浠ュ湪鏌愪簺闃舵鑺傜渷寰堝璁块棶銆傚綋鏈変笉绗﹀悎琛屽拰鍒楄〃缁撴瀯鐨勬暟鎹椂锛岃繖寰堝疄鐢ㄣ
  • 鍦ㄤ竴鑸儏鍐典笅锛屽簲璇ュ皾璇曚互闈炲啑浣欐柟寮(鏌ョ湅鏁版嵁搴撶悊璁轰腑鐨绗笁姝e垯褰㈠紡)瀛樻暟鎹紝浣嗘槸涓轰簡鑾峰緱鏇村揩鐨勯熷害锛屽彲浠ュ鍒朵俊鎭垨鍒涘缓鎬荤粨琛ㄣ
  • 瀛樺偍杩囩▼鎴UDF(鐢ㄦ埛瀹氫箟鍑芥暟)鍙兘鏄幏寰楁洿濂芥ц兘鐨勪竴涓ソ鏂规硶锛岃缁嗕俊鎭弬瑙绗20绔狅細瀛樺偍绋嬪簭鍜屽嚱鏁27.2鑺傦紝鈥滀负MySQL娣诲姞鏂板嚱鏁扳
  • 鎬绘槸鑳介氳繃鍦ㄥ簲鐢ㄧ▼搴忎腑缂撳瓨鏌ヨ/绛旀骞跺皾璇曞悓鏃舵墽琛屽緢澶氭彃鍏/鏇存柊鏉ヨ幏寰椾竴浜涘ソ澶勩傚鏋滄暟鎹簱鏀寔閿佸畾琛(MySQLOracle)锛岃繖搴旇鏈夊姪浜庣‘淇濈储寮曠紦瀛樺彧鍦ㄦ墍鏈夋洿鏂板悗鍒锋柊涓娆°傝繕鍙互鍒╃敤MySQL鐨勬煡璇㈢紦瀛樻潵鑾峰緱绫讳技鐨勭粨鏋滐紱鍙傝5.13鑺傦紝鈥淢ySQL鏌ヨ楂橀熺紦鍐测
  • 褰撲笉闇瑕佺煡閬撲綍鏃跺啓鍏ユ暟鎹椂锛屼娇鐢INSERT DELAYED銆傝繖鏍峰彲浠ュ姞蹇鐞嗭紝鍥犱负寰堝璁板綍鍙互閫氳繃涓娆$鐩樺啓鍏ヨ鍐欏叆銆
  • 褰撲綘鎯宠璁╅夋嫨鏄惧緱鏇撮噸瑕佹椂锛屼娇鐢INSERT /*! LOW_PRIORITY */
  • 浣跨敤INSERT LOW_PRIORITY鏉ュ彇寰楁彃鍏ラ槦鍒楃殑妫绱紝涔熷氨鏄嵆浣挎湁鍙︿竴涓鎴风瓑寰呭啓鍏ヤ篃瑕佹墽琛SELECT
  • 浣跨敤澶氳INSERT璇彞閫氳繃涓涓SQL鍛戒护鏉ュ瓨鍌ㄥ緢澶氳(璁稿SQL鏈嶅姟鍣ㄦ敮鎸佸畠锛屽寘鎷MySQL)
  • 浣跨敤LOAD DATA INFILE瑁呰浇杈冨ぇ鏁伴噺鐨勬暟鎹傝繖姣斾娇鐢INSERT瑕佸揩寰楀銆
  • 浣跨敤AUTO_INCREMENT鍒楁瀯鎴愬敮涓鍊笺
  • MyISAM浣跨敤鍔ㄦ佽〃鏍煎紡鏃讹紝鍋跺皵浣跨敤OPTIMIZE TABLE鍙互閬垮厤纰庣墖銆傚弬瑙15.1.3鑺傦紝鈥淢yISAM琛ㄧ殑瀛樺偍鏍煎紡鈥
  • 鍙兘鏃朵娇鐢MEMORY琛ㄤ互寰楀埌鏇村揩鐨勯熷害銆鍙傝15.4鑺傦紝鈥淢EMORY (HEAP)瀛樺偍寮曟搸鈥
  • Web鏈嶅姟鍣ㄤ腑锛屽浘璞″拰鍏跺畠浜岃繘鍒惰祫浜у簲璇ヤ綔涓烘枃浠跺瓨鍌ㄣ備篃灏辨槸浠呭湪鏁版嵁搴撲腑瀛樺偍鐨勬湰鏂囦欢鐨勫紩鐢ㄨ屼笉鏄枃浠舵湰韬傚ぇ澶氭暟Web鏈嶅姟鍣ㄥ湪缂撳瓨鏂囦欢鏂归潰姣旀暟鎹簱鍐呭瑕佸ソ寰楀锛屽洜姝や娇鐢ㄦ枃浠朵竴鑸蹇緱澶氥
  • 瀵圭粡甯歌闂殑涓嶉噸瑕佹暟鎹(濡備负娌℃湁鍦Web 娴忚鍣ㄤ腑鍚敤cookie鐨勭敤鎴锋渶鍚庢樉绀虹殑鏍囪鐨勭浉鍏充俊鎭)浣跨敤鍐呭瓨琛ㄣ傚湪璁稿Web搴旂敤绋嬪簭鐜涓篃鍙互浣跨敤鐢ㄦ埛浼氳瘽鏉ュ鐞嗗彲鍙樼姸鎬佹暟鎹
  • 鍦ㄤ笉鍚岃〃涓叿鏈夌浉鍚屼俊鎭殑鍒楀簲璇ヨ澹版槑涓虹浉鍚岀殑骞舵湁鐩稿悓鐨勫悕瀛椼傚皾璇曚娇鍚嶅瓧绠鍗曞寲銆備緥濡傦紝鍦customer琛ㄤ腑浣跨敤name鑰屼笉鏄customer_name銆備负浜嗕娇鍚嶅瓧鑳界Щ妞嶅埌鍏跺畠SQL鏈嶅姟鍣紝搴旇浣垮悕瀛楃煭浜18涓瓧绗︺
  • 濡傛灉纭疄闇瑕佸緢楂樼殑閫熷害锛屽簲璇ョ爺绌朵竴涓嬩笉鍚SQL鏈嶅姟鍣ㄦ敮鎸佺殑鏁版嵁瀛樺偍鐨勪綆灞傛帴鍙o紒渚嬪鐩存帴璁块棶MySQL MyISAM瀛樺偍寮曟搸锛屾瘮璧蜂娇鐢SQL鎺ュ彛锛岄熷害鍙互鎻愰珮2-5鍊嶃備负浜嗚兘瀹炵幇锛屾暟鎹繀椤讳笌搴旂敤绋嬪簭鍦ㄥ悓涓鍙版湇鍔″櫒涓婏紝骞朵笖閫氬父鍙簲璇ヨ涓涓繘绋嬭闂(鍥犱负澶栭儴鏂囦欢閿佸畾纭疄寰堟參)銆傞氳繃鍦MySQL鏈嶅姟鍣ㄤ腑寮曡繘浣庡眰MyISAM鍛戒护鑳芥秷闄や互涓婇棶棰(濡傛灉闇瑕侊紝杩欏彲鑳芥槸鑾峰緱鏇村ソ鎬ц兘鐨勪竴涓畝鍗曠殑鏂规硶)銆傞氳繃绮惧績璁捐鏁版嵁搴撴帴鍙o紝搴旇鑳界浉褰撳鏄撳湴鏀寔杩欑被浼樺寲銆
  • 濡傛灉姝d娇鐢ㄦ暟瀛楁暟鎹紝鍦ㄨ澶氭儏鍐典笅锛屼粠涓涓暟鎹簱璁块棶淇℃伅(浣跨敤瀹炴椂杩炴帴)姣旇闂竴涓枃鏈枃浠跺揩浜涖傝繖鏄洜涓烘暟鎹簱涓殑淇℃伅姣旀枃鏈枃浠舵洿绱у噾锛屽洜姝よ繖灏嗘秹鍙婃洿灏戠殑纾佺洏璁块棶銆傝繕鍙互鍦ㄥ簲鐢ㄧ▼搴忎腑鑺傜渷浠g爜锛屽洜涓轰笉椤诲垎鏋愭枃鏈枃浠舵潵鎵惧嚭琛屽拰鍒楃殑杈圭晫銆

         閫氳繃澶嶅埗鍙互鎻愰珮鏌愪簺鎿嶄綔鐨勬ц兘銆傚彲浠ュ湪澶嶅埗鏈嶅姟鍣ㄤ腑鍒嗗竷瀹㈡埛鐨勬绱互鍧囧垎璐熻浇銆備负浜嗛槻姝㈠浠芥椂涓绘湇鍔″櫒鍙樻參锛屽彲浠ヤ娇鐢ㄤ竴涓粠鏈嶅姟鍣ㄦ潵澶囦唤銆傚弬瑙绗6绔狅細MySQL涓殑澶嶅埗

         DELAY_KEY_WRITE=1閫夐」澹版槑MyISAM琛ㄥ彲浠ヤ娇绱㈠紩鏇存柊鏇村揩锛屽洜涓哄湪琛ㄥ叧闂箣鍓嶅畠浠笉鍒锋柊鍒扮‖鐩樹笂銆備笉鍒╀箣澶勬槸褰撹〃鎵撳紑鏃跺鏋滄潃鎺夋湇鍔″櫒锛屽簲纭繚鐢--myisam-recover閫夐」杩愯鏈嶅姟鍣ㄤ繚璇佹病鏈夐棶棰橈紝鎴栬呭湪閲嶅惎鏈嶅姟鍣ㄤ箣鍓嶈繍琛myisamchk(鐒惰岋紝鍗充娇鍦ㄨ繖绉嶆儏鍐典笅锛屽簲閫氳繃浣跨敤DELAY_KEY_WRITE淇濊瘉涓嶄涪澶辨暟鎹紝鍥犱负鍏抽敭瀛椾俊鎭绘槸鍙互浠庢暟鎹浜х敓锛

 

7.3. 閿佸畾浜嬪疁

7.3.1. 閿佸畾鏂规硶

MySQL 5.1鏀寔瀵MyISAMMEMORY琛ㄨ繘琛岃〃绾ч攣瀹氾紝瀵BDB琛ㄨ繘琛岄〉绾ч攣瀹氾紝瀵InnoDB琛ㄨ繘琛岃绾ч攣瀹氥

鍦ㄨ澶氭儏鍐典笅锛屽彲浠ユ牴鎹煿璁寽娴嬪簲鐢ㄧ▼搴忎娇鐢ㄥ摢绫婚攣瀹氱被鍨嬫渶濂斤紝浣嗕竴鑸緢闅捐鍑烘煇涓粰鍑虹殑閿佺被鍨嬪氨姣斿彟涓涓ソ銆備竴鍒囧彇鍐充簬搴旂敤绋嬪簭锛屽簲鐢ㄧ▼搴忕殑涓嶅悓閮ㄥ垎鍙兘闇瑕佷笉鍚岀殑閿佺被鍨嬨

涓轰簡纭畾鏄惁鎯宠浣跨敤琛岀骇閿佸畾鐨勫瓨鍌ㄥ紩鎿庯紝搴旂湅鐪嬪簲鐢ㄧ▼搴忓仛浠涔堝苟涓旀贩鍚堜娇鐢ㄤ粈涔堟牱鐨勯夋嫨鍜屾洿鏂拌鍙ャ備緥濡傦紝澶у鏁Web搴旂敤绋嬪簭鎵ц璁稿閫夋嫨锛岃屽緢灏戣繘琛屽垹闄わ紝鍙鍏抽敭瀛楃殑鍊艰繘琛屾洿鏂帮紝骞朵笖鍙彃鍏ュ皯閲忓叿浣撶殑琛ㄣ傚熀鏈MySQL MyISAM璁剧疆宸茬粡璋冭妭寰楀緢濂姐

MySQL涓浜庝娇鐢ㄨ〃绾ч攣瀹氱殑瀛樺偍寮曟搸锛岃〃閿佸畾鏃朵笉浼氭閿佺殑銆傝繖閫氳繃鎬绘槸鍦ㄤ竴涓煡璇㈠紑濮嬫椂绔嬪嵆璇锋眰鎵鏈夊繀瑕佺殑閿佸畾骞朵笖鎬绘槸浠ュ悓鏍风殑椤哄簭閿佸畾琛ㄦ潵绠$悊銆

WRITEMySQL浣跨敤鐨勮〃閿佸畾鏂规硶鍘熺悊濡備笅锛

  • 濡傛灉鍦ㄨ〃涓婃病鏈夐攣锛屽湪瀹冧笂闈㈡斁涓涓啓閿併
  • 鍚﹀垯锛屾妸閿佸畾璇锋眰鏀惧湪鍐欓攣瀹氶槦鍒椾腑銆

READMySQL浣跨敤鐨勯攣瀹氭柟娉曞師鐞嗗涓嬶細

  • 濡傛灉鍦ㄨ〃涓婃病鏈夊啓閿佸畾锛屾妸涓涓閿佸畾鏀惧湪瀹冧笂闈€
  • 鍚﹀垯锛屾妸閿佽姹傛斁鍦ㄨ閿佸畾闃熷垪涓

褰撲竴涓攣瀹氳閲婃斁鏃讹紝閿佸畾鍙鍐欓攣瀹氶槦鍒椾腑鐨勭嚎绋嬪緱鍒帮紝鐒跺悗鏄閿佸畾闃熷垪涓殑绾跨▼銆

杩欐剰鍛崇潃锛屽鏋滀綘鍦ㄤ竴涓〃涓婃湁璁稿鏇存柊锛SELECT璇彞灏嗙瓑寰呯洿鍒版病鏈夋洿澶氱殑鏇存柊銆

鍙互閫氳繃妫鏌table_locks_waitedtable_locks_immediate鐘舵佸彉閲忔潵鍒嗘瀽绯荤粺涓婄殑琛ㄩ攣瀹氫簤澶猴細

mysql> SHOW STATUS LIKE 'Table%';

+-----------------------+---------+

| Variable_name         | Value   |

+-----------------------+---------+

| Table_locks_immediate | 1151552 |

| Table_locks_waited    | 15324   |

+-----------------------+---------+

濡傛灉INSERT璇彞涓嶅啿绐侊紝鍙互鑷敱涓MyISAM琛ㄦ贩鍚堝苟琛岀殑INSERTSELECT璇彞鑰屼笉闇瑕侀攣瀹氥備篃灏辨槸璇达紝浣犲彲浠ュ湪鍏跺畠瀹㈡埛姝h鍙MyISAM琛ㄧ殑鏃跺欐彃鍏ヨ銆傚鏋滄暟鎹枃浠朵腑闂翠笉鍖呭惈绌洪棽鍧楋紝涓嶄細鍙戠敓鍐茬獊锛屽洜涓哄湪杩欑鎯呭喌涓嬶紝璁板綍鎬绘槸鎻掑叆鍦ㄦ暟鎹枃浠剁殑灏鹃儴銆(浠庤〃鐨勪腑閮ㄥ垹闄ゆ垨鏇存柊鐨勮鍙兘瀵艰嚧绌烘礊锛濡傛灉鏈夌┖娲烇紝褰撴墍鏈夌┖娲炲~鍏ユ柊鐨勬暟鎹椂锛屽苟琛岀殑鎻掑叆鑳藉閲嶆柊鑷姩鍚敤銆

濡傛灉涓嶈兘鍚屾椂鎻掑叆锛屼负浜嗗湪涓涓〃涓繘琛屽娆INSERTSELECT鎿嶄綔锛屽彲浠ュ湪涓存椂琛ㄤ腑鎻掑叆琛屽苟涓旂珛鍗崇敤涓存椂琛ㄤ腑鐨勮褰曟洿鏂扮湡姝g殑琛ㄣ

杩欏彲鐢ㄤ笅鍒椾唬鐮佸仛鍒帮細

mysql> LOCK TABLES real_table WRITE, insert_table WRITE;

mysql> INSERT INTO real_table SELECT * FROM insert_table;

mysql> TRUNCATE TABLE insert_table;

mysql> UNLOCK TABLES;

 

InnoDB浣跨敤琛岄攣瀹氾紝BDB浣跨敤椤甸攣瀹氥傚浜庤繖涓ょ瀛樺偍寮曟搸锛岄兘鍙兘瀛樺湪姝婚攣銆傝繖鏄洜涓猴紝鍦SQL璇彞澶勭悊鏈熼棿锛InnoDB鑷姩鑾峰緱琛岄攣瀹氬拰BDB鑾峰緱椤甸攣瀹氾紝鑰屼笉鏄湪浜嬪姟鍚姩鏃惰幏寰椼

琛岀骇閿佸畾鐨勪紭鐐癸細

         褰撳湪璁稿绾跨▼涓闂笉鍚岀殑琛屾椂鍙瓨鍦ㄥ皯閲忛攣瀹氬啿绐併

         鍥炴粴鏃跺彧鏈夊皯閲忕殑鏇存敼銆

         鍙互闀挎椂闂撮攣瀹氬崟涓鐨勮銆

琛岀骇閿佸畾鐨勭己鐐癸細

         姣旈〉绾ф垨琛ㄧ骇閿佸畾鍗犵敤鏇村鐨勫唴瀛樸

         褰撳湪琛ㄧ殑澶ч儴鍒嗕腑浣跨敤鏃讹紝姣旈〉绾ф垨琛ㄧ骇閿佸畾閫熷害鎱紝鍥犱负浣犲繀椤昏幏鍙栨洿澶氱殑閿併

         濡傛灉浣犲湪澶ч儴鍒嗘暟鎹笂缁忓父杩涜GROUP BY鎿嶄綔鎴栬呭繀椤荤粡甯告壂鎻忔暣涓〃锛屾瘮鍏跺畠閿佸畾鏄庢樉鎱㈠緢澶氥

         鐢ㄩ珮绾у埆閿佸畾锛岄氳繃鏀寔涓嶅悓鐨勭被鍨嬮攣瀹氾紝浣犱篃鍙互寰堝鏄撳湴璋冭妭搴旂敤绋嬪簭锛屽洜涓哄叾閿佹垚鏈皬浜庤绾ч攣瀹氥

鍦ㄤ互涓嬫儏鍐典笅锛岃〃閿佸畾浼樺厛浜庨〉绾ф垨琛岀骇閿佸畾锛

         琛ㄧ殑澶ч儴鍒嗚鍙ョ敤浜庤鍙栥

         瀵逛弗鏍肩殑鍏抽敭瀛楄繘琛岃鍙栧拰鏇存柊锛屼綘鍙互鏇存柊鎴栧垹闄ゅ彲浠ョ敤鍗曚竴鐨勮鍙栫殑鍏抽敭瀛楁潵鎻愬彇鐨勪竴琛岋細

                UPDATE tbl_name SET column=value WHERE unique_key_col=key_value;
                DELETE FROM tbl_name WHERE unique_key_col=key_value;

         SELECT 缁撳悎骞惰鐨INSERT璇彞锛屽苟涓斿彧鏈夊緢灏戠殑UPDATEDELETE璇彞銆

         鍦ㄦ暣涓〃涓婃湁璁稿鎵弿鎴GROUP BY鎿嶄綔锛屾病鏈変换浣曞啓鎿嶄綔銆

涓嶅悓浜庤绾ф垨椤电骇閿佸畾鐨勯夐」锛

         鐗堟湰(渚嬪锛屼负骞惰鐨勬彃鍏ュ湪MySQL涓娇鐢ㄧ殑鎶鏈)锛屽叾涓彲浠ヤ竴涓啓鎿嶄綔锛屽悓鏃舵湁璁稿璇诲彇鎿嶄綔銆傝繖璇存槑鏁版嵁搴撴垨琛ㄦ敮鎸佹暟鎹緷璧栫殑涓嶅悓瑙嗗浘锛屽彇鍐充簬璁块棶浣曟椂寮濮嬨傚叾瀹冨叡鍚岀殑鏈鏄鏃堕棿璺熻釜鈥濄佲鍐欏鍒鈥濇垨鑰呪滄寜闇澶嶅埗鈥濄

         鎸夐渶澶嶅埗鍦ㄨ澶氭儏鍐典笅浼樺厛浜庨〉绾ф垨琛岀骇閿佸畾銆傜劧鑰岋紝鍦ㄦ渶鍧忕殑鎯呭喌涓嬶紝瀹冨彲鑳芥瘮浣跨敤甯歌閿佸畾浣跨敤鏇村鐨勫唴瀛樸

         闄や簡琛岀骇閿佸畾澶栵紝浣犲彲浠ヤ娇鐢ㄥ簲鐢ㄧ▼搴忕骇閿佸畾锛屼緥濡傚湪MySQL涓娇鐢GET_LOCK()RELEASE_LOCK()銆傝繖浜涙槸寤鸿鎬ч攣瀹氾紝瀹冧滑鍙兘鍦ㄨ繍琛岃壇濂界殑搴旂敤绋嬪簭涓伐浣溿

7.3.2. 琛ㄩ攣瀹氫簨瀹

涓鸿揪鍒版渶楂橀攣瀹氶熷害锛岄櫎InnoDBBDB涔嬪锛屽鎵鏈夊瓨鍌ㄥ紩鎿庯紝MySQL浣跨敤琛ㄩ攣瀹(鑰屼笉鏄〉銆佽鎴栬呭垪閿佸畾)

瀵逛簬InnoDBBDB琛紝濡傛灉浣犵敤LOCK TABLES鏄惧紡閿佸畾琛紝MySQL鍙娇鐢ㄨ〃閿佸畾銆傚浜庤繖浜涜〃绫诲瀷锛屾垜浠缓璁綘鏍规湰涓嶈浣跨敤LOCK TABLES锛屽洜涓InnoDB浣跨敤鑷姩琛岀骇閿佸畾鑰BDB浣跨敤椤电骇閿佸畾鏉ヤ繚璇佷簨鍔¢殧绂汇

瀵逛簬澶ц〃锛屽浜庡ぇ澶氭暟搴旂敤绋嬪簭锛岃〃閿佸畾姣旇閿佸畾鏇村ソ锛屼絾瀛樺湪閮ㄥ垎缂洪櫡銆

琛ㄩ攣瀹氫娇璁稿绾跨▼鍚屾椂浠庝竴涓〃涓繘琛岃鍙栨搷浣滐紝浣嗗鏋滀竴涓嚎绋嬫兂瑕佸琛ㄨ繘琛屽啓鎿嶄綔锛屽畠蹇呴』棣栧厛鑾峰緱鐙崰璁块棶銆傛洿鏂版湡闂达紝鎵鏈夊叾瀹冩兂瑕佽闂琛ㄧ殑绾跨▼蹇呴』绛夊緟鐩村埌鏇存柊瀹屾垚銆

琛ㄦ洿鏂伴氬父鎯呭喌璁や负姣旇〃妫绱㈡洿閲嶈锛屽洜姝ょ粰浜堝畠浠洿楂樼殑浼樺厛绾с傝繖搴旂‘淇濇洿鏂颁竴涓〃鐨勬椿鍔ㄤ笉鑳解楗挎鈥濓紝鍗充娇璇ヨ〃涓婃湁寰堢箒閲嶇殑SELECT娲诲姩銆

琛ㄩ攣瀹氬湪杩欑鎯呭喌涓嬩細閫犳垚闂锛屼緥濡傚綋绾跨▼姝g瓑寰咃紝鍥犱负纭洏宸叉弧骞朵笖鍦ㄧ嚎绋嬪彲浠ュ鐞嗕箣鍓嶅繀椤绘湁绌洪棽绌洪棿銆傚湪杩欑鎯呭喌涓嬶紝鎵鏈夋兂瑕佽闂嚭鐜伴棶棰樼殑琛ㄧ殑绾跨▼涔熻璁剧疆鎴愮瓑寰呯姸鎬侊紝鐩村埌鏈夋洿澶氱殑纭洏绌洪棿鍙敤銆

琛ㄩ攣瀹氬湪涓嬮潰鐨勬儏鍐典笅涔熷瓨鍦ㄩ棶棰橈細

         涓涓鎴峰彂鍑洪暱鏃堕棿杩愯鐨勬煡璇€

         鐒跺悗锛屽彟涓涓鎴峰鍚屼竴涓〃杩涜鏇存柊銆傝瀹㈡埛蹇呴』绛夊緟鐩村埌SELECT瀹屾垚銆

         鍙︿竴涓鎴峰鍚屼竴涓〃涓婂彂鍑轰簡鍙︿竴涓SELECT璇彞銆傚洜涓UPDATESELECT浼樺厛绾ч珮锛岃SELECT璇彞绛夊緟UPDATE瀹屾垚锛屽苟涓旂瓑寰呯1SELECT瀹屾垚銆

涓嬮潰鎻忚堪浜嗕竴浜涙柟娉曟潵閬垮厤鎴栧噺灏戣〃閿佸畾閫犳垚鐨勭珵浜夛細

         璇曞浘浣SELECT璇彞杩愯寰楁洿蹇備綘鍙兘蹇呴』鍒涘缓涓浜涙憳瑕(summary)琛ㄥ仛鍒拌繖鐐广

         --low-priority-updates鍚姩mysqld銆傝繖灏嗙粰鎵鏈夋洿鏂(淇敼)涓涓〃鐨勮鍙ヤ互姣SELECT璇彞浣庣殑浼樺厛绾с傚湪杩欑鎯呭喌涓嬶紝鍦ㄥ厛鍓嶆儏褰㈢殑绗2SELECT璇彞灏嗗湪UPDATE璇彞鍓嶆墽琛岋紝鑰屼笉闇瑕佺瓑鍊欑1SELECT瀹屾垚銆

         鍙互浣跨敤SET LOW_PRIORITY_UPDATES=1璇彞鎸囧畾鍏蜂綋杩炴帴涓殑鎵鏈夋洿鏂板簲浣跨敤浣庝紭鍏堢骇銆傚弬瑙13.5.3鑺傦紝鈥淪ET璇硶鈥

         鍙互鐢LOW_PRIORITY灞炴х粰涓庝竴涓壒瀹氱殑INSERTUPDATEDELETE璇彞杈冧綆浼樺厛绾с

         鍙互鐢HIGH_PRIORITY灞炴х粰涓庝竴涓壒瀹氱殑SELECT璇彞杈冮珮浼樺厛绾с傚弬瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥

         max_write_lock_count绯荤粺鍙橀噺鎸囧畾涓涓綆鍊兼潵鍚姩mysqld鏉ュ己鍒MySQL鍦ㄥ叿浣撴暟閲忕殑鎻掑叆瀹屾垚鍚庝复鏃舵彁楂樻墍鏈夌瓑寰呬竴涓〃鐨SELECT璇彞鐨勪紭鍏堢骇銆傝繖鏍峰厑璁稿湪涓瀹氭暟閲忕殑WRITE閿佸畾鍚庣粰鍑READ閿佸畾銆

         濡傛灉浣犳湁鍏充簬INSERT缁撳悎SELECT鐨勯棶棰橈紝鍒囨崲鍒颁娇鐢ㄦ柊鐨MyISAM琛紝鍥犱负瀹冧滑鏀寔骞跺彂鐨SELECTINSERT

         濡傛灉浣犲鍚屼竴涓〃娣峰悎鎻掑叆鍜屽垹闄わ紝INSERT DELAYED灏嗕細鏈夊緢澶х殑甯姪銆傚弬瑙13.2.4.2鑺傦紝鈥淚NSERT DELAYED璇硶鈥

         濡傛灉浣犲鍚屼竴涓〃娣峰悎浣跨敤SELECTDELETE璇彞鍑虹幇闂锛DELETELIMIT閫夐」鍙互鏈夋墍甯姪銆傚弬瑙13.2.1鑺傦紝鈥淒ELETE璇硶鈥

         SELECT璇彞浣跨敤SQL_BUFFER_RESULT鍙互甯姪浣胯〃閿佸畾鏃堕棿鍙樼煭銆傚弬瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥

         鍙互鏇存敼mysys/thr_lock.c涓殑閿佷唬鐮佷互浣跨敤鍗曚竴鐨勯槦鍒椼傚湪杩欑鎯呭喌涓嬶紝鍐欓攣瀹氬拰璇婚攣瀹氬皢鍏锋湁鐩稿悓鐨勪紭鍏堢骇锛屽涓浜涘簲鐢ㄧ▼搴忎細鏈夊府鍔┿

杩欓噷鏄竴浜MySQL涓〃閿佸畾鐩稿叧鐨勬妧宸э細

         濡傛灉涓嶆贩鍚堟洿鏂颁笌闇瑕佸湪鍚屼竴涓〃涓鏌ヨ澶氳鐨勯夋嫨锛屽彲浠ヨ繘琛屽苟琛屾搷浣溿

         鍙互浣跨敤LOCK TABLES鏉ユ彁楂橀熷害锛屽洜涓哄湪涓涓攣瀹氫腑杩涜璁稿鏇存柊姣旀病鏈夐攣瀹氱殑鏇存柊瑕佸揩寰楀銆傚皢琛ㄤ腑鐨勫唴瀹瑰垏鍒嗕负鍑犱釜琛ㄤ篃鍙互鏈夋墍甯姪銆

         濡傛灉鍦MySQL涓〃閿佸畾鏃堕亣鍒伴熷害闂锛屽彲浠ュ皢浣犵殑琛ㄨ浆鎹负InnoDBBDB琛ㄦ潵鎻愰珮鎬ц兘銆傚弬瑙15.2鑺傦紝鈥淚nnoDB瀛樺偍寮曟搸鈥15.5鑺傦紝鈥淏DB (BerkeleyDB)瀛樺偍寮曟搸鈥

7.4. 浼樺寲鏁版嵁搴撶粨鏋

7.4.1. 璁捐閫夋嫨

MySQL灏嗚鏁版嵁鍜岀储寮曟暟鎹繚瀛樺湪涓嶅悓鐨勬枃浠朵腑銆傝澶(鍑犱箮鎵鏈)鍏跺畠鏁版嵁搴撳皢琛屾暟鎹拰绱㈠紩鏁版嵁娣峰悎淇濆瓨鍦ㄧ敤涓涓枃浠朵腑銆傛垜浠涓MySQL 閫夋嫨瀵瑰箍鑼冨洿鐨勭幇浠g郴缁熸洿濂戒竴浜涖

淇濆瓨琛屾暟鎹殑鍙︿竴绉嶆柟寮忔槸灏嗘瘡涓垪鐨勪俊鎭繚瀛樺湪鍗曠嫭鐨勫尯鍩(渚嬪SDBMFocus)銆傝繖鏍蜂細瀵规瘡涓闂涓垪鐨勬煡璇㈤犳垚鎬ц兘闂銆傚洜涓哄綋璁块棶澶氫釜鍒楁椂閫鍖栧緱寰堝揩锛屾垜浠涓鸿妯″瀷瀵逛竴鑸暟鎹簱涓嶅悎閫傘

鏇村父瑙佺殑鎯呭舰鏄储寮曞拰鏁版嵁淇濆瓨鍦ㄤ竴璧(渚嬪Oracle/Sybase)銆傚湪杩欑鎯呭喌涓嬶紝浣犲彲浠ュ湪绱㈠紩鐨勫彾绾ч〉鎵惧埌琛岀殑淇℃伅銆傝甯冨眬姣旇緝濂界殑浜嬫儏鏄湪璁稿鎯呭喌涓嬶紝鏍规嵁绱㈠紩缂撳瓨寰楁庢牱锛屽彲浠ヤ繚瀛樹竴涓‖鐩樿鍙栥傝甯冨眬鐨勪笉鍒╀箣澶勮〃鐜板湪锛

         琛ㄦ壂鎻忚鎱㈠緱澶氾紝鍥犱负浣犲繀椤婚氳绱㈠紩浠ヨ幏寰楁暟鎹

         浣犱笉鑳藉彧浣跨敤琛ㄦ潵妫绱㈡煡璇㈢殑鏁版嵁銆

         浣犻渶瑕佷娇鐢ㄦ洿澶氱殑绌洪棿锛屽洜涓轰綘蹇呴』浠庤妭鐐瑰鍒剁储寮(浣犱笉鑳戒繚瀛樿妭鐐逛笂鐨勮)

         鍒犻櫎瑕佺粡杩囦竴娈垫椂闂村悗鎵嶉鍖栬〃(鍥犱负鍒犻櫎鏃堕氬父涓嶄細鏇存柊鑺傜偣涓婄殑绱㈠紩)

         鍙紦瀛樼储寮曟暟鎹細鏇村姞鍥伴毦銆

7.4.2. 浣夸綘鐨勬暟鎹敖鍙兘灏

鏈鍩烘湰鐨勪紭鍖栦箣涓鏄娇琛ㄥ湪纾佺洏涓婂崰鎹殑绌洪棿灏藉彲鑳藉皬銆傝繖鑳界粰鍑哄法澶х殑鏀硅繘锛屽洜涓虹鐩樿鍏ヨ緝蹇紝骞朵笖鍦ㄦ煡璇㈡墽琛岃繃绋嬩腑灏忚〃鐨勫唴瀹硅澶勭悊鏃跺崰鐢ㄨ緝灏戠殑涓诲瓨鍌ㄥ櫒銆傚鏋滃湪鏇村皬鐨勫垪涓婂仛绱㈠紩锛岀储寮曚篃鍗犳嵁杈冨皯鐨勮祫婧愩

MySQL鏀寔璁稿涓嶅悓鐨勫瓨鍌ㄥ紩鎿(琛ㄧ被鍨)鍜岃鏍煎紡銆傚浜庢瘡涓〃锛屽彲浠ョ‘瀹氫娇鐢ㄥ摢涓瓨鍌ㄥ紩鎿庡拰绱㈠紩鏂规硶銆備负搴旂敤绋嬪簭閫夋嫨鍚堥傜殑琛ㄦ牸寮忓彲浠ュぇ澶ф彁楂樻ц兘銆傚弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

鍙互浣跨敤涓嬮潰鐨勬妧鏈彲浠ヤ娇琛ㄧ殑鎬ц兘鏇村ソ骞朵笖浣垮瓨鍌ㄧ┖闂存渶灏忥細

  • 灏藉彲鑳藉湴浣跨敤鏈鏈夋晥(鏈灏)鐨勬暟鎹被鍨嬨MySQL鏈夊緢澶氳妭鐪佺鐩樼┖闂村拰鍐呭瓨鐨勪笓涓氬寲绫诲瀷銆
  • 灏藉彲鑳戒娇鐢ㄨ緝灏忕殑鏁存暟绫诲瀷浣胯〃鏇村皬銆備緥濡傦紝MEDIUMINT缁忓父姣INT濂戒竴浜涳紝鍥犱负MEDIUMINT鍒椾娇鐢ㄧ殑绌洪棿瑕佸皯25%
  • 濡傛灉鍙兘锛屽0鏄庡垪涓NOT NULL銆傚畠浣夸换浣曚簨鎯呮洿蹇屼笖姣忓垪鍙互鑺傜渷涓浣嶃傛敞鎰忓鏋滃湪搴旂敤绋嬪簭涓‘瀹為渶瑕NULL锛屽簲璇ユ鏃犵枒闂娇鐢ㄥ畠锛屽彧鏄伩鍏 榛樿鍦板湪鎵鏈夊垪涓婃湁瀹冦
  • 瀵逛簬MyISAM琛紝濡傛灉娌℃湁浠讳綍鍙橀暱鍒(VARCHARTEXTBLOB)锛屼娇鐢ㄥ浐瀹氬昂瀵哥殑璁板綍鏍煎紡銆傝繖姣旇緝蹇絾鏄笉骞稿湴鍙兘浼氭氮璐逛竴浜涚┖闂淬傚弬瑙15.1.3鑺傦紝鈥淢yISAM琛ㄧ殑瀛樺偍鏍煎紡鈥銆傚嵆浣夸綘宸茬粡鐢CREATE閫夐」璁VARCHARROW_FORMAT=fixed锛屼篃鍙互鎻愮ず鎯充娇鐢ㄥ浐瀹氶暱搴︾殑琛屻
  • MySQL/InnoDB涓紝InnoDB琛ㄤ娇鐢ㄦ洿绱у噾鐨勫瓨鍌ㄦ牸寮忋傚湪浠ュ墠鐗堟湰鐨MySQL涓紝InnoDB璁板綍鍖呭惈涓浜涘啑浣欎俊鎭紝渚嬪鍒楁暟鐩拰姣忎釜鍒楃殑闀垮害锛屽嵆浣垮浜庡浐瀹氬ぇ灏忕殑鍒椼傞粯璁ゆ儏鍐碉紝鍒涘缓鐨勮〃涓虹揣鍑戞牸寮(ROW_FORMAT=COMPACT)銆傚鏋滄兂瑕侀檷绾ф棫鐗堟湰鐨MySQL/InnoDB锛屽彲浠ョ敤ROW_FORMAT=REDUNDANT瑕佹眰鏃х殑鏍煎紡銆
  • 绱у噾InnoDB鏍煎紡涔熸敼鍙樹簡鍖呭惈UTF-8鏁版嵁鐨CHAR鍒楃殑淇濆瓨鏂瑰紡銆傚湪ROW_FORMAT=REDUNDANT鏍煎紡涓UTF-8 CHAR(n)鍗犵敤3*n瀛楄妭UTF-8缂栫爜鐨勫瓧绗︾殑鏈澶ч暱搴︽槸3瀛楄妭銆傝澶氳瑷鍙互涓昏鐢ㄥ崟瀛楄妭UTF-8瀛楃鏉ョ紪鍐欙紝鍥哄畾鐨勫瓨鍌ㄩ暱搴﹂氬父浼氭氮璐圭┖闂淬傞氳繃鏍规嵁闇瑕佸墺绂诲熬閮ㄧ殑绌烘牸锛ROW_FORMAT=COMPACT鏍煎紡涓鸿繖浜涘垪鍒嗛厤鍙彉鏁伴噺鐨n..3*n瀛楄妭銆傛渶灏忓瓨鍌ㄩ暱搴︽寜椤哄簭淇濆瓨涓n瀛楄妭锛屼互鍦ㄥ吀鍨嬫儏鍐典笅甯姪鏇存柊銆
  • 姣忓紶琛ㄧ殑涓荤储寮曞簲璇ュ敖鍙兘鐭傝繖浣夸竴琛岀殑璇嗗埆瀹规槗鑰屾湁鏁堛
  • 鍙垱寤轰綘纭疄闇瑕佺殑绱㈠紩銆傜储寮曞妫绱㈡湁濂藉锛屼絾鏄綋浣犻渶瑕佸揩閫熷瓨鍌ㄤ笢瑗挎椂灏卞彉寰楃碂绯曘傚鏋滀富瑕侀氳繃鎼滅储鍒楃殑缁勫悎鏉ュ瓨鍙栦竴涓〃锛屽瀹冧滑鍋氫竴涓储寮曘傜涓涓储寮曢儴鍒嗗簲璇ユ槸鏈甯哥敤鐨勫垪銆傚鏋滀粠琛ㄤ腑閫夋嫨鏃舵绘槸浣跨敤璁稿鍒楋紝搴旇棣栧厛浠ユ洿澶氱殑鍓湰浣跨敤鍒椾互鑾峰緱鏇村ソ鐨勭储寮曞帇缂┿
  • 濡傛灉寰堝彲鑳戒竴涓储寮曞湪澶村嚑涓瓧绗︿笂鏈夊敮涓鐨勫墠缂锛屼粎浠呯储寮曡鍓嶇紑姣旇緝濂姐MySQL鏀寔瀵逛竴涓瓧绗﹀垪鐨勬渶宸﹁竟閮ㄥ垎鍒涘缓涓涓储寮(鍙傝13.1.4鑺傦紝鈥淐REATE INDEX璇硶鈥)銆傛洿鐭殑绱㈠紩浼氭洿蹇紝涓嶄粎鍥犱负瀹冧滑鍗犺緝灏戠殑纾佺洏绌洪棿锛岃屼笖鍥犱负瀹冧滑灏嗗湪绱㈠紩缂撳瓨涓彁渚涙洿澶氱殑璁块棶锛屽洜姝ょ鐩樻悳绱㈡洿灏戙傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

         鍦ㄤ竴浜涙儏褰笅锛屽皢涓涓粡甯歌鎵弿鐨勮〃鍒嗗壊涓2涓〃鏄湁鐩婄殑銆傜壒鍒槸濡傛灉瀹冩槸涓涓姩鎬佹牸寮忕殑琛紝骞朵笖鍙兘浣跨敤涓涓壂鎻忚〃鏃惰兘鐢ㄦ潵鎵惧嚭鐩稿叧琛岀殑杈冨皬闈欐佹牸寮忕殑琛ㄣ

7.4.3. 鍒楃储寮

鎵鏈MySQL鍒楃被鍨嬪彲浠ヨ绱㈠紩銆傚鐩稿叧鍒椾娇鐢ㄧ储寮曟槸鎻愰珮SELECT鎿嶄綔鎬ц兘鐨勬渶浣抽斿緞銆

鏍规嵁瀛樺偍寮曟搸瀹氫箟姣忎釜琛ㄧ殑鏈澶х储寮曟暟鍜屾渶澶х储寮曢暱搴︺傚弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷銆傛墍鏈夊瓨鍌ㄥ紩鎿庢敮鎸佹瘡涓〃鑷冲皯16涓储寮曪紝鎬荤储寮曢暱搴﹁嚦灏戜负256瀛楄妭銆傚ぇ澶氭暟瀛樺偍寮曟搸鏈夋洿楂樼殑闄愬埗銆

鍦ㄧ储寮曞畾涔変腑鐢col_name(length)璇硶锛屼綘鍙互鍒涘缓涓涓彧浣跨敤CHARVARCHAR鍒楃殑绗1length瀛楃鐨勭储寮曘傛寜杩欑鏂瑰紡鍙储寮曞垪鍊肩殑鍓嶇紑鍙互浣跨储寮曟枃浠跺皬寰楀銆

MyISAMInnoDB瀛樺偍寮曟搸杩樻敮鎸佸BLOBTEXT鍒楃殑绱㈠紩銆傚綋绱㈠紩涓涓BLOBTEXT鍒楁椂锛屼綘蹇呴』涓虹储寮曟寚瀹氬墠缂闀垮害銆備緥濡傦細

CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));

MySQL 5.1涓紝瀵逛簬MyISAMInnoDB琛紝鍓嶇紑鍙互杈惧埌1000瀛楄妭闀裤傝娉ㄦ剰鍓嶇紑鐨勯檺鍒跺簲浠ュ瓧鑺備负鍗曚綅杩涜娴嬮噺锛岃CREATE TABLE璇彞涓殑鍓嶇紑闀垮害瑙i噴涓哄瓧绗︽暟銆褰撲负浣跨敤澶氬瓧鑺傚瓧绗﹂泦鐨勫垪鎸囧畾鍓嶇紑闀垮害鏃朵竴瀹氳鍔犱互鑰冭檻

杩樺彲浠ュ垱寤FULLTEXT绱㈠紩銆傝绱㈠紩鍙互鐢ㄤ簬鍏ㄦ枃鎼滅储銆傚彧鏈MyISAM瀛樺偍寮曟搸鏀寔FULLTEXT绱㈠紩锛屽苟涓斿彧涓CHARVARCHARTEXT鍒椼傜储寮曟绘槸瀵规暣涓垪杩涜锛屼笉鏀寔灞閮(鍓嶇紑)绱㈠紩銆傝鎯呭弬瑙12.7鑺傦紝鈥滃叏鏂囨悳绱㈠姛鑳解

涔熷彲浠ヤ负绌洪棿鍒楃被鍨嬪垱寤虹储寮曘傚彧鏈MyISAM瀛樺偍寮曟搸鏀寔绌洪棿绫诲瀷銆傜┖闂寸储寮曚娇鐢R-鏍戙

榛樿鎯呭喌MEMORY(HEAP)瀛樺偍寮曟搸浣跨敤hash绱㈠紩锛屼絾涔熸敮鎸B-鏍戠储寮曘

7.4.4. 澶氬垪绱㈠紩

MySQL鍙互涓哄涓垪鍒涘缓绱㈠紩銆備竴涓储寮曞彲浠ュ寘鎷15涓垪銆傚浜庢煇浜涘垪绫诲瀷锛屽彲浠ョ储寮曞垪鐨勫墠缂(鍙傝7.4.3鑺傦紝鈥滃垪绱㈠紩鈥)

澶氬垪绱㈠紩鍙互瑙嗕负鍖呭惈閫氳繃杩炴帴绱㈠紩鍒楃殑鍊艰屽垱寤虹殑鍊肩殑鎺掑簭鐨勬暟缁勩

MySQL鎸夎繖鏍风殑鏂瑰紡浣跨敤澶氬垪绱㈠紩锛氬綋浣犲湪WHERE瀛愬彞涓负绱㈠紩鐨勭1涓垪鎸囧畾宸茬煡鐨勬暟閲忔椂锛屾煡璇㈠緢蹇紝鍗充娇浣犳病鏈夋寚瀹氬叾瀹冨垪鐨勫笺

鍋囧畾琛ㄥ叿鏈変笅闈㈢殑缁撴瀯锛

CREATE TABLE test (
    id INT NOT NULL,
    last_name CHAR(30) NOT NULL,
    first_name CHAR(30) NOT NULL,
    PRIMARY KEY (id),
    INDEX name (last_name,first_name)
);

name绱㈠紩鏄竴涓last_namefirst_name鐨勭储寮曘傜储寮曞彲浠ョ敤浜庝负last_name锛屾垨鑰呬负last_namefirst_name鍦ㄥ凡鐭ヨ寖鍥村唴鎸囧畾鍊肩殑鏌ヨ銆傚洜姝わ紝name绱㈠紩鐢ㄤ簬涓嬮潰鐨勬煡璇細

SELECT * FROM test WHERE last_name='Widenius';
 
SELECT * FROM test
    WHERE last_name='Widenius' AND first_name='Michael';
 
SELECT * FROM test
    WHERE last_name='Widenius'
    AND (first_name='Michael' OR first_name='Monty');
 
SELECT * FROM test
    WHERE last_name='Widenius'
    AND first_name >='M' AND first_name < 'N';

鐒惰岋紝name绱㈠紩鐢ㄤ簬涓嬮潰鐨勬煡璇細

SELECT * FROM test WHERE first_name='Michael';
 
SELECT * FROM test
    WHERE last_name='Widenius' OR first_name='Michael';

MySQL浣跨敤绱㈠紩鎻愰珮鏌ヨ鎬ц兘鐨勬柟寮忓皢鍦7.4.5鑺傦紝鈥淢ySQL濡備綍浣跨敤绱㈠紩鈥涓璁恒

7.4.5. MySQL濡備綍浣跨敤绱㈠紩

绱㈠紩鐢ㄤ簬蹇熸壘鍑哄湪鏌愪釜鍒椾腑鏈変竴鐗瑰畾鍊肩殑琛屻備笉浣跨敤绱㈠紩锛MySQL蹇呴』浠庣1鏉¤褰曞紑濮嬬劧鍚庤瀹屾暣涓〃鐩村埌鎵惧嚭鐩稿叧鐨勮銆傝〃瓒婂ぇ锛岃姳璐圭殑鏃堕棿瓒婂銆傚鏋滆〃涓煡璇㈢殑鍒楁湁涓涓储寮曪紝MySQL鑳藉揩閫熷埌杈句竴涓綅缃幓鎼滃鍒版暟鎹枃浠剁殑涓棿锛屾病鏈夊繀瑕佺湅鎵鏈夋暟鎹傚鏋滀竴涓〃鏈1000琛岋紝杩欐瘮椤哄簭璇诲彇鑷冲皯蹇100鍊嶃傛敞鎰忓鏋滀綘闇瑕佽闂ぇ閮ㄥ垎琛岋紝椤哄簭璇诲彇瑕佸揩寰楀锛屽洜涓烘鏃舵垜浠伩鍏嶇鐩樻悳绱€

澶у鏁MySQL绱㈠紩(PRIMARY KEYUNIQUEINDEXFULLTEXT)B鏍戜腑瀛樺偍銆傚彧鏄┖闂村垪绫诲瀷鐨勭储寮曚娇鐢R-鏍戯紝骞朵笖MEMORY琛ㄨ繕鏀寔hash绱㈠紩銆

瀛楃涓茶嚜鍔ㄥ湴鍘嬬缉鍓嶇紑鍜岀粨灏剧┖鏍笺傚弬瑙13.1.4鑺傦紝鈥淐REATE INDEX璇硶鈥

鎬荤殑鏉ヨ锛屾寜鍚庨潰鐨勮璁轰娇鐢ㄧ储寮曘傛湰鑺傛渶鍚庢弿杩hash绱㈠紩(鐢ㄤ簬MEMORY)鐨勭壒寰併

绱㈠紩鐢ㄤ簬涓嬮潰鐨勬搷浣滐細

         蹇熸壘鍑哄尮閰嶄竴涓WHERE瀛愬彞鐨勮銆

         鍒犻櫎琛屻傚鏋滃彲浠ュ湪澶氫釜绱㈠紩涓繘琛岄夋嫨锛MySQL閫氬父浣跨敤鎵惧埌鏈灏戣鐨勭储寮曘

         褰撴墽琛岃仈鎺ユ椂锛屼粠鍏跺畠琛ㄦ绱㈣銆

         瀵瑰叿浣撴湁绱㈠紩鐨勫垪key_col鎵惧嚭MAX()MIN()鍊笺傜敱棰勫鐞嗗櫒杩涜浼樺寲锛屾鏌ユ槸鍚﹀绱㈠紩涓湪key_col涔嬪墠鍙戠敓鎵鏈夊叧閿瓧鍏冪礌浣跨敤浜WHERE key_part_# = constant銆傚湪杩欑鎯呭喌涓嬶紝MySQL涓烘瘡涓MIN()MAX()琛ㄨ揪寮忔墽琛屼竴娆″叧閿瓧鏌ユ壘锛屽苟鐢ㄥ父鏁版浛鎹㈠畠銆傚鏋滄墍鏈夎〃杈惧紡鏇挎崲涓哄父閲忥紝鏌ヨ绔嬪嵆杩斿洖銆備緥濡傦細

                SELECT MIN(key_part2),MAX(key_part2)
                    FROM tbl_name WHERE key_part1=10;

         濡傛灉瀵逛竴涓彲鐢ㄥ叧閿瓧鐨勬渶宸﹂潰鐨勫墠缂杩涜浜嗘帓搴忔垨鍒嗙粍(渚嬪锛ORDER BY key_part_1,key_part_2)锛屾帓搴忔垨鍒嗙粍涓涓〃銆傚鏋滄墍鏈夊叧閿瓧鍏冪礌鍚庨潰鏈DESC锛屽叧閿瓧浠ュ掑簭琚鍙栥傚弬瑙7.2.12鑺傦紝鈥淢ySQL濡備綍浼樺寲ORDER BY

         鍦ㄤ竴浜涙儏鍐典腑锛屽彲浠ュ涓涓煡璇㈣繘琛屼紭鍖栦互渚夸笉鐢ㄦ煡璇㈡暟鎹鍗冲彲浠ユ绱㈠笺濡傛灉鏌ヨ鍙娇鐢ㄦ潵鑷煇涓〃鐨勬暟瀛楀瀷骞朵笖鏋勬垚鏌愪簺鍏抽敭瀛楃殑鏈宸﹂潰鍓嶇紑鐨勫垪锛屼负浜嗘洿蹇紝鍙互浠庣储寮曟爲妫绱㈠嚭鍊笺

                SELECT key_part3 FROM tbl_name
                    WHERE key_part1=1

鍋囧畾浣犳墽琛屼笅闈㈢殑SELECT璇彞锛

mysql> SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;

濡傛灉col1col2涓婂瓨鍦ㄤ竴涓鍒楃储寮曪紝鍙互鐩存帴鍙栧嚭鐩稿簲琛屻傚鏋col1col2涓婂瓨鍦ㄥ崟鍒楃储寮曪紝浼樺寲鍣ㄨ瘯鍥鹃氳繃鍐冲畾鍝釜绱㈠紩灏嗘壘鍒版洿灏戠殑琛屾潵鎵惧嚭鏇村叿闄愬埗鎬х殑绱㈠紩骞朵笖浣跨敤璇ョ储寮曞彇琛屻

濡傛灉琛ㄦ湁涓涓鍒楃储寮曪紝浼樺寲鍣ㄥ彲浠ヤ娇鐢ㄦ渶宸﹂潰鐨勭储寮曞墠缂鏉ユ壘鍑鸿銆備緥濡傦紝濡傛灉鏈変竴涓3鍒楃储寮(col1,col2,col3)锛屽垯宸茬粡瀵(col1)(col1,col2)(col1,col2,col3)涓婄殑鎼滅储杩涜浜嗙储寮曘

濡傛灉鍒椾笉鏋勬垚绱㈠紩鏈宸﹂潰鐨勫墠缂锛MySQL涓嶈兘浣跨敤灞閮ㄧ储寮曘傚亣瀹氭湁涓嬮潰鏄剧ず鐨SELECT璇彞銆

 
SELECT * FROM tbl_name WHERE col1=val1;
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;
 
SELECT * FROM tbl_name WHERE col2=val2;
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;
 

濡傛灉 (col1col2col3)鏈変竴涓储寮曪紝鍙湁鍓2涓煡璇娇鐢ㄧ储寮曘傜3涓拰绗4涓煡璇㈢‘瀹炲寘鎷储寮曠殑鍒楋紝浣(col2)(col2col3)涓嶆槸 (col1col2col3)鐨勬渶宸﹁竟鐨勫墠缂銆

涔熷彲浠ュ湪琛ㄨ揪寮忛氳繃=>>=<<=鎴栬BETWEEN鎿嶄綔绗︿娇鐢B-鏍戠储寮曡繘琛屽垪姣旇緝銆傚鏋LIKE鐨勫弬鏁版槸涓涓笉浠ラ氶厤绗﹀紑澶寸殑甯搁噺瀛楃涓诧紝绱㈠紩涔熷彲浠ョ敤浜LIKE姣旇緝銆備緥濡傦紝涓嬮潰鐨SELECT璇彞浣跨敤绱㈠紩锛

SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';

鍦ㄧ1涓鍙ヤ腑锛屽彧鑰冭檻甯'Patrick' <=key_col < 'Patricl'鐨勮銆傚湪绗2涓鍙ヤ腑锛屽彧鑰冭檻甯'Pat' <=key_col < 'Pau'鐨勮銆

涓嬮潰鐨SELECT璇彞涓嶄娇鐢ㄧ储寮曪細

SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE other_col;

鍦ㄧ涓鏉¤鍙ヤ腑锛LIKE鍊间互涓涓氶厤绗﹀瓧绗﹀紑濮嬨傚湪绗簩鏉¤鍙ヤ腑锛LIKE鍊间笉鏄竴涓父鏁般

濡傛灉浣跨敤... LIKE '%string%'骞朵笖string瓒呰繃3涓瓧绗︼紝MySQL浣跨敤Turbo Boyer-Moore绠楁硶鍒濆鍖栧瓧绗︿覆鐨勬ā寮忕劧鍚庝娇鐢ㄨ妯″紡鏉ユ洿蹇湴杩涜鎼滅储銆

濡傛灉col_name琚储寮曪紝浣跨敤col_name IS NULL鐨勬悳绱㈠皢浣跨敤绱㈠紩銆

浠讳綍涓嶈法瓒WHERE瀛愬彞涓殑鎵鏈AND绾х殑绱㈠紩涓嶇敤浜庝紭鍖栨煡璇€傛崲鍙ヨ瘽璇达紝涓轰簡鑳藉浣跨敤绱㈠紩锛屽繀椤诲湪姣忎釜AND缁勪腑浣跨敤绱㈠紩鍓嶇紑銆

涓嬮潰鐨WHERE瀛愬彞浣跨敤绱㈠紩锛

... WHERE index_part1=1 AND index_part2=2 AND other_column=3
    /* index = 1 OR index = 2 */
... WHERE index=1 OR A=10 AND index=2
    /* optimized like "index_part1='hello'" */
... WHERE index_part1='hello' AND index_part3=5
    /* Can use index on index1 but not on index2 or index3 */
... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3;

涓嬮潰鐨WHERE瀛愬彞涓嶄娇鐢ㄧ储寮曪細

    /* index_part1 is not used */
... WHERE index_part2=1 AND index_part3=2
 
    /*  Index is not used in both parts of the WHERE clause  */
... WHERE index=1 OR A=10
 
    /* No index spans all rows  */
... WHERE index_part1=1 OR index_part2=10

鏈夋椂MySQL涓嶄娇鐢ㄧ储寮曪紝鍗充娇鏈夊彲鐢ㄧ殑绱㈠紩銆備竴绉嶆儏褰㈡槸褰撲紭鍖栧櫒浼拌鍒颁娇鐢ㄧ储寮曞皢闇瑕MySQL璁块棶琛ㄤ腑鐨勫ぇ閮ㄥ垎琛屾椂銆(鍦ㄨ繖绉嶆儏鍐典笅锛岃〃鎵弿鍙兘浼氭洿蹇簺锛屽洜涓洪渶瑕佺殑鎼滅储瑕佸皯锛鐒惰岋紝濡傛灉姝ょ被鏌ヨ浣跨敤LIMIT鍙悳绱㈤儴鍒嗚锛MySQL鍒欎娇鐢ㄧ储寮曪紝鍥犱负瀹冨彲浠ユ洿蹇湴鎵惧埌鍑犺骞跺湪缁撴灉涓繑鍥炪

Hash绱㈠紩杩樻湁涓浜涘叾瀹冪壒寰侊細

         瀹冧滑鍙敤浜庝娇鐢=<=>鎿嶄綔绗︾殑绛夊紡姣旇緝(寰堝揩)銆傚畠浠敤浜庢瘮杈 鎿嶄綔绗︼紝渚嬪鍙戠幇鑼冨洿鍊肩殑<

         浼樺寲鍣ㄤ笉鑳戒娇鐢hash绱㈠紩鏉ュ姞閫ORDER BY鎿嶄綔銆(璇ョ被绱㈠紩涓嶈兘鐢ㄦ潵鎸夐『搴忔悳绱笅涓涓潯鐩級

         MySQL涓嶈兘纭畾鍦ㄤ袱涓间箣闂村ぇ绾︽湁澶氬皯琛(杩欒鑼冨洿浼樺寲鍣ㄧ敤鏉ョ‘瀹氫娇鐢ㄥ摢涓储寮)銆傚鏋滀綘灏嗕竴涓MyISAM琛ㄦ敼涓hash-绱㈠紩鐨MEMORY琛紝浼氬奖鍝嶄竴浜涙煡璇€

         鍙兘浣跨敤鏁翠釜鍏抽敭瀛楁潵鎼滅储涓琛屻(B-鏍戠储寮曪紝浠讳綍鍏抽敭瀛楃殑鏈宸﹂潰鐨勫墠缂鍙敤鏉ユ壘鍒拌锛

7.4.6. MyISAM閿珮閫熺紦鍐

涓轰簡浣跨‖鐩I/O鏈灏忓寲锛MyISAM瀛樺偍寮曟搸浣跨敤涓涓璁稿鏁版嵁搴撶鐞嗙郴缁熶娇鐢ㄧ殑绛栫暐銆傚畠浣跨敤涓涓紦瀛樻満鍒跺皢缁忓父璁块棶鐨勮〃閿佸湪鍐呭瓨涓細

         瀵逛簬绱㈠紩鍧楋紝缁存姢涓涓О涔嬩负閿珮閫缂撳啿(閿珮閫熺紦鍐插尯)鐨勭壒娈婄粨鏋勩傝缁撴瀯鍖呭惈澶ч噺鍧楃紦瀛樺尯锛屽叾涓斁缃簡鏈甯哥敤鐨勭储寮曞潡銆

         瀵逛簬鏁版嵁鍧楋紝MySQL涓嶄娇鐢ㄧ壒娈婄紦瀛樸傝屼娇鐢ㄥ師鐢熺殑鎿嶄綔绯荤粺鏂囦欢绯荤粺鐨勭紦瀛樸

鏈妭棣栧厛鎻忚堪浜MyISAM閿珮閫熺紦鍐茬殑鍩烘湰鎿嶄綔銆傜劧鍚庤璁轰簡鎻愰珮 閿珮閫熺紦鍐叉ц兘骞朵娇浣犳洿濂藉湴鎺у埗缂撳瓨鎿嶄綔鐨勬渶鏂扮殑鏇存敼锛

         澶氫釜绾跨▼鍙互骞惰璁块棶缂撳瓨銆

         鍙互璁剧疆澶氫釜閿珮閫熺紦鍐诧紝骞跺皢琛ㄧ储寮曟寚瀹氱粰鍏蜂綋缂撳瓨銆

鍙互浣跨敤key_buffer_size绯荤粺鍙橀噺鎺у埗 閿珮閫熺紦鍐茬殑澶у皬銆傚鏋滆鍙橀噺璁剧疆涓洪浂锛屼笉浣跨敤閿珮閫熺紦鍐层傚鏋key_buffer_size鍊煎お灏忎笉鑳藉垎閰嶆渶灏忔暟閲忕殑鍧楃紦瀛樺尯(8)锛屼篃涓嶄娇鐢 閿珮閫熺紦鍐层

濡傛灉閿珮閫熺紦鍐蹭笉宸ヤ綔锛屽彧浣跨敤鎿嶄綔绯荤粺鎻愪緵鐨勫師鐢熸枃浠剁郴缁熺紦瀛樺尯璁块棶绱㈠紩鏂囦欢銆(鎹㈠彞璇濊锛屼娇鐢ㄤ笌琛ㄦ暟鎹潡鐩稿悓鐨勭瓥鐣ヨ〃鏉ヨ闂储寮曞潡锛

绱㈠紩鍧楁槸涓涓繛缁殑璁块棶MyISAM绱㈠紩鏂囦欢鐨勫崟浣嶃傞氬父涓涓储寮曞潡鐨勫ぇ灏忕瓑浜庣储寮B-鏍戣妭鐐圭殑澶у皬銆(鍦ㄧ‖鐩樹笂浣跨敤B-鏍戞暟鎹粨鏋勮〃绀虹储寮曘傛爲搴曢儴鐨勮妭鐐逛负鍙跺瓙鑺傜偣銆傚彾瀛愯妭鐐逛笂闈㈢殑鑺傜偣涓洪潪鍙跺瓙鑺傜偣锛

閿珮閫熺紦鍐茬粨鏋勪腑鐨勬墍鏈夊潡缂撳瓨鍖哄ぇ灏忕浉鍚屻傝澶у皬鍙互绛変簬銆佸ぇ浜庢垨灏忎簬琛ㄧ储寮曞潡鐨勫ぇ灏忋傞氬父杩欎袱涓间腑鐨勪竴涓槸鍙︿竴涓殑鍑犲嶃

褰撳繀椤昏闂〃绱㈠紩鍧椾腑鐨勬暟鎹椂锛屾湇鍔″櫒棣栧厛妫鏌ユ槸鍚﹀畠鍙互鐢ㄤ簬閿珮閫熺紦鍐蹭腑鐨勬煇浜涘潡缂撳瓨鍖恒傚鏋滈傜敤锛屾湇鍔″櫒璁块棶閿珮閫熺紦鍐蹭腑鐨勬暟鎹屼笉鏄‖鐩樹笂鐨勬暟鎹備篃灏辨槸璇达紝浠庣紦瀛樿鍙栨垨鍐欏叆缂撳瓨锛岃屼笉鏄粠纭洏璇诲啓銆傚惁鍒欙紝鏈嶅姟鍣ㄩ夋嫨涓涓寘鍚竴涓笉鍚岀殑琛ㄧ储寮曞潡鐨勭紦瀛樺潡缂撳瓨鍖猴紝骞剁敤闇瑕佺殑琛ㄧ储寮曞潡鐨勬嫹璐濇浛鎹㈤偅閲岀殑鏁版嵁銆備竴鏃︽柊鐨勭储寮曞潡浣嶄簬缂撳瓨涓紝鍙互璁块棶绱㈠紩鏁版嵁銆

濡傛灉鐢ㄤ簬鏇挎崲鐨勫潡宸茬粡琚慨鏀逛簡锛屽潡琚涓衡滆剰浜嗏濄傚湪杩欑鎯呭喌涓嬶紝鍦ㄦ浛鎹㈠墠锛屽叾鍐呭琚埛鏂板埌瀹冩潵鑷殑琛ㄧ储寮曘

閫氬父鏈嶅姟鍣ㄩ伒浠LRU(鏈杩戞渶灏戜娇鐢)绛栫暐锛氬綋閫夋嫨涓涓潡鐢ㄤ簬鏇挎崲鏃讹紝瀹冮夋嫨鏈杩戞渶灏戜娇鐢ㄧ殑绱㈠紩鍧椼備负浜嗕娇璇ラ夋嫨鏇村鏄擄紝 閿珮閫熺紦鍐叉ā鍧楃淮鎶ゆ墍鏈変娇鐢ㄧ殑鍧楃殑涓撻棬闃熷垪(LRU)銆傚綋璁块棶鍧楁椂锛屽畠琚斁鍒伴槦鍒楁渶鍚庛傚綋鍧楅渶瑕佹浛鎹㈡椂锛岄槦鍒楀紑澶寸殑鍧楁槸鏈杩戞渶灏戜娇鐢ㄧ殑鍧楋紝骞舵垚涓虹1涓欓夎呫

7.4.6.1. 鍏变韩閿珮閫熺紦鍐茶闂

鍦ㄤ互涓嬫潯浠朵笅锛岀嚎绋嬪彲浠ュ悓鏃惰闂敭楂橀熺紦鍐茬紦瀛樺尯锛

         娌℃湁琚洿鏂扮殑缂撳瓨鍖哄彲浠ヨ澶氫釜绾跨▼璁块棶銆

         姝h鏇存柊鐨勭紦瀛樺尯璁╅渶瑕佷娇鐢ㄥ畠鐨勭嚎绋嬬瓑寰呯洿鍒版洿鏂板畬鎴愩

         澶氫釜绾跨▼鍙互鍙戣捣璇锋眰鏇挎崲缂撳瓨鍧楋紝鍙瀹冧滑涓嶅郊姝ゅ共鎵(涔熷氨鏄锛屽彧瑕佸畠浠渶瑕佷笉鍚岀殑绱㈠紩鍧楋紝骞朵笖浣夸笉鍚岀殑缂撳瓨鍧楄鏇挎崲)

瀵归敭楂橀熺紦鍐茬殑鍏变韩璁块棶鍏佽鏈嶅姟鍣ㄥぇ澶ф彁楂樺悶鍚愰噺銆

7.4.6.2. 澶氶敭楂橀熺紦鍐

瀵归敭楂橀熺紦鍐茬殑鍏变韩璁块棶鍙互鎻愰珮鎬ц兘浣嗕笉鑳藉畬鍏ㄦ秷闄ょ嚎绋嬩箣闂寸殑绔熶簤銆傚畠浠粛鐒剁珵浜夊閿珮閫熺紦鍐茬紦瀛樺尯鐨勮闂繘琛岀鐞嗙殑鎺у埗缁撴瀯銆備负浜嗚繘涓姝ラ檷浣 閿珮閫熺紦鍐茶闂珶浜夛紝MySQL 5.1杩樻彁渚涗簡澶氫釜閿珮閫熺紦鍐诧紝鍏佽浣犱负涓嶅悓鐨勯敭楂橀熺紦鍐插垎閰嶄笉鍚岀殑琛ㄧ储寮曘

鏈夊涓敭楂橀熺紦鍐叉椂锛屽綋涓虹粰瀹氱殑MyISAM琛ㄥ鐞嗘煡璇㈡椂锛屾湇鍔″櫒蹇呴』鐭ラ亾浣跨敤鍝釜缂撳瓨銆傞粯璁ゆ儏鍐碉紝鎵鏈MyISAM琛ㄧ储寮曡缂撳瓨鍒伴粯璁 閿珮閫熺紦鍐蹭腑銆傝鎯充负鍏蜂綋閿珮閫熺紦鍐插垎閰嶈〃绱㈠紩锛屽簲浣跨敤CACHE INDEX璇彞(鍙傝13.5.5.1鑺傦紝鈥淐ACHE INDEX璇硶鈥)

渚嬪锛屼笅闈㈢殑璇彞灏嗚〃t1t2t3鐨勭储寮曞垎閰嶇粰鍚嶄负hot_cache鐨 閿珮閫熺紦鍐诧細

mysql> CACHE INDEX t1, t2, t3 IN hot_cache;
+---------+--------------------+----------+----------+
| Table   | Op                 | Msg_type | Msg_text |
+---------+--------------------+----------+----------+
| test.t1 | assign_to_keycache | status   | OK       |
| test.t2 | assign_to_keycache | status   | OK       |
| test.t3 | assign_to_keycache | status   | OK       |
+---------+--------------------+----------+----------+
 

鍙互鐢SET GLOBAL鍙傛暟璁剧疆璇彞鎴栦娇鐢ㄦ湇鍔″櫒鍚姩閫夐」璁剧疆鍦CACHE INDEX璇彞涓紩鐢ㄧ殑閿珮閫熺紦鍐茬殑澶у皬鏉ュ垱寤洪敭楂橀熺紦鍐层備緥濡傦細

mysql> SET GLOBAL keycache1.key_buffer_size=128*1024;

瑕佹兂鍒犻櫎閿珮閫熺紦鍐诧紝灏嗗叾澶у皬璁剧疆涓洪浂锛

mysql> SET GLOBAL keycache1.key_buffer_size=0;

璇锋敞鎰忎笉鑳藉垹闄ら粯璁ら敭楂橀熺紦鍐层傚垹闄ら粯璁ら敭楂橀熺紦鍐茬殑灏濊瘯灏嗚蹇界暐锛

mysql> set global key_buffer_size = 0;
 
mysql> show variables like 'key_buffer_size';
+-----------------+---------+
| Variable_name   | Value   |
+-----------------+---------+
| key_buffer_size | 8384512 |
+-----------------+---------+
 

閿珮閫熺紦鍐插彉閲忔槸缁撴瀯寮忕郴缁熷彉閲忥紝鏈変竴涓悕鍜岀粍浠躲傚浜keycache1.key_buffer_sizekeycache1鏄紦瀛樺彉閲忓悕锛key_buffer_size鏄紦瀛樼粍浠躲傚叧浜庡紩鐢ㄧ粨鏋勫紡 閿珮閫熺紦鍐茬郴缁熷彉閲忔墍浣跨敤鐨勮娉曠殑鎻忚堪锛屽弬瑙9.4.1鑺傦紝鈥滅粨鏋勫紡绯荤粺鍙橀噺鈥

榛樿鎯呭喌涓嬶紝琛ㄧ储寮曡鍒嗛厤缁欐湇鍔″櫒鍚姩鏃跺垱寤虹殑涓昏(榛樿)閿珮閫熺紦鍐层傚綋 閿珮閫熺紦鍐茶鍒犻櫎鍚庯紝鎵鏈夊垎閰嶇粰瀹冪殑绱㈠紩琚噸鏂板垎閰嶇粰榛樿閿珮閫熺紦鍐层

瀵逛簬涓涓繖鐨勬湇鍔″櫒锛屾垜浠缓璁噰鐢ㄤ娇鐢ㄤ笁涓敭楂橀熺紦鍐茬殑绛栫暐锛

         鍗犵敤涓烘墍鏈夐敭楂橀熺紦鍐插垎閰嶇殑绌洪棿鐨20%鐨勨滅儹鈥濋敭楂橀熺紦鍐层傝缂撳瓨鐢ㄤ簬棰戠箒鐢ㄤ簬鎼滅储浣嗘病鏈夋洿鏂扮殑琛ㄣ

         鍗犵敤涓烘墍鏈夐敭楂橀熺紦鍐插垎閰嶇殑绌洪棿鐨20%鐨勨滃喎鈥濋敭楂橀熺紦鍐层傝缂撳瓨鐢ㄤ簬涓瓑澶у皬銆佸ぇ閲忎慨鏀圭殑琛紝渚嬪涓存椂琛ㄣ

         鍗犵敤閿珮閫熺紦鍐茬┖闂寸殑20%鐨勨滄俯鈥濋敭楂橀熺紦鍐层備娇鐢ㄥ畠浣滀负榛樿 閿珮閫熺紦鍐诧紝榛樿鎯呭喌琚墍鏈夊叾瀹冭〃浣跨敤銆

浣跨敤3涓敭楂橀熺紦鍐叉湁濂藉鐨勪竴涓師鍥犳槸瀵逛竴涓敭楂橀熺紦鍐茬粨鏋勭殑璁块棶涓嶄細闃绘尅瀵瑰叾瀹冪殑璁块棶銆傝闂垎閰嶇粰涓涓紦瀛樼殑琛ㄧ殑鏌ヨ涓嶄細涓庤闂垎閰嶇粰鍏跺畠缂撳瓨鐨勮〃鐨勬煡璇㈢珵浜夈傜敱浜庡叾瀹冨師鍥犱篃浼氭彁楂樻ц兘锛

         鐑紦瀛樺彧鐢ㄤ簬妫绱㈡煡璇紝鍥犳鍏跺唴瀹瑰喅涓嶄細琚慨鏀广傜粨鏋滄槸锛屾棤璁轰綍鏃堕渶瑕佷粠纭洏涓婃媺鍏ョ储寮曞潡锛岄夋嫨鐢ㄤ簬鏇挎崲鐨勭紦瀛樺潡鐨勫唴瀹逛笉闇瑕佸厛鍒锋柊銆

         瀵逛簬鍒嗛厤缁欑儹缂撳瓨鐨勭储寮曪紝濡傛灉娌℃湁鏌ヨ闇瑕佺储寮曟壂鎻忥紝寰堟湁鍙兘瀵瑰簲绱㈠紩B-鏍戠殑闈炲彾瀛愯妭鐐圭殑绱㈠紩鍧椾粛鐒跺湪缂撳瓨涓

         褰撴洿鏂扮殑鑺傜偣浣嶄簬缂撳瓨涓苟涓斾笉闇瑕佸厛浠庣‖鐩樿鍏ユ椂锛屼负涓存椂琛ㄩ绻佹墽琛岀殑鏇存柊鎿嶄綔浼氭墽琛屽緱鏇村揩銆傚鏋滀复鏃惰〃鐨勭储寮曠殑澶у皬鍙互涓庡喎閿珮閫熺紦鍐茬浉姣旇緝锛屽緢鍙兘鏇存柊鐨勮妭鐐逛綅浜庣紦瀛樹腑銆

CACHE INDEX鍦ㄤ竴涓〃鍜 閿珮閫熺紦鍐蹭箣闂村缓绔嬩竴绉嶈仈绯伙紝浣嗘瘡娆℃湇鍔″櫒閲嶅惎鏃惰鑱旂郴琚涪澶便傚鏋滀綘鎯宠姣忔鏈嶅姟鍣ㄩ噸鍚椂璇ヨ仈绯荤敓鏁堬紝涓涓彂鍔炴硶鏄娇鐢ㄩ夐」鏂囦欢锛氬寘鎷厤缃 閿珮閫熺紦鍐茬殑鍙橀噺璁惧畾鍊硷紝鍜屼竴涓init-file閫夐」鐢ㄦ潵鍛藉悕鍖呭惈寰呮墽琛岀殑CACHE INDEX璇彞鐨勪竴涓枃浠躲備緥濡傦細

key_buffer_size = 4G
hot_cache.key_buffer_size = 2G
cold_cache.key_buffer_size = 2G
init_file=/path/to/data-directory/mysqld_init.sql

姣忔鏈嶅姟鍣ㄥ惎鍔ㄦ椂鎵цmysqld_init.sql涓殑璇彞銆傝鏂囦欢姣忚搴斿寘鍚竴涓SQL璇彞銆備笅闈㈢殑渚嬪瓙鍒嗛厤鍑犱釜琛紝鍒嗗埆瀵瑰簲hot_cachecold_cache

CACHE INDEX a.t1, a.t2, b.t3 IN hot_cache
CACHE INDEX a.t4, b.t5, b.t6 IN cold_cache

7.4.6.3. 涓偣鎻掑叆绛栫暐

榛樿鎯呭喌锛岄敭楂橀熺紦鍐茬鐞嗙郴缁熼噰鐢LRU绛栫暐閫夋嫨瑕佹敹鍥炵殑閿珮閫熺紦鍐插潡锛屼絾瀹冧篃鏀寔鏇村鏉傜殑鏂规硶锛岀О涔嬩负鈥涓偣鎻掑叆绛栫暐鈥濄

褰撲娇鐢ㄤ腑鐐规彃鍏ョ瓥鐣ユ椂锛LRU閾捐鍒嗕负涓や釜閮ㄥ垎锛氫竴鏉$儹瀛愰摼鍜屼竴鏉℃俯瀛愰摼銆備袱閮ㄥ垎涔嬮棿鐨勫垝鍒嗙偣涓嶅浐瀹氾紝浣 閿珮閫熺紦鍐茬鐞嗙郴缁熷叧娉ㄦ俯閮ㄥ垎涓嶁澶煭鈥濓紝鎬绘槸鍖呭惈鑷冲皯key_cache_division_limit姣斾緥鐨 閿珮閫熺紦鍐插潡銆key_cache_division_limit鏄粨鏋勫紡 閿珮閫熺紦鍐插彉閲忕殑涓涓粍浠讹紝鍥犳鍏跺兼槸涓涓彲浠ユ牴鎹瘡涓紦瀛樿繘琛岃缃殑鍙傛暟銆

褰撲竴涓储寮曞潡浠庤〃涓鍏ラ敭楂橀熺紦鍐诧紝瀹冭鏀惧叆娓╁瓙閾剧殑鏈銆傜粡杩囦竴瀹氶噺鐨勮闂悗(璁块棶鍧)锛屽畠琚彁鍗囩粰鐑瓙閾俱傜洰鍓嶏紝闇瑕佺敤鏉ユ彁鍗囦竴涓潡(3)鐨勮闂鏁颁笌鎵鏈夌储寮曞潡鐨勭浉鍚屻

鎻愬崌鍒扮儹瀛愰摼鐨勫潡琚斁鍒板瓙閾剧殑鏈銆傚潡鐒跺悗鍦ㄨ瀛愰摼涓惊鐜傚鏋滃潡鍦ㄥ瓙閾剧殑寮澶村仠鐣欒冻澶熼暱鐨勬椂闂达紝瀹冭闄嶅埌娓╅摼銆傝鏃堕棿鐢遍敭楂橀熺紦鍐key_cache_age_threshold缁勪欢鐨勫肩‘瀹氥

瀵逛簬鍖呭惈N涓潡鐨 閿珮閫熺紦鍐诧紝闃堝艰〃绀猴紝鐑瓙閾惧紑澶寸殑娌℃湁鍦ㄦ渶鍚N *key_cache_age_threshold/100娆¤闂腑琚闂殑鍧楀皢琚Щ鍔ㄥ埌娓╁瓙閾惧紑澶淬傝鍧楃劧鍚庡彉涓虹1涓尋鍑虹殑鍊欓夎咃紝鍥犱负鏇挎崲鐨勫潡鎬绘槸鏉ヨ嚜娓╁瓙閾剧殑寮澶淬

涓偣鎻掑叆绛栫暐鍏佽浣犲皢鏇存湁浠峰肩殑鍧楁绘槸鍦ㄧ紦瀛樹腑銆傚鏋滀綘鎯充娇鐢ㄧ畝鍗曠殑LRU绛栫暐锛屼娇key_cache_division_limit鍊间繚鎸佸叾榛樿鍊100

鑻ユ墽琛岀殑鏌ヨ瑕佹眰绱㈠紩鎵弿鏈夋晥鎺ㄥ嚭鎵鏈夌储寮曞潡瀵瑰簲鏈夋暟鍊肩殑楂樼骇B-鏍戣妭鐐圭殑缂撳瓨锛屼腑鐐规彃鍏ョ瓥鐣ュ彲浠ュ府鍔╂彁楂樻ц兘銆傝鎯抽伩鍏嶏紝蹇呴』浣跨敤涓偣鎻掑叆绛栫暐锛岃key_cache_division_limit璁剧疆涓鸿繙灏忎簬100銆傜劧鍚庡湪绱㈠紩鎵弿鎿嶄綔杩囩▼涓紝鏈夋暟鍊肩殑缁忓父璁块棶鐨勮妭鐐硅淇濈暀鍦ㄧ儹瀛愰摼涓

7.4.6.4. 绱㈠紩棰勫姞杞

濡傛灉閿珮閫熺紦鍐插唴鏈夎冻澶熺殑鍧椾互瀹圭撼鏁翠釜绱㈠紩鐨勫潡锛屾垨鑰呰嚦灏戝绾冲搴斿叾闈炲彾鑺傜偣鐨勫潡锛屽垯鍦ㄤ娇鐢ㄥ墠锛岄瑁呭惈绱㈠紩鍧楃殑閿珮閫熺紦鍐插緢鏈夋剰涔夈傞瑁呭彲浠ヤ互鏇存湁鏁堢殑鏂瑰紡灏嗚〃绱㈠紩鍧楁斁鍏 閿珮閫熺紦鍐茬紦瀛樺尯涓細閫氳繃椤哄簭鍦颁粠纭洏璇诲彇绱㈠紩鍧椼

涓嶈繘琛岄瑁咃紝鍧椾粛鐒舵牴鎹煡璇㈤渶瑕佹斁鍏ラ敭楂橀熺紦鍐蹭腑銆傚敖绠″潡灏嗕粛鐒跺湪缂撳瓨涓紙鍥犱负鏈夎冻澶熺殑缂撳瓨鍖轰繚瀛樺畠浠級锛屽畠浠互闅忔満鏂瑰紡浠庣‖鐩樹笂绱㈠彇锛岃屼笉鏄互椤哄簭鏂瑰紡銆

瑕佹兂灏嗙储寮曢瑁呭埌缂撳瓨涓紝浣跨敤LOAD INDEX INTO CACHE璇彞銆備緥濡傦紝涓嬮潰鐨勮鍙ュ彲浠ラ瑁呰〃t1t2绱㈠紩鐨勮妭鐐(绱㈠紩鍧)

mysql> LOAD INDEX INTO CACHE t1, t2 IGNORE LEAVES;
+---------+--------------+----------+----------+
| Table   | Op           | Msg_type | Msg_text |
+---------+--------------+----------+----------+
| test.t1 | preload_keys | status   | OK       |
| test.t2 | preload_keys | status   | OK       |
+---------+--------------+----------+----------+

IGNORE LEAVES淇敼鍣ㄥ彧鍏佽棰勮绱㈠紩闈炲彾鑺傜偣鎵鐢ㄧ殑鍧椼傝繖鏍凤紝涓婅堪鐨勮鍙ラ瑁t1涓殑鎵鏈夌储寮曞潡锛屼絾鍙瑁t2涓殑闈炲彾鑺傜偣瀵瑰簲鐨勫潡銆

濡傛灉宸茬粡浣跨敤CACHE INDEX璇彞涓轰竴涓储寮曞垎閰嶄簡涓涓敭楂橀熺紦鍐诧紝棰勮鍙互灏嗙储寮曞潡鏀惧叆璇ョ紦瀛樸傚惁鍒欙紝绱㈠紩琚鍏ラ粯璁ら敭楂橀熺紦鍐层

7.4.6.5. 閿珮閫熺紦鍐插潡澶у皬

鍙互浣跨敤key_cache_block_size鍙橀噺涓哄叿浣撶殑 閿珮閫熺紦鍐叉寚瀹氬潡缂撳瓨鍖虹殑澶у皬銆傝繖鏍峰厑璁镐负绱㈠紩鏂囦欢璋冭妭I/O鎿嶄綔鐨勬ц兘銆

褰撹缂撳瓨鍖虹殑澶у皬绛変簬鍘熺敓鎿嶄綔绯荤粺I/O缂撳瓨鍖虹殑澶у皬鏃讹紝鍙互鑾峰緱I/O鎿嶄綔鐨勬渶浣虫ц兘銆備絾鏄皢鍏抽敭瀛楄妭鐐圭殑澶у皬璁剧疆涓虹瓑浜I/O缂撳瓨鍖虹殑澶у皬骞朵笉鎬绘槸鑳戒繚璇佹渶浣虫暣浣撴ц兘銆傚綋璇诲彇澶х殑鍙惰妭鐐规椂锛屾湇鍔″櫒璇诲叆澶ч噺鐨勪笉闇瑕佺殑鏁版嵁锛岀粨鏋滈槻姝㈣鍏ュ叾瀹冨彾瀛愮殑鑺傜偣銆

鐩墠锛屼綘涓嶈兘鎺у埗琛ㄥ唴绱㈠紩鍧楃殑澶у皬銆傝澶у皬鐢辨湇鍔″櫒鍦ㄥ垱寤.MYI绱㈠紩鏂囦欢鏃惰缃紝鍙栧喅浜庤〃瀹氫箟涓储寮曠殑鍏抽敭瀛楀ぇ灏忋傚湪澶у鏁版儏鍐典笅锛屽畠琚缃负涓I/O缂撳瓨鍖哄ぇ灏忕浉绛夈

7.4.6.6. 閲嶆瀯閿珮閫熺紦鍐

閿珮閫熺紦鍐插彲浠ラ氳繃鏇存柊鍏跺弬鏁板奸殢鏃堕噸鏂版瀯寤恒備緥濡傦細

mysql> SET GLOBAL cold_cachekey_buffer_size=4*1024*1024

濡傛灉浣犱负key_buffer_sizekey_cache_block_size閿珮閫熺紦鍐茬粍浠跺垎閰嶇殑鍊间笌缁勪欢褰撳墠鐨勫间笉鍚岋紝鏈嶅姟鍣ㄥ皢姣佹帀缂撳瓨鐨勬棫缁撴瀯骞舵牴鎹柊鍊煎垱寤轰竴涓柊鐨勩傚鏋滅紦瀛樺寘鍚换浣曡剰鐨勫潡锛屾湇鍔″櫒鍦ㄩ攢姣佸墠灏嗗畠浠繚瀛樺埌纭洏涓婂苟閲嶆柊鍒涘缓缂撳瓨銆傚鏋滀綘璁剧疆鍏跺畠 閿珮閫熺紦鍐插弬鏁帮紝鍒欎笉浼氬彂鐢熼噸鏂版瀯寤恒

褰撻噸鏂版瀯寤洪敭楂橀熺紦鍐叉椂锛屾湇鍔″櫒棣栧厛灏嗕换浣曡剰缂撳瓨鍖虹殑鍐呭鍒锋柊鍒扮‖鐩樹笂銆備箣鍚庯紝缂撳瓨鍐呭涓嶅啀闇瑕併傜劧鑰岋紝閲嶆柊鏋勫缓骞朵笉闃诲闇瑕佷娇鐢ㄥ垎閰嶇粰缂撳瓨鐨勭储寮曠殑鏌ヨ銆傜浉鍙嶏紝鏈嶅姟鍣ㄤ娇鐢ㄥ師鐢熸枃浠剁郴缁熺紦瀛樼洿鎺ヨ闂〃绱㈠紩銆傛枃浠剁郴缁熺紦瀛樹笉濡備娇鐢 閿珮閫熺紦鍐叉湁鏁堬紝鍥犳灏界鏌ヨ鍙互鎵ц锛屼絾閫熷害浼氬噺鎱€傜紦瀛樿閲嶆柊鏋勫缓鍚庯紝瀹冨張鍙互缂撳瓨鍒嗛厤缁欏畠鐨勭储寮曚簡锛屽苟涓旂储寮曚笉鍐嶄娇鐢ㄦ枃浠剁郴缁熺紦瀛樸

7.4.7.聽MyISAM绱㈠紩缁熻闆嗗悎

瀛樺偍寮曟搸鎼滈泦浼樺寲鍣ㄤ娇鐢ㄧ殑琛ㄧ殑缁熻淇℃伅銆傝〃缁熻鍩轰簬鏁版暟鍊肩粍锛屽叾涓暟鏁板肩粍鏄竴绯诲垪鏈夌浉鍚岀殑鍏抽敭瀛楀墠缂鍊肩殑璁板綍銆傚浜庝紭鍖栧櫒锛岄噸瑕佺殑缁熻鍗充负鏁版暟鍊肩粍鐨勫钩鍧囧ぇ灏忋

MySQL鐢ㄤ笅杩版柟寮忎娇鐢ㄥ钩鍧囨暟鏁板肩粍锛

         浼拌蹇呴』涓烘瘡涓ref璁块棶璇诲彇澶氬皯琛

         浼拌閮ㄥ垎鑱旀帴灏嗕骇鐢熷灏戣锛涗篃灏辨槸璇达紝涓嬭堪褰㈠紡鐨勬搷浣滃皢浜х敓鐨勮鏁帮細

                  (...) JOIN tbl_name ON tbl_name.key = expr

闅忕潃绱㈠紩鐨勫钩鍧囨暟鏁板肩粍澶у皬鐨勫鍔狅紝绱㈠紩灏嗘洿娌℃湁鐢紝鍥犱负姣忎釜鏌ユ壘鐨勫钩鍧囪鏁板鍔狅細涓轰簡璁╃储寮曟湁鍒╀簬浼樺寲鐩殑锛屾渶濂芥槸姣忎釜绱㈠紩鍊煎搴旇〃鍐呯殑灏戦噺琛屾暟銆傚綋鏌愪釜缁欏畾鐨勭储寮曞间骇鐢熻緝澶氳鏃讹紝绱㈠紩鏇村姞娌℃湁鐢紝MySQL鏇翠笉鍙兘浣跨敤瀹冦

骞冲潎鏁版暟鍊肩粍澶у皬涓庤〃鐨勯泦鐨勫娍鐩稿叧锛屽嵆鏁版暟鍊肩粍鐨勬暟鐩SHOW INDEX璇彞鏄剧ず闆嗙殑鍔垮硷紙鍩轰簬N/S锛夛紝鍏朵腑N鏄〃鍐呯殑璁板綍鏁帮紝S鏄钩鍧囨暟鏁板肩粍澶у皬銆傝姣斾緥浜х敓琛ㄥ唴鏁版暟鍊肩粍鐨勫ぇ绾︽暟銆

瀵逛簬鍩轰簬<=>姣旇緝 鎿嶄綔绗︾殑鑱旀帴锛NULL骞朵笉瑙嗕负涓庝换浣曞叾瀹冨间笉鍚岋細NULL <=> NULL锛屾濡傚浜庡叾瀹N N <=> N

鐒惰岋紝瀵逛簬鍩轰簬=鎿嶄綔绗︾殑鑱旀帴锛NULL涓庨潪NULL鍊间笉鍚岋細褰expr1expr2(鎴栦袱鑰)NULL鏃讹紝expr1 = expr2涓嶄负鐪熴傝繖鏍峰奖鍝嶆瘮杈冨舰寮tbl_name.key = exprref璁块棶锛氬鏋expr褰撳墠鐨勫间负NULLMySQL涓嶄細璁块棶琛紝鍥犱负姣旇緝涓嶈兘涓虹湡銆

瀵逛簬=姣旇緝锛岃〃鍐呮湁澶氬皯NULL鍊煎苟涓嶉噸瑕併備负浜嗕紭鍖栫洰鐨勶紝鐩稿叧鍊间负闈NULL鏁板肩粍鐨勫钩鍧囧ぇ灏忋傜劧鑰岋紝MySQL鐩墠涓嶅厑璁告悳闆嗘垨浣跨敤璇ュ钩鍧囧ぇ灏忋

瀵逛簬MyISAM琛紝浣犲彲浠ヤ娇鐢myisam_stats_method绯荤粺鍙橀噺閮ㄥ垎鎺у埗琛ㄧ粺璁′俊鎭殑鎼滈泦銆傝鍙橀噺鏈変袱涓彲鑳界殑涓嶅悓鍊硷紝濡備笅鎵绀猴細

         myisam_stats_methodnulls_equal鏃讹紝鎵鏈NULL鍊艰瑙嗕负鐩哥瓑鐨(涔熷氨鏄锛屽畠浠兘褰㈡垚涓涓暟鍊肩粍)

濡傛灉NULL鏁板肩粍澶у皬杩滃ぇ浜庡钩鍧囬潪NULL鏁板肩粍澶у皬锛岃鏂规硶鍚戜笂鍊炬枩骞冲潎鏁版暟鍊肩粍澶у皬銆傝繖鏍蜂娇绱㈠紩瀵逛簬浼樺寲鍣ㄦ潵璇存瘮瀹冨疄闄呬负鏌ユ壘闈NULL鍊肩殑鑱旀帴鏇村姞娌℃湁鐢ㄣ傜粨鏋滄槸锛nulls_equal鏂规硶浼氫娇浼樺寲鍣ㄨ繘琛ref璁块棶鏃舵湰搴斾娇鐢ㄧ储寮曡屾病鏈変娇鐢ㄣ

         myisam_stats_methodnulls_unequal鏃讹紝NULL鍊间笉瑙嗕负鐩稿悓銆傜浉鍙嶏紝姣忎釜NULL鍊煎舰鎴愪竴涓崟鐙殑鏁板肩粍锛屽ぇ灏忎负1

濡傛灉浣犳湁璁稿NULL鍊硷紝璇ユ柟娉曞悜涓嬪炬枩骞冲潎鏁版暟鍊肩粍澶у皬銆傚鏋滃钩鍧囬潪NULL鏁板肩粍杈冨ぇ锛岀粺璁″ぇ灏忎负1鐨勬瘡涓粍鐨NULL鍊间細浣夸紭鍖栧櫒杩囬珮浼拌鏌ユ壘闈NULL鍊肩殑鑱旀帴鐨勭储寮曞笺傜粨鏋滄槸锛屽綋鍏跺畠鏂规硶浼氭洿濂芥椂锛nulls_unequal鏂规硶浼氫娇浼樺寲鍣ㄤ负ref鏌ユ壘浣跨敤璇ョ储寮曘

濡傛灉浣犺浣跨敤璁稿浣跨敤<=>鑰屼笉鏄=鐨勮仈鎺ワ紝鍦ㄦ瘮杈冭繃绋嬩腑NULL鍊煎苟涓嶇壒娈婏紝涓涓NULL绛変簬鍙︿竴涓NULL銆傚湪杩欑鎯呭喌涓嬶紝nulls_equal鏄悎閫傜殑缁熻鏂规硶銆

myisam_stats_method绯荤粺鍙橀噺鏈夊叏灞鍜屼細璇濆笺傝缃叏灞鍊间細褰卞搷MyISAM 涓烘墍鏈MyISAM琛ㄧ殑缁熻鐨勬悳闆嗐傝缃細璇濆煎彧褰卞搷褰撳墠瀹㈡埛杩炴帴鐨勭粺璁$殑鎼滈泦銆傝繖璇存槑浣犲彲浠ュ己鍒剁敤缁欏畾鐨勬柟娉曢噸鏂扮敓鎴愯〃鐨勭粺璁$殑鎼滈泦锛岃屼笉闇瑕佸洜涓鸿缃myisam_stats_method鐨勪細璇濆艰屽奖鍝嶅叾瀹冨鎴枫

鍙互浣跨敤涓嬮潰浠讳竴鏂规硶鏉ラ噸鏂扮敓鎴愯〃鐨勭粺璁′俊鎭細

         璁剧疆myisam_stats_method锛岀劧鍚庢墽琛CHECK TABLE璇彞

         鎵цmyisamchk --stats_method=method_name --analyze

         鏇存敼琛紝浣垮叾缁熻淇℃伅涓嶄负鏈鏂(渚嬪锛屾彃鍏ヤ竴琛岀劧鍚庡垹闄ゅ畠)锛岀劧鍚庤缃myisam_stats_method骞舵墽琛ANALYZE TABLE璇彞

浣跨敤myisam_stats_method鐨勪竴浜涜鍛婏細

浣犲彲浠ュ己鍒舵樉寮忔悳闆嗚〃鐨勭粺璁′俊鎭紝濡備笂鎵杩般傜劧鑰岋紝MySQL涔熷彲浠ヨ嚜鍔ㄦ悳闆嗙粺璁′俊鎭備緥濡傦紝濡傛灉鍦ㄤ负琛ㄦ墽琛岃鍙ョ殑杩囩▼涓紝涓浜涜鍙ヤ慨鏀逛簡琛紝MySQL鍙互鎼滈泦缁熻淇℃伅銆(渚嬪锛屽ぇ鎵规彃鍏ユ垨鍒犻櫎锛屾垨鑰呮墽琛ALTER TABLE璇彞鏃跺彲鑳藉彂鐢燂級濡傛灉鍙戠敓锛屼娇鐢myisam_stats_method姝ゆ椂鎵鏈夌殑鍊兼悳闆嗙粺璁′俊鎭傝繖鏍凤紝濡傛灉浣犱娇鐢ㄤ竴涓柟娉曟悳闆嗙粺璁′俊鎭紝浣嗗綋鍚庨潰鑷姩鎼滈泦涓涓〃鐨勭粺璁′俊鎭椂myisam_stats_method琚缃负鍙︿竴涓柟娉曪紝灏嗕娇鐢ㄥ叾瀹冩柟娉曘

瀵逛簬缁欏畾鐨MyISAM琛紝杩樹笉鑳借鍑轰娇鐢ㄥ摢涓柟娉曟潵浜х敓缁熻淇℃伅銆

myisam_stats_method鍙傚悎MyISAM琛ㄣ傚叾瀹冨瓨鍌ㄥ紩鎿庡彧鏈変竴涓柟娉曟潵鎼滈泦琛ㄧ殑缁熻淇℃伅銆傞氬父瀹冩帴杩戜簬nulls_equal鏂规硶銆

7.4.8. MySQL濡備綍璁$畻鎵撳紑鐨勮〃

褰撹繍琛mysqladmin status鏃讹紝灏嗙湅瑙佽薄杩欐牱鐨勪竴浜涗笢瑗匡細

Uptime: 426 Running threads: 1 Questions: 11082
Reloads: 1 Open tables: 12
 

濡傛灉浣犱粎鏈6涓〃锛Open tables鍊间负12鍙兘鏈夌偣浠や汉鍥版儜銆

MySQL鏄绾跨▼鐨勶紝鍥犳璁稿瀹㈡埛鍙互鍚屾椂鍦ㄥ悓涓涓〃涓婅繘琛屾煡璇€備负浜嗕娇澶氫釜瀹㈡埛绾跨▼鍦ㄥ悓涓涓〃涓婃湁涓嶅悓鐘舵佺殑闂鍑忓埌鏈灏忥紝琛ㄨ姣忎釜骞跺彂杩涚▼鐙珛鍦版墦寮銆傝繖鏍烽渶瑕侀澶栫殑鍐呭瓨浣嗕竴鑸細鎻愰珮鎬ц兘銆傚浜MyISAM琛紝鏁版嵁鏂囦欢闇瑕佷负姣忎釜鎵撳紑琛ㄧ殑瀹㈡埛鎻愪緵涓涓澶栫殑鏂囦欢鎻忚堪绗︺(绱㈠紩鏂囦欢鎻忚堪绗﹀湪鎵鏈夌嚎绋嬩箣闂村叡浜)

涓嬩竴鑺備腑鎻愪緵浜嗚涓婚鐨勬洿澶氱殑淇℃伅銆傚弬瑙7.4.9鑺傦紝鈥淢ySQL濡備綍鎵撳紑鍜屽叧闂〃鈥

7.4.9. MySQL濡備綍鎵撳紑鍜屽叧闂〃

table_cachemax_connectionsmax_tmp_tables绯荤粺鍙橀噺褰卞搷鏈嶅姟鍣ㄤ繚鎸佹墦寮鐨勬枃浠剁殑鏈澶ф暟閲忋傚鏋滀綘澧炲姞杩欎簺鍊煎叾涓殑涓涓垨涓や釜锛屼細閬囧埌鎿嶄綔绯荤粺涓烘瘡涓繘绋嬫墦寮鏂囦欢鎻忚堪绗︾殑鏁伴噺寮哄姞鐨勯檺鍒躲傝澶氭搷浣滅郴缁熷厑璁镐綘澧炲姞鎵撳紑鐨勬枃浠剁殑闄愬埗锛屽敖绠¤鏂规硶闅忕郴缁熺殑涓嶅悓鑰屼笉鍚屻傛煡闃呮搷浣滅郴缁熸枃妗d互纭畾鏄惁鍙互澧炲姞闄愬埗浠ュ強濡備綍鎿嶄綔銆

table_cachemax_connections鏈夊叧銆備緥濡傦紝瀵逛簬200涓苟琛岃繍琛岀殑杩炴帴锛屽簲璇ヨ琛ㄧ殑缂撳瓨鑷冲皯鏈200 * N锛岃繖閲N鏄彲浠ユ墽琛岀殑鏌ヨ鐨勪竴涓仈鎺ヤ腑琛ㄧ殑鏈澶ф暟閲忋傝繕闇瑕佷负涓存椂琛ㄥ拰鏂囦欢淇濈暀涓浜涢澶栫殑鏂囦欢鎻忚堪绗︺

纭繚鎿嶄綔绯荤粺鍙互澶勭悊table_cache璁剧疆鎵鎸囩殑鎵撳紑鐨勬枃浠舵弿杩扮鐨勬暟鐩傚鏋table_cacheis璁惧緱澶珮锛MySQL鍙兘涓烘枃浠舵弿杩扮鑰楀敖璧勬簮骞舵嫆缁濊繛鎺ワ紝涓嶈兘鎵ц鏌ヨ锛屽苟涓斿緢涓嶅彲闈犮傝繕蹇呴』鑰冭檻鍒MyISAM瀛樺偍寮曟搸闇瑕佷负姣忎釜鎵撳紑鐨勮〃鎻愪緵涓や釜鏂囦欢鎻忚堪绗︺傚彲浠ュ湪mysqld_safe涓娇鐢--open-files-limit鍚姩閫夐」鏉ュ鍔MySQL閫傜敤鐨勬枃浠舵弿杩扮鐨勬暟閲忋傚弬瑙A.2.17鑺傦紝鈥滄枃浠舵湭鎵惧埌鈥

鎵撳紑琛ㄧ殑缂撳瓨鍙互淇濇寔鍦table_cache鏉° 榛樿涓64锛涘彲浠ョ敤mysqld--table_cache閫夐」鏉ユ洿鏀广傝娉ㄦ剰 MySQL鍙互涓存椂鎵撳紑鏇村鐨 琛ㄤ互鎵ц鏌ヨ銆

鍦ㄤ笅闈㈢殑鏉′欢涓嬶紝鏈娇鐢ㄧ殑琛ㄥ皢琚叧闂苟浠庤〃缂撳瓨涓Щ鍑猴細

         褰撶紦瀛樻弧浜嗗苟涓斾竴涓嚎绋嬭瘯鍥炬墦寮涓涓笉鍦ㄧ紦瀛樹腑鐨勮〃鏃躲

         褰撶紦瀛樺寘鍚秴杩table_cache涓潯鐩紝骞朵笖缂撳瓨涓殑琛ㄤ笉鍐嶈浠讳綍绾跨▼浣跨敤銆

         褰撹〃鍒锋柊鎿嶄綔鍙戠敓銆傚綋鎵цFLUSH TABLES璇彞鎴栨墽琛mysqladmin flush-tablesmysqladmin refresh鍛戒护鏃朵細鍙戠敓銆

褰撹〃缂撳瓨婊℃椂锛屾湇鍔″櫒浣跨敤涓嬪垪杩囩▼鎵惧埌涓涓紦瀛樺叆鍙f潵浣跨敤锛

         褰撳墠鏈娇鐢ㄧ殑琛ㄨ閲婃斁锛屼互鏈杩戞渶灏戜娇鐢ㄩ『搴忋

         濡傛灉缂撳瓨婊′簡骞朵笖娌℃湁琛ㄥ彲浠ラ噴鏀撅紝浣嗘槸涓涓柊琛ㄩ渶瑕佹墦寮锛岀紦瀛樺繀椤讳复鏃惰鎵╁ぇ銆

濡傛灉缂撳瓨澶勪簬涓涓复鏃舵墿澶х姸鎬佸苟涓斾竴涓〃浠庡湪鐢ㄥ彉涓轰笉鍦ㄧ敤鐘舵侊紝瀹冭鍏抽棴骞朵粠缂撳瓨涓噴鏀俱

瀵规瘡涓苟鍙戣闂墦寮涓涓〃銆傝繖鎰忓懗鐫锛屽鏋2涓嚎绋嬭闂悓涓涓〃鎴栧湪鍚屼竴涓煡璇腑璁块棶琛ㄤ袱娆(渚嬪锛屽皢琛ㄨ繛鎺ヤ负鑷韩鏃)锛岃〃闇瑕佽鎵撳紑涓ゆ銆傛瘡涓苟琛岀殑鎵撳紑瑕佹眰鍦ㄨ〃缂撳瓨涓湁涓涓潯鐩備换浣曡〃鐨勭涓娆℃墦寮鍗2涓枃浠舵弿杩扮锛氫竴涓敤浜庢暟鎹枃浠跺彟涓涓敤浜庣储寮曟枃浠躲傝〃鐨勬瘡涓娆¢澶栦娇鐢ㄤ粎鍗犱竴涓暟鎹枃浠剁殑鏂囦欢鎻忚堪绗︺傜储寮曟枃浠舵弿杩扮鍦ㄦ墍鏈夌嚎绋嬩箣闂村叡浜

濡傛灉浣犳鐢HANDLER tbl_name OPEN璇彞鎵撳紑涓涓〃锛屽皢涓鸿绾跨▼涓撻棬鍒嗛厤涓涓〃銆傝琛ㄤ笉琚叾瀹冪嚎绋嬪叡浜紝鍙湁绾跨▼璋冪敤HANDLER tbl_name CLOSE鎴栫嚎绋嬬粓姝㈠悗鎵嶈鍏抽棴銆傝〃鍏抽棴鍚庯紝琚媺鍥炶〃缂撳瓨涓(濡傛灉缂撳瓨涓嶆弧)銆傚弬瑙13.2.3鑺傦紝鈥淗ANDLER璇硶鈥

鍙互閫氳繃妫鏌mysqld鐨勭姸鎬佸彉閲Opened_tables纭畾琛ㄧ紦瀛樻槸鍚﹀お灏忥細

mysql> SHOW STATUS LIKE 'Opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 2741  |
+---------------+-------+

濡傛灉鍊煎緢澶э紝鍗充娇浣犳病鏈夊彂鍑鸿澶FLUSH TABLES璇彞锛屼篃搴斿鍔犺〃缂撳瓨鐨勫ぇ灏忋傚弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥5.3.4鑺傦紝鈥滄湇鍔″櫒鐘舵佸彉閲忊

7.4.10. 鍦ㄥ悓涓涓暟鎹簱涓垱寤哄涓〃鐨勭己闄

濡傛灉鍦ㄥ悓涓涓暟鎹簱鐩綍涓湁璁稿MyISAM琛紝鎵撳紑銆佸叧闂拰鍒涘缓鎿嶄綔灏嗕細寰堟參銆傚鏋滃璁稿涓嶅悓鐨勮〃鎵цSELECT璇彞锛屽綋琛ㄧ紦瀛樻弧鏃讹紝灏嗘湁涓鐐瑰紑閿锛屽洜涓哄姣忎釜蹇呴』鎵撳紑鐨勮〃锛屽彟澶栦竴涓繀椤昏鍏抽棴銆傚彲浠ラ氳繃浣胯〃缂撳瓨鏇村ぇ浜涙潵鍑忓皯杩欎釜寮閿銆

7.5. 浼樺寲MySQL鏈嶅姟鍣

7.5.1. 绯荤粺鍥犵礌鍜屽惎鍔ㄥ弬鏁扮殑璋冭妭

鎴戜滑浠庣郴缁熺骇鍥犵礌寮濮嬶紝鍥犱负蹇呴』灏芥棭鍦拌繘琛岄儴鍒嗗喅绛栦互鑾峰緱杈冨ぇ鎬ц兘銆傚湪鍏跺畠鎯呭喌涓嬶紝蹇熸祻瑙堣鑺傚氨瓒冲浜嗐備絾鏄紝浜嗚В涓涓嬫洿鏀硅灞傛鐨勫弬鏁拌兘澶熻幏寰楀灏戞ц兘鎻愰珮鏄緢鏈夋剰涔夌殑銆

浣跨敤鐨勬搷浣滅郴缁熷緢閲嶈銆備负浜嗘洿濂藉湴浣跨敤澶CPU鏈哄櫒锛屽簲浣跨敤Solaris(鍥犱负鍏剁嚎绋嬪伐浣滃緱寰堝ソ)Linux(鍥犱负2.4鍜屼互鍚庣殑鍐呮牳鏈夊緢濂界殑SMP鏀寔)銆傝娉ㄦ剰榛樿鎯呭喌鏃х殑Linux鍐呮牳鏈変竴涓2GB鐨勬枃浠跺ぇ灏忛檺鍒躲傚鏋滄湁杩欐牱鐨勪竴涓唴鏍稿苟涓旈渶瑕佹枃浠跺ぇ浜2GB锛屽簲寰楀埌ext2鏂囦欢绯荤粺鐨勫ぇ鏂囦欢鏀寔(LFS)琛ヤ竵銆傚叾瀹冩枃浠剁郴缁熶緥濡ReiserFSXFS娌℃湁姝2GB闄愬埗銆

MySQL鐢ㄤ簬鐢熶骇鍓嶏紝鎴戜滑寤鸿浣犲湪鎯崇敤鐨勫钩鍙颁笂瀵瑰畠杩涜娴嬭瘯銆

鍏跺畠鎶宸э細

         濡傛灉鏈夎冻澶熺殑RAM锛屽彲浠ョЩ闄ゆ墍鏈夌殑浜ゆ崲璁惧銆傛湁浜涙搷浣滅郴缁熷嵆浣挎湁鑷敱鍐呭瓨涔熶娇鐢ㄤ氦鎹㈣澶囥

         浣跨敤--skip-external-locking MySQL閫夐」浠ラ伩鍏嶅閮ㄩ攣瀹氥傝閫夐」榛樿寮鍚

璇锋敞鎰忓彧瑕佷綘鍙繍琛屼竴涓湇鍔″櫒锛--skip-external-locking閫夐」涓嶅奖鍝MySQL鐨勫姛鑳姐傚彧瑕佽浣忚繍琛myisamchk涔嬪墠鍏抽棴鏈嶅姟鍣(鎴栭攣瀹氬苟鍒锋柊鐩稿叧琛)銆傚湪涓浜涚郴缁熶笂璇ラ夐」鏄己鍒剁殑锛屽洜涓哄湪浠讳綍鎯呭喌涓嬪閮ㄩ攣瀹氬潎涓嶅伐浣溿

涓嶈兘浣跨敤--skip-external-locking鐨勫敮涓鎯呭喌鏄鐩稿悓鐨勬暟鎹繍琛屽涓MySQL鏈嶅姟鍣(闈炲鎴)鐨勬儏鍐碉紝鎴栬呭鏋滄病鏈変簨鍏堝憡璇夋湇鍔″櫒鍒锋柊骞堕攣瀹氫竴涓〃鍗宠繍琛myisamchk鏉ユ鏌(闈炰慨澶)璇ヨ〃銆傝娉ㄦ剰涓鑸笉寤鸿浣跨敤澶氫釜MySQL鏈嶅姟鍣ㄦ潵骞惰璁块棶鐩稿悓鐨勬暟鎹紝闄や簡浣跨敤MySQL Cluster鏃躲

鍗充娇浣跨敤--skip-external-locking锛屼粛鐒跺彲浠ヤ娇鐢LOCK TABLESUNLOCK TABLES

7.5.2. 璋冭妭鏈嶅姟鍣ㄥ弬鏁

鍙互鐢ㄨ繖涓懡浠ゅ緱鍒mysqld鏈嶅姟鍣 榛樿缂撳瓨鍖虹殑澶у皬锛

shell> mysqld --verbose --help

杩欎釜鍛戒护鐢熸垚鎵鏈mysqld閫夐」鍜屽彲閰嶇疆鍙橀噺鐨勫垪琛ㄣ傝緭鍑哄寘鎷 榛樿鍊煎苟涓旂湅涓婂幓璞¤繖鏍凤細

help                              TRUE
abort-slave-event-count           0
allow-suspicious-udfs             FALSE
auto-increment-increment          1
auto-increment-offset             1
automatic-sp-privileges           TRUE
basedir                           /home/jon/bin/mysql/
bdb                               FALSE
bind-address                      (No default value)
character-set-client-handshake    TRUE
character-set-server              latin1
character-sets-dir                /home/jon/bin/mysql/share/mysql/charsets/
chroot                            (No default value)
collation-server                  latin1_swedish_ci
completion-type                   0
concurrent-insert                 1
console                           FALSE
datadir                           /home/jon/bin/mysql/var/
default-character-set             latin1
default-collation                 latin1_swedish_ci
default-time-zone                 (No default value)
disconnect-slave-event-count      0
enable-locking                    FALSE
enable-pstack                     FALSE
engine-condition-pushdown         FALSE
external-locking                  FALSE
gdb                               FALSE
large-pages                       FALSE
init-connect                      (No default value)
init-file                         (No default value)
init-slave                        (No default value)
innodb                            TRUE
innodb_checksums                  TRUE
innodb_data_home_dir              (No default value)
innodb_doublewrite                TRUE
innodb_fast_shutdown              1
innodb_file_per_table             FALSE
innodb_flush_log_at_trx_commit    1
innodb_flush_method               (No default value)
innodb_locks_unsafe_for_binlog    FALSE
innodb_log_arch_dir               (No default value)
innodb_log_group_home_dir         (No default value)
innodb_max_dirty_pages_pct        90
innodb_max_purge_lag              0
innodb_status_file                FALSE
innodb_table_locks                TRUE
innodb_support_xa                 TRUE
isam                              FALSE
language                          /home/jon/bin/mysql/share/mysql/english
local-infile                      TRUE
log                               /home/jon/bin/mysql/var/master1.log
log-bin                           /home/jon/bin/mysql/var/master1
log-bin-index                     (No default value)
log-bin-trust-routine-creators    FALSE
log-error                         /home/jon/bin/mysql/var/master1.err
log-isam                          myisam.log
log-queries-not-using-indexes     FALSE
log-short-format                  FALSE
log-slave-updates                 FALSE
log-slow-admin-statements         FALSE
log-slow-queries                  (No default value)
log-tc                            tc.log
log-tc-size                       24576
log-update                        (No default value)
log-warnings                      1
low-priority-updates              FALSE
master-connect-retry              60
master-host                       (No default value)
master-info-file                  master.info
master-password                   (No default value)
master-port                       3306
master-retry-count                86400
master-ssl                        FALSE
master-ssl-ca                     (No default value)
master-ssl-capath                 (No default value)
master-ssl-cert                   (No default value)
master-ssl-cipher                 (No default value)
master-ssl-key                    (No default value)
master-user                       test
max-binlog-dump-events            0
memlock                           FALSE
myisam-recover                    OFF
ndbcluster                        FALSE
ndb-connectstring                 (No default value)
ndb-mgmd-host                     (No default value)
ndb-nodeid                        0
ndb-autoincrement-prefetch-sz     32
ndb-distibution                   KEYHASH
ndb-force-send                    TRUE
ndb_force_send                    TRUE
ndb-use-exact-count               TRUE
ndb_use_exact_count               TRUE
ndb-shm                           FALSE
ndb-optimized-node-selection      TRUE
ndb-cache-check-time              0
ndb-index-stat-enable             TRUE
ndb-index-stat-cache-entries      32
ndb-index-stat-update-freq        20
new                               FALSE
old-alter-table                   FALSE
old-passwords                     FALSE
old-style-user-limits             FALSE
pid-file                          /home/jon/bin/mysql/var/hostname.pid1
port                              3306
relay-log                         (No default value)
relay-log-index                   (No default value)
relay-log-info-file               relay-log.info
replicate-same-server-id          FALSE
report-host                       (No default value)
report-password                   (No default value)
report-port                       3306
report-user                       (No default value)
rpl-recovery-rank                 0
safe-user-create                  FALSE
secure-auth                       FALSE
server-id                         1
show-slave-auth-info              FALSE
skip-grant-tables                 FALSE
skip-slave-start                  FALSE
slave-load-tmpdir                 /tmp/
socket                            /tmp/mysql.sock
sporadic-binlog-dump-fail         FALSE
sql-mode                          OFF
symbolic-links                    TRUE
tc-heuristic-recover              (No default value)
temp-pool                         TRUE
timed_mutexes                     FALSE
tmpdir                            (No default value)
use-symbolic-links                TRUE
verbose                           TRUE
warnings                          1
back_log                          50
binlog_cache_size                 32768
bulk_insert_buffer_size           8388608
connect_timeout                   5
date_format                       (No default value)
datetime_format                   (No default value)
default_week_format               0
delayed_insert_limit              100
delayed_insert_timeout            300
delayed_queue_size                1000
expire_logs_days                  0
flush_time                        0
ft_max_word_len                   84
ft_min_word_len                   4
ft_query_expansion_limit          20
ft_stopword_file                  (No default value)
group_concat_max_len              1024
innodb_additional_mem_pool_size   1048576
innodb_autoextend_increment       8
innodb_buffer_pool_awe_mem_mb     0
innodb_buffer_pool_size           8388608
innodb_concurrency_tickets        500
innodb_file_io_threads            4
innodb_force_recovery             0
innodb_lock_wait_timeout          50
innodb_log_buffer_size            1048576
innodb_log_file_size              5242880
innodb_log_files_in_group         2
innodb_mirrored_log_groups        1
innodb_open_files                 300
innodb_sync_spin_loops            20
innodb_thread_concurrency         20
innodb_commit_concurrency         0
innodb_thread_sleep_delay         10000
interactive_timeout               28800
join_buffer_size                  131072
key_buffer_size                   8388600
key_cache_age_threshold           300
key_cache_block_size              1024
key_cache_division_limit          100
long_query_time                   10
lower_case_table_names            0
max_allowed_packet                1048576
max_binlog_cache_size             4294967295
max_binlog_size                   1073741824
max_connect_errors                10
max_connections                   100
max_delayed_threads               20
max_error_count                   64
max_heap_table_size               16777216
max_join_size                     4294967295
max_length_for_sort_data          1024
max_relay_log_size                0
max_seeks_for_key                 4294967295
max_sort_length                   1024
max_tmp_tables                    32
max_user_connections              0
max_write_lock_count              4294967295
multi_range_count                 256
myisam_block_size                 1024
myisam_data_pointer_size          6
myisam_max_extra_sort_file_size   2147483648
myisam_max_sort_file_size         2147483647
myisam_repair_threads             1
myisam_sort_buffer_size           8388608
myisam_stats_method               nulls_unequal
net_buffer_length                 16384
net_read_timeout                  30
net_retry_count                   10
net_write_timeout                 60
open_files_limit                  0
optimizer_prune_level             1
optimizer_search_depth            62
preload_buffer_size               32768
query_alloc_block_size            8192
query_cache_limit                 1048576
query_cache_min_res_unit          4096
query_cache_size                  0
query_cache_type                  1
query_cache_wlock_invalidate      FALSE
query_prealloc_size               8192
range_alloc_block_size            2048
read_buffer_size                  131072
read_only                         FALSE
read_rnd_buffer_size              262144
div_precision_increment           4
record_buffer                     131072
relay_log_purge                   TRUE
relay_log_space_limit             0
slave_compressed_protocol         FALSE
slave_net_timeout                 3600
slave_transaction_retries         10
slow_launch_time                  2
sort_buffer_size                  2097144
sync-binlog                       0
sync-frm                          TRUE
sync-replication                  0
sync-replication-slave-id         0
sync-replication-timeout          10
table_cache                       64
table_lock_wait_timeout           50
thread_cache_size                 0
thread_concurrency                10
thread_stack                      196608
time_format                       (No default value)
tmp_table_size                    33554432
transaction_alloc_block_size      8192
transaction_prealloc_size         4096
updatable_views_with_limit        1
wait_timeout                      28800

濡傛灉鏈変竴涓mysqld鏈嶅姟鍣ㄦ鍦ㄨ繍琛岋紝閫氳繃杩炴帴瀹冨苟鎵ц杩欎釜鍛戒护锛屽彲浠ョ湅鍒板疄闄呬笂浣跨敤鐨勫彉閲忕殑鍊硷細

mysql> SHOW VARIABLES;

杩樺彲浠ラ氳繃涓嬮潰鐨勮鍙ョ湅鍒拌繍琛屾湇鍔″櫒鐨勭粺璁″拰鐘舵佹寚鏍囷細

mysql>SHOW STATUS

浣跨敤mysqladmin杩樺彲浠ヨ幏寰楃郴缁熷彉閲忓拰鐘舵佷俊鎭細

shell> mysqladmin variables
shell> mysqladmin extended-status

鍏充簬鎵鏈夌郴缁熷拰鐘舵佸彉閲忕殑瀹屽叏鎻忚堪鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥5.3.4鑺傦紝鈥滄湇鍔″櫒鐘舵佸彉閲忊

MySQL浣跨敤瀹屽叏鍙互鍗囩骇鐨勭畻娉曪紝鍥犳閫氬父杩愯鏃跺彲浠ョ敤寰堝皯鐨勫唴瀛樸傜劧鑰岋紝閫氬父鎯呭喌鑻ョ粰MySQL鏇村鐨勫唴瀛樻ц兘浼氭洿濂姐

褰撹皟鑺MySQL鏈嶅姟鍣ㄦ椂锛岃閰嶇疆鐨勪袱涓渶閲嶈鐨勫彉閲忔槸key_buffer_sizetable_cache銆傚湪璇曞浘鏇存敼鍏跺畠鍙橀噺鍓嶄綘搴斿厛纭俊宸茬粡閫傚綋鍦伴厤缃簡杩欎簺鍙橀噺銆

涓嬮潰鐨勪緥瀛愭樉绀轰簡閮ㄥ垎鍏稿瀷鐨勪笉鍚岀殑杩愯鏃堕厤缃殑鍙橀噺鍊笺

         濡傛灉鑷冲皯鏈256MB鍐呭瓨鍜岃澶氳〃锛屾兂瑕佸湪涓瓑鏁伴噺鐨勫鎴锋椂鑾峰緱鏈澶фц兘锛屽簲浣跨敤锛

                shell> mysqld_safe --key_buffer_size=64M --table_cache=256 \
                           --sort_buffer_size=4M --read_buffer_size=1M &

         濡傛灉鍙湁128MB鍐呭瓨鍜屽皯閲忚〃锛屼絾浠嶇劧瑕佽繘琛屽ぇ閲忕殑鎺掑簭锛屽彲浠ヤ娇鐢細

                shell> mysqld_safe --key_buffer_size=16M --sort_buffer_size=1M

濡傛灉鏈夎澶氬苟琛岃繛鎺ワ紝浜ゆ崲闂浼氬彂鐢燂紝闄ら潪mysqld宸茬粡閰嶇疆鎴愪负姣忎釜杩炴帴鍒嗛厤寰堝皯鐨勫唴瀛樸傚鏋滄湁瓒冲鐨勫唴瀛樼敤浜庢墍鏈夎繛鎺ワ紝mysqld浼氭墽琛屽緱鏇村ソ銆

         瀵逛簬灏戦噺鍐呭瓨鍜屽ぇ閲忚繛鎺ワ紝搴斾娇鐢細

                shell> mysqld_safe --key_buffer_size=512K --sort_buffer_size=100K \
                           --read_buffer_size=100K &

鎴栫敋鑷充负锛

shell> mysqld_safe --key_buffer_size=512K --sort_buffer_size=16K \
           --table_cache=32 --read_buffer_size=8K \
           --net_buffer_length=1K &

濡傛灉姝e杩滆繙澶т簬鍙敤鍐呭瓨鐨勮〃鎵цGROUP BYORDER BY鎿嶄綔锛屽簲澧炲姞read_rnd_buffer_size鐨勫间互鍔犻熸帓搴忔搷浣滃悗闈㈢殑琛岃鍙栥

濡傛灉宸茬粡瀹夎浜MySQLsupport-files鐩綍鍖呭惈涓浜涗笉鍚岀殑my.cnf绀轰緥鏂囦欢锛my-huge.cnfmy-.cnfmy-medium.cnfmy-small.cnf銆傚彲浠ヤ娇鐢ㄨ繖浜涙枃浠舵潵浼樺寲绯荤粺銆

璇锋敞鎰忓鏋滃湪鍛戒护琛屼腑涓mysqldmysqld_safe鎸囧畾涓涓夐」锛屽畠鍙湪璇ユ鏈嶅姟鍣ㄨ皟鐢ㄤ腑淇濇寔鏈夋晥銆傝鎯虫瘡娆℃湇鍔″櫒杩愯鏃朵娇鐢ㄨ閫夐」锛屽皢瀹冩斁鍦ㄩ夐」鏂囦欢涓

瑕佹兂鐪嬪弬鏁版洿鏀圭殑鏁堟灉锛屽簲鎵ц锛

shell> mysqld --key_buffer_size=32M --verbose ---help

鍙橀噺鍊煎垪浜庤緭鍑虹殑鏈鍚庛傜‘淇--verbose---help閫夐」鍦ㄦ渶鍚庛傚惁鍒欙紝鍦ㄥ懡浠よ涓垪浜庡畠浠悗闈㈢殑閫夐」鐨勬晥鏋滀笉浼氬弽鏄犲埌杈撳嚭涓

鍏充簬璋冭妭InnoDB瀛樺偍寮曟搸鐨勪俊鎭紝鍙傝15.2.11鑺傦紝鈥淚nnoDB鎬ц兘璋冭妭鎻愮ず鈥

7.5.3. 鎺у埗鏌ヨ浼樺寲鍣ㄧ殑鎬ц兘

鏌ヨ浼樺寲鍣ㄧ殑浠诲姟鏄彂鐜版墽琛SQL鏌ヨ鐨勬渶浣虫柟妗堛傚洜涓衡鈥濇柟妗堝拰鈥鈥濇柟妗堜箣闂寸殑鎬ц兘宸埆浼氬法澶(涔熷氨鏄锛岀鐩稿浜庡皬鏃舵垨鐢氳嚦澶)锛屽ぇ澶氭暟鏌ヨ浼樺寲鍣紝鍖呮嫭MySQL鐨勬煡璇紭鍖栧櫒锛屾绘垨澶氭垨灏戝湴鍦ㄦ墍鏈夊彲鑳界殑鏌ヨ璇勪及鏂规涓悳绱㈡渶浣虫柟妗堛傚浜庤仈鎺ユ煡璇紝MySQL浼樺寲鍣ㄦ墍璋冩煡鐨勫彲鑳界殑鏂规鏁伴殢鏌ヨ涓墍寮曠敤鐨勮〃鐨勬暟鐩憟鎸囨暟澧為暱銆傚浜庡皬鏁伴噺鐨勮〃(鍏稿瀷灏忎簬7-10)锛岃繖涓嶆槸涓涓棶棰樸傜劧鑰岋紝褰撴彁浜ょ殑鏌ヨ鏇村ぇ鏃讹紝鏌ヨ浼樺寲鎵鑺辩殑鏃堕棿浼氬緢瀹规槗鍦版垚涓烘湇鍔″櫒鎬ц兘鐨勪富瑕佺摱棰堛

鏌ヨ浼樺寲鐨勪竴涓洿鍔犵伒娲荤殑鏂规硶鏄厑璁哥敤鎴锋帶鍒朵紭鍖栧櫒璇﹀敖鍦版悳绱㈡渶浣虫煡璇㈣瘎浼版柟妗堛備竴鑸濇兂鏄紭鍖栧櫒璋冩煡鐨勬柟妗堣秺灏戯紝瀹冪紪璇戜竴涓煡璇㈡墍鑺辫垂鐨勬椂闂磋秺灏戙傚彟涓鏂归潰锛屽洜涓轰紭鍖栧櫒璺宠繃浜嗕竴浜涙柟妗堬紝瀹冨彲鑳介敊杩囦竴涓渶浣虫柟妗堛

浼樺寲鍣ㄥ叧浜庢柟妗堟暟閲忚瘎浼扮殑琛屼负鍙互閫氳繃涓や釜绯荤粺鍙橀噺鏉ユ帶鍒讹細

         optimizer_prune_level鍙橀噺鍛婅瘔浼樺寲鍣ㄦ牴鎹姣忎釜琛ㄨ闂殑琛屾暟鐨勪及璁¤烦杩囨煇浜涙柟妗堛傛垜浠殑璇曢獙鏄剧ず璇ョ被鈥鏈夋牴鎹殑鐚滄祴鈥濆緢灏戦敊杩囨渶浣虫柟妗堬紝骞朵笖鍙互澶уぇ闄嶄綆鏌ヨ缂栬緫娆℃暟銆傝繖灏辨槸涓轰粈涔堥粯璁ゆ儏鍐佃閫夐」涓on(optimizer_prune_level=1)銆傜劧鑰岋紝濡傛灉浣犺涓轰紭鍖栧櫒閿欒繃浜嗕竴涓洿濂界殑鏌ヨ鏂规锛屽垯璇ラ夐」鍙互鍏抽棴(optimizer_prune_level=0)锛岄闄╂槸鏌ヨ缂栬緫鑺辫垂鐨勬椂闂存洿闀裤傝娉ㄦ剰鍗充娇浣跨敤璇ュ惎鍙戯紝浼樺寲鍣ㄤ粛鐒跺彲浠ユ帰娴嬪憟鎸囨暟鏁扮洰鐨勬柟妗堛

         optimizer_search_depth鍙橀噺鍛婅瘔浼樺寲鍣ㄥ浜庢瘡涓湭瀹屾垚鐨勨滄湭鏉ョ殑鈥濇柟妗堬紝搴旀煡鐪嬪娣憋紝浠ヨ瘎浼版槸鍚﹀簲瀵瑰畠杩涗竴姝ユ墿澶сoptimizer_search_depth鍊艰緝灏忎細浣挎煡璇㈢紪杈戞鏁板ぇ澶у噺灏忋備緥濡傦紝濡傛灉optimizer_search_depth鎺ヨ繎浜庢煡璇腑琛ㄧ殑鏁伴噺锛屽1213鎴栨洿澶氳〃鐨勬煡璇㈠緢鍙兘闇瑕佸嚑灏忔椂鐢氳嚦鍑犲ぉ鐨勬椂闂存潵缂栬瘧銆傚悓鏃讹紝濡傛灉鐢optimizer_search_depth绛変簬34缂栬緫锛屽浜庡悓涓涓煡璇紝缂栬瘧鍣ㄧ紪璇戞椂闂村彲浠ュ皯浜1鍒嗛挓銆傚鏋滀笉鑳界‘瀹氬悎鐞嗙殑optimizer_search_depth鍊硷紝璇ュ彉閲忓彲浠ヨ缃负0锛屽憡璇変紭鍖栧櫒鑷姩纭畾璇ュ笺

7.5.4. 缂栬瘧鍜岄摼鎺ユ庢牱褰卞搷MySQL鐨勯熷害

涓嬪垪澶у鏁版祴璇曟槸鍦Linux涓婂苟鐢MySQL鍩哄噯杩涜鐨勶紝浣嗘槸瀹冧滑鑳藉鍏跺畠鎿嶄綔绯荤粺鍜屽伐浣滆礋杞界粰鍑轰竴浜涙寚绀恒

褰撲綘鐢-static閾炬帴鏃讹紝鍙互寰楀埌鏈蹇殑鍙墽琛屾枃浠躲

Linux涓婏紝鏈濂界敤pgcc-O3缂栬瘧鏈嶅姟鍣ㄣ備负浜嗙敤杩欎簺閫夐」缂栬瘧鈥渟ql_yacc.cc鈥锛岄渶瑕佸ぇ绾200M鍐呭瓨锛屽洜涓gccpgcc闇瑕佸ぇ閲忕殑鍐呭瓨浣挎墍鏈夊嚱鏁板祵鍏ワ紙inline锛夈傚湪閰嶇疆MySQL鏃讹紝涔熷簲璇ヨ瀹CXX=gcc浠ラ伩鍏嶅寘鎷libstdc++(瀹冧笉闇瑕)銆傝娉ㄦ剰瀵逛簬鏌愪簺鐗堟湰鐨pgcc锛岀敓鎴愮殑浜岃繘鍒跺彧鑳借繍琛屽湪鐪Pentium澶勭悊鍣ㄤ笂锛屽嵆浣夸綘浣跨敤缂栬瘧鍣ㄩ夐」璇存槑浣犳兂璁╂渶缁堢殑浠g爜鍦ㄦ墍鏈x586-绫诲鐞嗗櫒涓婂伐浣(渚嬪AMD)

鍙氳繃浣跨敤涓涓緝濂界殑缂栬瘧鍣ㄦ垨杈冨ソ鐨勭紪璇戝櫒閫夐」锛屽湪搴旂敤涓兘寰楀埌10-30%鐨勫姞閫熴傚鏋滀綘鑷繁缂栬瘧SQL鏈嶅姟鍣紝杩欑壒鍒噸瑕侊紒

褰撴垜浠祴璇Cygnus CodeFusionFujitsu缂栬瘧鍣ㄦ椂锛屼簩鑰呭潎杩樻病瓒冲涓嶅嚭閿欐潵璁MySQL鍚敤浼樺寲杩涜缂栬瘧銆

鏍囧噯MySQL浜岃繘鍒跺垎鍙戠紪璇戜负鏀寔鎵鏈夊瓧绗﹂泦銆傚綋浣犺嚜宸辩紪璇MySQL鏃讹紝搴斿彧鍖呮嫭灏嗕娇鐢ㄧ殑瀛楃闆嗙殑鏀寔銆傞氳繃configure--with-charset閫夐」鏉ユ帶鍒躲

杩欓噷鏄垜浠仛杩囩殑涓浜涙祴閲忚〃锛

         濡傛灉浣犱娇鐢pgcc骞剁敤-O6缂栬瘧锛mysqld鏈嶅姟鍣ㄦ瘮鐢gcc 2.95.211%

         濡傛灉浣犲姩鎬佸湴閾炬帴(娌℃湁-static)锛屽湪Linux涓粨鏋滄參浜13%銆傛敞鎰忎綘浠嶈兘鍦ㄥ鎴峰簲鐢ㄧ▼搴忎腑浣跨敤鍔ㄦ侀摼鎺MySQL搴撱傚彧鏈夋湇鍔″櫒瀵规ц兘鏄叧閿殑銆

         濡傛灉浣犵敤strip mysqld鍓ョmysqld浜岃繘鍒讹紝鐢熸垚鐨勪簩杩涘埗鍙互蹇4%

         瀵逛簬鍦ㄥ悓涓涓绘満涓婅繍琛岀殑瀹㈡埛涓庢湇鍔″櫒涔嬮棿鐨勮繛鎺ワ紝濡傛灉浣犱娇鐢TCP/IP鑰岄潪Unix濂楁帴瀛楁枃浠惰繘琛岃繛鎺ワ紝缁撴灉鎱7.5%(Unix涓紝濡傛灉浣犺繛鎺localhost涓绘満锛MySQL榛樿浣跨敤涓涓鎺ュ瓧鏂囦欢锛

         瀵逛簬浠庡鎴峰埌鏈嶅姟鍣ㄧ殑TCP/IP杩炴帴锛屼粠鍙︿竴鍙颁富鏈鸿繛鎺ヤ竴鍙拌繙绋嬫湇鍔″櫒瑕佹瘮杩炴帴鍚屼竴涓绘満涓婄殑鏈嶅姟鍣ㄦ參8-11%锛屽嵆浣块氳繃100Mb/s浠ュお缃戣繘琛岃繛鎺ャ

         褰撲娇鐢ㄥ畨鍏ㄨ繛鎺ヨ繍琛屾垜浠殑鍩哄噯娴嬭瘯鏃(鎵鏈夋暟鎹敤鍐呴儴SSL鏀寔杩涜鍔犲瘑)锛屾ц兘姣旀湭鍔犲瘑杩炴帴鎱55%

         濡傛灉浣犵敤--with-debug=full缂栬瘧锛屽ぇ澶氭暟鏌ヨ鎱20%銆傞儴鍒嗘煡璇㈡椂闂翠細寰堥暱锛涗緥濡傦紝MySQL鍩哄噯鐨勮繍琛岃鎱35%銆傚鏋滀綘浣跨敤--with-debug(娌℃湁=full)锛岄熷害鍙笅闄15%銆傚浜庣敤--with-debug=full缂栬瘧鐨mysqld鐗堟湰锛屽彲浠ョ敤--skip-safemalloc閫夐」鍚姩浠ヤ究鍦ㄨ繍琛屾椂绂佺敤鍐呭瓨妫鏌ャ傛墽琛岄熷害鍒欐帴杩戠敤--with-debug閰嶇疆鐨勬椂鍊欍

         Sun UltraSPARC-Iie涓婏紝鐢Forte 5.0缂栬瘧鐨勬湇鍔″櫒姣旂敤gcc 3.2缂栬瘧鐨勮蹇4%

         Sun UltraSPARC-Iie涓婏紝鐢Forte 5.0缂栬瘧鐨32浣嶆ā寮忔湇鍔″櫒姣64浣嶆ā寮忔湇鍔″櫒瑕佸揩4%

         gcc 2.95.2缂栬瘧甯-mcpu=v8 -WaUltraSPARC锛屼娇鐢-xarch=v8plusa閫夐」鎬ц兘浼氭彁楂4%

         Solaris 2.5.1涓婏紝鍦ㄥ崟涓鐞嗗櫒涓MIT-pthreads姣斿甫鍘熺敓绾跨▼鐨Solaris8-12%銆傚鏋滄湁鏇村ぇ鐨勮礋杞/cpus锛屽樊鍒簲璇ユ洿澶с

         Linux-x86涓婁娇鐢gcc缂栬瘧鑰屼笉鐢ㄥ抚鎸囬拡(-fomit-frame-pointer or -fomit-frame-pointer -ffixed-ebp)锛屽彲浠ヤ娇mysqld1-4%

MySQL AB鎻愪緵鐨Linux涓婄殑浜岃繘鍒MySQL鍒嗗彂涓鑸敤pgcc缂栬瘧銆傛垜浠繀椤昏繑鍥炲埌甯歌gcc锛屽洜涓pgcc涓湁涓涓bug锛屼娇鐢熸垚鐨勪簩杩涘埗涓嶈兘鍦AMD涓婅繍琛屻傛垜浠皢缁х画浣跨敤gcc鐩村埌璇bug琚В鍐炽傚悓鏃讹紝濡傛灉浣犳湁涓涓潪AMD鏈猴紝浣犲彲浠ョ敤pgcc缂栬瘧鏋勫缓涓涓洿蹇殑浜岃繘鍒躲傛爣鍑MySQL Linux浜岃繘鍒舵槸閫氳繃闈欐侀摼鎺ワ紝浠ヤ娇瀹冩洿蹇苟涓旀洿鍔犳槗浜庣Щ妞嶃

7.5.5. MySQL濡備綍浣跨敤鍐呭瓨

涓嬮潰鐨勫垪琛ㄤ腑鍒楀嚭浜mysqld鏈嶅姟鍣ㄤ娇鐢ㄥ唴瀛樼殑涓浜涙柟娉曘傚湪閫傜敤鐨勫湴鏂癸紝缁欏嚭浜嗗唴瀛樼浉鍏崇殑绯荤粺鍙橀噺鍚嶏細

         閿紦瀛(鍙橀噺key_buffer_size)琚墍鏈夌嚎绋嬪叡浜紱鏈嶅姟鍣ㄤ娇鐢ㄧ殑鍏跺畠缂撳瓨鍒欐牴鎹渶瑕佸垎閰嶃傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥

         姣忎釜杩炴帴浣跨敤鍏蜂綋绾跨▼鐨勭┖闂达細

o        鍫嗘爤(榛樿64KB锛屽彉閲thread_stack)

o        杩炴帴缂撳瓨鍖(鍙橀噺net_buffer_length)

o        缁撴灉缂撳瓨鍖(鍙橀噺net_buffer_length)

杩炴帴缂撳瓨鍖哄拰缁撴灉缂撳瓨鍖哄彲浠ユ牴鎹渶瑕佸姩鎬佹墿鍏呭埌max_allowed_packet銆傚綋鏌愪釜鏌ヨ杩愯鏃讹紝涔熶负褰撳墠鏌ヨ瀛楃涓插垎閰嶅唴瀛樸

         鎵鏈夌嚎绋嬪叡浜浉鍚岀殑鍩烘湰鍐呭瓨銆

         鍙湁鍘嬬缉MyISAM琛ㄦ槧灏勫埌鍐呭瓨銆傝繖鏄洜涓4GB32浣嶅唴瀛樼┖闂翠笉瓒充互瀹圭撼澶у鏁板ぇ琛ㄣ傚綋64浣嶅湴鍧绌洪棿鐨勭郴缁熷彉寰楄秺鏉ヨ秺鏅亶鍚庯紝鎴戜滑鍙互澧炲姞甯歌鐨勫唴瀛樻槧灏勬敮鎸併

         瀵硅〃杩涜椤哄簭鎵弿鐨勮姹傚皢鍒嗛厤涓涓缂撳瓨鍖(鍙橀噺read_buffer_size)

         褰撴寜浠绘剰椤哄簭璇诲彇琛屾椂(渚嬪锛屾寜鐓ф帓搴忛『搴)锛屽皢鍒嗛厤涓涓殢鏈鸿 缂撳瓨鍖(鍙橀噺read_rnd_buffer_size)浠ラ伩鍏嶇‖鐩樻悳绱€

         鎵鏈夎仈鍚堝湪涓涓护鐗屽唴瀹屾垚锛屽苟涓斿ぇ澶氭暟鑱斿悎鐢氳嚦鍙互涓嶇敤涓存椂琛ㄥ嵆鍙互瀹屾垚銆傚ぇ澶氭暟涓存椂琛ㄦ槸鍩轰簬鍐呭瓨鐨(HEAP)琛ㄣ傚叿鏈夊ぇ鐨勮褰曢暱搴︾殑涓存椂琛 (鎵鏈夊垪鐨勯暱搴︾殑鍜)鎴栧寘鍚BLOB鍒楃殑琛ㄥ瓨鍌ㄥ湪纭洏涓娿

濡傛灉鏌愪釜鍐呴儴heap锛堝爢绉級琛ㄥぇ灏忚秴杩tmp_table_sizeMySQL鍙互鏍规嵁闇瑕佽嚜鍔ㄥ皢鍐呭瓨涓殑heap琛ㄦ敼涓哄熀浜庣‖鐩樼殑MyISAM琛ㄣ傝繕鍙互閫氳繃璁剧疆mysqldtmp_table_size閫夐」鏉ュ鍔犱复鏃惰〃鐨勫ぇ灏忥紝鎴栬缃鎴风▼搴忎腑鐨SQL閫夐」SQL_BIG_TABLE銆傚弬瑙13.5.3鑺傦紝鈥淪ET璇硶鈥

         杩涜鎺掑簭鐨勫ぇ澶氭暟璇锋眰灏嗗垎閰嶄竴涓帓搴忕紦瀛樺尯锛屽苟鏍规嵁缁撴灉闆嗙殑澶у皬涓轰袱涓复鏃舵枃浠跺垎閰嶉浂銆傚弬瑙A.4.4鑺傦紝鈥淢ySQL灏嗕复鏃舵枃浠跺偍瀛樺湪鍝噷鈥

         鍑犱箮鎵鏈夎В鏋愬拰璁$畻鍦ㄥ眬閮ㄥ唴瀛樹腑瀹屾垚銆傚皬椤圭洰涓嶉渶瑕佸唴瀛橈紝鍥犳閬垮厤浜嗘櫘閫氱殑鎱㈠唴瀛樺垎閰嶅拰閲婃斁銆傚彧涓轰笉鏈熸湜鐨勫ぇ瀛楃涓插垎閰嶅唴瀛橈紱浣跨敤鍑芥暟malloc()free()鏉ュ畬鎴愩

         瀵逛簬姣忎釜鎵撳紑鐨MyISAM琛紝绱㈠紩鏂囦欢鎵撳紑涓娆★紱鏁版嵁鏂囦欢涓烘瘡涓苟琛岃繍琛岀殑绾跨▼鎵撳紑涓娆°傚浜庢瘡涓苟琛岀嚎绋嬶紝灏嗗垎閰嶄竴涓〃缁撴瀯銆佷竴涓瘡涓垪鐨勫垪缁撴瀯鍜屽ぇ灏忎负3 * N鐨勭紦瀛樺尯(鍏朵腑N鏄渶澶ц鐨勯暱搴︼紝鑰屼笉鏄绠BLOB)銆備竴涓BLOB鍒楅渶瑕58涓瓧鑺傚姞涓BLOB鏁版嵁鐨勯暱搴︺MyISAM 瀛樺偍寮曟搸缁存姢涓涓澶栫殑琛岀紦瀛樺尯渚涘唴閮ㄥ簲鐢ㄣ

         瀵逛簬姣忎釜鍏锋湁BLOB鍒楃殑琛紝灏嗗缂撳瓨鍖鸿繘琛屽姩鎬佹墿澶т互璇诲叆澶х殑BLOB 鍊笺傚鏋滀綘鎵弿涓涓〃锛屽垯鍒嗛厤涓涓笌鏈澶х殑BLOB鍊间竴鏍峰ぇ鐨勭紦瀛樺尯銆

         鎵鏈変娇鐢ㄧ殑琛ㄧ殑鍙ユ焺缁撴瀯淇濆瓨鍦ㄩ珮閫熺紦瀛樹腑骞朵互FIFO绠$悊銆傞粯璁ゆ儏鍐碉紝楂橀熺紦瀛樻湁64涓叆鍙c傚鏋滄煇涓〃鍚屾椂琚袱涓繍琛岀殑绾跨▼浣跨敤锛岄珮閫熺紦瀛樺垯涓鸿鎻愪緵涓や釜鍏ュ彛銆傚弬瑙7.4.9鑺傦紝鈥淢ySQL濡備綍鎵撳紑鍜屽叧闂〃鈥

         褰撳苟琛屾墽琛岀殑绾跨▼缁撴潫鏃讹紝FLUSH TABLE璇彞鎴mysqladmin flush-table鍛戒护鍙互绔嬪嵆鍏抽棴鎵鏈変笉浣跨敤鐨勮〃骞跺皢鎵鏈変娇鐢ㄤ腑鐨勮〃鏍囪涓哄凡缁忓叧闂傝繖鏍峰彲浠ユ湁鏁堥噴鏀惧ぇ澶氭暟浣跨敤涓殑鍐呭瓨銆FLUSH TABLE鍦ㄥ叧闂墍鏈夎〃涔嬪墠涓嶈繑鍥炵粨鏋溿

ps鍜屽叾瀹冪郴缁熺姸鎬佺▼搴忓彲浠ユ姤瀵mysqld浣跨敤寰堝鍐呭瓨銆傝繖鍙互鏄湪涓嶅悓鐨勫唴瀛樺湴鍧涓婄殑绾跨▼鏍堥犳垚鐨勩備緥濡傦紝Solaris鐗堟湰鐨ps灏嗘爤闂存湭鐢ㄧ殑鍐呭瓨绠椾綔宸茬敤鐨勫唴瀛樸備綘鍙互閫氳繃鐢swap -s妫鏌ュ彲鐢ㄤ氦鎹㈠尯鏉ラ獙璇佸畠銆傛垜浠敤鍟嗕笟鍐呭瓨婕忔礊鎺㈡煡鍣ㄦ祴璇曚簡mysqld锛屽洜姝ゅ簲璇ユ湁娌℃湁鍐呭瓨婕忔礊銆

7.5.6. MySQL濡備綍浣跨敤DNS

褰撴柊鐨勫鎴疯繛鎺mysqld鏃讹紝mysqld鍒涘缓涓涓柊鐨勭嚎绋嬫潵澶勭悊璇锋眰銆傝绾跨▼鍏堟鏌ユ槸鍚︿富鏈哄悕鍦ㄤ富鏈哄悕缂撳瓨涓傚鏋滀笉鍦紝绾跨▼璇曞浘瑙f瀽涓绘満鍚嶏細

         濡傛灉鎿嶄綔绯荤粺鏀寔绾跨▼瀹夊叏gethostbyaddr_r ()gethostbyname_r()璋冪敤锛岀嚎绋嬩娇鐢ㄥ畠浠潵鎵ц涓绘満鍚嶈В鏋愩

         濡傛灉鎿嶄綔绯荤粺涓嶆敮鎸佺嚎绋嬪畨鍏ㄨ皟鐢紝绾跨▼閿佸畾涓涓簰鏂ヤ綋骞惰皟鐢gethostbyaddr()gethostbyname()銆傚湪杩欑鎯呭喌涓嬶紝鍦ㄧ1涓嚎绋嬭В閿佷簰鏂ヤ綋鍓嶏紝娌℃湁鍏跺畠绾跨▼鍙互瑙f瀽涓嶅湪涓绘満鍚嶇紦瀛樹腑鐨勪富鏈哄悕銆

浣犲彲浠ョ敤--skip-name-resolve閫夐」鍚姩mysqld鏉ョ鐢DNS涓绘満鍚嶆煡鎵俱傜劧鑰岋紝鍦ㄨ繖绉嶆儏鍐典笅锛屼綘鍙彲浠ヤ娇鐢MySQL涓殑鎺堟潈琛ㄤ腑鐨IP鍙枫

濡傛灉浣犳湁涓涓緢鎱㈢殑DNS鍜岃澶氫富鏈猴紝浣犲彲浠ラ氳繃鐢--skip-name-resolve绂佺敤DNS鏌ユ壘鎴栧鍔HOST_CACHE_SIZE瀹氫箟(榛樿鍊硷細128)骞堕噸鏂扮紪璇mysqld鏉ユ彁楂樻ц兘銆

浣犲彲浠ョ敤--skip-host-cache閫夐」鍚姩鏈嶅姟鍣ㄦ潵绂佺敤涓绘満鍚嶇紦瀛樸傝鎯虫竻闄や富鏈哄悕缂撳瓨锛屾墽琛FLUSH HOSTS璇彞鎴栨墽琛mysqladmin flush-hosts鍛戒护銆

濡傛灉浣犳兂瑕佸畬鍏ㄧ姝TCP/IP杩炴帴锛岀敤--skip-networking閫夐」鍚姩mysqld

7.6. 纾佺洏浜嬪疁

7.6.1. 浣跨敤绗﹀彿閾炬帴

         纾佺洏鎼滅储鏄法澶х殑鎬ц兘鐡堕銆傚綋鏁版嵁閲忓彉寰楅潪甯稿ぇ浠ヨ嚧浜庣紦瀛樻ц兘鍙樺緱涓嶅彲鑳芥湁鏁堟椂锛岃闂鍙樺緱鏇村姞鏄庢樉銆傚浜庡ぇ鏁版嵁搴擄紝鍏朵腑浣犳垨澶氭垨灏戝湴闅忔満璁块棶鏁版嵁锛屼綘鍙互纭俊瀵硅鍙栨搷浣滈渶瑕佽嚦灏戜竴娆$‖鐩樻悳绱紝鍐欐搷浣滈渶瑕佸娆$‖鐩樻悳绱€傝鎯充娇璇ラ棶棰樻渶灏忓寲锛屽簲浣跨敤鎼滅储娆℃暟杈冨皯鐨勭鐩樸

         閫氳繃閾炬帴鏂囦欢鍒颁笉鍚岀殑纾佺洏鎴栧纭洏鍒嗘鏉ュ鍔犲彲鐢ㄧ鐩橀敪鏁伴噺锛堝洜姝ら檷浣庢悳绱㈡垚鏈級锛

o        浣跨敤绗﹀彿閾炬帴

杩欒鏄庯紝瀵逛簬MyISAM琛紝浣犵鍙烽摼鎺ョ储寮曟枃浠跺拰/鎴栨暟鎹枃浠讹紝浠庡畠浠暟鎹洰褰曞唴鐨勯氬父浣嶇疆鍒板彟涓涓‖鐩橈紙涔熷彲浠ヨ鏉$汗鍖栵級銆傝繖灏嗕娇鎼滅储鍜岃娆℃暟杈惧埌鏈濂斤紝鍋囧畾纭洏涓嶅啀涓哄叾瀹冪洰鐨勪娇鐢ㄣ傚弬瑙7.6.1鑺傦紝鈥滀娇鐢ㄧ鍙烽摼鎺モ

o        鍒嗘潯

鍒嗘潯鎰忓懗鐫浣犳湁璁稿纾佺洏锛屽皢绗1涓潡鏀惧埌绗1涓‖鐩橈紝绗2涓潡鏀惧埌绗2涓鐩橈紝骞朵笖绗N鍧楀湪锛N mod number_of_disks锛夌鐩樹笂绛夌瓑銆傝繖鎰忓懗鐫濡傛灉姝e父鏁版嵁澶у皬灏忎簬鍒嗘潯澶у皬锛堟垨瀹屽叏鍖归厤锛夛紝鑳藉寰楀埌鏈浣虫ц兘銆傚垎鏉″畬鍏ㄥ彇鍐充簬鎿嶄綔绯荤粺鍜屽垎鏉″ぇ灏忥紝鍥犳鐢ㄤ笉鍚岀殑鏉$汗澶у皬瀵瑰簲鐢ㄧ▼搴忚繘琛屽熀鍑嗘祴璇曘傚弬瑙7.1.5鑺傦紝鈥滀娇鐢ㄨ嚜宸辩殑鍩哄噯鈥

鍒嗘潯鐨勪笉鍚岄熷害瀹屽叏渚濊禆浜庡弬鏁般備緷璧栦簬鎬庢牱璁剧疆鏉$汗鍙傛暟鍜岀‖鐩樻暟閲忥紝鍙互鏍规嵁涓嶅悓鏁伴噺绾у埆寰楀埌涓嶅悓鐨勬爣鍑嗐備綘蹇呴』杩涜閫夋嫨浠ヤ究浼樺寲闅忔満鎴栭『搴忓瓨鍙栥

         涓轰簡楂樺彲闈犳т綘鍙兘鎯充娇鐢RAID 0+1锛堟潯绾瑰姞闀滃儚锛夛紝浣嗗湪杩欑鎯呭喌涓嬶紝闇瑕2*N鍧楃鐩樻潵淇濇寔N涓鐩樼殑鏁版嵁銆傚鏋滀綘鑲负瀹冭姳閽憋紝杩欏彲鑳芥槸鏈濂界殑閫夐」銆傜劧鑰岋紝浣犲彲鑳借繕蹇呴』鎶曡祫涓閮ㄥ垎璧勯噾鍒板嵎绠$悊杞欢涓互渚挎湁鏁堝湴绠$悊瀹冦

         涓涓緝濂界殑閫夋嫨鏄牴鎹暟鎹被鍨嬬殑閲嶈鎬х▼搴︽敼鍙RAID绾у埆銆備緥濡傦紝淇濆瓨鍙互鍦RAID 0纭洏涓婇噸鏂扮敓鎴愮殑涓嶅お閲嶈鐨勬暟鎹紝浣嗕繚瀛 鐪熸閲嶈鏁版嵁锛堜緥濡備富鏈轰俊鎭拰鏃ュ織锛夊埌RAID 0+1RAID N纭洏銆傚鏋滀綘鏈夎澶氬啓鎿嶄綔锛RAID N鍙兘浼氬瓨鍦ㄩ棶棰橈紝鍥犱负闇瑕佹椂闂存潵鏇存柊鏍¢獙浣嶃

         Linux涓婏紝閫氳繃閰嶇疆纾佺洏鎺ュ彛鏃朵娇鐢hdparm锛屼綘鍙互鑾峰緱鏇村鐨勬ц兘銆傦紙鍦ㄤ竴鑸礋杞戒笅杈惧埌100%骞朵笉鍥伴毦銆傦級涓嬮潰鐨hdparm閫夐」 搴旇闈炲父閫傜敤浜MySQL锛屽苟涓斿彲鑳介傜敤浜庤澶氬叾瀹冨簲鐢ㄧ▼搴忥細

     hdparm -m 16 -d 1

璇锋敞鎰忥紝褰撲娇鐢ㄨ鍛戒护鏃讹紝鎬ц兘鍜屽彲闈犳т緷璧栦簬纭欢锛屽洜姝ゆ垜浠己鐑堝缓璁紝浣跨敤hdparm瀹屽叏娴嬭瘯浣犵殑绯荤粺銆備负鑾峰彇鏇村璇︾粏淇℃伅锛岃鏌ラ槄hdparm鎵嬪唽甯姪椤点傚鏋hdparm浣跨敤鐨勪笉濂斤紝浼氬鑷存枃浠剁郴缁熺牬鍧忥紝鍥犳璇曢獙涔嬪墠璇峰仛濂芥瘡涓浠!

         涔熷彲浠ヨ缃暟鎹簱浣跨敤鐨勬枃浠剁郴缁熷弬鏁帮細

濡傛灉涓嶉渶瑕佺煡閬撴枃浠舵渶鍚庝竴娆¤闂殑鏃堕棿锛堣繖瀵规暟鎹簱鏈嶅姟鍣ㄥ苟娌℃湁瀹為檯鐨勭敤閫旓級锛屽彲浠ョ敤-o noatime閫夐」瀹夎鏂囦欢绯荤粺銆傝繖灏嗚烦杩囧鏂囦欢绯荤粺涓殑鑺傜偣鐨勬渶鍚庝竴娆¤闂椂闂寸殑鏇存柊锛屼粠鑰岃兘澶熼伩鍏嶄竴浜涚‖鐩樻悳绱€

鍦ㄨ澶氭搷浣滅郴缁熶腑锛岀敤-o async閫夐」瀹夎锛屽彲浠ュ皢鏂囦欢绯荤粺璁剧疆涓哄紓姝ユ洿鏂般傚鏋滅數鑴戠浉褰撶ǔ瀹氾紝杩欏簲璇ョ粰浜堜綘鏇撮珮鐨勬ц兘鑰屼笉闇瑕佺壓鐗插お澶氱殑鍙潬鎬с傦紙璇ユ爣蹇楁槸Linux涓粯璁ゅ紑鍚傦級

7.6.1. 浣跨敤绗﹀彿閾炬帴

浣犲彲浠ュ皢琛ㄥ拰鏁版嵁搴撲粠鏁版嵁搴撶洰褰曠Щ鍔ㄥ埌鍏跺畠鐨勪綅缃苟涓旂敤鎸囧悜鏂颁綅缃殑绗﹀彿閾炬帴杩涜鏇挎崲銆備綘鍙兘闇瑕佽繖鏍蜂綔锛屼緥濡傦紝绉诲姩鏁版嵁搴撳埌涓涓湁鏇村绌洪棽绌洪棿鐨勬枃浠剁郴缁熶腑鎴栭氳繃灏嗚〃鍒嗘暎鍒颁笉鍚岀殑纾佺洏涓婁互澧炲姞绯荤粺鐨勮闂熷害銆

鎺ㄨ崘鐨勬柟娉曞奸渶瑕佸皢鏁版嵁搴撻氳繃绗﹀彿閾炬帴鎸囧埌涓嶅悓鐨勭鐩樸傜鍙烽摼鎺ヨ〃浠呬綔涓烘槸鏈鍚庣殑鍔炴硶銆

7.6.1.1.聽鍦║nix涓婁娇鐢ㄩ拡瀵规暟鎹簱鐨勭鍙烽摼鎺

Unix涓紝绗﹀彿閾炬帴涓涓暟鎹簱鐨勬柟娉曟槸锛岄鍏堝湪涓浜涙湁绌洪棽绌洪棿鐨勭‖鐩樹笂鍒涘缓涓涓洰褰曪紝鐒跺悗浠MySQL鏁版嵁鐩綍涓垱寤哄畠鐨勪竴涓鍙烽摼鎺ャ

shell> mkdir /dr1/databases/test
shell> ln -s /dr1/databases/test /path/to/datadir

MySQL涓嶆敮鎸侀摼鎺ヤ竴涓洰褰曞埌澶氫釜鏁版嵁搴撱傜敤涓涓鍙烽摼鎺ユ浛鎹竴涓暟鎹簱鐩綍锛屽彧瑕佷綘涓嶅湪鏁版嵁搴撲箣闂村埗浣滅鍙烽摼鎺ャ傚亣瀹氫綘鏈変竴涓暟鎹簱db1MySQL鏁版嵁鐩綍涓嬶紝鐒跺悗鐢熸垚涓涓鍙烽摼鎺db2鎸囧悜 db1

shell> cd /path/to/datadir
shell> ln -s db1 db2

瀵逛簬db1涓殑浠讳綍琛tbl_a锛屽湪db2涔熷嚭鐜板苟涓旇繕鏄〃tbl_a銆傚鏋滀竴涓鎴锋洿鏂db1.tbl_a骞朵笖鍙︿竴涓鎴锋洿鏂db2.tbl_a锛屽彲鑳戒細鍑虹幇闂銆

鐒惰岋紝濡傛灉浣犵湡鐨勯渶瑕佽繖鏍峰仛锛屽彲浠ラ氳繃鏀瑰彉婧愭枃浠mysys/my_symlink.c鏉ュ疄鐜帮紝鍦ㄨ鏂囦欢涓綘搴旀煡鎵句笅闈㈢殑璇彞锛

if (!(MyFlags & MY_RESOLVE_LINK) ||

    (!lstat(filename,&stat_buff) && S_ISLNK(stat_buff.st_mode)))

灏嗚璇彞淇敼涓猴細

if (1)

璇锋敞鎰忥紝瀵逛簬鎵鏈夌殑Windows鏈嶅姟鍣紝榛樿鍚敤绗﹀彿閾炬帴鏀寔銆

7.6.1.2.聽鍦║nix骞冲彴涓婁娇鐢ㄨ〃鐨勭鍙烽摼鎺
 

浣犱笉搴斿湪娌℃湁瀹屽叏鍙搷浣滅殑realpath()璋冪敤鐨勭郴缁熶腑瀵硅〃杩涜绗﹀彿閾炬帴銆傦紙LinuxSolaris鏀寔realpath())銆傚彲浠ラ氳繃鍙戝嚭涓涓SHOW VARIABLES LIKE 'have_symlink'璇彞锛屾鏌ョ郴缁熸槸鍚︽敮鎸佺鍙烽摼鎺ャ

鍙湁MyISAM琛ㄥ畬鍏ㄦ敮鎸佺鍙烽摼鎺ャ傚浜庡叾瀹冭〃绫诲瀷锛屽鏋滆瘯鍥惧湪鎿嶄綔绯荤粺涓殑鏂囦欢涓婄敤鍓嶉潰鐨勪换浣曡鍙ヤ娇鐢ㄧ鍙烽摼鎺ワ紝鍙兘浼氬嚭鐜板鎬殑闂銆

瀵逛簬MyISAM琛ㄧ殑绗﹀彿閾炬帴鐨勫鐞嗗涓嬶細

         鍦ㄦ暟鎹洰褰曟寚锛屼竴瀹氫細鏈夎〃瀹氫箟鏂囦欢銆佹暟鎹枃浠跺拰绱㈠紩鏂囦欢銆傛暟鎹枃浠跺拰绱㈠紩鏂囦欢鍙互绉诲埌鍒鍜屽湪鏁版嵁鐩綍涓鍙烽摼鎺ユ浛浠c傝〃瀹氫箟鏂囦欢涓嶈兘杩涜绗﹀彿閾炬帴鏇挎崲銆

         鍙互鍒嗗埆閫氳繃绗﹀彿閾炬帴灏嗘暟鎹枃浠跺拰绱㈠紩鏂囦欢鎸囧埌涓嶅悓鐨勭洰褰曘

         濡傛灉mysqld娌℃湁杩愯锛岀鍙烽摼鎺ュ彲浠ヤ粠鏈嶅姟鍣ㄥ懡浠よ浣跨敤ln -s鎵嬪姩瀹屾垚銆傚悓鏍凤紝閫氳繃浣跨敤DATA DIRECTORYINDEX DIRECTORY閫夐」鍒涘缓琛紝浣犲彲浠ユ寚绀鸿繍琛岀殑MySQL鏈嶅姟鍣ㄦ墽琛岀鍙烽摼鎺ャ傚弬瑙13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥

         myisamchk涓嶇敤鏁版嵁鏂囦欢鎴栫储寮曟枃浠舵浛鎹㈢鍙烽摼鎺ャ傚畠鐩存帴宸ヤ綔鍦ㄧ鍙烽摼鎺ユ寚鍚戠殑鏂囦欢銆備换浣曚复鏃舵枃浠跺垱寤哄湪鏁版嵁鏂囦欢鎴栫储寮曟枃浠舵墍澶勭殑鐩綍涓

         娉ㄩ噴锛褰撲綘鍒犳帀涓涓〃鏃讹紝濡傛灉璇ヨ〃浣跨敤浜嗙鍙烽摼鎺ワ紝绗﹀彿閾炬帴鍜岃绗﹀彿閾炬帴鎸囧悜鐨勬枃浠堕兘琚垹闄ゆ帀銆傝繖灏辨槸浣犱笉搴斾互绯荤粺root鐢ㄦ埛杩愯mysqld鎴栧厑璁哥郴缁熺敤鎴峰MySQL鏁版嵁搴撶洰褰曟湁鍐欒闂潈闄愮殑鍘熷洜銆

         濡傛灉浣犵敤ALTER TABLE ... RENAME閲嶅懡鍚嶄竴涓〃骞朵笖涓嶅皢琛ㄧЩ鍒板彟涓涓暟鎹簱锛屾暟鎹簱鐩綍涓殑绗﹀彿閾炬帴琚噸鏂板懡鍚嶄负涓涓柊鍚嶅瓧骞朵笖鏁版嵁鏂囦欢鍜岀储寮曟枃浠朵篃鐩稿簲鍦伴噸鏂板懡鍚嶃

         濡傛灉浣犵敤ALTER TABLE ... RENAME绉诲姩涓涓〃鍒板彟涓涓暟鎹簱锛岃〃绉诲姩鍒板彟涓涓暟鎹簱鐩綍銆傛棫鐨勭鍙烽摼鎺ュ拰鍏舵墍鎸囧悜鐨勬枃浠惰鍒犻櫎銆傛崲鍙ヨ瘽璇达紝鏂拌〃涓嶅啀琚摼鎺ャ

         濡傛灉涓嶄娇鐢ㄧ鍙烽摼鎺ワ紝浣犲簲瀵mysqld浣跨敤--skip-symbolic-links閫夐」浠ョ‘淇濇病鏈変汉鑳藉浣跨敤mysqld鏉ュ垹闄ゆ垨閲嶆柊鍛藉悕鏁版嵁鐩綍涔嬪鐨勬枃浠躲

琛ㄧ鍙烽摼鎺ヨ繕涓嶆敮鎸佷互涓嬫搷浣滐細

         ALTER TABLE蹇界暐DATA DIRECTORY INDEX DIRECTORY琛ㄩ夐」銆

         BACKUP TABLE RESTORE TABLE涓嶈冭檻绗﹀彿閾炬帴銆

         .frm鏂囦欢蹇呴』缁濅笉鑳芥槸涓涓鍙閾炬帴锛堝鍓嶉潰鎵杩帮紝鍙湁鏁版嵁鍜岀储寮曟枃浠跺彲浠ユ槸绗﹀彿閾炬帴锛夈傚鏋滆瘯鍥捐繖鏍峰仛锛堜緥濡傦紝鐢熸垚绗﹀彿閾炬帴锛変細浜х敓涓嶆纭殑缁撴灉銆傚亣瀹氫綘鍦MySQL鏁版嵁鐩綍涓嬫湁涓涓暟鎹簱db1锛岃鏁版嵁搴撴湁涓涓〃tbl1锛屽苟涓斿湪db1鐩綍涓綘鍒朵綔浜嗕竴涓鍙烽摼鎺tbl2鎸囧悜tbl1

                shell> cd /path/to/datadir/db1
                shell> ln -s tbl1.frm tbl2.frm
                shell> ln -s tbl1.MYD tbl2.MYD
                shell> ln -s tbl1.MYI tbl2.MYI

濡傛灉涓涓嚎绋嬭鍙db1.tbl1鍚屾椂鍙︿竴涓嚎绋嬫洿鏂 db1.tbl2浼氬彂鐢熼棶棰橈細

o        鏌ヨ缂撳瓨灏嗗彉涓衡滃偦鐡溾 (瀹冩病鏈夊姙娉曠煡閬tbl1鏄惁琚洿鏂帮紝鍥犳瀹冭繑鍥炶繃鏃剁殑缁撴灉)

o        tbl2涓婄殑ALTER璇彞涔熶細澶辫触銆

7.6.1.3.聽鍦╓indows骞冲彴涓婁娇鐢ㄥ叧浜庢暟鎹簱鐨勭鍙烽摼鎺

Windowsmysqld-maxmysql-max-nt鏈嶅姟鍣ㄤ娇鐢-DUSE_SYMDIR閫夐」缂栬瘧鎴愩傚厑璁镐綘鏀剧疆鏁版嵁搴撶洰褰曞埌涓涓笉鍚岀殑纭洏锛岄氳繃璁剧疆涓涓鍙烽摼鎺ユ寚鍚戝畠銆傝繖绫讳技浜Unix涓殑绗﹀彿閾炬帴锛屽敖绠¤缃摼鎺ョ殑杩囩▼涓嶅悓銆

绗﹀彿閾炬帴榛樿涓哄惎鐢ㄣ傚鏋滀綘涓嶉渶瑕侊紝浣跨敤skip-symbolic-links閫夐」鏉ョ鐢ㄥ畠锛

[mysqld]

skip-symbolic-links

Windows涓紝閫氳繃鍦ㄦ暟鎹洰褰曚腑鍒涘缓涓涓枃浠讹紝璇ユ枃浠跺寘鍚洰鏍囩洰褰曠殑璺緞锛屼綘鍙互涓MySQL鏁版嵁搴撳垱寤轰竴涓鍙烽摼鎺ャ傝鏂囦欢搴旇琚懡鍚嶄负db_name.sym锛屽叾涓db_name鏄暟鎹簱鍚嶃

鍋囧畾MySQ鏁版嵁鐩綍鏄C:\mysql\data骞朵笖浣犳兂瑕佹暟鎹簱foo鏀剧疆鍦D:\data\foo銆傝缃竴涓鍙烽摼鎺ュ涓嬫墍绀猴細

1.    纭繚D:\data\foo鐩綍瀛樺湪锛屽鏋滃繀瑕佸垱寤哄畠銆傚鏋滀綘鍦ㄦ暟鎹洰褰曞唴鏈変竴涓懡鍚嶄负foo鐨勬暟鎹簱鐩綍锛屼綘搴旂Щ鍔ㄥ畠鍒D:\data鐩綍銆傚惁鍒欙紝绗﹀彿閾炬帴鏃犳晥銆備负閬垮厤鍑虹幇闂锛屽綋浣犵Щ鍔ㄦ暟鎹簱鐩綍鏃舵湇鍔″櫒涓嶅簲璇ヨ繍琛屻

2.    鍒涘缓涓涓枃鏈枃浠C:\mysql\data\foo.sym锛岃鏈枃鏂囦欢鍖呭惈璺緞鍚D:\data\foo\

姝ゅ悗锛屾暟鎹簱foo鍒涘缓鐨勬墍鏈夎〃灏嗗垱寤哄湪D:\data\foo鏂囦欢涓璇锋敞鎰忥紝濡傛灉鍦MySQL鏁版嵁鐩綍涓瓨鍦ㄥ悓鍚嶇殑鏁版嵁搴撶洰褰曪紝涓嶈兘浣跨敤绗﹀彿閾炬帴


杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆