MongoDB數(shù)據(jù)庫(kù)索引用法詳解
一.索引詳講
索引是什么,索引就好比一本書的目錄,當(dāng)我們想找某一章節(jié)的時(shí)候,通過(guò)書籍的目錄可以很快的找到,所以適當(dāng)?shù)募尤胨饕梢蕴岣呶覀儾樵兊臄?shù)據(jù)的速度。
準(zhǔn)備工作,向MongoDB中插入20000條記錄,沒(méi)條記錄都有number和name
> for(var i = 0 ; i<200000 ;i++){ ... db.books.insert({number:i,name:"book"+i}) ... } WriteResult({ "nInserted" : 1 }) > db.books.find({},{_id:0}) { "number" : 0, "name" : "book0" } { "number" : 1, "name" : "book1" } { "number" : 2, "name" : "book2" } { "number" : 3, "name" : "book3" } { "number" : 4, "name" : "book4" } { "number" : 5, "name" : "book5" } { "number" : 6, "name" : "book6" } { "number" : 7, "name" : "book7" } …… >
1.對(duì)比加入索引和不加入索引的查詢效率
例:查詢number為65535的name
不使用索引的情況下,查詢時(shí)間請(qǐng)看millis
> db.books.find({number:65535},{_id:0,name:1}).explain() { "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 1, "nscannedObjects" : 200000, "nscanned" : 200000, "nscannedObjectsAllPlans" : 200000, "nscannedAllPlans" : 200000, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 1562, "nChunkSkips" : 0, "millis" : 172, "server" : "G08FNSTD131598:27017", "filterSet" : false } >
使用索引的情況下,先創(chuàng)建一個(gè)簡(jiǎn)單索引,用number建立一個(gè)索引
db.books.ensureIndex({number:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
> db.books.find({number:65535},{_id:0,name:1}).explain() { "cursor" : "BtreeCursor number_1", "isMultiKey" : false, "n" : 1, "nscannedObjects" : 1, "nscanned" : 1, "nscannedObjectsAllPlans" : 1, "nscannedAllPlans" : 1, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "number" : [ [ 65535, 65535 ] ] }, "server" : "G08FNSTD131598:27017", "filterSet" : false } >
從上面可以看到,查詢的時(shí)間上帶索引的情況要有明顯的縮短
2.從插入的數(shù)據(jù)的時(shí)間上進(jìn)行對(duì)比
準(zhǔn)備工作,刪除剛剛建立的books文檔
定義一個(gè)函數(shù),來(lái)完成記錄時(shí)間和插入數(shù)據(jù)的操作
> var time = function(){ ... var start = new Date(); ... for(var i = 0;i < 200000 ; i++){ ... db.books.insert({number:i,name:"book"+i}); ... } ... var end = new Date(); ... return end - start; ... }
不進(jìn)行添加索引的時(shí)候:
> var x = time(); > x 63057
創(chuàng)建索引
> db.books.ensureIndex({number:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
存在索引的時(shí)候的插入數(shù)據(jù)所用的時(shí)間
> var x = time(); > x 67223
可以看到不存在索引的時(shí)候,插入的數(shù)據(jù)所用的時(shí)間較短
綜上:當(dāng)我們對(duì)一個(gè)文檔需要進(jìn)行頻繁的插入操作的時(shí)候,建立不巧當(dāng)?shù)乃饕龝?huì)導(dǎo)致插入效率的降低。
3.建立索引需要注意的地方
創(chuàng)建索引的時(shí)候注意1是正序創(chuàng)建索引-1是倒序創(chuàng)建索引
索引的創(chuàng)建在提高查詢性能的同事會(huì)影響插入的性能
對(duì)于經(jīng)常查詢少插入的文檔可以考慮用索引
符合索引要注意索引的先后順序
每個(gè)鍵全建立索引不一定就能提高性能呢,索引不是萬(wàn)能的
在做排序工作的時(shí)候如果是超大數(shù)據(jù)量也可以考慮加上索引用來(lái)提高排序的性能
4.詳細(xì)介紹索引的創(chuàng)建
①在創(chuàng)建索引的時(shí)候,使用了ensureIndex()這個(gè)方法,使用它會(huì)創(chuàng)建索引,名字就是鍵的名字加上一個(gè)數(shù)字,例如number_1或者number_-1,其中1代表是正序索引,-1代表逆序索引
②如果覺(jué)得1或-1比較不容易記,還可以使用自定義名字來(lái)創(chuàng)建索引
> db.books.ensureIndex({name:1},{name:"bookNameIndex"}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }
③一個(gè)文檔建立了多個(gè)索引,但是我又想強(qiáng)制使用其中的一個(gè)索引,怎么辦
例如,我在上面的文檔中對(duì)number建立了逆序索引,對(duì)name建立了正序索引,現(xiàn)在我想查找的時(shí)候用name進(jìn)行索引,我應(yīng)該這么寫:
> db.books.find({name:"book2016"},{_id:0}).hint({name:1}) { "number" : 2016, "name" : "book2016" } { "number" : 2016, "name" : "book2016" } >
如果使用了沒(méi)有創(chuàng)建的索引,那么會(huì)返回一個(gè)“bad hint”的錯(cuò)誤。
④查看所用的索引和查詢數(shù)據(jù)狀態(tài)信息,可以使用explain()方法
> db.books.find({name:"book2016"},{_id:0}).hint({name:1}).explain() { "cursor" : "BtreeCursor bookNameIndex", "isMultiKey" : false, "n" : 2, "nscannedObjects" : 2, "nscanned" : 2, "nscannedObjectsAllPlans" : 2, "nscannedAllPlans" : 2, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "name" : [ [ "book2016", "book2016" ] ] }, "server" : "G08FNSTD131598:27017", "filterSet" : false }
上面看到,我們的索引的名字是bookNameIndex,并且millis是0,nscanned是查到了幾個(gè)文檔
⑤在關(guān)系型數(shù)據(jù)庫(kù)中嘗嘗會(huì)有約束條件,比較常用的就是唯一性,在MongoDB中也可以指定唯一
建立唯一索引:db.books.ensureIndex({name:-1},{unique:true})
上面我通過(guò)有索引和無(wú)索引插入了兩組完全一樣的數(shù)據(jù),此時(shí)如果去建立唯一的索引,那么就會(huì)出錯(cuò)
> db.books.ensureIndex({name:1},{unique:true}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "ok" : 0, "errmsg" : "E11000 duplicate key error index: mongoDBTest.books.$name_1 dup key: { : \"book0\" }", "code" : 11000 }
此時(shí)可以通過(guò)dropDups:true屬性來(lái)進(jìn)行刪除重復(fù)的數(shù)據(jù)
> db.books.ensureIndex({name:1},{unique:true,dropDups:true}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } >
刪除重復(fù)之后,再去加入一個(gè)相同名字的數(shù)據(jù),就會(huì)出現(xiàn)下面的情況
> db.books.insert({number:1,name:"book1"}) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicat e key error index: mongoDBTest.books.$name_1 dup key: { : \"book1\" }" } }) >
⑥刪除索引
指定要?jiǎng)h除的索引
db.runCommand({dropIndexes : ”books” , index:”name_-1”})
刪除所有的索引
db.runCommand({dropIndexes : ”books” , index:”*”})
注意:索引的創(chuàng)建時(shí)同步的,所以如果想指定異步的去創(chuàng)建索引,就要指定在后臺(tái)去創(chuàng)建
db.books.ensureIndex({name:-1},{background:true})
二.空間索引
2D索引,舉例在一片區(qū)域中建立坐標(biāo)系,那么很多地點(diǎn)可以看做是一個(gè)個(gè)的坐標(biāo),此時(shí)2d索引就可以幫助我們進(jìn)行快速的查詢某一個(gè)范圍的地點(diǎn)了。
例:我在MongoDB中建立一個(gè)擁有很多坐標(biāo)點(diǎn)的文檔
> db.map.find({},{_id:0}) { "gis" : { "x" : 185, "y" : 150 } } { "gis" : { "x" : 70, "y" : 180 } } { "gis" : { "x" : 75, "y" : 180 } } { "gis" : { "x" : 185, "y" : 185 } } { "gis" : { "x" : 65, "y" : 185 } } { "gis" : { "x" : 50, "y" : 50 } } { "gis" : { "x" : 50, "y" : 100 } } { "gis" : { "x" : 60, "y" : 55 } } { "gis" : { "x" : 65, "y" : 80 } } { "gis" : { "x" : 55, "y" : 80 } } { "gis" : { "x" : 0, "y" : 0 } } { "gis" : { "x" : 0, "y" : 200 } } { "gis" : { "x" : 200, "y" : 0 } } { "gis" : { "x" : 200, "y" : 200 } } >
添加一個(gè)2D索引
db.map.ensureIndex({"gis":"2d"},{min:-1,max:201})
默認(rèn)會(huì)建立一個(gè)[-180,180]之間的2D索引
例子:
①查詢點(diǎn)(70,180)最近的3個(gè)點(diǎn)
> db.map.find({"gis":{$near:[70,180]}},{gis:1,_id:0}).limit(3) { "gis" : { "x" : 70, "y" : 180 } } { "gis" : { "x" : 75, "y" : 180 } } { "gis" : { "x" : 65, "y" : 185 } }
②查詢以點(diǎn)(50,50)和點(diǎn)(190,190)為對(duì)角線的正方形中的所有的點(diǎn)
> db.map.find({gis:{$within:{$box:[[50,50],[190,190]]}}},{_id:0,gis:1}) { "gis" : { "x" : 185, "y" : 150 } } { "gis" : { "x" : 75, "y" : 180 } } { "gis" : { "x" : 70, "y" : 180 } } { "gis" : { "x" : 65, "y" : 185 } } { "gis" : { "x" : 50, "y" : 100 } } { "gis" : { "x" : 65, "y" : 80 } } { "gis" : { "x" : 55, "y" : 80 } } { "gis" : { "x" : 60, "y" : 55 } } { "gis" : { "x" : 50, "y" : 50 } } { "gis" : { "x" : 185, "y" : 185 } } >
③查詢出以圓心為(56,80)半徑為50規(guī)則下的圓心面積中的點(diǎn)
> db.map.find({gis:{$within:{$center:[[56,80],50]}}},{_id:0,gis:1}) { "gis" : { "x" : 55, "y" : 80 } } { "gis" : { "x" : 50, "y" : 100 } } { "gis" : { "x" : 50, "y" : 50 } } { "gis" : { "x" : 60, "y" : 55 } } { "gis" : { "x" : 65, "y" : 80 } }
到此這篇關(guān)于MongoDB數(shù)據(jù)庫(kù)索引用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MongoDB中sort()排序方法、aggregate()聚合方法和索引代碼示例
這篇文章主要給大家介紹了關(guān)于MongoDB中sort()排序方法、aggregate()聚合方法和索引的相關(guān)資料,MongoDB的聚合函數(shù)Aggregate是一組用于對(duì)MongoDB中的數(shù)據(jù)集進(jìn)行聚合操作的函數(shù),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04MongoDB orm框架的注意事項(xiàng)及簡(jiǎn)單使用
MongoDB官方提供的orm的輕量級(jí)封裝的 zfoo orm 框架,只對(duì)官方提供的進(jìn)行了簡(jiǎn)單的封裝,還做了一些官方不支持的語(yǔ)法校驗(yàn)。本文介紹了該框架的注意事項(xiàng)及簡(jiǎn)單使用2021-06-06mongodb命令行連接及基礎(chǔ)命令總結(jié)大全
大家可能平時(shí)在開發(fā)過(guò)程中都使用客戶端工具來(lái)連接和查詢mongodb,但是一般生產(chǎn)當(dāng)中的數(shù)據(jù)庫(kù)是不允許本地客戶端連接的,下面這篇文章主要給大家介紹了關(guān)于mongodb命令行連接及基礎(chǔ)命令總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-04-04關(guān)于MongoDB數(shù)據(jù)庫(kù)核心概念
這篇文章主要介紹了關(guān)于MongoDB數(shù)據(jù)庫(kù)核心概念,MongoDB由C++語(yǔ)言編寫,是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫(kù)系統(tǒng),需要的朋友可以參考下2023-04-04