欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

mongodb數(shù)據(jù)庫(kù)中怎么使用索引

這篇文章將為大家詳細(xì)講解有關(guān)MongoDB數(shù)據(jù)庫(kù)中怎么使用索引,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

成都創(chuàng)新互聯(lián)公司主營(yíng)泉州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),泉州h5成都小程序開(kāi)發(fā)搭建,泉州網(wǎng)站營(yíng)銷(xiāo)推廣歡迎泉州等地區(qū)企業(yè)咨詢

        在關(guān)系型數(shù)據(jù)庫(kù)中索引可以加快對(duì)數(shù)據(jù)輸出,非關(guān)系型數(shù)據(jù)庫(kù)也是如此,可以減小磁盤(pán)IO訪問(wèn),對(duì)大數(shù)據(jù)量有顯著的效果。目前mongodb支持B-Tree,unique,sparse,hash索引

在mongodb集群中生成數(shù)據(jù)

[root@node2 mongodb-4.0.8]# ./bin/mongo --host 172.16.8.24 --port 27017

MongoDB shell version v4.0.8

connecting to: mongodb://172.16.8.24:27017/?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("a1abbd3a-fe32-46ac-a959-4f8a62abd990") }

MongoDB server version: 4.0.8

Server has startup warnings:

wuhan:PRIMARY> for (var i=1;i<=1000;i++) { db.stu.insert({sn:i,name:"student"+i})}

WriteResult({ "nInserted" : 1 })

wuhan:PRIMARY> db.stu.find().count();

1000

wuhan:PRIMARY> db.stu.find();

{ "_id" : ObjectId("5ca3004015fc3dad4a419a75"), "sn" : 1, "name" : "student1" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a76"), "sn" : 2, "name" : "student2" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a77"), "sn" : 3, "name" : "student3" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a78"), "sn" : 4, "name" : "student4" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a79"), "sn" : 5, "name" : "student5" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a7a"), "sn" : 6, "name" : "student6" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a7b"), "sn" : 7, "name" : "student7" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a7c"), "sn" : 8, "name" : "student8" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a7d"), "sn" : 9, "name" : "student9" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a7e"), "sn" : 10, "name" : "student10" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a7f"), "sn" : 11, "name" : "student11" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a80"), "sn" : 12, "name" : "student12" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a81"), "sn" : 13, "name" : "student13" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a82"), "sn" : 14, "name" : "student14" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a83"), "sn" : 15, "name" : "student15" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a84"), "sn" : 16, "name" : "student16" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a85"), "sn" : 17, "name" : "student17" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a86"), "sn" : 18, "name" : "student18" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a87"), "sn" : 19, "name" : "student19" }

{ "_id" : ObjectId("5ca3004015fc3dad4a419a88"), "sn" : 20, "name" : "student20" }

Type "it" for more

wuhan:PRIMARY> 

一.B-Tree索引的使用

1.單列索引使用

wuhan:PRIMARY> db.stu.ensureIndex({sn:1})       --在sn字段上創(chuàng)建索引

wuhan:PRIMARY> db.stu.find({sn:50}).explain();   --查看執(zhí)行計(jì)劃是否走索引

wuhan:PRIMARY> db.stu.getIndexKeys()              --查看表中有多少鍵

[ { "_id" : 1 }, { "sn" : 1 } ]

wuhan:PRIMARY> db.stu.getIndexes()           --查看一個(gè)表所有索引

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "tong.stu"
},
{
"v" : 2,
"key" : {
"sn" : 1
},
"name" : "sn_1",
"ns" : "tong.stu"
}
]

wuhan:PRIMARY> db.stu.dropIndex({sn:1});          --刪除sn字段的索引

wuhan:PRIMARY> db.stu.dropIndexes();               --刪除所有索引

2.多列索引使用

wuhan:PRIMARY> db.stu.ensureIndex({name:1},{name:"IX_name"})    --創(chuàng)建多列索引

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"operationTime" : Timestamp(1554188377, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1554188377, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

wuhan:PRIMARY> db.stu.getIndexes()     --查看索引信息

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "tong.stu"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "IX_name",
"ns" : "tong.stu"
}
]

wuhan:PRIMARY> db.stu.dropIndex({name:"IX_name"})     --刪除其中一個(gè)索引

