Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Cosmos): Query optimizations #457

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Equinox.CosmosStore/CosmosStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -665,17 +665,20 @@ module internal Query =
yield map i t res
i <- i + 1 }
let private mkQuery (log: ILogger) (container: Container, stream: string) includeTip (maxItems: int) (direction: Direction, minIndex, maxIndex): FeedIterator<Batch> =
let order = if direction = Direction.Forward then "ASC" else "DESC"
let asc, orderString = if direction = Direction.Forward then true, "ASC" else false, "DESC"
let query =
let orderByField = if minIndex |> Option.isSome then 'n' else 'i'
let args = [
match minIndex with None -> () | Some x -> yield "c.n > @minPos", fun (q: QueryDefinition) -> q.WithParameter("@minPos", x)
match maxIndex with None -> () | Some x -> yield "c.i < @maxPos", fun (q: QueryDefinition) -> q.WithParameter("@maxPos", x) ]
let whereClause =
let notTip = sprintf "c.id!=\"%s\"" Tip.WellKnownDocumentId
let notTip = $"c.id!=\"%s{Tip.WellKnownDocumentId}\""
let conditions = Seq.map fst args
if List.isEmpty args && includeTip then null
else "WHERE " + String.Join(" AND ", if includeTip then conditions else Seq.append conditions (Seq.singleton notTip))
let queryString = $"SELECT c.id, c.i, c._etag, c.n, c.e FROM c %s{whereClause} ORDER BY c.i %s{order}"
let queryString =
if asc then $"SELECT c.id, c.i, c._etag, c.n, c.e FROM c %s{whereClause}"
else $"SELECT c.id, c.i, c._etag, c.n, c.e FROM c %s{whereClause} ORDER BY c.{orderByField} %s{orderString}"
let prams = Seq.map snd args
(QueryDefinition queryString, prams) ||> Seq.fold (fun q wp -> q |> wp)
log.Debug("EqxCosmos Query {stream} {query}; n>{minIndex} i<{maxIndex}", stream, query.QueryText, Option.toNullable minIndex, Option.toNullable maxIndex)
Expand Down