人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

MongoDB數(shù)據(jù)庫索引用法詳解

發(fā)布日期:2022-07-15 19:52 | 文章來源:gibhub

一.索引詳講

索引是什么,索引就好比一本書的目錄,當(dāng)我們想找某一章節(jié)的時候,通過書籍的目錄可以很快的找到,所以適當(dāng)?shù)募尤胨饕梢蕴岣呶覀儾樵兊臄?shù)據(jù)的速度。

準(zhǔn)備工作,向MongoDB中插入20000條記錄,沒條記錄都有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.對比加入索引和不加入索引的查詢效率

例:查詢number為65535的name

不使用索引的情況下,查詢時間請看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)建一個簡單索引,用number建立一個索引

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
}
>

從上面可以看到,查詢的時間上帶索引的情況要有明顯的縮短

2.從插入的數(shù)據(jù)的時間上進行對比

準(zhǔn)備工作,刪除剛剛建立的books文檔

定義一個函數(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;
... }

不進行添加索引的時候:

> var x = time();
> x
63057

創(chuàng)建索引

> db.books.ensureIndex({number:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

存在索引的時候的插入數(shù)據(jù)所用的時間

> var x = time();
> x
67223

可以看到不存在索引的時候,插入的數(shù)據(jù)所用的時間較短

綜上:當(dāng)我們對一個文檔需要進行頻繁的插入操作的時候,建立不巧當(dāng)?shù)乃饕龝?dǎo)致插入效率的降低。

3.建立索引需要注意的地方

創(chuàng)建索引的時候注意1是正序創(chuàng)建索引-1是倒序創(chuàng)建索引

索引的創(chuàng)建在提高查詢性能的同事會影響插入的性能

對于經(jīng)常查詢少插入的文檔可以考慮用索引

符合索引要注意索引的先后順序

每個鍵全建立索引不一定就能提高性能呢,索引不是萬能的

在做排序工作的時候如果是超大數(shù)據(jù)量也可以考慮加上索引用來提高排序的性能

4.詳細介紹索引的創(chuàng)建

①在創(chuàng)建索引的時候,使用了ensureIndex()這個方法,使用它會創(chuàng)建索引,名字就是鍵的名字加上一個數(shù)字,例如number_1或者number_-1,其中1代表是正序索引,-1代表逆序索引

②如果覺得1或-1比較不容易記,還可以使用自定義名字來創(chuàng)建索引

> db.books.ensureIndex({name:1},{name:"bookNameIndex"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}

③一個文檔建立了多個索引,但是我又想強制使用其中的一個索引,怎么辦

例如,我在上面的文檔中對number建立了逆序索引,對name建立了正序索引,現(xiàn)在我想查找的時候用name進行索引,我應(yīng)該這么寫:

> db.books.find({name:"book2016"},{_id:0}).hint({name:1})
{ "number" : 2016, "name" : "book2016" }
{ "number" : 2016, "name" : "book2016" }
>

如果使用了沒有創(chuàng)建的索引,那么會返回一個“bad hint”的錯誤。

④查看所用的索引和查詢數(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是查到了幾個文檔

⑤在關(guān)系型數(shù)據(jù)庫中嘗嘗會有約束條件,比較常用的就是唯一性,在MongoDB中也可以指定唯一

建立唯一索引:db.books.ensureIndex({name:-1},{unique:true})

上面我通過有索引和無索引插入了兩組完全一樣的數(shù)據(jù),此時如果去建立唯一的索引,那么就會出錯

> 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
}

此時可以通過dropDups:true屬性來進行刪除重復(fù)的數(shù)據(jù)

> db.books.ensureIndex({name:1},{unique:true,dropDups:true})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
>

刪除重復(fù)之后,再去加入一個相同名字的數(shù)據(jù),就會出現(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\" }"
        }
})
>

⑥刪除索引

指定要刪除的索引

db.runCommand({dropIndexes : ”books” , index:”name_-1”})

刪除所有的索引

db.runCommand({dropIndexes : ”books” , index:”*”})

注意:索引的創(chuàng)建時同步的,所以如果想指定異步的去創(chuàng)建索引,就要指定在后臺去創(chuàng)建

db.books.ensureIndex({name:-1},{background:true})

二.空間索引

2D索引,舉例在一片區(qū)域中建立坐標(biāo)系,那么很多地點可以看做是一個個的坐標(biāo),此時2d索引就可以幫助我們進行快速的查詢某一個范圍的地點了。

例:我在MongoDB中建立一個擁有很多坐標(biāo)點的文檔

> 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 } }
>

添加一個2D索引

db.map.ensureIndex({"gis":"2d"},{min:-1,max:201})

默認會建立一個[-180,180]之間的2D索引

例子:

①查詢點(70,180)最近的3個點

> 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 } }

②查詢以點(50,50)和點(190,190)為對角線的正方形中的所有的點

> 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ī)則下的圓心面積中的點

> 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ù)庫索引用法的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。

海外穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部