5.0 supports DESC collation during index creation. Try the following.
CREATE INDEX desc_int ON default(AInt DESC);
SELECT default.* FROM default WHERE AInt IS NOT MISSING ORDER BY AInt DESC LIMIT 50;
In pre 5.0.0
CREATE INDEX neg_int ON default(-AInt);
SELECT default.* FROM default WHERE -AInt IS NOT MISSING ORDER BY -AInt LIMIT 50;
Index key here is -AInt. Every where in predicate needs to refer as -AInt and the corresponding values, relation conditions, Order collation needs to be reversed such that query results will not be altered.
But this method requires us to create indexes on all keys that may need to be ordered. If I have highly dynamic entries, It is unlikely for us to create indexes on all of those keys.
If the query ORDER BY clause not same as that Index keys order including collision it requires sort. Which means it needs to retrieve all qualified documents before applying pagination.
Thank you. Moreover, based on what you posted earlier, if we can certain which variable(s) we need on query (with order by), we can use composite indexes as well, is this right?
You can use composite indexes also.
NOTE: Query to use Index Order. ORDER BY clause and Index keys order including collision need to match from left to right to avoid SORT. When query uses Index order there will not be Order
operator of the EXPLAIN plan of the query.
In 5.0.0 you can specify the DESC collation for each index key. In Pre 5.0.0 you can use above approach for numeric values or string of timestamp(convert to string of timestamp to numeric using STR_TO_MILLIS()).
Hi! Sorry to resurrect an old thread, but does this same (pre-5.0) method work for 4.5.1? I’m noticing that it completely ignores my ORDER BY when applying the same logic.
Index:
CREATE INDEX `idx_activity__teamID__public_desc` ON `default`((`team`.`_id`),(-`created_at`)) WHERE ((`type` = "activity") and (`private` = false))
Query:
SELECT a._id, a.team._id as teamID, a.created_at
FROM default a
USE INDEX (`idx_activity__teamID__public_desc`)
WHERE a.`type` = "activity" AND a.`private` = false AND a.`team`.`_id` = "f51e4695-479d-4ea6-beab-425ad9484ee9" AND -created_at IS NOT MISSING
ORDER BY -created_at
LIMIT 20
I tried to follow the same negative fieldname as stated in your previous replies, but it seems to ignore the ORDER BY field altogether. If I omit the ORDER BY then it runs insanely fast on 50k+ records (< 20ms). If I change the ORDER BY to have -created_at DESC then it takes 200-250ms, and still does not order the records. If I removed the negative in the index, and keep the ORDER BY created_at DESC then it takes 500+ms. I’ve tried several different ways of omitting, specifying fields (created_at is never missing, by the way), but alas, I have come up empty handed. Any help would be wonderful!
created_at is string. negative can be done on numerics. So change both index and query to use
-MILLIS(created_at) because created_at is timestamp you can convert it to numeric using MILLIS.
Worked amazingly! If I keep the ORDER_BY it still is around 200ms (big improvement over the 500+ms it was before), however, removing the ORDER_BY brings it all the way down to 7ms. Do I need to keep the ORDER_BY to ensure the order? Or will the index with the -MILLIS() do that?
Post the index definition and query EXPLAIN. it should not based on query. May be the query might not covered because you are projecting created_at. So add created_at to index at the end.
Or Porject MILLIS_TO_STR(-(-MILLIS(created_at)))
I apologize, I’m not exactly understanding what you’re saying.
This is the updated index:
CREATE INDEX `idx_activity__teamID__public_desc` ON `default`((`team`.`_id`),(-MILLIS(`created_at`))) WHERE ((`type` = "activity") and (`private` = false))
Didn’t seem to change anything. I’ve removed the created_at check entirely from the WHERE query (as it always exists, I was just using the examples above for specifying it).
CREATE INDEX `idx_activity__teamID__public_desc` ON `default`((`team`.`_id`),(-MILLIS(`created_at`)))
WHERE ((`type` = "activity") and (`private` = false));
SELECT a.*
FROM default a USE INDEX (`idx_activity__teamID__public_desc`)
WHERE a.`type` = "activity" AND a.`private` = false AND a.`team`.`_id` = "f51e4695-479d-4ea6-beab-425ad9484ee9"
AND -MILLIS(a.created_at) <= 0
ORDER BY a.`team`.`_id`, -MILLIS(a.created_at)
LIMIT 20;
In 4.5.1 Try above query. added ORDER BY a.team._id, -MILLIS(a.created_at)
In query where clause a.team._id is equality predicate the result will not change. In 4.6.x Query optimizer detects this and takes care of it.