ttlsa Tutorial Series mongodb - (four) mongodb index & explain & profile

The index is used to speed up queries, there are two-sided nature of things, at the same time each insert, update, and will result in additional overhead deletion. Sometimes the index does not solve the problem of slow queries, in general, returns a collection of more than half of the results, a full table scan query index more efficient than some. Creating too many indexes will result in the insertion is very slow, but will take up a lot of space. Can be analyzed by hint and explain tool. Directional index, descending or ascending order. Each set of default maximum number of index 64. 1. View index
> db.ttlsa_events.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "ttlsa_login.ttlsa_events",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "stmp" : -1
                },
                "ns" : "ttlsa_login.ttlsa_events",
                "name" : "stmp_-1"
        },
        {
                "v" : 1,
                "key" : {
                        "uid" : 1,
                        "stmp" : -1
                },
                "ns" : "ttlsa_login.ttlsa_events",
                "name" : "uid_1_stmp_-1"
        }
]
In this example there are three indexes, where _id index is created automatically when you create a table can not be deleted. uid_1_stmp_-1 is a combination index. 1 for ascending, descending -1 means. 2. Create Index Index parameters are:
option			values				default
backgroud		true/false			false
dropDups		true/false			false
unique			true/false			false
sparse			true/false			false
 
> db.ttlsa_posts.ensureIndex({pid:1});
When there are large amounts of data to create the index will be very time-consuming, can be assigned to the background, simply specify "backgroud: true" can be. Such as
> db.ttlsa_posts.ensureIndex({pid:1},{backgroud:true});
3. Embedded Index creates an index with common key is embedded in the document to create a bond index did not differ.
> db.ttlsa_posts.ensureIndex({"post.date":1})
4. Document type index
> db.ttlsa_comments.insert({cid:222, properties:{user:'ttlsa', email:'[email protected]'}})
> db.ttlsa_comments.ensureIndex({properties:1})
5. Combination Index
> db.ttlsa_comments.ensureIndex({"properties.user":1,"properties.email":1})
The following query will use this index
> db.ttlsa_comments.find({"properties.user":'ttlsa',"properties.email":'[email protected]'})
> db.ttlsa_comments.find({"properties.user":'ttlsa'})
> db.ttlsa_comments.find().sort({"properties.user":1})
Combining a plurality of index values, query, subquery prefix matching index, you can use the combination index. 6. unique index only needs to be specified in ensureIndex naming "unique: true" can be.
> db.ttlsa_posts.ensureIndex({pid:1},{unique:true})
When creating an index to an existing collection, some data may have been repeated, then create a unique index will fail. DropDups can be used to retain the first document, then the duplicate documents will be removed, this method is careful operation.
> db.ttlsa_posts.ensureIndex({pid:1},{unique:true, dropDups:true})
7. Force index hint command can be forced to use an index
> db.ttlsa_posts.find({pid:{$lt:333}}).hint({pid:1,date:1})
8. Delete Index Deleting a collection of all indexes:
> db.collection.dropIndexes()
Delete a set of indexes:
> db.collection.dropIndex({x:1})
9. rebuild the index
> db.collection.reIndex()
> db.runCommand({reIndex:'collection'})
Rebuild the index will lock the collection. When repairing a database with a repair order, it will rebuild the index. 10. mongodb full-text indexing full-text index was introduced in version 2.4, later to say this. 11. explain the implementation of plans to use the explain command returns the query using the index case, time-consuming, and so the number of scanning a document statistics.
> db.ttlsa_events.find({uid:178620830}).explain()
{
        "cursor" : "BtreeCursor uid_1_stmp_-1",
        "isMultiKey" : false,
        "n" : 2,
        "nscannedObjects" : 2,
        "nscanned" : 2,
        "nscannedObjectsAllPlans" : 2,
        "nscannedAllPlans" : 2,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 4,
        "indexBounds" : {
                "uid" : [
                        [
                                178620830,
                                178620830
                        ]
                ],
                "stmp" : [
                        [
                                {
                                        "$maxElement" : 1
                                },
                                {
                                        "$minElement" : 1
                                }
                        ]
                ]
        },
        "server" : "TTLSA-191155:27017"
}
Field Description: Cursor: Returns the cursor type isMultiKey: whether to use a combination of the index n: Returns the number of documents nscannedObjects: nscanned number of documents to be scanned: the inspection document or index entry number scanAndOrder: whether ordering indexOnly in memory: nYields: The query for wait for the write operation to perform a read lock waiting times nChunkSkips: millis: time-consuming (ms) indexBounds: used index server: server hostname 12. turn profiling feature to view profiling levels:
> db.getProfilingLevel()
0
Setting profiling level:
> db.setProfilingLevel( level , slowms ) 
> db.setProfilingLevel(2,10)
{ "was" : 0, "slowms" : 100, "ok" : 1 }
Significance level profile can take 0,1,2 expressed as follows: 0 - no default Open Class 1 - Slow recording command (default> 100ms) 2 - log all command queries profiling 13. MongoDB Profile record is a direct recording system db in the recording position system.profile.
> db.system.profile.find().sort({$natural:-1}).limit(1)
{ "ts" : ISODate("2013-07-18T09:56:59.546Z"), "op" : "query", "ns" : "ttlsa_event.ttlsa_events", "query" : { "$query" : { "uid" : 161484152, "stmp" : { "$gt" : 0 } }, "$orderby" : { "stmp" : -1 } }, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 35, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : NumberLong(354), "w" : NumberLong(0) }, "timeAcquiringMicros" : { "r" : NumberLong(3), "w" : NumberLong(3) } }, "nreturned" : 35, "responseLength" : 7227, "millis" : 0, "client" : "10.1.242.209", "user" : "" }
> db.system.profile.find().pretty().limit(1)
{
        "ts" : ISODate("2013-07-18T09:53:40.103Z"),
        "op" : "query",
        "ns" : "ttlsa_event.ttlsa_event_friends",
        "query" : {
                "_id" : 195794232
        },
        "ntoreturn" : 1,
        "idhack" : true,
        "keyUpdates" : 0,
        "numYield" : 0,
        "lockStats" : {
                "timeLockedMicros" : {
                        "r" : NumberLong(45),
                        "w" : NumberLong(0)
                },
                "timeAcquiringMicros" : {
                        "r" : NumberLong(3),
                        "w" : NumberLong(5)
                }
        },
        "responseLength" : 20,
        "millis" : 0,
        "client" : "10.1.22.199",
        "user" : ""
}
Field Description: TS: When this command is executed in op: Operation Type query: Detailed responseLength this command: return a result set size ntoreturn: The actual query returns a result set of millis: This command takes, in milliseconds 14. modified high write efficiency than ordinary profiling Collections Collections size of the capped. Capped Collections Collection types is highly efficient, it has the following characteristics: a fixed size; Capped Collections must be created in advance, and set the size:.> Db.createCollection ( "collection", {capped: true, size: 100000}) b. Capped Collections can insert and update operations can not delete operation. Only) method to delete the entire Collection with a drop (. c. Insert the default sort order based. If not sorted query is always returned in the order of the insert. d. FIFO. If more than the defined size Collection, FIFO algorithm is used, the new record will replace record of the first insert
> db.setProfilingLevel(0)
{ "was" : 2, "slowms" : 10, "ok" : 1 }
> 
> db.getProfilingLevel()
0
> db.system.profile.drop()
true
> db.createCollection("system.profile",{capped:true, size: 1000000})
{ "ok" : 1 }
> db.system.profile.stats()
{
        "ns" : "ttlsa_event.system.profile",
        "count" : 0,
        "size" : 0,
        "storageSize" : 1003520,
        "numExtents" : 1,
        "nindexes" : 0,
        "lastExtentSize" : 1003520,
        "paddingFactor" : 1,
        "systemFlags" : 0,
        "userFlags" : 0,
        "totalIndexSize" : 0,
        "indexSizes" : {

        },
        "capped" : true,
        "max" : 2147483647,
        "ok" : 1
}
Please indicate the source : http: //www.ttlsa.com/html/1661.html

Reproduced in: https: //my.oschina.net/766/blog/210857

Guess you like

Origin blog.csdn.net/weixin_33743248/article/details/91546541