溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MongoDB 索引簡(jiǎn)單使用技巧

發(fā)布時(shí)間:2020-07-30 04:34:22 來(lái)源:網(wǎng)絡(luò) 閱讀:572 作者:raygtr 欄目:MongoDB數(shù)據(jù)庫(kù)

先在數(shù)據(jù)庫(kù)上增加一些數(shù)據(jù),輸入下面的命令:

for(var i=1;i<10;i++) db.customers.insert({name:"jordan"+i,country:"American"})
for(var i=1;i<10;i++) db.customers.insert({name:"gaga"+i,country:"American"})
for(var i=1;i<10;i++) db.customers.insert({name:"ham"+i,country:"UK"})
for(var i=1;i<10;i++) db.customers.insert({name:"brown"+i,country:"UK"})
for(var i=1;i<10;i++) db.customers.insert({name:"ramda"+i,country:"Malaysia"})

使用下面的命令查看當(dāng)前數(shù)據(jù)庫(kù)并不存在索引(_id除外)

db.system.indexes.find()


現(xiàn)在在name字段增加一列索引,索引語(yǔ)法:

db.collection.ensureIndex(keys,options)

keys是一個(gè)document,包含要增加索引的字段和索引的排序方向;option是可選參數(shù),控制索引的創(chuàng)建排序方式。具體命令如下:

db.customers.ensureIndex({name:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

利用indexes.find()可以查詢到剛剛建立好的索引。如果建立唯一索引可以使用如下命令:

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

(此文使用唯一索引)

查看數(shù)據(jù)是否使用索引:

db.customers.find({name:"ramda9"}).explain()
{
    "cursor" : "BtreeCursor name_1",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 1,
    "nscannedAllPlans" : 1,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "name" : [
            [
                "ramda9",
                "ramda9"
            ]
        ]
    },
    "server" : "localhost.localdomain:27017",
    "filterSet" : false
}

從粗體字的"nscannedObjects"看出查詢過(guò)程中掃描的總文檔數(shù)是使用了索引?,F(xiàn)在再次查詢country字段:

db.customers.find().count()
45

db.customers.find({country:"Malaysia"}).explain()
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 9,
    "nscannedObjects" : 45,
    "nscanned" : 45,
    "nscannedObjectsAllPlans" : 45,
    "nscannedAllPlans" : 45,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "server" : "localhost.localdomain:27017",
    "filterSet" : false
}

可以看出這是使用了全表掃描,并沒有使用到索引。


 建立復(fù)合索引:

  現(xiàn)在為country建立一個(gè)索引:

 for(var i=1;i<10;i++) db.customers.insert({name:"lanbo"+i,country:"Malaysia"})

查詢索引情況:

db.customers.find({country:"Malaysia"}).explain()

結(jié)果是全表掃描。現(xiàn)在嘗試在country上建立一個(gè)普通索引:

db.customers.ensureIndex({country:1})

重新再次執(zhí)行explain語(yǔ)句:

db.customers.find({country:"Malaysia"}).explain()
{
    "cursor" : "BtreeCursor country_1",
    "isMultiKey" : false,
    "n" : 18,
    "nscannedObjects" : 18,
    "nscanned" : 18,
    "nscannedObjectsAllPlans" : 18,
    "nscannedAllPlans" : 18,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "country" : [
            [
                "Malaysia",
                "Malaysia"
            ]
        ]
    },
    "server" : "localhost.localdomain:27017",
    "filterSet" : false
}

使用了索引并且查詢到18條記錄?,F(xiàn)在創(chuàng)建一個(gè)復(fù)合索引:

db.customers.ensureIndex({name:1,coutry:1})

db.customers.find({name:"lanbo2",country:"Malaysia"}).explain()
{
    "cursor" : "BtreeCursor name_1_coutry_1",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 3,
    "nscannedAllPlans" : 3,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "name" : [
            [
                "lanbo2",
                "lanbo2"
            ]
        ],
        "coutry" : [
            [
                {
                    "$minElement" : 1
                },
                {
                    "$maxElement" : 1
                }
            ]
        ]
    },
    "server" : "localhost.localdomain:27017",
    "filterSet" : false
}


此處使用了name與country的復(fù)合索引。(要?jiǎng)h除了name的唯一索引才可以,db.customers.dropIndex("name_1"))


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI