Query Languages
Snuba supports two query languages:- SnQL (Snuba Query Language): A SQL-like language for querying events, transactions, and other entities
- MQL (Metrics Query Language): A specialized language for querying metrics and timeseries data
Endpoints
Snuba provides dataset-specific endpoints for executing queries:SnQL Endpoint
/discover/snql- Query the discover dataset/events/snql- Query the events dataset/transactions/snql- Query the transactions dataset
MQL Endpoint
/generic_metrics/mql- Query metrics data
Request Format
SnQL Request
SnQL queries are sent as JSON payloads with the following structure:The SnQL query string to execute
The dataset name (optional if specified in the URL)
When true, returns additional debugging information including the generated SQL query and detailed statistics
Forces single-threaded execution and routes to the same node for sequential consistency
Applies sampling to speed up query execution at the cost of accuracy
Identification information for the query:
organization_id: Organization identifierreferrer: String identifying the source of the query
MQL Request
MQL queries use a different structure with a query string and context object:Response Format
Successful Response
A successful query returns a JSON response with the following structure:Response Fields
Array of result rows, where each row is an object with column names as keys
Schema information for each column in the result set:
name: Column nametype: ClickHouse data type
Performance metrics:
timestamp: Unix timestamp when the query was executedduration_ms: Total query duration in millisecondsmarks_ms: Breakdown of time spent in each processing phase
Query execution statistics:
clickhouse_table: The table that was queriedfinal: Whether FINAL was applied to the querysample: Sampling rate if appliedconsistent: Whether consistent mode was usedresult_rows: Number of rows returnedresult_cols: Number of columns returnedquery_id: Unique identifier for this query
Debug Mode Response
Whendebug: true is set in the request, the response includes an additional sql field:
sql field contains the actual ClickHouse SQL query that was generated and executed.
Error Response
Error responses follow this format:HTTP Status Codes
- 200 OK
- 400 Bad Request
- 429 Too Many Requests
- 500 Internal Server Error
Query executed successfully
Using the Web UI
Snuba provides a minimal web UI for testing queries. When running Snuba locally, access it at:- Enter your SnQL query
- Set query options (debug, consistent, turbo)
- View the response in formatted JSON
Using curl
You can send queries using curl or any HTTP client:Using the Python SDK
The recommended way to build queries programmatically is using the Snuba SDK:Query Processing Pipeline
Snuba processes queries through multiple stages:- Parsing: SnQL/MQL string is parsed into an AST
- Validation: Query is validated against entity requirements
- Logical Processing: Entity-specific transformations are applied
- Storage Selection: Optimal storage/table is selected
- Translation: Logical query is translated to physical ClickHouse query
- Physical Processing: Optimizations are applied
- Execution: Query is executed on ClickHouse
Some entities require specific conditions (e.g., project_id and timestamp range) to prevent expensive queries. Check entity documentation for requirements.
Best Practices
- Always include time bounds: Most entities require timestamp conditions to prevent full table scans
- Use appropriate limits: Default limit is 1000 rows if not specified
- Enable debug mode during development: Helps understand query performance and optimization
- Use consistent mode sparingly: It reduces concurrency and may impact performance
- Leverage the SDK: Building queries with the SDK prevents syntax errors and provides type safety
Next Steps
SnQL Syntax
Learn the complete SnQL syntax and available functions
MQL Syntax
Explore MQL for metrics and timeseries queries
Optimization
Optimize your queries for better performance