@Tarun_Gaur Thanks for the attention.
My Mongodb version is: MongoDB server version: 4.2.23-rc0. MongoDB compass is version 1.33.0.
Explain Response:
{ stages:
[ { '$cursor':
{ query:
{ start_time:
{ '$gte': 2022-09-06T04:28:31.779Z,
'$lte': 2022-10-06T04:28:31.779Z } },
fields:
{ distance: 1,
harsh_breaking_counts: 1,
idling_counts: 1,
rpm_counts: 1,
speeding_incident_counts: 1,
start_time: 1,
vehicle_id: 1,
_id: 0 },
queryPlanner:
{ plannerVersion: 1,
namespace: 'kpi_rps.journeys',
indexFilterSet: false,
parsedQuery:
{ '$and':
[ { start_time: { '$lte': 2022-10-06T04:28:31.779Z } },
{ start_time: { '$gte': 2022-09-06T04:28:31.779Z } } ] },
queryHash: '1367AE20',
planCacheKey: '4A44DFBB',
winningPlan:
{ stage: 'FETCH',
inputStage:
{ stage: 'IXSCAN',
keyPattern: { start_time: 1 },
indexName: 'start_time_1',
isMultiKey: false,
multiKeyPaths: { start_time: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { start_time: [ '[{ $date: { $numberLong: "1662438511779" } }, { $date: { $numberLong: "1665030511779" } }]' ] } } },
rejectedPlans: [] },
executionStats:
{ executionSuccess: true,
nReturned: 0,
executionTimeMillis: 2,
totalKeysExamined: 0,
totalDocsExamined: 0,
executionStages:
{ stage: 'FETCH',
nReturned: 0,
executionTimeMillisEstimate: 0,
works: 1,
advanced: 0,
needTime: 0,
needYield: 0,
saveState: 1,
restoreState: 1,
isEOF: 1,
docsExamined: 0,
alreadyHasObj: 0,
inputStage:
{ stage: 'IXSCAN',
nReturned: 0,
executionTimeMillisEstimate: 0,
works: 1,
advanced: 0,
needTime: 0,
needYield: 0,
saveState: 1,
restoreState: 1,
isEOF: 1,
keyPattern: { start_time: 1 },
indexName: 'start_time_1',
isMultiKey: false,
multiKeyPaths: { start_time: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { start_time: [ '[{ $date: { $numberLong: "1662438511779" } }, { $date: { $numberLong: "1665030511779" } }]' ] },
keysExamined: 0,
seeks: 1,
dupsTested: 0,
dupsDropped: 0 } } } } },
{ '$lookup':
{ from: 'vehicles',
as: 'vehicle',
localField: 'vehicle_id',
foreignField: 'id',
unwinding: { preserveNullAndEmptyArrays: false },
matching: { status: { '$in': [ 'Roadworthy', 'Roadworthy (with defects)', 'VOR' ] } } } },
{ '$group':
{ _id: { '$dateToString': { date: '$start_time', format: { '$const': '%Y-%m-%d' } } },
total_distance: { '$sum': '$distance' },
total_speeding_incidents: { '$sum': '$speeding_incident_counts' },
total_breaking_incidents: { '$sum': '$harsh_breaking_counts' },
total_idlinging_incidents: { '$sum': '$idling_counts' },
total_rpm_incidents: { '$sum': '$rpm_counts' } } } ],
serverInfo:
{ host: 'DESKTOP-1IBPCFM',
port: 27017,
version: '4.2.23-rc0',
gitVersion: 'cf91e1fbb5f45590d8e356e57522648381fea93c' },
ok: 1 }
Journeys Documents :
{ _id: ObjectId("631360201fe5008c790afe10"),
id: 1,
vehicle_id: 181,
user_id: 1,
start_time: 2022-02-07T12:37:11.000Z,
end_time: 2022-02-07T12:43:19.000Z,
start_lat: Decimal128("51.52837"),
start_lon: Decimal128("-3.07873"),
end_lat: Decimal128("51.530338"),
end_lon: Decimal128("-3.102211"),
engine_duration: 368,
idle_duration: 128,
fuel: Decimal128("0.43"),
co2: Decimal128("1.10"),
distance: 1890,
odometer: 17370605,
odometer_start: 17368668,
odometer_end: 17370605,
avg_speed: 3.44,
max_speed: 12,
incident_count: 0,
harsh_breaking_count: 0,
harsh_acceleration_count: 0,
harsh_cornering_count: 0,
speeding_count: 0,
speeding_incident_count: null,
rpm_count: 0,
idling_count: 0,
updated_at: 2022-09-03T14:09:36.838Z,
created_at: 2022-09-03T14:09:36.838Z }
{ _id: ObjectId("631360201fe5008c790afe11"),
id: 2,
vehicle_id: 181,
user_id: 1,
start_time: 2022-02-07T12:45:26.000Z,
end_time: 2022-02-07T13:34:13.000Z,
start_lat: Decimal128("51.53034"),
start_lon: Decimal128("-3.10221"),
end_lat: Decimal128("51.656443"),
end_lon: Decimal128("-3.337073"),
engine_duration: 2927,
idle_duration: 1261,
fuel: Decimal128("2.98"),
co2: Decimal128("7.70"),
distance: 32559,
odometer: 17403336,
odometer_start: 17370605,
odometer_end: 17403336,
avg_speed: 8.81,
max_speed: 26,
incident_count: 2,
harsh_breaking_count: 0,
harsh_acceleration_count: 0,
harsh_cornering_count: 0,
speeding_count: 0,
speeding_incident_count: null,
rpm_count: 0,
idling_count: 2,
updated_at: 2022-09-03T14:09:36.839Z,
created_at: 2022-09-03T14:09:36.839Z }
{ _id: ObjectId("631360201fe5008c790afe12"),
id: 3,
vehicle_id: 181,
user_id: 1,
start_time: 2022-02-07T13:48:58.000Z,
end_time: 2022-02-07T13:59:11.000Z,
start_lat: Decimal128("51.65644"),
start_lon: Decimal128("-3.33707"),
end_lat: Decimal128("51.656464"),
end_lon: Decimal128("-3.337021"),
engine_duration: 613,
idle_duration: 608,
fuel: Decimal128("0.00"),
co2: Decimal128("0.00"),
distance: 0,
odometer: 17403336,
odometer_start: 17403336,
odometer_end: 17403336,
avg_speed: 0,
max_speed: 0,
incident_count: 1,
harsh_breaking_count: 0,
harsh_acceleration_count: 0,
harsh_cornering_count: 0,
speeding_count: 0,
speeding_incident_count: null,
rpm_count: 0,
idling_count: 1,
updated_at: 2022-09-03T14:09:36.840Z,
created_at: 2022-09-03T14:09:36.840Z }
{ _id: ObjectId("631360201fe5008c790afe13"),
id: 4,
vehicle_id: 181,
user_id: 1,
start_time: 2022-02-07T14:30:12.000Z,
end_time: 2022-02-07T14:47:54.000Z,
start_lat: Decimal128("51.65646"),
start_lon: Decimal128("-3.33702"),
end_lat: Decimal128("51.696191"),
end_lon: Decimal128("-3.346751"),
engine_duration: 1062,
idle_duration: 248,
fuel: Decimal128("0.59"),
co2: Decimal128("1.50"),
distance: 9755,
odometer: 17413179,
odometer_start: 17403336,
odometer_end: 17413179,
avg_speed: 8.05,
max_speed: 21,
incident_count: 0,
harsh_breaking_count: 0,
harsh_acceleration_count: 0,
harsh_cornering_count: 0,
speeding_count: 0,
speeding_incident_count: null,
rpm_count: 0,
idling_count: 0,
updated_at: 2022-09-03T14:09:36.841Z,
created_at: 2022-09-03T14:09:36.841Z }
{ _id: ObjectId("631360201fe5008c790afe14"),
id: 5,
vehicle_id: 181,
user_id: 1,
start_time: 2022-02-07T15:11:53.000Z,
end_time: 2022-02-07T15:14:38.000Z,
start_lat: Decimal128("51.69619"),
start_lon: Decimal128("-3.34675"),
end_lat: Decimal128("51.696226"),
end_lon: Decimal128("-3.346785"),
engine_duration: 165,
idle_duration: 0,
fuel: Decimal128("0.00"),
co2: Decimal128("0.00"),
distance: 0,
odometer: 17413179,
odometer_start: 17413179,
odometer_end: 17413179,
avg_speed: 0,
max_speed: 0,
incident_count: 0,
harsh_breaking_count: 0,
speeding_incident_count: null,
rpm_count: 0,
idling_count: 0 }
Vehicles Documents:
{ _id: ObjectId("6313601f1fe5008c790afbc5"),
id: 1,
status: 'Roadworthy',
telematics_status: 'tm8.gps.ign.off',
last_location_lat: Decimal128("51.353667"),
last_location_lon: Decimal128("-0.482931"),
last_location_time: 2022-09-02T17:38:59.000Z,
updated_at: 2022-09-03T14:09:35.564Z,
created_at: 2022-09-03T14:09:35.564Z }
{ _id: ObjectId("6313601f1fe5008c790afbc6"),
id: 2,
status: 'Roadworthy',
telematics_status: 'tm8.gps.ign.off',
last_location_lat: Decimal128("56.206825"),
last_location_lon: Decimal128("-3.17141"),
last_location_time: 2022-06-14T10:27:16.000Z,
updated_at: 2022-09-03T14:09:35.566Z,
created_at: 2022-09-03T14:09:35.566Z }
{ _id: ObjectId("6313601f1fe5008c790afbc7"),
id: 3,
status: 'Roadworthy',
telematics_status: null,
last_location_lat: null,
last_location_lon: null,
last_location_time: null,
updated_at: 2022-09-03T14:09:35.566Z,
created_at: 2022-09-03T14:09:35.566Z }
{ _id: ObjectId("6313601f1fe5008c790afbc8"),
id: 4,
status: 'Archived',
telematics_status: null,
last_location_lat: null,
last_location_lon: null,
last_location_time: null,
updated_at: 2022-09-03T14:09:35.567Z,
created_at: 2022-09-03T14:09:35.567Z }
{ _id: ObjectId("6313601f1fe5008c790afbc9"),
id: 5,
status: 'Roadworthy',
telematics_status: 'tm8.gps.ign.off',
last_location_lat: Decimal128("53.382753"),
last_location_lon: Decimal128("-2.189482"),
last_location_time: 2022-09-02T11:56:21.000Z,
updated_at: 2022-09-03T14:09:35.568Z,
created_at: 2022-09-03T14:09:35.568Z }
My Query that takes approx 1 min:
db.journeys.aggregate([
{
"$match": {
"start_time": {
"$gte": { "$date": { "$numberLong": "1662440173747" } },
"$lte": { "$date": { "$numberLong": "1665032173747" } }
}
}
},
{
"$lookup": {
"from": "vehicles",
"localField": "vehicle_id",
"foreignField": "id",
"as": "vehicle"
}
},
{ "$unwind": { "path": "$vehicle" } },
{
"$match": {
"vehicle.status": {
"$in": ["Roadworthy", "Roadworthy (with defects)", "VOR"]
}
}
},
{
"$group": {
"_id": {
"$dateToString": { "format": "%Y-%m-%d", "date": "$start_time" }
},
"total_distance": { "$sum": "$distance" },
"total_speeding_incidents": { "$sum": "$speeding_incident_counts" },
"total_breaking_incidents": { "$sum": "$harsh_breaking_counts" },
"total_idlinging_incidents": { "$sum": "$idling_counts" },
"total_rpm_incidents": { "$sum": "$rpm_counts" }
}
}
])
Indexes applied on all the columns that used inside query:
-
Start_time
-
speeding_incident_count
-
harsh_breaking_count
-
distance
-
idling_count
-
rpm_count
And other fields applied Indexes and also it is used when query run.
Currently it is in my local system and still it takes time to load. My system has 8 GB RAM and using i5 processor.
Thanks.