Is there a way to create the index based on some field that is order by desc.
I don’t wish to add the order by in the query.
{
“name” : “value”,
“lastModified” : 1633593117523,
“type” : “group”
}
Sample:
select * from bucket as doc where doc.name = "value " where doc.type = “group” order by doc.lastModified desc
Current Index
create index ‘value’ ON bucket (‘name’, ‘lastModified’) where type = “group”
Desired State
Query : select * from bucket as doc where doc.name = "value " where type = “group”
Index : create index ‘value’ ON bucket (‘name’, ‘lastModified desc’) where type = “group” // something like this
select *
from bucket as doc
where doc.name = "value " where doc.type = “group”
order by doc.lastModified desc
create index ix1ON `bucket` (name, lastModified DESC ) where type = “group”;
If your query requires ORDER BY, You must provide ORDER BY otherwise there is no guarantee (Sort is expensive operation. If user not required and doing one take out all resources and impact latency).
Based on query if it can utilize index order it will avoid sort (Can be seen in EXPLAIN missing Order operator at the end). If it can’t use index order it will do explicit sort.
Without Query ORDER BY, results can be in sorted order of index some cases, but it is no guarantee.