Amazon Timestream query editor
The Amazon Timestream query editor lets you write SQL queries against your Timestream databases. It includes a code editor with IntelliSense, macros for dynamic values, and configurable output formats.
Before you begin
- Configure the Amazon Timestream data source.
- Verify that your IAM identity has permissions to query the target database and table.
Key concepts
The following terms are used throughout the query editor documentation.
Query editor fields
The query editor provides the following fields and controls.
Write a query
Use the SQL editor to write Timestream SQL queries. The editor supports IntelliSense for column names, table names, and macros. Press Ctrl+Space to trigger auto-complete suggestions.
To run a query, press Ctrl+Enter or click Run query.
Basic query example
The following query selects all records from the configured database and table within the dashboard time range:
SELECT *
FROM $__database.$__table
WHERE $__timeFilter
ORDER BY time ASC
LIMIT 100Macros
Use macros in your queries to insert dynamic values like time ranges, intervals, and configured defaults. The query engine replaces macros with their computed values before sending the query to Timestream.
Macro example
The following query combines several macros to aggregate data into dashboard-appropriate intervals:
SELECT
bin(time, $__interval_ms) AS binned_time,
measure_name,
avg(measure_value::double) AS avg_value
FROM $__database.$__table
WHERE $__timeFilter
AND measure_name = '$__measure'
GROUP BY bin(time, $__interval_ms), measure_name
ORDER BY binned_time ASCThis query uses $__database, $__table, and $__measure to reference the selections in the query editor, $__timeFilter to scope results to the dashboard time range, and $__interval_ms to group data into intervals that match the panel width.
Plugin macros vs Grafana global variables
The macros listed in the table (such as $__timeFrom and $__timeTo) are plugin macros that the backend expands before sending the query to Timestream. They return raw millisecond values.
Grafana also provides global variables ($__from, $__to) that support custom formatting. These are expanded by the frontend before the query reaches the backend. You can use them in Timestream queries when you need a specific time format:
SELECT *
FROM $__database.$__table
WHERE time >= from_iso8601_timestamp('${__from:date:iso}')
AND time <= from_iso8601_timestamp('${__to:date:iso}')Common Grafana time variable formats:
Note
Use
$__timeFilterinstead of manually constructing time range filters when possible. It handles the time range conversion correctly and is less error-prone than formatting time variables yourself.
For the full list of global variable formats, refer to Global variables.
Sample queries
The query editor includes built-in sample queries you can select from the Sample queries drop-down. These queries use macros and adapt to your selected database and table.
Use cases
The following examples demonstrate common query patterns for Timestream data.
Aggregate metrics over time
Use bin() with $__interval_ms to group data into intervals that match the panel resolution. This is the most common pattern for time-series visualizations.
SELECT
bin(time, $__interval_ms) AS binned_time,
instance_name,
avg(measure_value::double) AS avg_cpu
FROM $__database.$__table
WHERE $__timeFilter
AND measure_name = 'cpu_utilization'
GROUP BY bin(time, $__interval_ms), instance_name
ORDER BY binned_time ASCCompare metrics across dimensions
Group by a dimension column to compare values across hosts, regions, or other attributes in a single panel.
SELECT
bin(time, $__interval_ms) AS binned_time,
hostname,
max(measure_value::double) AS peak_memory,
avg(measure_value::double) AS avg_memory
FROM $__database.$__table
WHERE $__timeFilter
AND measure_name = 'memory_utilization'
GROUP BY bin(time, $__interval_ms), hostname
ORDER BY binned_time ASCExplore table schema
Use Timestream’s metadata queries to discover available data before building visualizations.
SHOW MEASURES FROM $__database.$__tableDESCRIBE $__database.$__tableQuery the latest values
Use $__now_ms and $__timeFrom to build custom time-range logic, for example, to find the most recent reading per sensor.
SELECT
sensor_id,
measure_name,
time,
measure_value::double AS value
FROM $__database.$__table
WHERE time BETWEEN from_milliseconds($__timeFrom) AND from_milliseconds($__now_ms)
AND measure_name = 'temperature'
ORDER BY time DESC
LIMIT 10IoT multi-measure queries
Query multiple measure values from multi-measure records in a single query.
SELECT
bin(time, $__interval_ms) AS binned_time,
device_id,
avg(temperature) AS avg_temp,
avg(humidity) AS avg_humidity
FROM $__database.$__table
WHERE $__timeFilter
GROUP BY bin(time, $__interval_ms), device_id
ORDER BY binned_time ASCOptimize query performance and cost
Amazon Timestream charges based on the amount of data scanned by queries. Poorly optimized dashboards with frequent refreshes and broad queries can lead to significant costs. The following practices help minimize data scanned and reduce your Timestream bill.
Filter by measure name
Always include a measure_name filter in the WHERE clause. Timestream partitions data by measure name, so filtering on it dramatically reduces the volume of data scanned.
-- Costly: scans all measures in the table
SELECT * FROM $__database.$__table WHERE $__timeFilter
-- Optimized: scans only the cpu_utilization partition
SELECT * FROM $__database.$__table
WHERE $__timeFilter AND measure_name = 'cpu_utilization'Narrow the time range
Use the shortest time range that meets your needs. Timestream separates data into memory and magnetic storage tiers based on age, and wider time ranges may scan both.
- Set dashboard time ranges to the minimum necessary (for example, “Last 1 hour” instead of “Last 7 days”).
- Use
$__timeFilterto automatically scope queries to the dashboard time range.
Use aggregation with bin()
Aggregate data with bin() and $__interval_ms instead of returning raw data points. This reduces the number of rows returned and aligns data to the panel resolution.
-- Returns potentially millions of raw rows
SELECT time, measure_value::double FROM $__database.$__table WHERE $__timeFilter
-- Returns one row per interval per series
SELECT bin(time, $__interval_ms) AS t, avg(measure_value::double) AS value
FROM $__database.$__table
WHERE $__timeFilter AND measure_name = 'cpu_utilization'
GROUP BY bin(time, $__interval_ms)
ORDER BY t ASCReduce dashboard refresh frequency
Each dashboard refresh re-executes all panel queries. Set the auto-refresh interval to an appropriate frequency for your use case (for example, every 30 seconds or every minute instead of every 5 seconds).
Reuse query results across panels
If multiple panels display different aspects of the same data, query the data once and reuse the results:
- Dashboard data source: Use the built-in Dashboard data source to reference results from another panel, avoiding duplicate queries to Timestream.
- Transformations: Apply transformations to reshape a single query’s results for different visualizations.
Enable query caching
In Grafana Enterprise and Grafana Cloud, enable query caching to avoid re-running identical queries within a configurable time window.


