Query the relevant records
Use explicit filters and the correct endpoint. Check transaction and replica behavior for the deployment you use.
Operational analytics · SQL
Count orders, track queues and summarize activity from the records your application writes.
A grouped SQL query turns individual orders into counts and totals. Your application renders the returned rows as a chart, table or alert.
| id | status | amount_usd |
|---|---|---|
| o-101 | paid | 120 |
| o-102 | paid | 80 |
| o-103 | pending | 60 |
| o-104 | paid | 200 |
| o-105 | pending | 140 |
| o-106 | failed | 50 |
SELECT status,
COUNT(*) AS orders,
SUM(amount_usd) AS total_usd
FROM shop.orders
GROUP BY status;One result row per status, with its order count and total order value.
6 source rows → 3 status groups. Select order value to compare the sums.
$650 across all six orders. Only the paid group represents $400 of paid order value.
From business question to SQL
Choose the rows that belong in the calculation, the grouping key and the meaning of the result.
Read the aggregate guideA plan you can inspect
EXPLAIN returns a plan without executing the SELECT. Inspect scans, filters and aggregates before putting a query behind a frequently refreshed dashboard.
If you expected an index scan, review the schema's index declarations and the predicate. Measure against your own data and instance configuration.
Read query plansEXPLAIN
SELECT status, COUNT(*) AS orders
FROM shop.orders
WHERE status = 'pending'
GROUP BY status;Run this on your instance to inspect the operators chosen for your schema and indexes.
Querying operational records removes a separate analytical copy from this flow. The dashboard still needs a clear refresh policy.
Use explicit filters and the correct endpoint. Check transaction and replica behavior for the deployment you use.
Schedule requests, cache intentionally and display when the last successful query completed.
Keep the previous result during refresh and surface failures. A stale chart should not appear current.
Keep the workload focused
Use this approach for application dashboards, order summaries and workload queues. For large historical scans, evaluate a separate analytical workflow against your requirements.
Sorts, joins and window functions can buffer their inputs. Narrow the dataset before expensive operations; LIMIT after ORDER BY does not remove that work. Handle query limits and retry responses in the application.
Review SQL execution limits