欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年07月08日 16:27:05   作者:奮斗的大橙子  
本文詳細講解了MongoDB數(shù)據(jù)庫索引的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一.索引詳講

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

準備工作,向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ù)的時間上進行對比

準備工作,刪除剛剛建立的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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c#操作mongodb插入數(shù)據(jù)效率

    c#操作mongodb插入數(shù)據(jù)效率

    今天小編就為大家分享一篇關(guān)于c#操作mongodb插入數(shù)據(jù)效率,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Mongodb 用戶權(quán)限管理及配置詳解

    Mongodb 用戶權(quán)限管理及配置詳解

    這篇文章主要介紹了Mongodb 用戶權(quán)限管理及配置詳解,包括Mongodb訪問控制配置,Mongodb開啟權(quán)限驗證,Mongodb賬戶創(chuàng)建,Mongodb賬戶管理命令需要的朋友可以參考下
    2023-01-01
  • MongoDB教程之索引介紹

    MongoDB教程之索引介紹

    這篇文章主要介紹了MongoDB教程之索引介紹,本文講解了索引基礎(chǔ)、唯一索引、使用explain、索引管理等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • MongoDB中sort()排序方法、aggregate()聚合方法和索引代碼示例

    MongoDB中sort()排序方法、aggregate()聚合方法和索引代碼示例

    這篇文章主要給大家介紹了關(guān)于MongoDB中sort()排序方法、aggregate()聚合方法和索引的相關(guān)資料,MongoDB的聚合函數(shù)Aggregate是一組用于對MongoDB中的數(shù)據(jù)集進行聚合操作的函數(shù),文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-04-04
  • MongoDB orm框架的注意事項及簡單使用

    MongoDB orm框架的注意事項及簡單使用

    MongoDB官方提供的orm的輕量級封裝的 zfoo orm 框架,只對官方提供的進行了簡單的封裝,還做了一些官方不支持的語法校驗。本文介紹了該框架的注意事項及簡單使用
    2021-06-06
  • MongoDB設(shè)計方法以及技巧示例詳解

    MongoDB設(shè)計方法以及技巧示例詳解

    這篇文章主要給大家介紹了關(guān)于MongoDB設(shè)計方法以及技巧的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MongoDB具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • mongodb命令行連接及基礎(chǔ)命令總結(jié)大全

    mongodb命令行連接及基礎(chǔ)命令總結(jié)大全

    大家可能平時在開發(fā)過程中都使用客戶端工具來連接和查詢mongodb,但是一般生產(chǎn)當(dāng)中的數(shù)據(jù)庫是不允許本地客戶端連接的,下面這篇文章主要給大家介紹了關(guān)于mongodb命令行連接及基礎(chǔ)命令總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • MongoDB副本集遷移實操案例詳解

    MongoDB副本集遷移實操案例詳解

    文中詳細闡述了通過全量?+?增量?Oplog?的遷移方式,完成一套副本集?MongoDB?遷移的全過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • 關(guān)于MongoDB數(shù)據(jù)庫核心概念

    關(guān)于MongoDB數(shù)據(jù)庫核心概念

    這篇文章主要介紹了關(guān)于MongoDB數(shù)據(jù)庫核心概念,MongoDB由C++語言編寫,是一個基于分布式文件存儲的開源數(shù)據(jù)庫系統(tǒng),需要的朋友可以參考下
    2023-04-04
  • windows下mongodb安裝與使用圖文教程(整理)

    windows下mongodb安裝與使用圖文教程(整理)

    這篇文章主要介紹了windows下mongodb安裝與使用圖文教程(整理)的相關(guān)資料,需要的朋友可以參考下
    2016-06-06

最新評論