iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mongodb数据库中怎么使用索引
  • 907
分享到

mongodb数据库中怎么使用索引

2024-04-02 19:04:59 907人浏览 独家记忆
摘要

这篇文章将为大家详细讲解有关mongoDB数据库中怎么使用索引,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。      &nb

这篇文章将为大家详细讲解有关mongoDB数据库中怎么使用索引,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

        在关系型数据库中索引可以加快对数据输出,非关系型数据库也是如此,可以减小磁盘io访问,对大数据量有显著的效果。目前monGodb支持B-Tree,unique,sparse,hash索引

在mongodb集群中生成数据

[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字段上创建索引

wuhan:PRIMARY> db.stu.find({sn:50}).explain();   --查看执行计划是否走索引

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

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

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

[
{
"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"})    --创建多列索引

{
"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"})     --删除其中一个索引

{
"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"}})    --写入数据

WriteResult({ "nInserted" : 1 })

wuhan:PRIMARY> db.shop.insert({name:"sanxing",spc:{weight:100,area:"hanguo"}})  --写入数据

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

wuhan:PRIMARY> db.shop.find({"spc.area":"hanguo"});     --查询子文档的数据

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

wuhan:PRIMARY> db.shop.ensureIndex({"spc.area":1});     --子文档创建索引

{
"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> 

三.稀疏索引(字段有值就创建索引,没有值不创建索引)

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({})          --插入一个空值

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});     --创建稀疏索引

{
"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> 

五.索引重建(当索引效率不高时可以考虑重建索引)

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

关于mongodb数据库中怎么使用索引就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

您可能感兴趣的文档:

--结束END--

本文标题: mongodb数据库中怎么使用索引

本文链接: https://www.lsjlt.com/news/56479.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-14
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-14
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-14
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-14
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-14
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-14
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-14
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-14
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作