MongoDB · 寮曟搸鐗规€?· MongoDB绱㈠紩鍘熺悊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB · 寮曟搸鐗规€?· MongoDB绱㈠紩鍘熺悊相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/position' title='position'>position   绫诲瀷   鏌ユ壘   闆嗗悎   楂樻晥   鏇存柊   explain   鍝簺   index   

MongoDB 路 寮曟搸鐗规€?路 MongoDB绱㈠紩鍘熺悊
鏁版嵁搴撳唴鏍告湀鎶?br>鍘熸枃閾炬帴 http://mysql.taobao.org/monthly/2018/09/06/

涓轰粈涔堥渶瑕佺储寮曪紵
褰撲綘鎶辨€∕ongoDB闆嗗悎鏌ヨ鏁堢巼浣庣殑鏃跺€欙紝鍙兘浣犲氨闇€瑕佽€冭檻浣跨敤绱㈠紩浜嗭紝涓轰簡鏂逛究鍚庣画浠嬬粛锛屽厛绉戞櫘涓婱ongoDB閲岀殑绱㈠紩鏈哄埗锛堝悓鏍烽€傜敤浜庡叾浠栫殑鏁版嵁搴撴瘮濡俶ysql锛夈€?/p>

mongo-9552:PRIMARY> db.person.find()
{ "_id" : ObjectId("571b5da31b0d530a03b3ce82"), "name" : "jack", "age" : 19 }
{ "_id" : ObjectId("571b5dae1b0d530a03b3ce83"), "name" : "rose", "age" : 20 }
{ "_id" : ObjectId("571b5db81b0d530a03b3ce84"), "name" : "jack", "age" : 18 }
{ "_id" : ObjectId("571b5dc21b0d530a03b3ce85"), "name" : "tony", "age" : 21 }
{ "_id" : ObjectId("571b5dc21b0d530a03b3ce86"), "name" : "adam", "age" : 18 }
褰撲綘寰€鏌愬悇涓泦鍚堟彃鍏ュ涓枃妗e悗锛屾瘡涓枃妗e湪缁忚繃搴曞眰鐨勫瓨鍌ㄥ紩鎿庢寔涔呭寲鍚庯紝浼氭湁涓€涓綅缃俊鎭紝閫氳繃杩欎釜浣嶇疆淇℃伅锛屽氨鑳戒粠瀛樺偍寮曟搸閲岃鍑鸿鏂囨。銆?br>姣斿mmapv1寮曟搸閲岋紝浣嶇疆淇℃伅鏄€庢枃浠秈d + 鏂囦欢鍐卭ffset 銆忥紝 鍦╳iredtiger瀛樺偍寮曟搸锛堜竴涓狵V瀛樺偍寮曟搸锛夐噷锛屼綅缃俊鎭槸wiredtiger鍦ㄥ瓨鍌ㄦ枃妗?br>鏃剁敓鎴愮殑涓€涓猭ey锛岄€氳繃杩欎釜key鑳借闂埌瀵瑰簲鐨勬枃妗o紱涓烘柟渚夸粙缁嶏紝缁熶竴鐢╬os(position鐨勭缉鍐?鏉ヤ唬琛ㄤ綅缃俊鎭€?/p>

姣斿涓婇潰鐨勪緥瀛愰噷锛宲erson闆嗗悎閲屽寘鍚彃鍏ヤ簡4涓枃妗o紝鍋囪鍏跺瓨鍌ㄥ悗浣嶇疆淇℃伅濡備笅(涓烘柟渚挎弿杩帮紝鏂囨。鐪佸幓_id瀛楁)

浣嶇疆淇℃伅 鏂囨。
pos1 {鈥渘ame鈥?: 鈥渏ack鈥? 鈥渁ge鈥?: 19 }
pos2 {鈥渘ame鈥?: 鈥渞ose鈥? 鈥渁ge鈥?: 20 }
pos3 {鈥渘ame鈥?: 鈥渏ack鈥? 鈥渁ge鈥?: 18 }
pos4 {鈥渘ame鈥?: 鈥渢ony鈥? 鈥渁ge鈥?: 21}
pos5 {鈥渘ame鈥?: 鈥渁dam鈥? 鈥渁ge鈥?: 18}
鍋囪鐜板湪鏈変釜鏌ヨ db.person.find( {age: 18} ), 鏌ヨ鎵€鏈夊勾榫勪负18宀佺殑浜猴紝杩欐椂闇€瑕侀亶鍘嗘墍鏈夌殑鏂囨。锛堛€庡叏琛ㄦ壂鎻忋€忥級锛屾牴鎹綅缃俊鎭鍑烘枃妗o紝
瀵规瘮age瀛楁鏄惁涓?8銆傚綋鐒跺鏋滃彧鏈?涓枃妗o紝鍏ㄨ〃鎵弿鐨勫紑閿€骞朵笉澶э紝浣嗗鏋滈泦鍚堟枃妗f暟閲忓埌鐧句竾銆佺敋鑷冲崈涓囦笂浜跨殑鏃跺€欙紝瀵归泦鍚堣繘琛屽叏琛ㄦ壂鎻忓紑閿€
鏄潪甯稿ぇ鐨勶紝涓€涓煡璇㈣€楄垂鏁板崄绉掔敋鑷冲嚑鍒嗛挓閮芥湁鍙兘銆?/p>

濡傛灉鎯冲姞閫?db.person.find( {age: 18} 锛夛紝灏卞彲浠ヨ€冭檻瀵筽erson琛ㄧ殑age瀛楁寤虹珛绱㈠紩銆?/p>

db.person.createIndex( {age: 1} ) // 鎸塧ge瀛楁鍒涘缓鍗囧簭绱㈠紩
寤虹珛绱㈠紩鍚庯紝MongoDB浼氶澶栧瓨鍌ㄤ竴浠芥寜age瀛楁鍗囧簭鎺掑簭鐨勭储寮曟暟鎹紝绱㈠紩缁撴瀯绫讳技濡備笅锛岀储寮曢€氬父閲囩敤绫讳技btree鐨勭粨鏋勬寔涔呭寲瀛樺偍锛屼互淇濊瘉浠庣储寮曢噷蹇€?br>锛圤(logN)鐨勬椂闂村鏉傚害锛夋壘鍑烘煇涓猘ge鍊煎搴旂殑浣嶇疆淇℃伅锛岀劧鍚庢牴鎹綅缃俊鎭氨鑳借鍙栧嚭瀵瑰簲鐨勬枃妗c€?/p>

age 浣嶇疆淇℃伅
18 pos3
18 pos5
19 pos1
20 pos2
21 pos4
绠€鍗曠殑璇达紝绱㈠紩灏辨槸灏嗘枃妗f寜鐓ф煇涓紙鎴栨煇浜涳級瀛楁椤哄簭缁勭粐璧锋潵锛屼互渚胯兘鏍规嵁璇ュ瓧娈甸珮鏁堢殑鏌ヨ銆傛湁浜嗙储寮曪紝鑷冲皯鑳戒紭鍖栧涓嬪満鏅殑鏁堢巼锛?/p>

鏌ヨ锛屾瘮濡傛煡璇㈠勾榫勪负18鐨勬墍鏈変汉
鏇存柊/鍒犻櫎锛屽皢骞撮緞涓?8鐨勬墍鏈変汉鐨勪俊鎭洿鏂版垨鍒犻櫎锛屽洜涓烘洿鏂版垨鍒犻櫎鏃讹紝闇€瑕佹牴鎹潯浠跺厛鏌ヨ鍑烘墍鏈夌鍚堟潯浠剁殑鏂囨。锛屾墍浠ユ湰璐ㄤ笂杩樻槸鍦ㄤ紭鍖栨煡璇?br>鎺掑簭锛屽皢鎵€鏈変汉鐨勪俊鎭寜骞撮緞鎺掑簭锛屽鏋滄病鏈夌储寮曪紝闇€瑕佸叏琛ㄦ壂鎻忔枃妗o紝鐒跺悗鍐嶅鎵弿鐨勭粨鏋滆繘琛屾帓搴?br>浼楁墍鍛ㄧ煡锛孧ongoDB榛樿浼氫负鎻掑叆鐨勬枃妗g敓鎴恄id瀛楁锛堝鏋滃簲鐢ㄦ湰韬病鏈夋寚瀹氳瀛楁锛夛紝_id鏄枃妗e敮涓€鐨勬爣璇嗭紝涓轰簡淇濊瘉鑳芥牴鎹枃妗d蹇€掓煡璇㈡枃妗o紝
MongoDB榛樿浼氫负闆嗗悎鍒涘缓_id瀛楁鐨勭储寮曘€?/p>

mongo-9552:PRIMARY> db.person.getIndexes() // 鏌ヨ闆嗗悎鐨勭储寮曚俊鎭?br>[
{
"ns" : "test.person", // 闆嗗悎鍚?br> "v" : 1, // 绱㈠紩鐗堟湰
"key" : { // 绱㈠紩鐨勫瓧娈靛強鎺掑簭鏂瑰悜
"_id" : 1 // 鏍规嵁_id瀛楁鍗囧簭绱㈠紩
},
"name" : "_id_" // 绱㈠紩鐨勫悕绉?br> }
]
MongoDB绱㈠紩绫诲瀷
MongoDB鏀寔澶氱绫诲瀷鐨勭储寮曪紝鍖呮嫭鍗曞瓧娈电储寮曘€佸鍚堢储寮曘€佸key绱㈠紩銆佹枃鏈储寮曠瓑锛屾瘡绉嶇被鍨嬬殑绱㈠紩鏈変笉鍚岀殑浣跨敤鍦哄悎銆?/p>

鍗曞瓧娈电储寮?锛圫ingle Field Index锛?br> db.person.createIndex( {age: 1} )
涓婅堪璇彞閽堝age鍒涘缓浜嗗崟瀛楁绱㈠紩锛屽叾鑳藉姞閫熷age瀛楁鐨勫悇绉嶆煡璇㈣姹傦紝鏄渶甯歌鐨勭储寮曞舰寮忥紝MongoDB榛樿鍒涘缓鐨刬d绱㈠紩涔熸槸杩欑绫诲瀷銆?/p>

{age: 1} 浠h〃鍗囧簭绱㈠紩锛屼篃鍙互閫氳繃{age: -1}鏉ユ寚瀹氶檷搴忕储寮曪紝瀵逛簬鍗曞瓧娈电储寮曪紝鍗囧簭/闄嶅簭鏁堟灉鏄竴鏍风殑銆?/p>

澶嶅悎绱㈠紩 (Compound Index)
澶嶅悎绱㈠紩鏄疭ingle Field Index鐨勫崌绾х増鏈紝瀹冮拡瀵瑰涓瓧娈佃仈鍚堝垱寤虹储寮曪紝鍏堟寜绗竴涓瓧娈垫帓搴忥紝绗竴涓瓧娈电浉鍚岀殑鏂囨。鎸夌浜屼釜瀛楁鎺掑簭锛?br>渚濇绫绘帹锛屽涓嬮拡瀵筧ge, name杩?涓瓧娈靛垱寤轰竴涓鍚堢储寮曘€?/p>

db.person.createIndex( {age: 1, name: 1} )
涓婅堪绱㈠紩瀵瑰簲鐨勬暟鎹粍缁囩被浼间笅琛紝涓巤age: 1}绱㈠紩涓嶅悓鐨勬椂锛屽綋age瀛楁鐩稿悓鏃讹紝鍦ㄦ牴鎹畁ame瀛楁杩涜鎺掑簭锛屾墍浠os5瀵瑰簲鐨勬枃妗f帓鍦╬os3涔嬪墠銆?/p>

age,name 浣嶇疆淇℃伅
18,adam pos5
18,jack pos3
19,jack pos1
20,rose pos2
21,tony pos4
澶嶅悎绱㈠紩鑳芥弧瓒崇殑鏌ヨ鍦烘櫙姣斿崟瀛楁绱㈠紩鏇翠赴瀵岋紝涓嶅厜鑳芥弧瓒冲涓瓧娈电粍鍚堣捣鏉ョ殑鏌ヨ锛屾瘮濡俤b.person.find( {age锛?18锛?name: "jack"} )锛屼篃鑳芥弧
瓒虫墍浠ヨ兘鍖归厤绗﹀悎绱㈠紩鍓嶇紑鐨勬煡璇紝杩欓噷{age: 1}鍗充负{age: 1, name: 1}鐨勫墠缂€锛屾墍浠ョ被浼糳b.person.find( {age锛?18} )鐨勬煡璇篃鑳介€氳繃璇ョ储寮曟潵鍔犻€燂紱
浣哾b.person.find( {name: "jack"} )鍒欐棤娉曚娇鐢ㄨ澶嶅悎绱㈠紩銆傚鏋滅粡甯搁渶瑕佹牴鎹€巒ame瀛楁銆忎互鍙娿€巒ame鍜宎ge瀛楁缁勫悎銆忔潵鏌ヨ锛屽垯搴旇鍒涘缓濡備笅鐨勫鍚堢储寮?/p>

db.person.createIndex( {name: 1, age: 1} )
闄や簡鏌ヨ鐨勯渶姹傝兘澶熷奖鍝嶇储寮曠殑椤哄簭锛屽瓧娈电殑鍊煎垎甯冧篃鏄竴涓噸瑕佺殑鑰冮噺鍥犵礌锛屽嵆浣縫erson闆嗗悎鎵€鏈夌殑鏌ヨ閮芥槸銆巒ame鍜宎ge瀛楁缁勫悎銆忥紙鎸囧畾鐗瑰畾鐨刵ame鍜宎ge锛夛紝瀛楁鐨勯『搴忎篃鏄湁褰卞搷鐨勩€?/p>

age瀛楁鐨勫彇鍊煎緢鏈夐檺锛屽嵆鎷ユ湁鐩稿悓age瀛楁鐨勬枃妗d細鏈夊緢澶氾紱鑰宯ame瀛楁鐨勫彇鍊煎垯涓板瘜寰堝锛屾嫢鏈夌浉鍚宯ame瀛楁鐨勬枃妗e緢灏戯紱鏄剧劧鍏堟寜name瀛楁鏌ユ壘锛屽啀鍦?br>鐩稿悓name鐨勬枃妗i噷鏌ユ壘age瀛楁鏇翠负楂樻晥銆?/p>

澶歬ey绱㈠紩 锛圡ultikey Index锛?br>褰撶储寮曠殑瀛楁涓烘暟缁勬椂锛屽垱寤哄嚭鐨勭储寮曠О涓哄key绱㈠紩锛屽key绱㈠紩浼氫负鏁扮粍鐨勬瘡涓厓绱犲缓绔嬩竴鏉$储寮曪紝姣斿person琛ㄥ姞鍏ヤ竴涓猦abbit瀛楁锛堟暟缁勶級鐢ㄤ簬鎻忚堪
鍏磋叮鐖卞ソ锛岄渶瑕佹煡璇㈡湁鐩稿悓鍏磋叮鐖卞ソ鐨勪汉灏卞彲浠ュ埄鐢╤abbit瀛楁鐨勫key绱㈠紩銆?/p>

{"name" : "jack", "age" : 19, habbit: ["football, runnning"]}
db.person.createIndex( {habbit: 1} ) // 鑷姩鍒涘缓澶歬ey绱㈠紩
db.person.find( {habbit: "football"} )
鍏朵粬绫诲瀷绱㈠紩
鍝堝笇绱㈠紩锛圚ashed Index锛夋槸鎸囨寜鐓ф煇涓瓧娈电殑hash鍊兼潵寤虹珛绱㈠紩锛岀洰鍓嶄富瑕佺敤浜嶮ongoDB Sharded Cluster鐨凥ash鍒嗙墖锛宧ash绱㈠紩鍙兘婊¤冻瀛楁瀹屽叏鍖归厤鐨勬煡璇紝涓嶈兘婊¤冻鑼冨洿鏌ヨ绛夈€?/p>

鍦扮悊浣嶇疆绱㈠紩锛圙eospatial Index锛夎兘寰堝ソ鐨勮В鍐砄2O鐨勫簲鐢ㄥ満鏅紝姣斿銆庢煡鎵鹃檮杩戠殑缇庨銆忋€併€庢煡鎵炬煇涓尯鍩熷唴鐨勮溅绔欍€忕瓑銆?/p>

鏂囨湰绱㈠紩锛圱ext Index锛夎兘瑙e喅蹇€熸枃鏈煡鎵剧殑闇€姹傦紝姣斿鏈変竴涓崥瀹㈡枃绔犻泦鍚堬紝闇€瑕佹牴鎹崥瀹㈢殑鍐呭鏉ュ揩閫熸煡鎵撅紝鍒欏彲浠ラ拡瀵瑰崥瀹㈠唴瀹瑰缓绔嬫枃鏈储寮曘€?/p>

绱㈠紩棰濆灞炴€?/span>
MongoDB闄や簡鏀寔澶氱涓嶅悓绫诲瀷鐨勭储寮曪紝杩樿兘瀵圭储寮曞畾鍒朵竴浜涚壒娈婄殑灞炴€с€?/p>

鍞竴绱㈠紩 (unique index)锛氫繚璇佺储寮曞搴旂殑瀛楁涓嶄細鍑虹幇鐩稿悓鐨勫€硷紝姣斿_id绱㈠紩灏辨槸鍞竴绱㈠紩
TTL绱㈠紩锛氬彲浠ラ拡瀵规煇涓椂闂村瓧娈碉紝鎸囧畾鏂囨。鐨勮繃鏈熸椂闂达紙缁忚繃鎸囧畾鏃堕棿鍚庤繃鏈?鎴?鍦ㄦ煇涓椂闂寸偣杩囨湡锛?br>閮ㄥ垎绱㈠紩 (partial index): 鍙拡瀵圭鍚堟煇涓壒瀹氭潯浠剁殑鏂囨。寤虹珛绱㈠紩锛?.2鐗堟湰鎵嶆敮鎸佽鐗规€?br>绋€鐤忕储寮?sparse index): 鍙拡瀵瑰瓨鍦ㄧ储寮曞瓧娈电殑鏂囨。寤虹珛绱㈠紩锛屽彲鐪嬪仛鏄儴鍒嗙储寮曠殑涓€绉嶇壒娈婃儏鍐?br>绱㈠紩浼樺寲
db profiling
MongoDB鏀寔瀵笵B鐨勮姹傝繘琛宲rofiling锛岀洰鍓嶆敮鎸?绉嶇骇鍒殑profiling銆?/p>

