I want to fetch all data from a collection. Since running just find({}) crashes the mongo container (the liveness probe seams to fail because k8s reboot the container), I ask myself if using skip/limit will be better in order to fragment fetch into multiple parts.
Is that query optimized?
db.getCollection('collectionName').find({}).skip(300000).limit(200000)
Mongo logs gives: planSummary: COLLSCAN. Is that normal? Index on _id should be used, isn't it?
Here is some logs while request is performed:
2019-05-05T15:27:48.383+0000 I COMMAND [conn7] command off.TrunkOff command: getMore { getMore: 19694089771, collection: "TrunkOff", batchSize: 1000, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } originatingCommand: { find: "TrunkOff", filter: {}, skip: 350000, limit: 100000, returnKey: false, showRecordId: false, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } planSummary: COLLSCAN cursorid:19694089771 keysExamined:0 docsExamined:1000 numYields:15 nreturned:1000 reslen:10290608 locks:{ Global: { acquireCount: { r: 16 } }, Database: { acquireCount: { r: 16 } }, Collection: { acquireCount: { r: 16 } } } storage:{ data: { bytesRead: 10311365, timeReadingMicros: 300059 } } protocol:op_query 315ms
2019-05-05T15:28:21.281+0000 I COMMAND [conn6] command off.TrunkOff command: getMore { getMore: 19694089771, collection: "TrunkOff", batchSize: 1000, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } originatingCommand: { find: "TrunkOff", filter: {}, skip: 350000, limit: 100000, returnKey: false, showRecordId: false, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } planSummary: COLLSCAN cursorid:19694089771 keysExamined:0 docsExamined:1000 numYields:20 nreturned:1000 reslen:11985643 locks:{ Global: { acquireCount: { r: 21 } }, Database: { acquireCount: { r: 21 } }, Collection: { acquireCount: { r: 21 } } } storage:{ data: { bytesRead: 12016653, timeReadingMicros: 414440 } } protocol:op_query 427ms
2019-05-05T15:30:19.713+0000 I COMMAND [conn6] command off.TrunkOff command: getMore { getMore: 19694089771, collection: "TrunkOff", batchSize: 1000, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } originatingCommand: { find: "TrunkOff", filter: {}, skip: 350000, limit: 100000, returnKey: false, showRecordId: false, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } planSummary: COLLSCAN cursorid:19694089771 keysExamined:0 docsExamined:1000 numYields:13 nreturned:1000 reslen:9986640 locks:{ Global: { acquireCount: { r: 14 } }, Database: { acquireCount: { r: 14 } }, Collection: { acquireCount: { r: 14 } } } storage:{ data: { bytesRead: 10017227, timeReadingMicros: 270095 } } protocol:op_query 287ms
2019-05-05T15:30:44.023+0000 I COMMAND [conn7] command off.TrunkOff command: getMore { getMore: 19694089771, collection: "TrunkOff", batchSize: 1000, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } originatingCommand: { find: "TrunkOff", filter: {}, skip: 350000, limit: 100000, returnKey: false, showRecordId: false, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } planSummary: COLLSCAN cursorid:19694089771 keysExamined:0 docsExamined:1000 numYields:9 nreturned:1000 reslen:9228715 locks:{ Global: { acquireCount: { r: 10 } }, Database: { acquireCount: { r: 10 } }, Collection: { acquireCount: { r: 10 } } } storage:{ data: { bytesRead: 9247329, timeReadingMicros: 102849 } } protocol:op_query 110ms
2019-05-05T15:31:14.048+0000 I COMMAND [conn6] command off.TrunkOff command: getMore { getMore: 19694089771, collection: "TrunkOff", batchSize: 1000, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } originatingCommand: { find: "TrunkOff", filter: {}, skip: 350000, limit: 100000, returnKey: false, showRecordId: false, lsid: { id: UUID("02770ee3-0b54-4b38-9b8e-23063b514434") }, $db: "off" } planSummary: COLLSCAN cursorid:19694089771 keysExamined:0 docsExamined:1000 numYields:21 nreturned:1000 reslen:11922134 locks:{ Global: { acquireCount: { r: 22 } }, Database: { acquireCount: { r: 22 } }, Collection: { acquireCount: { r: 22 } } } storage:{ data: { bytesRead: 11958959, timeReadingMicros: 410002 } } protocol:op_query 428ms
What is a good way to traverse a collection from end to end?
Virtual memory is gbs upper than mapped memory. cacheSizeGb is 1.
If you run mongod in a container (e.g. lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container.
The default is 50% of (available RAM -1 GB).cacheSizeGB
setting._id
field in the query predicate, the default index on the_id
will not be used. For any index to be used, it needs to be in the query filter fields (in case of compound index all the fields of the index or the index-prefix fields need to be used). You can always verify any index usage by generating a query plan using theexplain
. This will show a COLLSCAN or IXSCAN (along with the index used) as appropriate.