mongodb 使用sort時的索引利用問題探究
在實際操作mongodb的過程中,發現將find與sort結合使用時,mongodb優先使用sort查詢條件可以利用的索引,只有當sort查詢條件沒有索引可用時,才嘗試利用find查詢條件中的索引。
mongodb版本2.6.3
> db.version()
2.6.3
下面用命令列進行實際操作演示
collection中有如下記錄
> db.TestOrder.find()
{ "_id" : ObjectId("56dfb5af9f436cc02505fac0"), "cust_id" : 12, "amount" : 199, "status" : "A", "create_time" : "14512345678" }
{ "_id" : ObjectId("56dfb61a9f436cc02505fac2"), "cust_id" : 121, "amount" : 1999, "status" : "A", "create_time" : "14512345679" }
{ "_id" : ObjectId("56dfb6359f436cc02505fac3"), "cust_id" : 119, "amount" : 23999, "status" : "B", "create_time" : "14512345680" }
{ "_id" : ObjectId("56dfb6419f436cc02505fac4" ), "cust_id" : 1192, "amount" : 2999, "status" : "B", "create_time" : "14512345681" }
{ "_id" : ObjectId("56dfb64d9f436cc02505fac5"), "cust_id" : 11921, "amount" : 2992, "status" : "B", "create_time" : "14512345683" }
{ "_id" : ObjectId("56dfb9089f436cc02505fac8"), "cust_id" : 1000, "amount" : 399, "status" : "A", "create_time" : "14312345678" }
{ "_id" : ObjectId("56dfb9089f436cc02505fac9"), "cust_id" : 1100, "amount" : 599, "status" : "C", "create_time" : "14212345680" }
現有索引
建立了cust_id升序索引,create_time降序索引。
> db.TestOrder.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.TestOrder"
},
{
"v" : 1,
"key" : {
"cust_id" : 1
},
"name" : "cust_id_1",
"ns" : "test.TestOrder"
},
{
"v" : 1,
"key" : {
"create_time" : -1
},
"name" : "create_time_-1",
"ns" : "test.TestOrder"
}
]
僅使用find查詢
利用上了cust_id升序索引
> db.TestOrder.find({cust_id:{$in:[12,121,1000,1100]}}).explain()
{
"cursor" : "BtreeCursor cust_id_1",
"isMultiKey" : false,
"n" : 4,
"nscannedObjects" : 4,
"nscanned" : 5,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 5,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"cust_id" : [
[
12,
12
],
[
121,
121
],
[
1000,
1000
],
[
1100,
1100
]
]
},
"server" : "Test_Node003:27020",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 5,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 4,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ cust_id: 1.0 }",
"boundsVerbose" : "field #0['cust_id']: [12.0, 12.0], [121.0, 121.0], [1000.0, 1000.0], [1100.0, 1100.0]",
"isMultiKey" : 0,
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 5,
"children" : [ ]
}
]
}
}
同時使用find、sort查詢
利用上了create_time索引進行全表排序,未用上cust_id索引。
> db.TestOrder.find({cust_id:{$in:[12,121,1000,1100]}}).sort({create_time:-1}).explain()
{
"cursor" : "BtreeCursor create_time_-1",
"isMultiKey" : false,
"n" : 4,
"nscannedObjects" : 7,
"nscanned" : 7,
"nscannedObjectsAllPlans" : 11,
"nscannedAllPlans" : 12,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"create_time" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
},
"server" : "Test_Node003:27020",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 9,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 3,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 4,
"children" : [
{
"type" : "IXSCAN",
"works" : 7,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 7,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ create_time: -1.0 }",
"boundsVerbose" : "field #0['create_time']: [MaxKey, MinKey]",
"isMultiKey" : 0,
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 7,
"children" : [ ]
}
]
}
}
刪除sort查詢條件中的索引
刪除create_time降序索引
> db.TestOrder.dropIndex({create_time:-1})
{ "nIndexesWas" : 3, "ok" : 1 }
再次同時使用find、sort查詢
因為sort查詢無索引可用,mongodb利用上了cust_id索引。
> db.TestOrder.find({cust_id:{$in:[12,121,1000,1100]}}).sort({create_time:-1}).explain()
{
"cursor" : "BtreeCursor cust_id_1",
"isMultiKey" : false,
"n" : 4,
"nscannedObjects" : 4,
"nscanned" : 5,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 5,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"cust_id" : [
[
12,
12
],
[
121,
121
],
[
1000,
1000
],
[
1100,
1100
]
]
},
"server" : "Test_Node003:27020",
"filterSet" : false,
"stats" : {
"type" : "SORT",
"works" : 11,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 5,
"needFetch" : 0,
"isEOF" : 1,
"forcedFetches" : 0,
"memUsage" : 424,
"memLimit" : 33554432,
"children" : [
{
"type" : "KEEP_MUTATIONS",
"works" : 5,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 5,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 4,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 4,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ cust_id: 1.0 }",
"boundsVerbose" : "field #0['cust_id']: [12.0, 12.0], [121.0, 121.0], [1000.0, 1000.0], [1100.0, 1100.0]",
"isMultiKey" : 0,
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 5,
"children" : [ ]
}
]
}
]
}
]
}
}
相關推薦
mongodb 使用sort時的索引利用問題探究
在實際操作mongodb的過程中,發現將find與sort結合使用時,mongodb優先使用sort查詢條件可以利用的索引,只有當sort查詢條件沒有索引可用時,才嘗試利用find查詢條件中的索引。 mongodb版本2.6.3 > db.versio
mongodb 文本索引
一個 包含 spa min .com span 我們 ans rop 啟用文本搜索: 最初文本搜索是一個實驗性功能,但2.6版本開始,配置是默認啟用的。但是,如果使用的是以前 MongoDB 的版本,那麽必須啟用文本搜索,使用下面的代碼: >db.adminComma
4.非關系型數據庫(Nosql)之mongodb:普通索引,唯一索引
log 索引 xpl sys watermark lang mon style gravity 一:普通索引 1創建一個新的數據庫 > use toto; switched to db toto
MongoDB查詢、索引和聚合
-h 初始 _id 組合 otto agg margin lang expire 初始化mongodb數據庫 > use deng switched to db deng > db.createCollection("jingdong
2、MongoDB學習之索引的管理
字段 是否 reat 手動 基本 默認 uniq 匹配 會有 目標:實現索引的創建、查詢、刪除、explan管理等操作 環境: > db.version() 3.4.7 索引創建滿足的基本需求: 1;索引提高查詢速度 2;在mongodb中,索引可以按自動列升序/
給MongoDB添加索引
div ... find() mongodb dir 方便 指定 foo log 用過數據庫的都知道,數據庫索引與書籍的索引類似,都是用來幫助快速查找的。 MongoDB的索引跟關系型數據庫的索引幾乎一致。 1. 索引的創建 mongodb采用ensur
Python異常捕捉try except else finally有return時執行順序探究
復制代碼 捕獲 key clas 沖突 light 問題 進入 odi 轉載自 https://www.cnblogs.com/JohnABC/p/4065437.html 學習python或者其他有異常控制的編程語 言, 大家很有可能說try except finally
js中遍歷註冊事件時索引怎麽獲取
遍歷註冊單擊事件 索引註意:這種寫法,是有問題的。註冊事件是在頁面加載完畢以後就完成了,但此時並沒有觸發事件。事件觸發是由用戶在頁面上點擊時才會觸發,所以說當用戶點擊時,才會執行事件處理函數,那麽此時的i已經變成了4,最後最終彈出來的結果是i+1,也就是5,而且每一個元素的彈出來的結果都是5。此時的做法應該是
解決like '%字符串%'時索引不被使用的方法
for read img 掃描 之前 cee album media 字符串 解決like ‘%字符串%‘時索引不被使用的方法 分步閱讀 解決like ‘%字符串%‘時索引不被使用的方法,如果like以通配符開頭(‘%abc‘)時索引會
mongodb 安裝時錯誤
-o 安裝出現 ESS warning mon let win 重新 時間 1.安裝MongoDB進度條長時間不動 根據在網上搜的步驟安裝mongoDB到這步,就基本上卡死不動,在網上查到的辦法是死等,等了半個小時,但運氣不好半個小時也不一定安裝成功。 如果進行到這步,卡
深入理解MongoDB的複合索引
更新時間:2018年03月26日 10:17:37 作者:Fundebug 我要評論 對於MongoDB的多鍵查詢,建立複合索引可以有效提高效能。這篇文章主要給大家介紹了關於MongoDB複合
mongodb新增刪除索引
用過資料庫的都知道,資料庫索引與書籍的索引類似,都是用來幫助快速查詢的。 MongoDB的索引跟關係型資料庫的索引幾乎一致。 1. 索引的建立 mongodb採用ensureIndex來建立索引,如
MongoDB的全文索引
Table of Contents 背景 如何使用 準備工作:插入資料 建立全域性索引 查詢結果 使用中存在哪些問題? 英文存在停止詞 中文無法採用全文索引 前面瞭解了多種索引方式,比如單鍵索引,多鍵索引,複合索引等,這些感覺都太空,咱今天學習一下實用的索引——全文索引。 背景
Mongodb sort()方法的size限制
問題描述 當我對一個沒有建索引的欄位做find,然後做sort的時候,可能觸發sort的size的32MB限制,例如: db.getCollection('Hotel').find({"time_stamp":{"$lte":1485878400}}).sort({"time_sta
MongoDB——地理空間索引和查詢
MongoDB提供了一系列的索引和查詢機制來處理地理空間資訊。這一節會介紹MongoDB的地理索引特性。您可以閱讀 地理索引教程 來了解關於MongoDB中地理查詢的完整示例。 表面 在您儲存地理資料和編寫查詢條件前,首先,您必須選擇表面型別,這將被用在計算中。您所選
MongoDB排序、索引
以下例項演示了 col 集合中的資料按欄位 likes 的降序排列: db.col.find().sort({"likes":-1}) MongoDB使用 ensureIndex() 方法來建立索引。 語法中 Key 值為你要建立的索引欄位,1為指定按升序建立索引,如果你想按降序來建立索引指定為-1
優化mysql實驗(explain;索引)+利用 index、explain和profile優化mysql資料庫查詢小結
1.優化實驗 實驗環境: 1、sql工具:Navicat2、sql資料庫,使用openstack資料庫作為示例 一、mysql索引查詢 show index from instances 結果欄
ios 獲取avplayer播放聲音完成時的衝突探究
最近專案裡有一個需求,是個直播專案,需要主播在開播端,點選聊天區某條語音資訊的播放按鈕來播放觀眾的語音(這裡我用的是在語音播放單例新增播放完畢監聽;在本vc控制器將出現時為語音播放處理單例新增語音播放完成監聽,在vc控制器將要消失時為單例移出監聽),並且播放完成,按鈕切換為初始狀態;而且在主播端開播
GridView刪除資料時索引超出範圍的解決方法
刪除購物車裡面的某一條記錄時,會報索引超出範圍。必須為非負值並小於集合大小。後來參看網上其他人的部落格。設定了gridview的主鍵就解決了這個問題。this.GridView1.DataKeyNames = new string[] { "BookId" };
MongoDB查詢時的卡頓與記憶體問題
這兩天從Remote MongoDB讀資料的時候遇到了一個很奇葩的問題,就是使用DBCursor讀資料的時候每六千多條就卡頓很久,這個六千多是一個固定的數。然後是58w的資料每次到20w putty終端就會卡死,然後MongoDB也會退出。雖然到現在問題還沒解決