索引

在认识索引的之前我们先建立一张表,并往其中插入200万条数据。

// test.js
//生成随机数
function GetRandomNum(min,max){let range = max-min;   //得到随机数区间let rand = Math.random(); //得到随机值return (min + Math.round(rand *range)); //最小值+随机数取整
}//console.log(GetRandomNum(10000,99999));//生成随机用户名
function GetRadomUserName(min,max){let tempStringArray= "123456789qwertyuiopasdfghjklzxcvbnm".split("");//构造生成时的字母库数组let outPuttext = ""; //最后输出的变量//进行循环,随机生产用户名的长度,这里需要生成随机数方法的配合for(let i=1 ;i<GetRandomNum(min,max);i++){//随机抽取字母,拼装成需要的用户名outPuttext=outPuttext+tempStringArray[GetRandomNum(0,tempStringArray.length)]}return outPuttext;
}var db = connect('company');
db.randomInfo.drop();
var  tempInfo = [];
for (let i=0;i<2000000;i++){tempInfo.push({username:GetRadomUserName(7,16),regeditTime:new Date(),randNum0:GetRandomNum(100000,999999),randNum1:GetRandomNum(100000,999999),randNum2:GetRandomNum(100000,999999),randNum3:GetRandomNum(100000,999999),randNum4:GetRandomNum(100000,999999),randNum5:GetRandomNum(100000,999999),randNum6:GetRandomNum(100000,999999),randNum7:GetRandomNum(100000,999999),randNum8:GetRandomNum(100000,999999),randNum8:GetRandomNum(100000,999999),})
}db.randomInfo.insert(tempInfo);
> mongo
> load("./test.js")
connecting to: mongodb://127.0.0.1:27017/company
MongoDB server version: 3.4.10
···
// 这个过程可能需要2分钟左右> use company
switched to db company
> db.randomInfo.stats()            // 使用这个查看插入了几条数据
{"ns" : "company.randomInfo","size" : 421908971,"count" : 1835000,"avgObjSize" : 229,"storageSize" : 188686336,"capped" : false,"wiredTiger" : {"metadata" : {"formatVersion" : 1},"creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=true),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u","type" : "file","uri" : "statistics:table:collection-0-5869292382622143333","LSM" : {"bloom filter false positives" : 0,"bloom filter hits" : 0,"bloom filter misses" : 0,"bloom filter pages evicted from cache" : 0,"bloom filter pages read into cache" : 0,"bloom filters in the LSM tree" : 0,"chunks in the LSM tree" : 0,"highest merge generation in the LSM tree" : 0,"queries that could have benefited from a Bloom filter that did not exist" : 0,"sleep for LSM checkpoint throttle" : 0,"sleep for LSM merge throttle" : 0,"total size of bloom filters" : 0},"block-manager" : {"allocations requiring file extension" : 15471,"blocks allocated" : 15475,"blocks freed" : 39,"checkpoint size" : 188481536,"file allocation unit size" : 4096,"file bytes available for reuse" : 188416,"file magic number" : 120897,"file major version number" : 1,"file size in bytes" : 188686336,"minor version number" : 0},"btree" : {"btree checkpoint generation" : 20,"column-store fixed-size leaf pages" : 0,"column-store internal pages" : 0,"column-store variable-size RLE encoded values" : 0,"column-store variable-size deleted values" : 0,"column-store variable-size leaf pages" : 0,"fixed-record size" : 0,"maximum internal page key size" : 368,"maximum internal page size" : 4096,"maximum leaf page key size" : 2867,"maximum leaf page size" : 32768,"maximum leaf page value size" : 67108864,"maximum tree depth" : 4,"number of key/value pairs" : 0,"overflow pages" : 0,"pages rewritten by compaction" : 0,"row-store internal pages" : 0,"row-store leaf pages" : 0},"cache" : {"bytes currently in the cache" : 502018875,"bytes read into cache" : 0,"bytes written from cache" : 437640755,"checkpoint blocked page eviction" : 0,"data source pages selected for eviction unable to be evicted" : 12,"hazard pointer blocked page eviction" : 0,"in-memory page passed criteria to be split" : 130,"in-memory page splits" : 62,"internal pages evicted" : 0,"internal pages split during eviction" : 1,"leaf pages split during eviction" : 56,"modified pages evicted" : 56,"overflow pages read into cache" : 0,"overflow values cached in memory" : 0,"page split during eviction deepened the tree" : 1,"page written requiring lookaside records" : 0,"pages read into cache" : 0,"pages read into cache requiring lookaside entries" : 0,"pages requested from the cache" : 2232017,"pages written from cache" : 15472,"pages written requiring in-memory restoration" : 0,"tracked dirty bytes in the cache" : 0,"unmodified pages evicted" : 0},"cache_walk" : {"Average difference between current eviction generation when the page was last considered" : 0,"Average on-disk page image size seen" : 0,"Clean pages currently in cache" : 0,"Current eviction generation" : 0,"Dirty pages currently in cache" : 0,"Entries in the root page" : 0,"Internal pages currently in cache" : 0,"Leaf pages currently in cache" : 0,"Maximum difference between current eviction generation when the page was last considered" : 0,"Maximum page size seen" : 0,"Minimum on-disk page image size seen" : 0,"On-disk page image sizes smaller than a single allocation unit" : 0,"Pages created in memory and never written" : 0,"Pages currently queued for eviction" : 0,"Pages that could not be queued for eviction" : 0,"Refs skipped during cache traversal" : 0,"Size of the root page" : 0,"Total number of pages currently in cache" : 0},"compression" : {"compressed pages read" : 0,"compressed pages written" : 15312,"page written failed to compress" : 0,"page written was too small to compress" : 158,"raw compression call failed, additional data available" : 0,"raw compression call failed, no additional data available" : 0,"raw compression call succeeded" : 0},"cursor" : {"bulk-loaded cursor-insert calls" : 0,"create calls" : 3,"cursor-insert key and value bytes inserted" : 429166606,"cursor-remove key bytes removed" : 0,"cursor-update value bytes updated" : 0,"insert calls" : 1835000,"next calls" : 162051,"prev calls" : 1,"remove calls" : 0,"reset calls" : 30748,"restarted searches" : 0,"search calls" : 0,"search near calls" : 1227,"truncate calls" : 0,"update calls" : 0},"reconciliation" : {"dictionary matches" : 0,"fast-path pages deleted" : 0,"internal page key bytes discarded using suffix compression" : 31112,"internal page multi-block writes" : 4,"internal-page overflow keys" : 0,"leaf page key bytes discarded using prefix compression" : 0,"leaf page multi-block writes" : 66,"leaf-page overflow keys" : 0,"maximum blocks required for a page" : 242,"overflow values written" : 0,"page checksum matches" : 209,"page reconciliation calls" : 171,"page reconciliation calls for eviction" : 57,"pages deleted" : 1},"session" : {"object compaction" : 0,"open cursor count" : 3},"transaction" : {"update conflicts" : 0}},"nindexes" : 1,"totalIndexSize" : 18272256,"indexSizes" : {"_id_" : 18272256},"ok" : 1
}