0锛?涓嶅紑鍚痯rofiling
1锛?灏嗗鐞嗘椂闂磋秴杩囨煇涓槇鍊?榛樿100ms)鐨勮姹傞兘璁板綍鍒癉B涓嬬殑system.profile闆嗗悎 锛堢被浼间簬mysql銆乺edis鐨剆lowlog锛?br>2锛?灏嗘墍鏈夌殑璇锋眰閮借褰曞埌DB涓嬬殑system.profile闆嗗悎锛堢敓浜х幆澧冩厧鐢級
閫氬父锛岀敓浜х幆澧冨缓璁娇鐢?绾у埆鐨刾rofiling锛屽苟鏍规嵁鑷韩闇€姹傞厤缃悎鐞嗙殑闃堝€硷紝鐢ㄤ簬鐩戞祴鎱㈣姹傜殑鎯呭喌锛屽苟鍙婃椂鐨勫仛绱㈠紩浼樺寲銆?/p>

濡傛灉鑳藉湪闆嗗悎鍒涘缓鐨勬椂鍊欏氨鑳姐€庢牴鎹笟鍔℃煡璇㈤渶姹傚喅瀹氬簲璇ュ垱寤哄摢浜涚储寮曘€忥紝褰撶劧鏄渶浣崇殑閫夋嫨锛涗絾鐢变簬涓氬姟闇€姹傚鍙橈紝瑕佹牴鎹疄闄呮儏鍐典笉鏂殑杩涜浼樺寲銆傜储寮曞苟涓嶆槸瓒婂瓒婂ソ锛岄泦鍚堢殑绱㈠紩澶锛屼細褰卞搷鍐欏叆銆佹洿鏂扮殑鎬ц兘锛屾瘡娆″啓鍏ラ兘闇€瑕佹洿鏂版墍鏈夌储寮曠殑鏁版嵁锛涙墍浠ヤ綘system.profile閲岀殑鎱㈣姹傚彲鑳芥槸绱㈠紩寤虹珛鐨勪笉澶熷鑷达紝涔熷彲鑳芥槸绱㈠紩杩囧瀵艰嚧銆?/p>

