Azure notified me that a query I had was not performant because of an ORDER BY clause and it would greatly benefit from adding a composite index. I've done a bunch of reading and tried repeatedly to add a composite index for this query but unfortunately I've either had no improvement or a slight increase in RUs for my query with my attempts.
I have the following piece to my query:
WHERE
p.BusinessId = 'id' AND
p.PurchaseDate >= '2018-07-04T16:26:42.0882176-06:00' AND p.PurchaseDate < '2023-07-10T15:26:42.0882176-06:00' AND
NOT Contains(LOWER(p.TransactionIdentifier), 'history')
ORDER BY p.PurchaseDate DESC
Based on all the reading I've done I should have two composite indexes, a (businessId, purchaseDate descending) and a (businessId, transactionIdentifier). Because I can only have one range or function per composite index and I have two so I would need two indexes. That does not work, however, because it's complaining about not having a composite index that matches. The only configuration that it doesn't complain about is (businessId, purchaseDate descending, transactionIdentifer) which actually increases the RUs required to run the query.
Documentation also states that I should include these filters in my ORDER BY for improved performance. I've tried adding those as well. Nothing seems to give me any reduction in RUs.
I've been trying to follow along with the following resources and I'm just not making progress:
- https://learn.microsoft.com/en-us/azure/cosmos-db/index-policy
- https://willvelida.medium.com/understanding-indexing-in-azure-cosmos-db-62299c351a19
- https://learn.microsoft.com/en-us/azure/cosmos-db/index-overview
- https://devblogs.microsoft.com/cosmosdb/new-ways-to-use-composite-indexes/
I'm stuck. Can anyone help me figure out the proper composite index configuration for this query?