![MongoDB數(shù)據(jù)庫索引介紹_第1頁](http://file4.renrendoc.com/view/e98e79f6ae809ce1c0008b25cb0e5e38/e98e79f6ae809ce1c0008b25cb0e5e381.gif)
![MongoDB數(shù)據(jù)庫索引介紹_第2頁](http://file4.renrendoc.com/view/e98e79f6ae809ce1c0008b25cb0e5e38/e98e79f6ae809ce1c0008b25cb0e5e382.gif)
![MongoDB數(shù)據(jù)庫索引介紹_第3頁](http://file4.renrendoc.com/view/e98e79f6ae809ce1c0008b25cb0e5e38/e98e79f6ae809ce1c0008b25cb0e5e383.gif)
![MongoDB數(shù)據(jù)庫索引介紹_第4頁](http://file4.renrendoc.com/view/e98e79f6ae809ce1c0008b25cb0e5e38/e98e79f6ae809ce1c0008b25cb0e5e384.gif)
![MongoDB數(shù)據(jù)庫索引介紹_第5頁](http://file4.renrendoc.com/view/e98e79f6ae809ce1c0008b25cb0e5e38/e98e79f6ae809ce1c0008b25cb0e5e385.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、MongoDB數(shù)據(jù)庫索引介紹課程內(nèi)容索引基礎(chǔ)聯(lián)合索引多鍵索引地理位置索引全文索引TTL索引部分索引哈希索引索引基礎(chǔ)為什么需要索引?explain()- 寫入10000條文檔for (var i=1;iexecutionStats :executionSuccess : true, nReturned : 1,executionTimeMillis : 58,totalKeysExamined : 0,totalDocsExamined : 99999,executionStages : stage : COLLSCAN,filter : name : $eq : 1111, nReturned
2、 : 1,executionTimeMillisEstimate : 53,works : 100001,advanced : 1,needTime : 99999,needYield : 0,saveState : 783,restoreState : 783,isEOF : 1,invalidates : 0, direction : forward, docsExamined : 99999explain()- 寫入10000條文檔for (var i=1;i executionStats : executionSuccess : true, nReturned : 1,executio
3、nTimeMillis : 3,totalKeysExamined : 1,totalDocsExamined : 1,executionStages : stage : FETCH, nReturned : 1,executionTimeMillisEstimate : 0,docsExamined : 1,alreadyHasObj : 0, inputStage : stage : IXSCAN,nReturned : 1,executionTimeMillisEstimate : 0,works : 2,advanced : 1,索引使用注意點(diǎn)索引加速查詢性能、但會降低寫操作性能單個(gè)集
4、合支持最大索引個(gè)數(shù)64將索引cache在內(nèi)存中以獲得更好性能每個(gè)index都應(yīng)該被查詢使用,定期檢查無用索引單屬性索引單屬性索引單屬性索引創(chuàng)建單屬性索引創(chuàng)建索引方式_id 索引單屬性索引- 默認(rèn) _id _id : 1 - 創(chuàng)建 name屬性的單調(diào)遞增索引db.col.createIndex(name:1)- 查看所建索引 db.getIndexes( ) db.getIndexKeys( )- 創(chuàng)建索引的完整方法db.collection.createIndex(keys , options) (常用)options:backgroud unique name 聯(lián)合索引聯(lián)合索引聯(lián)合索引創(chuàng)建聯(lián)
5、合索引創(chuàng)建聯(lián)合索引的影響聯(lián)合索引符合查詢時(shí)的影響聯(lián)合索引的限制聯(lián)合索引- 創(chuàng)建 compound Indexdb.createIndex(“a”:1 , “b”:-1)- 查看所建索引 db.getIndexes( ) db.getIndexKeys( )- 對比查詢db.find( ).sort(a:-1,b:1).explain(true)db.find( ).sort(a:1,b:1).explain(true)聯(lián)合索引- 創(chuàng)建聯(lián)合索引db.col.createIndex(”a”:1, ”b”:1, ”c”:1)- 查詢且查看執(zhí)行計(jì)劃db.col.find(”a”:100 , ”b” :
6、 100, “c” : 100).explain( true )db.col.find(”a”:100 , ”b” : 100, “c” : 100“_id” : 0, a : 1 , b : 1 , c : 1 ).explain( true )- 查詢且查看執(zhí)行計(jì)劃executionStats : executionSuccess : true, nReturned : 1,executionTimeMillis : 0,totalKeysExamined : 1,totalDocsExamined : 0, executionStages : stage : PROJECTION,nRe
7、turned : 1,executionTimeMillisEstimate : 0,works : 2,advanced : 1,needTime : 0,needYield : 0,聯(lián)合索引- 創(chuàng)建聯(lián)合索引db.col.createIndex(”a”:1, ”b”:1, ”c”:1)- 聯(lián)合索引前綴db.getIndexes( ) db.getIndexKeys( ) a : 1, b : 1 , c : 1 a : 1 a : 1, b : 1 a : 1, b : 1 , c : 1 - 查詢且查看執(zhí)行計(jì)劃db.col.find(”a”:100 , ”b” : 100, “c” : 1
8、00).explain( true )db.col.find(”a”:100 , ”b” : 100).explain( true )db.col.find(”a”:100).explain( true )聯(lián)合索引- ESR規(guī)則E : equal, S : sort, R : range- 創(chuàng)建聯(lián)合索引db.col.createIndex(”a”:1, ”b”:1, ”c”:1)- 查詢且查看執(zhí)行計(jì)劃db.col.find(a:100).sort(b:1).explain( true )db.col.find(a:$gt:100 ).sort(b:1).explain( true )db.co
9、l.find(a:100,c:$gte:100).sort(b:1).explain( true )實(shí)驗(yàn):優(yōu)化聯(lián)合索引實(shí)驗(yàn):聯(lián)合索引優(yōu)化a = timestamp : 1, username : anonymous, rating : 3 , timestamp : 2, username : anonymous, rating : 5 , timestamp : 3, username : sam, rating : 1 , timestamp : 4, username : anonymous, rating : 2 , timestamp : 5, username : martha,
10、rating : 5 - 插入數(shù)據(jù)db.insertMany(a)- 創(chuàng)建索引db.createIndex( timestamp:1 )- 查詢且查看執(zhí)行計(jì)劃db.find(timestamp : $gte : 2 , $lte: 4 ).explain( true )- 查詢且查看執(zhí)行計(jì)劃executionStats : executionSuccess : true, nReturned : 3,executionTimeMillis : 0,totalKeysExamined : 3,totalDocsExamined : 3, executionStages : stage : FET
11、CH, nReturned : 3,works : 4,docsExamined : 3,alreadyHasObj : 0, inputStage : stage : IXSCAN,實(shí)驗(yàn):聯(lián)合索引優(yōu)化- 查詢且查看執(zhí)行計(jì)劃db.find(timestamp : $gte : 2 , $lte: 4 , username : anonymous).explain( true )- 執(zhí)行計(jì)劃executionStats : executionSuccess : true, nReturned : 2,executionTimeMillis : 0,totalKeysExamined : 3,to
12、talDocsExamined : 3, executionStages : stage : FETCH, filter : username : $eq : anonymous”,nReturned : 2,executionTimeMillisEstimate : 0,docsExamined : 3,alreadyHasObj : 0, inputStage : stage : IXSCAN,上述結(jié)果集合timestampusername1“anonymous”2“anonymous”3“sam”4“anonymous”5“martha”實(shí)驗(yàn):聯(lián)合索引優(yōu)化- 執(zhí)行過程query_set
13、: 2 timestamp 4query_set .fliter(username = anonymous)實(shí)驗(yàn):聯(lián)合索引優(yōu)化- 目標(biāo)查詢db.find(timestamp : $gte : 2 , $lte: 4 , username : anonymous).explain( true )- 解決方案db.createIndex( timestamp:1 , username:1 )db.createIndex( username:1 , timestamp:1 )實(shí)驗(yàn):聯(lián)合索引優(yōu)化- 創(chuàng)建索引 db.createIndex( timestamp:1 , username:1 )- 查詢并
14、查看執(zhí)行計(jì)劃 db.find( timestamp : $gte : 2, $lte : 4 , username : anonymous).explain(executionStats)- 執(zhí)行計(jì)劃executionStats : executionSuccess : true, nReturned : 2,executionTimeMillis : 2,totalKeysExamined : 4,totalDocsExamined : 2, executionStages : stage : FETCH, nReturned : 2,executionTimeMillisEstimate
15、: 11, alreadyHasObj : 0,inputStage : stage : IXSCAN,實(shí)驗(yàn):聯(lián)合索引優(yōu)化- 創(chuàng)建索引 db.createIndex( username:1 , timestamp:1 )- 查詢且查看執(zhí)行計(jì)劃 db.messages.find( timestamp : $gte : 2, $lte : 4 , username : anonymous ).explain(executionStats)- 執(zhí)行計(jì)劃executionStats : executionSuccess : true, nReturned : 2,executionTimeMillis
16、 : 0,totalKeysExamined : 2,totalDocsExamined : 2, executionStages : stage : FETCH, nReturned : 2,executionTimeMillisEstimate : 0,docsExamined : 2,alreadyHasObj : 0, inputStage : stage : IXSCAN,usernametimestamp“anonymous”1“anonymous”2“sam”3“anonymous”4“martha”5索引交集索引交集- 創(chuàng)建索引 db.col.createIndex( ”a”:
17、1 ) db.col.createIndex( ”b”:1 )- 查詢且查看執(zhí)行計(jì)劃 db.col.find(a:100,b:100).explain(true)- 執(zhí)行計(jì)劃inputStage : stage : AND_SORTED,“inputStages” : “stage” : “IXSCAN”,“keyPattern” : “b” : 1,“indexName” : “b_1”, “isMultiKey” : false,multiKeyPaths : b : ,direction : forward,多鍵索引多鍵索引多鍵索引多鍵索引使用多鍵索引原理多鍵索引排序多鍵索引限制多鍵索引
18、- 插入數(shù)據(jù)db.insert(name : test, age : 20, favorite :basketball, game,tennis”)- 創(chuàng)建索引 db.createIndex(favorite : 1 )- 查詢并且查看執(zhí)行計(jì)劃db.find (favorite : tennis).explain( true )- 執(zhí)行計(jì)劃queryPlanner : plannerVersion : 1, namespace : test.col3, indexFilterSet : false, parsedQuery : favorite : $eq : tennis”, winning
19、Plan : stage : FETCH, inputStage : stage : IXSCAN,keyPattern : favorite : 1,indexName : favorite_1, isMultiKey : true, multiKeyPaths : favorite : favorite”實(shí)驗(yàn):多鍵索引實(shí)驗(yàn):多鍵索引- 插入數(shù)據(jù)db.insert( comments : name : Luke, rating : 1.4 , name : Matt, rating : 5 , name : Sue, rating : 7 )- 創(chuàng)建索引db.createIndex(comm
20、ents:1 )db.blog.find( comments : name : Luke, rating : 1.4 ).explain(true)- 執(zhí)行計(jì)劃winningPlan : stage : FETCH, inputStage : stage : IXSCAN,keyPattern : comments : 1,indexName : comments_1, isMultiKey : true, multiKeyPaths : comments : comments”,isUnique : false, isSparse : false, isPartial : false, in
21、dexVersion : 2,實(shí)驗(yàn):多鍵索引- 插入數(shù)據(jù)db.insert( comments : name : Luke, rating : 1.4 , name : Matt, rating : 5 , name : Sue, rating : 7 )- 創(chuàng)建索引db.createIndex(“comments”.name:1 )db . find( : Luke ).explain(true )- 執(zhí)行計(jì)劃winningPlan : stage : FETCH, inputStage : stage : IXSCAN,keyPattern : comments
22、.name : 1,indexName : _1, isMultiKey : true, multiKeyPaths : : comments”,isUnique : false, isSparse : false, isPartial : false, indexVersion : 2實(shí)驗(yàn):多鍵索引- 插入數(shù)據(jù) a = _id :1001 , x : 1, 6 , _id :1002 , x : 2, 7 , “_id” :1003 , x : 3 , _id :1004 , x : 4 , _id :1005 , x : 5 db.in
23、sertMany(a)- 創(chuàng)建索引 db.array_sort.createIndex( x : 1 )- 利用多鍵索引正向排序 db.array_sort.find().sort(x:1) _id : 1001, x : 1, 6 _id : 1002, x : 2, 7 _id : 1003, x : 3 _id : 1004, x : 4 _id : 1005, x : 5 - 利用多鍵索引反向排序 db.colll.find().sort(x:-1) _id : 1002, x : 2, _id : 1001, x : 1, _id : 1005, x : _id : 1004, x
24、: _id : 1003, x : 76543 限制:多鍵索引不能同時(shí)對兩個(gè)或者是多個(gè)數(shù)組建立多鍵索引不能對數(shù)組進(jìn)行哈希片鍵不能是多鍵索引_id 索引不能是多鍵索引地理位置索引地理位置索引地理位置索引使用地理位置索引類別創(chuàng)建地理位置索引GeoJSON 對象地理位置索引- 創(chuàng)建索引db.geo_col.createIndex( location: “2d” , min:-20, max: 20 , bits: 10,collation:locale: simple )- 查詢db.geo_col.find( location : $geoWithin : $box : 1, 1 , 3, 3 )
25、- 查詢結(jié)果 _id : ObjectId(5c7e7a6243513eb45bf06125), location : 1, 1 _id : ObjectId(5c7e7a6643513eb45bf06126), location : 1, 2 _id : ObjectId(5c7e7a6943513eb45bf06127), location : 2, 2 _id : ObjectId(5c7e7a6d43513eb45bf06128), location : 2, 1 _id : ObjectId(5c7e7a7343513eb45bf06129), location : 3, 1 _id
26、 : ObjectId(5c7e7a7543513eb45bf0612a), location : 3, 2 _id : ObjectId(5c7e7a7743513eb45bf0612b), location : 3, 3 地理位置索引- 插入數(shù)據(jù)a = location :type: “Point” , coordinates: -73.97, 40.77 , name: Central Park, category : Parks”, location : type: Point, coordinates: -73.88, 40.78 , name: La Guardia Airport
27、, category : Airport ,location : 50, 50 db.insertMany(a)- 創(chuàng)建索引db.geo_2ds.creatIndex( location : “ 2dsphere ” )- 查詢db.find( : $near : $geometry : type : Point, coordinates : lng, lat ,$maxDistance : )地理位置索引關(guān)于Geo Json 對象- 點(diǎn) : type : Point,coordinates : , - 線 : type : ”LineString, coordinates : , , , -
28、 面 : type : Polygon,coordinates : , 全文索引全文索引全文索引的目的如何創(chuàng)建全文索引全文索引查詢查詢結(jié)果排序全文索引- 插入數(shù)據(jù)db.insert( _id: 1, content: “This morning I had a cup of coffee.”, about: “beverage”, keywords: “coffee” , _id: 2, content: Who doesnt like cake?, about: food, keywords: cake, food, dessert , _id: 3, content: Why need c
29、offee?, about: ”food, keywords: ”drink, food )- 創(chuàng)建索引 db.createIndex(content : “text” )- 查詢db.find( $text : $search : ”cup coffee like )db.find( $text : $search : “” a cup of coffee” )- 查詢排序db.find( $text : $search : ”coffee , textScore: $meta : textScore ).sort( textScore: $meta: textScore )索引屬性索引屬性TTL 索 引唯一索引部分索引哈希索引TTL 索引- 創(chuàng)建TTL索引 db.createIndex( field_name : 1,expireAfterSeconds : number )- TTL索引的限制不能建立在 _id field不能建立在 Capped Collection不支持 Compound index不能在原有fi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版地理八年級下冊8.1《自然特征與農(nóng)業(yè)》聽課評課記錄1
- 湘教版數(shù)學(xué)八年級上冊2.5《全等三角形及其性質(zhì)》聽評課記錄1
- 人教版數(shù)學(xué)九年級上冊聽評課記錄21.2.3《因式分解法》
- 生產(chǎn)設(shè)備技術(shù)轉(zhuǎn)讓協(xié)議書(2篇)
- 環(huán)保保潔服務(wù)協(xié)議書(2篇)
- 蘇科版數(shù)學(xué)七年級下冊12.3《互逆命題》聽評課記錄1
- 部編版八年級道德與法治下冊第四課《公民義務(wù)》第1課時(shí)《公民基本義務(wù)》聽課評課記錄
- 【部編人教版】八年級上冊歷史聽課評課記錄 第18課 從九一八事變到西安事變
- 浙教版數(shù)學(xué)七年級下冊1.3《平行線的判定》聽評課記錄2
- 2025年超低頻傳感器標(biāo)定系統(tǒng)合作協(xié)議書
- 北京市海淀區(qū)2024-2025學(xué)年八年級上學(xué)期期末考試數(shù)學(xué)試卷(含答案)
- 23G409先張法預(yù)應(yīng)力混凝土管樁
- 煤礦機(jī)電運(yùn)輸培訓(xùn)課件
- “德能勤績廉”考核測評表
- 三年級下冊口算天天100題(A4打印版)
- 鍋爐房危害告知卡
- 江西省農(nóng)村信用社(農(nóng)商銀行)
- 陳子性藏書卷七
- NPI流程管理分解
- 物業(yè)公司財(cái)務(wù)部各崗位工作職責(zé)
- 政務(wù)信息培訓(xùn)ppt課件
評論
0/150
提交評論