I have the following document data model named tvSessions:
“id”: “MEMCW:118:E81863521C47:2020-10-20”
{
"propCode": "MEMCW",
"roomNumber": "118",
"macAddress": "E81863521C47",
"date": "2020-10-20",
"source": "iptv",
"lastReportedTime": 1603224693,
"channels": [
{
"channelName": "TBS",
"channelId": 22,
"sessions": [
{
"isOccupied": 0,
"created": 1603224693,
"startTime": 1603224693,
"stopTime": 1603224693,
"duration": 665
},
{
"isOccupied": 0,
"created": 1603224693,
"startTime": 1603224693,
"stopTime": 1603224693,
"duration": 611
}
]
},
{
"channelName": "NBC",
"channelId": 28,
"sessions": [
{
"isOccupied": 0,
"created": 1603224693,
"startTime": 1603224693,
"stopTime": 1603224693,
"duration": 665
},
{
"isOccupied": 0,
"created": 1603224693,
"startTime": 1603224693,
"stopTime": 1603224693,
"duration": 611
}
]
}
]
}
I have the following indexes and I can not change or add any.
CREATE INDEX index_tvSessions_date ON connectedroom.ecmp.tvSessions(date);
CREATE INDEX index_tvSessions_source ON connectedroom.ecmp.tvSessions(source);
CREATE INDEX index_tvSessions_composite ON connectedroom.ecmp.tvSessions(propCode, roomNumber, macAddress);
My query:
SELECT document.*,
(
SELECT c.*,
(
SELECT s.*
FROM c.sessions AS s
WHERE s.duration >= $minDuration
AND s.isOccupied = 1 ) AS sessions
FROM document.channels AS c
WHERE ($channelName IS NULL
OR c.channelName = $channelName)
AND ($channelId IS NULL
OR c.channelId = $channelId) ) AS channels
FROM tvSessions AS document
WHERE document.date BETWEEN $startDate AND $endDate
AND document.source = "iptv"
AND ($propCode IS NULL
OR document.propCode = $propCode)
AND ($roomNumber IS NULL
OR document.roomNumber = $roomNumber)
AND ($macAddress IS NULL
OR document.macAddress = $macAddress)
AND ($channelName IS NULL
OR ANY c IN document.channels SATISFIES c.channelName = $channelName
AND ($channelId IS NULL
OR c.channelId = $channelId)
AND (ANY s IN c.sessions SATISFIES s.duration >= $minDuration
AND s.isOccupied = 1 END) END)
ORDER BY document.date DESC,
document.lastReportedTime DESC;
My query is not covered but this is strictly for analytics. I’m using this with adhoc(true)
to let the query planner build it and not cache the query based on this discussion:
-
I believe the default ordering of the date index is
ASC
. When usingDESC
, is the comparator flipped resulting in the same performance? -
The
date
field has an index, which is already sorted, but the format does not have enough entropy and results in multiple documents with the same date. SincelastReportedTime
is aLong
in seconds, I break the same values withlastReportedTime DESC
. SincelastReportedTime
does not have an index, from my understanding, a Quicksort is being applied to the results?
Attached is the EXPLAIN if it adds any value.