{
"operationTime" : Timestamp(1554188499, 1),
"ok" : 0,
"errmsg" : "can't find index with key: { name: \"IX_name\" }",
"code" : 27,
"codeName" : "IndexNotFound",
"$clusterTime" : {
"clusterTime" : Timestamp(1554188499, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

3.子文檔索引使用

wuhan:PRIMARY> db.shop.insert({name:"Nokia",spc:{weight:120,area:"taiwan"}})    --寫(xiě)入數(shù)據(jù)

WriteResult({ "nInserted" : 1 })

wuhan:PRIMARY> db.shop.insert({name:"sanxing",spc:{weight:100,area:"hanguo"}})  --寫(xiě)入數(shù)據(jù)

WriteResult({ "nInserted" : 1 })

wuhan:PRIMARY> db.shop.find()       --查詢數(shù)據(jù)

{ "_id" : ObjectId("5ca337ff15fc3dad4a419e5d"), "name" : "Nokia", "spc" : { "weight" : 120, "area" : "taiwan" } }

{ "_id" : ObjectId("5ca3382c15fc3dad4a419e5e"), "name" : "sanxing", "spc" : { "weight" : 100, "area" : "hanguo" } }

wuhan:PRIMARY> db.shop.find({"spc.area":"hanguo"});     --查詢子文檔的數(shù)據(jù)

{ "_id" : ObjectId("5ca3382c15fc3dad4a419e5e"), "name" : "sanxing", "spc" : { "weight" : 100, "area" : "hanguo" } }

wuhan:PRIMARY> db.shop.ensureIndex({"spc.area":1});     --子文檔創(chuàng)建索引

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"operationTime" : Timestamp(1554200928, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1554200928, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

wuhan:PRIMARY> db.shop.getIndexes();

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "tong.shop"
},
{
"v" : 2,
"key" : {
"spc.area" : 1
},
"name" : "spc.area_1",
"ns" : "tong.shop"
}
]

wuhan:PRIMARY>

二.唯一索引(唯一索引中字段值必須是唯一的)

wuhan:PRIMARY> db.stu.ensureIndex({name:1},{unique:true})

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"operationTime" : Timestamp(1554188549, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1554188549, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

wuhan:PRIMARY> db.stu.getIndexes()

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "tong.stu"
},
{
"v" : 2,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "tong.stu"
}
]

wuhan:PRIMARY> db.stu.totalIndexSize()     --查看索引大小

40960

wuhan:PRIMARY> db.stu.totalSize()

69632

wuhan:PRIMARY> 

三.稀疏索引(字段有值就創(chuàng)建索引,沒(méi)有值不創(chuàng)建索引)

wuhan:PRIMARY> db.shop.find()

{ "_id" : ObjectId("5ca337ff15fc3dad4a419e5d"), "name" : "Nokia", "spc" : { "weight" : 120, "area" : "taiwan" } }

{ "_id" : ObjectId("5ca3382c15fc3dad4a419e5e"), "name" : "sanxing", "spc" : { "weight" : 100, "area" : "hanguo" } }

wuhan:PRIMARY> db.shop.insert({})          --插入一個(gè)空值

WriteResult({ "nInserted" : 1 })

wuhan:PRIMARY> db.shop.find();         

{ "_id" : ObjectId("5ca337ff15fc3dad4a419e5d"), "name" : "Nokia", "spc" : { "weight" : 120, "area" : "taiwan" } }

{ "_id" : ObjectId("5ca3382c15fc3dad4a419e5e"), "name" : "sanxing", "spc" : { "weight" : 100, "area" : "hanguo" } }

{ "_id" : ObjectId("5ca3419c15fc3dad4a419e5f") }

wuhan:PRIMARY> db.shop.ensureIndex({name:1},{sparse:true});     --創(chuàng)建稀疏索引

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"operationTime" : Timestamp(1554203093, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1554203093, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

wuhan:PRIMARY> db.shop.find({name:"null"});            --null值不顯示

wuhan:PRIMARY> 

四.哈稀索引

wuhan:PRIMARY> db.t.ensureIndex({a:"hashed"});

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"operationTime" : Timestamp(1554203661, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1554203661, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

wuhan:PRIMARY> db.t.find({a:"25"}).explain();

{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "tong.t",
"indexFilterSet" : false,
"parsedQuery" : {
"a" : {
"$eq" : "25"
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"a" : {
"$eq" : "25"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : "hashed"
},
"indexName" : "a_hashed",        --顯示為hash索引
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[7200060250846542811, 7200060250846542811]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "node2",
"port" : 27017,
"version" : "4.0.8",
"gitVersion" : "9b00696ed75f65e1ebc8d635593bed79b290cfbb"
},
"ok" : 1,
"operationTime" : Timestamp(1554203690, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1554203690, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

wuhan:PRIMARY> 

五.索引重建(當(dāng)索引效率不高時(shí)可以考慮重建索引)

wuhan:PRIMARY> db.t.reIndex();            --重建t表的索引

關(guān)于mongodb數(shù)據(jù)庫(kù)中怎么使用索引就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享文章:mongodb數(shù)據(jù)庫(kù)中怎么使用索引
分享路徑:http://aaarwkj.com/article34/ipocpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
国产又大又黄又粗的黄色| 日韩性生活视频免费播放| 可以免费看黄的网久久| 日韩精品视频性色首页| 精品人妻一区三区蜜桃| 射精视频在线观看免费| 亚洲一区二区精品免费视频| 黄色亚洲日本欧美在线观看| 国产免费很黄很色视频| 日韩欧美一区二区三区| 亚洲欧美激情国产综合久久| 亚洲欧美日韩老汉影院| 熟女高潮av一区二区| 伊人婷婷综合激情网| 夫妻过性生活视频播放| 国产亚洲精品久在线| 亚洲一区在线观看激情| 国产一级性生活高清在线| 在线看电影亚洲一区| 成人性生交免大片免费| 狼人综合狼人综合网站| 亚洲精品日本一区二区| 黄色大全欧美在线观看| 久久中文人妻丝袜不卡| 国产亚洲超级97免费视频| 午夜黄色福利在线观看| 97在线观看视频免费| 国产综合永久精品日韩鬼片| 欧美性极品少妇精品网站| 亚洲乱码日韩电影网站| 亚洲精品国产精品乱码不卞| 国产视频一区2区三区| 日韩亚洲中文一区三级黄片| 三级日本一区二区三区| 91久久精品人妻一区二区| 国产农村妇女一区二区三区| 久久精品夜夜夜夜夜久久| 精品久久亚洲一区二区欧美| 国产女主播在线观看一区| 青青草免费公开视频久久| 国产三级网站在线观看播放|