鏌ヨ璁″垝
绱㈠紩宸茬粡寤虹珛浜嗭紝浣嗘煡璇㈣繕鏄緢鎱㈡€庝箞鐮达紵杩欐椂灏卞緱娣卞叆鐨勫垎鏋愪笅绱㈠紩鐨勪娇鐢ㄦ儏鍐典簡锛屽彲閫氳繃鏌ョ湅涓嬭缁嗙殑鏌ヨ璁″垝鏉ュ喅瀹氬浣曚紭鍖栥€傞€氳繃鎵ц璁″垝鍙互鐪嬪嚭濡備笅闂

鏍规嵁鏌愪釜/浜涘瓧娈垫煡璇紝浣嗘病鏈夊缓绔嬬储寮?br>鏍规嵁鏌愪釜/浜涘瓧娈垫煡璇紝浣嗗缓绔嬩簡澶氫釜绱㈠紩锛屾墽琛屾煡璇㈡椂娌℃湁浣跨敤棰勬湡鐨勭储寮曘€?br>寤虹珛绱㈠紩鍓嶏紝db.person.find( {age锛?18} )蹇呴』鎵цCOLLSCAN锛屽嵆鍏ㄨ〃鎵弿銆?/p>

mongo-9552:PRIMARY> db.person.find({age: 18}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.person",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 18
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"age" : {
"$eq" : 18
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost",
"port" : 9552,
"version" : "3.2.3",
"gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937"
},
"ok" : 1
}
寤虹珛绱㈠紩鍚庯紝閫氳繃鏌ヨ璁″垝鍙互鐪嬪嚭锛屽厛杩涜[IXSCAN]((https://docs.mongodb.org/manual/reference/explain-results/#queryplanner)(浠庣储寮曚腑鏌ユ壘)锛岀劧鍚嶧ETCH锛岃鍙栧嚭婊¤冻鏉′欢鐨勬枃妗c€?/p>

mongo-9552:PRIMARY> db.person.find({age: 18}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.person",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 18
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"age" : 1
},
"indexName" : "age_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"age" : [
"[18.0, 18.0]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost",
"port" : 9552,
"version" : "3.2.3",
"gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937"
},
"ok" : 1
}


































































































































以上是关于MongoDB · 寮曟搸鐗规€?· MongoDB绱㈠紩鍘熺悊的主要内容,如果未能解决你的问题,请参考以下文章

Atitit 娓告垙寮曟搸---鐗╃悊绯荤粺锛?)------鐖嗙偢鏁堟灉

銆怉sphyre寮曟搸銆戝彂甯冧簡鏂扮増鏈琕101

Zotero锝滃鐢≒ubMed鎼滅储寮曟搸锛屾煡鎵剧浉鍏虫枃鐚紒

30+鎼滅储寮曟搸锛亀u闄愬埗@鑷绾稿肪

鎼滅储寮曟搸鏄紒涓氭渶濂界殑钀ラ攢娓犻亾涔嬩竴

鎼滅储寮曟搸鍦烘櫙涓婦ebug鏋舵瀯璁捐