Relatude Help

Heavy queries, and stopping one

Queries run side by side, and a query that reads many nodes from disk does not hold up writes. A query sees the database as it was when it started.

To stop a query, give its context a cancellation token. The query throws an OperationCanceledException at its next check:

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var ctx = QueryContext.Default.WithCancellation(cts.Token);   // or HttpContext.RequestAborted
var result = db.Query<IEvent>(ctx).Where(e => e.Description.Contains("jazz")).Execute();

Set the token last, after the context's other settings. MaxQueryDurationInSeconds (§12.1) stops every query that runs longer, with a TimeoutException. A query that reads more nodes from disk than WarnWhenQueryReadsNodesFromDisk is noted in the system log: page it, or filter on indexed properties. The admin UI's Query page has a Stop button.

A query waits for a running transaction to finish, and the wait counts toward both limits. To show "busy" rather than wait, set WithLockWait, also last. A query that cannot start in time throws StoreBusyException (a TimeoutException) and has read nothing. Zero does not wait at all.

var ctx = QueryContext.Default.WithLockWait(TimeSpan.FromMilliseconds(100));
try {
    count = db.Query<IEvent>(ctx).Count();
} catch (StoreBusyException) {
    count = null;   // a transaction is running: show "unknown" and ask again later
}