// 执行
> db.randomInfo.getIndexes()
[{"v" : 2,"key" : {"_id" : 1},"name" : "_id_","ns" : "company.randomInfo"}
]
// 这是默认的索引,我们一般不会使用这个索引的

建立一个索引

> db.randomInfo.ensureIndex({username: 1})
{"createdCollectionAutomatically" : false,"numIndexesBefore" : 1,"numIndexesAfter" : 2,"ok" : 1
}
> db.randomInfo.getIndexes()            // 然后查看发现有两条索引了
[{"v" : 2,"key" : {"_id" : 1},"name" : "_id_","ns" : "company.randomInfo"},{"v" : 2,"key" : {"username" : 1},"name" : "username_1","ns" : "company.randomInfo"}
]
>
//test1.js
var startTime = new Date().getTime()  //得到程序运行的开始时间
var  db = connect('company')          //链接数据库
var   rs=db.randomInfo.find({username:"tfruhjy8k"})  //根据用户名查找用户
rs.forEach(rs=>{printjson(rs)})                     //循环输出
var  runTime = new Date().getTime()-startTime;      //得到程序运行时间
print('[SUCCESS]This run time is:'+runTime+'ms')    //打印出运行时间
// 执行查找
> load('./test1.js')
connecting to: mongodb://127.0.0.1:27017/company
MongoDB server version: 3.4.10
{"_id" : ObjectId("5ac8b73b5646d96c6db3e1a8"),"username" : "od2umr6kec","regeditTime" : ISODate("2018-04-07T12:18:44.292Z"),"randNum0" : 577322,"randNum1" : 961443,"randNum2" : 999621,"randNum3" : 968291,"randNum4" : 834839,"randNum5" : 637084,"randNum6" : 172311,"randNum7" : 219693,"randNum8" : 617081
}
[SUCCESS]This run time is:11ms   // 关键看这里,你会发现时间缩短了好多呢
true
>

