近似クエリ
Approximate Query Processing (AQP) lets PyroSQL answer aggregate questions — COUNT, SUM, AVG — by scanning a statistical sample of the data instead of reading every row. The result is returned in milliseconds even on tables with billions of rows, at the cost of a small, bounded error margin.
PyroSQL always reports the actual error and confidence level alongside the approximate value, so you can decide programmatically whether the result is precise enough for your use case.
When to use AQP:
- Analytics dashboards that refresh every few seconds and can tolerate ±2 % error.
- Real-time metrics where a response in 20 ms is more valuable than exact precision.
- Capacity planning queries on very large tables where full scans are prohibitively slow.
- Exploratory analysis before deciding whether to run a more expensive exact query.
ApproximateQueryBuilder
ApproximateQueryBuilder is a decorator around EntityQueryBuilder that issues SELECT APPROXIMATE … WITHIN n% CONFIDENCE m% queries against PyroSQL's AQP engine.
Getting a builder from a repository
Use PyroQueryBuilderExtension::approximate() on any repository that mixes in the trait:
$result = $orderRepo->approximate(within: 2.0, confidence: 99.0)
->where('status', 'shipped')
->count();
You can also construct the builder directly:
use Weaver\ORM\PyroSQL\Approximate\ApproximateQueryBuilder;
$qb = $orderRepo->query();
$aqp = new ApproximateQueryBuilder($qb, $connection, within: 5.0, confidence: 95.0);
Default values when not specified: within: 5.0, confidence: 95.0.