Skip to main content

Session: Querying: How to get query statistics?

Query statistics can provide important information about query e.g. duration, total number of results, staleness information, etc. To access statistics use Statistics method.

Syntax

List<Employee> employees = session
.Query<Employee>()
.Where(x => x.FirstName == "Anne")
// Get query stats:
// * Call 'Statistics'
// * Pass an out 'QueryStatistics' param for getting the stats
.Statistics(out QueryStatistics stats)
.ToList();

long numberOfResults = stats.TotalResults; // Get results count
long queryDuration = stats.DurationInMs; // Get query duration
string indexNameUsed = stats.IndexName; // Get index name used in query
// ...
Parameters
statsRavenQueryStatisticsStatistics for query.
Return Value
RavenQueryStatisticsStatistics for query.

Example

List<Employee> employees = await asyncSession
.Query<Employee>()
.Where(x => x.FirstName == "Anne")
// Get query stats:
// * Call 'Statistics'
// * Pass an out 'QueryStatistics' param for getting the stats
.Statistics(out QueryStatistics stats)
.ToListAsync();

long numberOfResults = stats.TotalResults; // Get results count
long queryDuration = stats.DurationInMs; // Get query duration
string indexNameUsed = stats.IndexName; // Get index name used in query
// ...