網(wǎng)站首頁 編程語言 正文
一.索引詳講
索引是什么,索引就好比一本書的目錄,當我們想找某一章節(jié)的時候,通過書籍的目錄可以很快的找到,所以適當?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ù)所用的時間較短
綜上:當我們對一個文檔需要進行頻繁的插入操作的時候,建立不巧當?shù)乃饕龝е虏迦胄实慕档汀?/p>
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屬性來進行刪除重復的數(shù)據(jù)
> db.books.ensureIndex({name:1},{unique:true,dropDups:true}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } >
刪除重復之后,再去加入一個相同名字的數(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ū)域中建立坐標系,那么很多地點可以看做是一個個的坐標,此時2d索引就可以幫助我們進行快速的查詢某一個范圍的地點了。
例:我在MongoDB中建立一個擁有很多坐標點的文檔
> 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 } }
原文鏈接:https://www.cnblogs.com/dcz2015/p/5258577.html
相關(guān)推薦
- 2022-07-21 Gitee:使用ssh提交代碼卻提示:DeployKey does not support push
- 2022-05-01 淺談Redis哨兵模式高可用解決方案_Redis
- 2022-10-04 基于WPF實現(xiàn)帶蒙版的MessageBox消息提示框_C#教程
- 2023-07-27 修改el-button 的樣式
- 2022-05-18 ASP.NET?MVC實現(xiàn)區(qū)域路由_實用技巧
- 2023-07-25 使用POI導出Excel
- 2022-12-05 python中的腳本性能分析_python
- 2023-04-06 C語言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支