DEXã§ããMongoDBè·äººã¯è¦ããªããªãã®å·»
â»ãã®ã¨ã³ããªã¯å人ã®è¦è§£ã§ãããæå±ããçµç¹ã®å ¬å¼è¦è§£ã§ã¯ããã¾ãã
ãã®ã¨ã³ããªã¯ãMongoDB Advent Calendar 2015 18æ¥ç®ã®ã¨ã³ããªã§ãã
ã©ããã©ããä¹ã«ã¬ã¼æ§ã§ããæ¡éã§ãã
MongoDB on AWSçãªãªã«ããæ¸ããã¨ããã®ã§ããããã®åã«ããç´¹ä»ãããã¨ãªãã£ããªã¼ã¨æãDEXã®ç´¹ä»ãããã¨æãã¾ãã
DEXã¨ã¯
ãã®MongoDBãªããã®äººéã®Mongolabããã®ä½ã£ããMongoDBã®Slowlogãªã©ããé©åãªINDEXè¨å®ãRecommendãã¦ããããããã¯ãã«ãªãã¾ãã
ç¥æ§ä»æ§Mongolabæ§ã
ã¤ã³ã¹ãã¼ã«
pipã§ç°¡åã
$ pip install dex
ã³ãã³ãã©ã¤ã³æé
åºæ¬çã«ã¯ãMongoDBã®URLã¨ãLogã®ãã¹ãæå®ãã¦ããã¾ãããã
$ dex -f /var/log/mongodb/mongodb.log mongodb://localhost { 'runStats': { 'linesRecommended': 0, 'linesProcessed': 1, 'linesPassed': 149 }, 'results': [] }
ã»ãã§ãã¾ããã
ãã¼ã¿ãã¼ã¹ä¾
ãã¼ã¿ãã¼ã¹ã¯この前 ä½ã£ããã¼ã¿ãã¼ã¹ã«ãã¼ã¿ã足ãã¦ä½ã£ã¦ã¿ã¾ãããã
é©å½ã«1000ä¸ã¬ã³ã¼ãã»ã©è¿½å ãã¾ããã
$ mongo d1 (snip) > db.t1.find().count() 10110005
ããã§ããä¸åDEXãå®è¡ãã¦ã¿ã¾ãããã¾ã ã¯ã¨ãªãå®è¡ãã¦ããªãã®ã§å½ç¶ä½ãåºã¾ããã
$ dex -f /var/log/mongodb/mongodb.log mongodb://localhost { 'runStats': { 'linesRecommended': 0, 'linesProcessed': 1, 'linesPassed': 149 }, 'results': [] }
ã§ã¯ãIndexã®ããã¦ããªãã¯ã¨ãªãæã£ã¦ã¿ã¾ããã
ã©ãã10ç§ä»¥ä¸ãããã¾ããéãã
$ mongo d1 # ã¯ã¨ãª1ã¤ç® > db.t1.find( { "nosql" : "mongodb"} ) { "_id" : ObjectId("567365cf760961552b000001"), "created_at" : ISODate("2015-12-16T13:04:10Z"), "name" : "kuwa_tw", "nosql" : "mongodb", "price" : 10 } # ã¯ã¨ãª2ã¤ç® > db.t1.find( { "name" : "saeoshi"} ) { "_id" : ObjectId("567365cf760961552b000005"), "created_at" : ISODate("2015-12-16T13:04:38Z"), "name" : "saeoshi", "nosql" : "voldemote", "price" : 2000 } # ã¯ã¨ãª3ã¤ç® > db.t1.find( { "price" : { $gt: 9000 } } ) ï¼ãã£ã±ãåºãã®ã§snipï¼ > # ã¯ã¨ãª4ã¤ç®ï¼è¤åæ¡ä»¶ > db.t1.find( { "name" : "kakerukaeru" , "price" : { $gt: 10 } } ) >
ãããããããã§ããä¸åDEXãå®è¡ãã¦ã¿ã¾ãããã
ããããã¨ãéãã£ãã¯ã¨ãªãããã¯ã¢ãããã¦ãã©ã®ãããªã¤ã³ããã¯ã¹ãã¯ãã°ããã®ãRecommendãã¦ããã¾ããããã¯ç´ æ´ããã
$ dex -f /var/log/mongodb/mongodb.log mongodb://localhost { 'runStats': { 'linesRecommended': 7, 'linesProcessed': 7, 'linesPassed': 201 }, 'results': [ { 'queryMask': '{"$query":{"name":"<val>","price":{"$gt":"<val>"}}}', 'namespace': 'd1.t1', 'recommendation': { 'index': '{"name": 1, "price": 1}', 'namespace': 'd1.t1', 'shellCommand': 'db["t1"].ensureIndex({"name": 1, "price": 1}, {"background": true})' }, 'details': { 'count': 3, 'totalTimeMillis': 52289, 'avgTimeMillis': 17429 } }, { 'queryMask': '{"$query":{"name":"<val>"}}', 'namespace': 'd1.t1', 'recommendation': { 'index': '{"name": 1}', 'namespace': 'd1.t1', 'shellCommand': 'db["t1"].ensureIndex({"name": 1}, {"background": true})' }, 'details': { 'count': 2, 'totalTimeMillis': 35758, 'avgTimeMillis': 17879 } }, { 'queryMask': '{"$query":{"nosql":"<val>"}}', 'namespace': 'd1.t1', 'recommendation': { 'index': '{"nosql": 1}', 'namespace': 'd1.t1', 'shellCommand': 'db["t1"].ensureIndex({"nosql": 1}, {"background": true})' }, 'details': { 'count': 1, 'totalTimeMillis': 17983, 'avgTimeMillis': 17983 } }, { 'queryMask': '{"$query":{"price":{"$gt":"<val>"}}}', 'namespace': 'd1.t1', 'recommendation': { 'index': '{"price": 1}', 'namespace': 'd1.t1', 'shellCommand': 'db["t1"].ensureIndex({"price": 1}, {"background": true})' }, 'details': { 'count': 1, 'totalTimeMillis': 17488, 'avgTimeMillis': 17488 } } ] }
ã¤ãã§ã«explainããã¦ã¿ã¾ãããã
> db.t1.find( { "name" : "kakerukaeru" , "price" : { $gt: 10 } } ).explain() { "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 0, "nscannedObjects" : 10110005, "nscanned" : 10110005, "nscannedObjectsAllPlans" : 10110005, "nscannedAllPlans" : 10110005, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 18, "nChunkSkips" : 0, "millis" : 17893, "indexBounds" : { }, "server" : "ip-172-31-20-34:27017" }
å½ããåãªããIndexãç¡ãã®ã§å½ç¶BasicCursorã§ãã
ã§ã¯Recommendã«ãããã£ã¦Indexãè²¼ã£ã¦ã¿ã¾ããã
> db["t1"].ensureIndex({"name": 1, "price": 1}, {"background": true}) > db["t1"].getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "d1.t1", "name" : "_id_" }, { "v" : 1, "key" : { "name" : 1, "price" : 1 }, "ns" : "d1.t1", "name" : "name_1_price_1", "background" : true } ]
ã¯ãIndexãã¾ãããã
ã§ã¯ããä¸åexplainãã¦ã¿ã¾ããã
çéï¼ï¼ï¼
> db.t1.find( { "name" : "kakerukaeru" , "price" : { $gt: 10 } } ).explain() { "cursor" : "BtreeCursor name_1_price_1", "isMultiKey" : false, "n" : 0, "nscannedObjects" : 0, "nscanned" : 0, "nscannedObjectsAllPlans" : 0, "nscannedAllPlans" : 0, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "name" : [ [ "kakerukaeru", "kakerukaeru" ] ], "price" : [ [ 10, 1.7976931348623157e+308 ] ] }, "server" : "ip-172-31-20-34:27017" }
BtreeCursorã«ãªã£ã¦Index[name_1_price_1]ã使ããã¦ããã®ããããã¨æãã¾ãã
ã¨ããããã§ãIndexã¯ã¡ã¢ãªãé£ãã®ã§ä¸æ¦ã«è²¼ãã¾ãã£ããããã¨ããããã§ã¯ãªããã§ãããå®æçã«DEXãæµããã¨ã§Indexã使ããã¦ããªãã¯ã¨ãªãæµãã¦ããªããã確èªãããã¨ãã§ãã¾ãã
ç°¡åã«ä½¿ããã®ã§ä½¿ã£ã¦ããã¨å¹¸ãã«ãªãã¾ããã
MongoDB使ã£ã¦ãæç¹ã§å¹¸ããªã®ããããããã¾ãããããããï¼æªè«çãªçµããæ¹ï¼
- ä½è : Kyle Banker,Peter Bakkum,Shaun Verch,Douglas Garrett,Tim Hawkins
- åºç社/ã¡ã¼ã«ã¼: Manning Pubns Co
- çºå£²æ¥: 2016/04/15
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- ãã®ååãå«ãããã°ãè¦ã
ã¨ããéè¡ã®ç¦æ¸ç®é²(ã¤ã³ããã¯ã¹) æ庫 å ¨22å·» å®çµã»ãã (é»ææ庫)
- åºç社/ã¡ã¼ã«ã¼: ã¢ã¹ãã¼ã»ã¡ãã£ã¢ã¯ã¼ã¯ã¹
- çºå£²æ¥: 2010/12/15
- ã¡ãã£ã¢: æ庫
- è³¼å ¥: 2人 ã¯ãªãã¯: 5å
- ãã®ååãå«ãããã° (3件) ãè¦ã