Perform a Lazy Query
-
Query execution can be deferred: the query can be defined as Lazy, and executed at a later time, when its results are actually needed.
-
This article contains examples for lazy queries. Prior to reading it, please refer to perform requests lazily for general knowledge about RavenDB's lazy behavior and other request types that can be executed lazily within a session.
-
Learn more about queries in this query overview.
-
In this page:
Lazy query
/** @var Lazy<array<Employee>> $employeesLazy */
$employeesLazy = $session
->query(Employee::class)
->whereEquals("FirstName", "Robert")
->lazily();
/** @var array<Employee> $employees */
$employees = $employeesLazy->getValue(); // query will be executed here
Lazy count query
/** @var Lazy<int> $countLazy */
$countLazy = $session
->query(Employee::class)
->whereEquals("FirstName", "Robert")
->countLazily();
/** @var int $count */
$count = $countLazy->getValue(); // query will be executed here
Lazy suggestions query
/** @var Lazy<array<string, SuggestionResult>> $suggestLazy */
$suggestLazy = $session
->query(Employee::class, Query::index("Employees_ByFullName"))
->suggestUsing(function($builder) { $builder->byField("FullName", "johne"); })
->executeLazy();
/** @var array<string, SuggestionResult> $suggest */
$suggest = $suggestLazy->getValue(); // query will be executed here
/** @var array<string> $suggestions */
$suggestions = $suggest["FullName"]->getSuggestions();
- Learn more about suggestions in query for suggestions.
Lazy aggregation
/** @var Lazy<array<string, FacetResult>> $facetsLazy */
$facetsLazy = $session
->query(Camera::class, Query::index("Camera/Costs"))
->aggregateUsing("facets/CameraFacets")
->executeLazy();
/** @var array<string, FacetResult> $facets */
$facets = $facetsLazy->getValue(); // query will be executed here
/** @var FacetResult $results */
$results = $facets["manufacturer"];
Syntax
/**
* Usage
* - lazily();
* - lazily(Closure $onEval)
*/
function lazily(?Closure $onEval = null): Lazy;
function countLazily(): Lazy;
/**
* Usage
* - executeLazy();
* - executeLazy(Closure $onEval)
*/
public function executeLazy(?Closure $onEval = null): Lazy;
Parameters | Type | Description |
---|---|---|
$onEval | ?Closure |
An action to perform on the query results when the query is executed |
Return Value | |
---|---|
Lazy |
A lazy instance that will evaluate the query only when needed |