简体   繁体   中英

DocumentClient takes too long when queried over a DateTime field

I have the following code to get the list of some objects from a DocumentDB database:

var document = this._client.CreateDocumentQuery<T>(UriFactory.CreateCollectionUri(dbName, collectionName), queryOptions)
.Where(r => r.pDate >= startDate && r.pDate <= endDate);

var result = document.ToList();

pDate is of type DateTime , and stored in the database as string with ISO8601 format.

The query takes unreasonably too long, like 4 to 5 minutes, to return the results back. When I trace the program it is that .ToList() where the program gets stuck. Oddly, the query quickly returns for some specific start and end dates. The query also quickly comes back with some results if I put the filter on some fields other than pDate .

My settings are consistent with explanations in this document but I still get a very poor performance almost all the time except for those few exceptions.

I have tried several methods mentioned here and there to resolve the issue, but no luck so far. I appreciate any comment or solution to the problem.

It could be because the indexing that is applied on this particular field.

From this link https://docs.microsoft.com/en-us/azure/cosmos-db/indexing-policies , you would need an Range index for range or Order by Queries.

The default index policy that is applied to a collection is "Hash for Strings and Range for Numbers"

Datetimes are stored as strings, so for running range comparison queries or order by queries you would need to set the indexing policy to "Range" for string datatypes with precision to -1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM