Documentation

Optimize queries

Troubleshoot query performance

Use the following tools to help you identify performance bottlenecks and troubleshoot problems in queries:

EXPLAIN and ANALYZE

To view the query engine’s execution plan and metrics for an SQL query, prepend EXPLAIN or EXPLAIN ANALYZE to the query. The report can reveal query bottlenecks such as a large number of table scans or parquet files, and can help triage the question, “Is the query slow due to the amount of work required or due to a problem with the schema, compactor, etc.?”

The following example shows how to use the InfluxDB v3 Python client library and pandas to view EXPLAIN and EXPLAIN ANALYZE results for a query:

from influxdb_client_3 import InfluxDBClient3
import pandas as pd
import tabulate # Required for pandas.to_markdown()

def explain_and_analyze():
  print('Use SQL EXPLAIN and ANALYZE to view query plan information.')

  # Instantiate an InfluxDB client.
  client = InfluxDBClient3(token = f"
API_TOKEN
"
,
host = f"cloud2.influxdata.com", database = f"
BUCKET_NAME
"
)
sql_explain = '''EXPLAIN SELECT * FROM home WHERE time >= now() - INTERVAL '90 days' ORDER BY time''' table = client.query(sql_explain) df = table.to_pandas() sql_explain_analyze = '''EXPLAIN ANALYZE SELECT * FROM home WHERE time >= now() - INTERVAL '90 days' ORDER BY time''' table = client.query(sql_explain_analyze) # Combine the Dataframes and output the plan information. df = pd.concat([df, table.to_pandas()]) assert df.shape == (3, 2) and df.columns.to_list() == ['plan_type', 'plan'] print(df[['plan_type', 'plan']].to_markdown(index=False)) client.close() explain_and_analyze()

Replace the following:

  • BUCKET_NAME: the bucket to query
  • API_TOKEN: a token with sufficient permissions to the specified database

The output is similar to the following:

| plan_type         | plan                                                                                                                                         |
|:------------------|:---------------------------------------------------------------------------------------------------------------------------------------------|
| logical_plan      | Sort: home.time ASC NULLS LAST                                                                                                               |
|                   |   TableScan: home projection=[co, hum, room, sensor, temp, time], full_filters=[home.time >= TimestampNanosecond(1688491380936276013, None)] |
| physical_plan     | SortExec: expr=[time@5 ASC NULLS LAST]                                                                                                       |
|                   |   EmptyExec: produce_one_row=false                                                                                                           |
| Plan with Metrics | SortExec: expr=[time@5 ASC NULLS LAST], metrics=[output_rows=0, elapsed_compute=1ns, spill_count=0, spilled_bytes=0]                         |
|                   |   EmptyExec: produce_one_row=false, metrics=[]

Enable trace logging

Customers with an InfluxDB Cloud Serverless annual or support contract can contact InfluxData Support to enable tracing and request help troubleshooting your query. With tracing enabled, InfluxDB Support can trace system processes and analyze log information for a query instance. The tracing system follows the OpenTelemetry traces model for providing observability into a request.


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

InfluxDB Cloud Serverless