无论是在关系型数据库还是文档数据库,建立索引都是非常重要的。前边讲了,索引这东西是要消耗硬盘和内存资源的,所以还是要根据程序需要进行建立了。MongoDB也给我们进行了限制,只允许我们建立64个索引值。

复合索引

复合索引就是两条以上的索引

// 在建立一个索引
> db.randomInfo.ensureIndex({randNum0: 1});
{"createdCollectionAutomatically" : false,"numIndexesBefore" : 2,"numIndexesAfter" : 3,"ok" : 1
}
> db.randomInfo.getIndexes();
[{"v" : 2,"key" : {"_id" : 1},"name" : "_id_","ns" : "company.randomInfo"},{"v" : 2,"key" : {"username" : 1},"name" : "username_1","ns" : "company.randomInfo"},{"v" : 2,"key" : {"randNum0" : 1},"name" : "randNum0_1","ns" : "company.randomInfo"}
]
>

我们同时查询两个索引的值,看看效果是怎么样的。

//
var startTime=new Date().getTime();
var db = connect('company');var  rs= db.randomInfo.find({username:'7xwb8y3',randNum0:565509});rs.forEach(rs=>{printjson(rs)});var runTime = new Date().getTime()-startTime;
print('[Demo]this run time is '+runTime+'ms');
// 从性能上看并没有什么特殊的变化,查询时间还是在10ms左右。MongoDB的复合查询是按照我们的索引顺序进行查询的。就是我们用db.randomInfo.getIndexes()查询出的数组。

指定索引查找

//
var  rs= db.randomInfo.find({username:'7xwb8y3',randNum0:565509}).hint({randNum0:1});

删除索引

db.randomInfo.dropIndex('randNum0_1');//索引的唯一ID

这里需要注意的是删除时填写的值,并不是我们的字段名称(key),而是我们索引查询表中的name值。这是一个小坑。

全文索引

有些时候需要在大篇幅的文章中搜索关键词,比如我写的文章每篇都在万字以上,这时候你想搜索关键字是非常不容易的,MongoDB为我们提供了全文索引。

// 插入两条数据
db.info.insert({contextInfo:"I am a programmer, I love life, love family. Every day after work, I write a diary."})
db.info.insert({contextInfo:"I am a programmer, I love PlayGame, love drink. Every day after work, I playGame and drink."})

建立全文索引

db.info.ensureIndex({contextInfo:'text'});
//需要注意的是这里使用text关键词来代表全文索引,我们在这里就不建立数据模型了。

全文索引查找

// $text:表示要在全文索引中查东西。这里的$test指的就是contextInfo
// $search:后边跟查找的内容。
db.info.find({$text:{$search:"programmer"}}); // 查找contextInfo中含有programmer关键字的

查找多个词

// 比如我们希望查找数据中有programmer,family,diary,drink的数据(这是或的关系),所以两条数据都会出现。
db.info.find({$text:{$search:"programmer family diary drink"}})// 如果我们这时候希望不查找出来有drink这个单词的记录,我们可以使用“-”减号来取消。
db.info.find({$text:{$search:"programmer family diary -drink"}})// 全文搜索中是支持转义符的,比如我们想搜索的是两个词(love PlayGame和drink),这时候需要使用\斜杠来转意。
db.info.find({$text:{$search:"\"love PlayGame\" drink"}})

全文索引在工作还是经常使用的,比如博客文章的搜索,长文件的关键词搜索,这些都需要使用全文索引来进行。


到这里Mongodb的基本知识就基本结束了,下一节我们将会学习如何管理Mongodb

  • 参考文献

技术胖

