mongodb 性能优化
如果 nscanned(扫描的记录数)远大于 nreturned(返回结果的记录数)的话,那么我们就要考虑通过加索引来优化记录定位了。reslen 如果过大,那么说明我们返回的结果集太大了,这时请查看 find 函数的第二个参数是否只写上了你需要的属性名。对于创建索引的建议是:如果很少读,那么尽量不要添加索引,因为索引越多,写操作会越慢。如果读量很大,那么创建索引还是比较划算的。
创建索引
在查询条件的字段上,或者排序条件的字段上创建索引,可以显著提高执行效率:
db.posts.createIndex({ts:1});
限定返回结果条数
使用 limit()限定返回结果集的大小,可以减少 database server 的资源消耗,可以减少网络传输数据量。
db.posts.find().sort({ts:-1}).limit(10);
只查询使用到的字段,而不查询所有字段
博客日志记录内容可能非常大,而且还包括了评论内容(作为 embeded 文档)。所以只查询使用的字段,比查询所有字段效率更高:
articles = db.posts.find({}, {ts:1,title:1,author:1,abstract:1}).sort({ts:-1}).limit(10);注意:如果只查询部分字段的话,不能用返回的对象直接更新数据库。下面的代码是错误的:
a_post = db.posts.findOne({}, Post.summaryFields);a_post.x = 3;db.posts.save(a_post);
采用 capped collection
capped Collections 比普通 Collections 的读写效率高。Capped Collections 是高效率的 Collection 类型,它有如下特点:
- 固定大小; Capped Collections 必须事先创建,并设置大小:
db.createCollection("mycoll", {capped:true, size:100000})- Capped Collections 可以 insert 和 update 操作;不能 delete 操作。只能用 drop() 方法删除整个 Collection。
- 默认基于 Insert 的次序排序的。如果查询时没有排序,则总是按照 insert 的顺序返回。
- FIFO。如果超过了 Collection 的限定大小,则用 FIFO 算法,新记录将替代最先 insert 的记录。
采用 Server Side Code Execution
Server-Side Processing 类似于 SQL 数据库的存储过程,使用 Server-Side Processing 可以减小网络通讯的开销。
Hint
一般情况下 MongoDB query optimizer 都工作良好,但有些情况下使用 hint()可以提高操作效
率。 Hint 可以强制要求查询操作使用某个索引。例如,如果要查询多个字段的值,如果在其
中一个字段上有索引,可以使用 hint:db.collection.find({user:u, foo:d}).hint({user:1});
采用 Profiling
Profiling 功能肯定是会影响效率的,但是不太严重,原因是他使用的是system.profile 来记录,而 system.profile 是一个 capped collection 这种 collection 在操作上有一些限制和特点,但是效率更高.
性能监控
Mongostat
此工具可以快速的查看某组运行中的 MongoDB 实例的统计信息,用法如下:
[root@localhost bin]# ./mongostat
下面是执行结果(部分):
[root@localhost bin]# ./mongostat
insert query update delete ...... locked % idx miss % qr|qw ar|aw conn time
*0 *0 *0 *0 ...... 0 0 0|0 1|0 4 01:19:15
*0 *0 *0 *0 ...... 0 0 0|0 1|0 4 01:19:16
*0 *0 *0 *0 ...... 0 0 0|0 1|0 4 01:19:17
字段说明:
insert: 每秒插入量
query: 每秒查询量
update: 每秒更新量
delete: 每秒删除量
locked: 锁定量
qr | qw: 客户端查询排队长度(读|写)
ar | aw: 活跃客户端量(读|写)
conn: 连接数
time: 当前时间
它每秒钟刷新一次状态值,提供良好的可读性,通过这些参数可以观察到一个整体的性能情况。
db.serverStatus()
> db.serverStatus()
{
"host" : "anzi-PC",
"version" : "3.4.9",
"process" : "mongod",
"pid" : NumberLong(4216),
"uptime" : 10549,
"uptimeMillis" : NumberLong(10549489),
"uptimeEstimate" : NumberLong(10549),
"localTime" : ISODate("2017-11-16T09:46:45.998Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 0,
"rollovers" : 0
},
"connections" : {
"current" : 1,
"available" : 818,
"totalCreated" : 3
},
"extra_info" : {
"note" : "fields vary by platform",
"page_faults" : 12
},
"globalLock" : {
"totalTime" : NumberLong("10549494000"),
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 7,
"readers" : 0,
"writers" : 0
}
},
"locks" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(31709),
"w" : NumberLong(8),
"W" : NumberLong(3)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(10567),
"w" : NumberLong(1),
"R" : NumberLong(6),
"W" : NumberLong(7)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(10564),
"w" : NumberLong(1)
}
},
"Metadata" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
},
"network" : {
"bytesIn" : NumberLong(4281),
"bytesOut" : NumberLong(40809),
"physicalBytesIn" : NumberLong(4281),
"physicalBytesOut" : NumberLong(40809),
"numRequests" : NumberLong(99)
},
"opLatencies" : {
"reads" : {
"latency" : NumberLong(748),
"ops" : NumberLong(5)
},
"writes" : {
"latency" : NumberLong(105),
"ops" : NumberLong(1)
},
"commands" : {
"latency" : NumberLong(67484),
"ops" : NumberLong(43)
}
},
"opcounters" : {
"insert" : 0,
"query" : 6,
"update" : 0,
"delete" : 1,
"getmore" : 0,
"command" : 44
},
"opcountersRepl" : {
"insert" : 0,
"query" : 0,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 0
},
"storageEngine" : {
"name" : "wiredTiger",
"supportsCommittedReads" : true,
"readOnly" : false,
"persistent" : true
},
"tcmalloc" : {
"generic" : {
"current_allocated_bytes" : 60927760,
"heap_size" : 63660032
},
"tcmalloc" : {
"pageheap_free_bytes" : 1712128,
"pageheap_unmapped_bytes" : 16384,
"max_total_thread_cache_bytes" : 1038090240,
"current_total_thread_cache_bytes" : 508728,
"total_free_bytes" : 1003760,
"central_cache_free_bytes" : 177592,
"transfer_cache_free_bytes" : 317440,
"thread_cache_free_bytes" : 508728,
"aggressive_memory_decommit" : 0,
"formattedString" : "------------------------------------------------\nMALLOC: 60927760 ( 58.1 MiB) Bytes in use by application\nMALLOC: + 1712128 ( 1.6 MiB) Bytes in page heap freelist\nMALLOC: + 177592 ( 0.2 MiB) Bytes in central cache freelist\nMALLOC: + 317440 ( 0.3 MiB) Bytes in transfer cache freelist\nMALLOC: + 508728 ( 0.5 MiB) Bytes in thread cache freelists\nMALLOC: + 1302720 ( 1.2 MiB) Bytes in malloc metadata\nMALLOC: ------------\nMALLOC: = 64946368 ( 61.9 MiB) Actual memory used (physical + swap)\nMALLOC: + 16384 ( 0.0 MiB) Bytes released to OS (aka unmapped)\nMALLOC: ------------\nMALLOC: = 64962752 ( 62.0 MiB) Virtual address space used\nMALLOC:\nMALLOC: 468 Spans in use\nMALLOC: 13 Thread heaps in use\nMALLOC: 4096 Tcmalloc page size\n------------------------------------------------\nCall ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).\nBytes released to the OS take up virtual address space but no physical memory.\n"
}
},
"wiredTiger" : {
"uri" : "statistics:",
"LSM" : {
"application work units currently queued" : 0,
"merge work units currently queued" : 0,
"rows merged in an LSM tree" : 0,
"sleep for LSM checkpoint throttle" : 0,
"sleep for LSM merge throttle" : 0,
"switch work units currently queued" : 0,
"tree maintenance operations discarded" : 0,
"tree maintenance operations executed" : 0,
"tree maintenance operations scheduled" : 0,
"tree queue hit maximum" : 0
},
"async" : {
"current work queue length" : 0,
"maximum work queue length" : 0,
"number of allocation state races" : 0,
"number of flush calls" : 0,
"number of operation slots viewed for allocation" : 0,
"number of times operation allocation failed" : 0,
"number of times worker found no work" : 0,
"total allocations" : 0,
"total compact calls" : 0,
"total insert calls" : 0,
"total remove calls" : 0,
"total search calls" : 0,
"total update calls" : 0
},
"block-manager" : {
"blocks pre-loaded" : 9,
"blocks read" : 32,
"blocks written" : 23,
"bytes read" : 143360,
"bytes written" : 118784,
"bytes written for checkpoint" : 118784,
"mapped blocks read" : 0,
"mapped bytes read" : 0
},
"cache" : {
"application threads page read from disk to cache count" : 8,
"application threads page read from disk to cache time (usecs)" : 11,
"application threads page write from cache to disk count" : 0,
"application threads page write from cache to disk time (usecs)" : 0,
"bytes belonging to page images in the cache" : 24235,
"bytes currently in the cache" : 66886,
"bytes not belonging to page images in the cache" : 42651,
"bytes read into cache" : 22440,
"bytes written from cache" : 36405,
"checkpoint blocked page eviction" : 0,
"eviction calls to get a page" : 8,
"eviction calls to get a page found queue empty" : 8,
"eviction calls to get a page found queue empty after locking" : 0,
"eviction currently operating in aggressive mode" : 0,
"eviction empty score" : 0,
"eviction server candidate queue empty when topping up" : 0,
"eviction server candidate queue not empty when topping up" : 0,
"eviction server evicting pages" : 0,
"eviction server slept, because we did not make progress with eviction" : 0,
"eviction server unable to reach eviction goal" : 0,
"eviction state" : 16,
"eviction walks abandoned" : 0,
"eviction worker thread active" : 0,
"eviction worker thread created" : 0,
"eviction worker thread evicting pages" : 0,
"eviction worker thread removed" : 0,
"eviction worker thread stable number" : 0,
"failed eviction of pages that exceeded the in-memory maximum" : 0,
"files with active eviction walks" : 0,
"files with new eviction walks started" : 0,
"force re-tuning of eviction workers once in a while" : 0,
"hazard pointer blocked page eviction" : 0,
"hazard pointer check calls" : 0,
"hazard pointer check entries walked" : 0,
"hazard pointer maximum array length" : 0,
"in-memory page passed criteria to be split" : 0,
"in-memory page splits" : 0,
"internal pages evicted" : 0,
"internal pages split during eviction" : 0,
"leaf pages split during eviction" : 0,
"lookaside table insert calls" : 0,
"lookaside table remove calls" : 0,
"maximum bytes configured" : 3617587200,
"maximum page size at eviction" : 0,
"modified pages evicted" : 0,
"modified pages evicted by application threads" : 0,
"overflow pages read into cache" : 0,
"overflow values cached in memory" : 0,
"page split during eviction deepened the tree" : 0,
"page written requiring lookaside records" : 0,
"pages currently held in the cache" : 19,
"pages evicted because they exceeded the in-memory maximum" : 0,
"pages evicted because they had chains of deleted items" : 0,
"pages evicted by application threads" : 0,
"pages queued for eviction" : 0,
"pages queued for urgent eviction" : 0,
"pages queued for urgent eviction during walk" : 0,
"pages read into cache" : 18,
"pages read into cache requiring lookaside entries" : 0,
"pages requested from the cache" : 224,
"pages seen by eviction walk" : 0,
"pages selected for eviction unable to be evicted" : 0,
"pages walked for eviction" : 0,
"pages written from cache" : 11,
"pages written requiring in-memory restoration" : 0,
"percentage overhead" : 8,
"tracked bytes belonging to internal pages in the cache" : 18760,
"tracked bytes belonging to leaf pages in the cache" : 48126,
"tracked dirty bytes in the cache" : 0,
"tracked dirty pages in the cache" : 0,
"unmodified pages evicted" : 0
},
"connection" : {
"auto adjusting condition resets" : 26,
"auto adjusting condition wait calls" : 63348,
"detected system time went backwards" : 0,
"files currently open" : 13,
"memory allocations" : 151394,
"memory frees" : 150579,
"memory re-allocations" : 42302,
"pthread mutex condition wait calls" : 169952,
"pthread mutex shared lock read-lock calls" : 63537,
"pthread mutex shared lock write-lock calls" : 10593,
"total fsync I/Os" : 31,
"total read I/Os" : 672,
"total write I/Os" : 38
},
"cursor" : {
"cursor create calls" : 44,
"cursor insert calls" : 11,
"cursor next calls" : 76,
"cursor prev calls" : 6,
"cursor remove calls" : 1,
"cursor reset calls" : 206,
"cursor restarted searches" : 0,
"cursor search calls" : 225,
"cursor search near calls" : 1,
"cursor update calls" : 0,
"truncate calls" : 0
},
"data-handle" : {
"connection data handles currently active" : 10,
"connection sweep candidate became referenced" : 0,
"connection sweep dhandles closed" : 0,
"connection sweep dhandles removed from hash list" : 3,
"connection sweep time-of-death sets" : 3,
"connection sweeps" : 1054,
"session dhandles swept" : 0,
"session sweep attempts" : 18
},
"lock" : {
"checkpoint lock acquisitions" : 3,
"checkpoint lock application thread wait time (usecs)" : 0,
"checkpoint lock internal thread wait time (usecs)" : 1,
"handle-list lock eviction thread wait time (usecs)" : 2024,
"metadata lock acquisitions" : 3,
"metadata lock application thread wait time (usecs)" : 0,
"metadata lock internal thread wait time (usecs)" : 0,
"schema lock acquisitions" : 14,
"schema lock application thread wait time (usecs)" : 0,
"schema lock internal thread wait time (usecs)" : 0,
"table lock acquisitions" : 0,
"table lock application thread time waiting for the table lock (usecs)" : 0,
"table lock internal thread time waiting for the table lock (usecs)" : 0
},
"log" : {
"busy returns attempting to switch slots" : 0,
"consolidated slot closures" : 8,
"consolidated slot join active slot closed" : 0,
"consolidated slot join races" : 0,
"consolidated slot join transitions" : 8,
"consolidated slot joins" : 10,
"consolidated slot transitions unable to find free slot" : 0,
"consolidated slot unbuffered writes" : 0,
"log bytes of payload data" : 3474,
"log bytes written" : 4736,
"log files manually zero-filled" : 0,
"log flush operations" : 105336,
"log force write operations" : 115901,
"log force write operations skipped" : 115898,
"log records compressed" : 4,
"log records not compressed" : 0,
"log records too small to compress" : 6,
"log release advances write LSN" : 5,
"log scan operations" : 5,
"log scan records requiring two reads" : 0,
"log server thread advances write LSN" : 3,
"log server thread write LSN walk skipped" : 11486,
"log sync operations" : 8,
"log sync time duration (usecs)" : 694065,
"log sync_dir operations" : 1,
"log sync_dir time duration (usecs)" : 16829,
"log write operations" : 10,
"logging bytes consolidated" : 4352,
"maximum log file size" : 104857600,
"number of pre-allocated log files to create" : 2,
"pre-allocated log files not ready and missed" : 1,
"pre-allocated log files prepared" : 2,
"pre-allocated log files used" : 0,
"records processed by log scan" : 9,
"total in-memory size of compressed records" : 5809,
"total log buffer size" : 33554432,
"total size of compressed records" : 3330,
"written slots coalesced" : 0,
"yields waiting for previous log file close" : 0
},
"reconciliation" : {
"fast-path pages deleted" : 0,
"page reconciliation calls" : 11,
"page reconciliation calls for eviction" : 0,
"pages deleted" : 0,
"split bytes currently awaiting free" : 0,
"split objects currently awaiting free" : 0
},
"session" : {
"open cursor count" : 29,
"open session count" : 16,
"table alter failed calls" : 0,
"table alter successful calls" : 0,
"table alter unchanged and skipped" : 0,
"table compact failed calls" : 0,
"table compact successful calls" : 0,
"table create failed calls" : 0,
"table create successful calls" : 0,
"table drop failed calls" : 0,
"table drop successful calls" : 0,
"table rebalance failed calls" : 0,
"table rebalance successful calls" : 0,
"table rename failed calls" : 0,
"table rename successful calls" : 0,
"table salvage failed calls" : 0,
"table salvage successful calls" : 0,
"table truncate failed calls" : 0,
"table truncate successful calls" : 0,
"table verify failed calls" : 0,
"table verify successful calls" : 0
},
"thread-state" : {
"active filesystem fsync calls" : 0,
"active filesystem read calls" : 0,
"active filesystem write calls" : 0
},
"thread-yield" : {
"application thread time evicting (usecs)" : 0,
"application thread time waiting for cache (usecs)" : 0,
"page acquire busy blocked" : 0,
"page acquire eviction blocked" : 0,
"page acquire locked blocked" : 0,
"page acquire read blocked" : 0,
"page acquire time sleeping (usecs)" : 0
},
"transaction" : {
"number of named snapshots created" : 0,
"number of named snapshots dropped" : 0,
"transaction begins" : 16,
"transaction checkpoint currently running" : 0,
"transaction checkpoint generation" : 3,
"transaction checkpoint max time (msecs)" : 472,
"transaction checkpoint min time (msecs)" : 105,
"transaction checkpoint most recent time (msecs)" : 105,
"transaction checkpoint scrub dirty target" : 0,
"transaction checkpoint scrub time (msecs)" : 0,
"transaction checkpoint total time (msecs)" : 719,
"transaction checkpoints" : 3,
"transaction checkpoints skipped because database was clean" : 173,
"transaction failures due to cache overflow" : 0,
"transaction fsync calls for checkpoint after allocating the transaction ID" : 3,
"transaction fsync duration for checkpoint after allocating the transaction ID (usecs)" : 0,
"transaction range of IDs currently pinned" : 0,
"transaction range of IDs currently pinned by a checkpoint" : 0,
"transaction range of IDs currently pinned by named snapshots" : 0,
"transaction sync calls" : 0,
"transactions committed" : 2,
"transactions rolled back" : 14
},
"concurrentTransactions" : {
"write" : {
"out" : 0,
"available" : 128,
"totalTickets" : 128
},
"read" : {
"out" : 1,
"available" : 127,
"totalTickets" : 128
}
}
},
"mem" : {
"bits" : 64,
"resident" : 40,
"virtual" : 929,
"supported" : true,
"mapped" : 0,
"mappedWithJournal" : 0
},
"metrics" : {
"commands" : {
"buildInfo" : {
"failed" : NumberLong(0),
"total" : NumberLong(6)
},
"delete" : {
"failed" : NumberLong(0),
"total" : NumberLong(1)
},
"find" : {
"failed" : NumberLong(0),
"total" : NumberLong(5)
},
"getLog" : {
"failed" : NumberLong(0),
"total" : NumberLong(3)
},
"isMaster" : {
"failed" : NumberLong(0),
"total" : NumberLong(23)
},
"listCollections" : {
"failed" : NumberLong(0),
"total" : NumberLong(3)
},
"listDatabases" : {
"failed" : NumberLong(0),
"total" : NumberLong(1)
},
"replSetGetStatus" : {
"failed" : NumberLong(3),
"total" : NumberLong(3)
},
"serverStatus" : {
"failed" : NumberLong(0),
"total" : NumberLong(2)
},
"whatsmyuri" : {
"failed" : NumberLong(0),
"total" : NumberLong(3)
}
},
"cursor" : {
"timedOut" : NumberLong(0),
"open" : {
"noTimeout" : NumberLong(0),
"pinned" : NumberLong(0),
"total" : NumberLong(0)
}
},
"document" : {
"deleted" : NumberLong(0),
"inserted" : NumberLong(0),
"returned" : NumberLong(5),
"updated" : NumberLong(0)
},
"getLastError" : {
"wtime" : {
"num" : 0,
"totalMillis" : 0
},
"wtimeouts" : NumberLong(0)
},
"operation" : {
"scanAndOrder" : NumberLong(0),
"writeConflicts" : NumberLong(0)
},
"queryExecutor" : {
"scanned" : NumberLong(0),
"scannedObjects" : NumberLong(5)
},
"record" : {
"moves" : NumberLong(0)
},
"repl" : {
"executor" : {
"counters" : {
"eventCreated" : 0,
"eventWait" : 0,
"cancels" : 0,
"waits" : 0,
"scheduledNetCmd" : 0,
"scheduledDBWork" : 0,
"scheduledXclWork" : 0,
"scheduledWorkAt" : 0,
"scheduledWork" : 0,
"schedulingFailures" : 0
},
"queues" : {
"networkInProgress" : 0,
"dbWorkInProgress" : 0,
"exclusiveInProgress" : 0,
"sleepers" : 0,
"ready" : 0,
"free" : 0
},
"unsignaledEvents" : 0,
"eventWaiters" : 0,
"shuttingDown" : false,
"networkInterface" : "\nNetworkInterfaceASIO Operations' Diagnostic:\nOperation: Count: \nConnecting 0 \nIn Progress 0 \nSucceeded 0 \nCanceled 0 \nFailed 0 \nTimed Out 0 \n\n"
},
"apply" : {
"attemptsToBecomeSecondary" : NumberLong(0),
"batches" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0)
},
"buffer" : {
"count" : NumberLong(0),
"maxSizeBytes" : NumberLong(0),
"sizeBytes" : NumberLong(0)
},
"initialSync" : {
"completed" : NumberLong(0),
"failedAttempts" : NumberLong(0),
"failures" : NumberLong(0)
},
"network" : {
"bytes" : NumberLong(0),
"getmores" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0),
"readersCreated" : NumberLong(0)
},
"preload" : {
"docs" : {
"num" : 0,
"totalMillis" : 0
},
"indexes" : {
"num" : 0,
"totalMillis" : 0
}
}
},
"storage" : {
"freelist" : {
"search" : {
"bucketExhausted" : NumberLong(0),
"requests" : NumberLong(0),
"scanned" : NumberLong(0)
}
}
},
"ttl" : {
"deletedDocuments" : NumberLong(0),
"passes" : NumberLong(175)
}
},
"ok" : 1
}
db.stat()
> db.stats()
{
"db" : "mydb",
"collections" : 1,
"views" : 0,
"objects" : 1,
"avgObjSize" : 87,
"dataSize" : 87,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 16384,
"ok" : 1
} 坚持原创技术分享,您的支持将鼓励我继续创作!
打赏
微信支付
支付宝
- 本文作者: 杨玉龙
- 本文链接: fm126.xyz/2017/11/16/…
- 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!