explain
UK[?k?sple?n] US[?k?splen]
vt.& vi. explain, explain
vt. explain the reasons for... , justify
vi.Explain, explain, defend
MongoDB explain() operation syntax
Function: explain operation provides query information, usage index and query statistics, etc. It is helpful for us to optimize the index.
Syntax:>db.users.ensureIndex({gender:1,user_name:1})
Use explain in the statement: </p>
MongoDB explain() operation example
>db.users.ensureIndex({gender:1,user_name:1}) </p> <p>現(xiàn)在在查詢語句中使用 explain :</p> <pre> >db.users.find({gender:"M"},{user_name:1,_id:0}).explain() 以上的 explain() 查詢返回如下結(jié)果: { "cursor" : "BtreeCursor gender_1_user_name_1", "isMultiKey" : false, "n" : 1, "nscannedObjects" : 0, "nscanned" : 1, "nscannedObjectsAllPlans" : 0, "nscannedAllPlans" : 1, "scanAndOrder" : false, "indexOnly" : true, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "gender" : [ [ "M", "M" ] ], "user_name" : [ [ { "$minElement" : 1 }, { "$maxElement" : 1 } ] ] } } 現(xiàn)在,我們看看這個(gè)結(jié)果集的字段: indexOnly: 字段為 true ,表示我們使用了索引。 cursor:因?yàn)檫@個(gè)查詢使用了索引,MongoDB中索引存儲在B樹結(jié)構(gòu)中,所以這是也使用了BtreeCursor類型的游標(biāo)。如果沒有使用索引,游標(biāo)的類型是BasicCursor。這個(gè)鍵還會給出你所使用的索引的名稱,你通過這個(gè)名稱可以查看當(dāng)前數(shù)據(jù)庫下的system.indexes集合(系統(tǒng)自動(dòng)創(chuàng)建,由于存儲索引信息,這個(gè)稍微會提到)來得到索引的詳細(xì)信息。 n:當(dāng)前查詢返回的文檔數(shù)量。 nscanned/nscannedObjects:表明當(dāng)前這次查詢一共掃描了集合中多少個(gè)文檔,我們的目的是,讓這個(gè)數(shù)值和返回文檔的數(shù)量越接近越好。 millis:當(dāng)前查詢所需時(shí)間,毫秒數(shù)。 indexBounds:當(dāng)前查詢具體使用的索引。