MongoDB ( 五 )高级_索引相关推荐

  1. go mongodb排序查询_【赵强老师】MongoDB中的索引(下)

    (四)索引的类型三:复合索引(Compound Index) MongoDB支持复合索引,即将多个键组合到一起创建索引.该方式称为复合索引,或者也叫组合索引,该方式能够满足多键值匹配查询使用索引的情形 ...

  2. go mongodb排序查询_「赵强老师」MongoDB中的索引(下)

    (四)索引的类型三:复合索引(Compound Index)** MongoDB支持复合索引,即将多个键组合到一起创建索引.该方式称为复合索引,或者也叫组合索引,该方式能够满足多键值匹配查询使用索引的 ...

  3. 【MySQL高级篇】第06章_索引的数据结构

    第06章_索引的数据结构 1. 为什么使用索引 索引是存储引擎用于快速找到数据记录的一种数据结构,就好比一本教科书的目录部分,通过目录中找到对应文章的页码,便可快速定位到需要的文章.MySQL中也是一 ...

  4. MongoDB:详细解释mongodb的高级操作,聚合和游标

    前几天总结了mongodb的安装入门.详细解释了增删改查的基本操作,今天再来总结下mongodb更高级的操作,聚合和游标. 一.聚合,mongodb的聚合操作一般分为四种情景,分别是:count.di ...

  5. 8天学通MongoDB——第四天 索引操作

    好,今天分享下mongodb中关于索引的基本操作,我们日常做开发都避免不了要对程序进行性能优化,而程序的操作无非就是CURD,通常我们 又会花费50%的时间在R上面,因为Read操作对用户来说是非常敏 ...

  6. go mongodb排序查询_《MongoDB》day two

    Mongodb的更新方式有? db.集合名.update() 函数:用于更新已存在的文档. 语法格式:db.COLLECTION_NAME.update({查询条件},{更新内容},{更新参数(可选) ...

  7. MySQL高级 —— 高性能索引

    引言 最近一直在抱着<高性能MySQL(第三版)>研究MySQL相关热点问题,诸如索引.查询优化等,这阶段的学习是前一段时间MySQL基础与官方的"阅读理解"的进一步延 ...

  8. 8天学通MongoDB——第四天 索引操作

    原文:8天学通MongoDB--第四天 索引操作 这些天项目改版,时间比较紧,博客也就没跟得上,还望大家见谅. 好,今天分享下mongodb中关于索引的基本操作,我们日常做开发都避免不了要对程序进行性 ...

  9. ElasticSearch_高级_(集群+分片)

    ElasticSearch_高级_(集群+分片) 文章目录 ElasticSearch_高级_(集群+分片) 课程内容 1.spring data elasticsearch 1.1 spring d ...

最新文章

  1. 你的能力代表你能走多远
  2. Sicily 1153: 马的周游问题(DFS+剪枝)
  3. 技术负责人所需的四个核心能力,你具备几个?
  4. 微软发布的python教程_微软发布Python 教程《Develop with Python on Windows》
  5. 从windows server的文件服务到分布式文件服务(十)
  6. CentOS开发ASP.NET Core入门教程
  7. 【HDU - 6186】CS Course(按位与,按位或,按位异或的区间统计,二进制拆位)
  8. Docker 部署java服务
  9. 三星折叠屏手机出事!才用两天就“挂了”
  10. 基于设备树的TQ2440 DMA学习(2)—— 简单的DMA传输
  11. VR安全很重要:Magic Leap低调收购安全公司
  12. C++11并发之std::mutex
  13. c# 小票机打印二维条码_C#小票打印机
  14. 加密狗映射至虚拟服务器,ESXI 5.1/5.5 主机添加或映射USB设备(加密狗)(示例代码)...
  15. 炫酷收徒技术网络网站源码
  16. matlab车牌倾斜校正,求做车牌倾斜校正程序。
  17. libVLC库推流,sout参数如何设置
  18. (Python文件处理)doc文档转UTF-8格式的TXT文档
  19. 泰坦尼克号 第三章 模型搭建和评估
  20. 为什么看P1dB压缩,而不是2dB,3dB压缩

热门文章

  1. makefile使用--命令(三)
  2. linux内核网络协议栈--数据包的网卡缓冲区(二十四)
  3. ssms没有弹出服务器验证_Nginx服务器负载均衡及生成ssl密钥对、Nginx配置ssl操作示例...
  4. AWS云使用100条宝贵经验分享
  5. 【初探移动前端开发02】移动设备的页面
  6. 多线程断点续传及下载
  7. 车小米O2O保养平台搭建完毕
  8. 第三次作业——(涂江凤、邓洪虹)
  9. 其他OJ 树型DP 选课
  10. Silverlight专题(WatermarkedTextBox使用)--摘录 很受用