Native queries let you use your database’s native query language instead of SQL. This is particularly useful for NoSQL databases like MongoDB, or when you need database-specific features.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/metabase/metabase/llms.txt
Use this file to discover all available pages before exploring further.
What are native queries?
Native queries are written in your database’s native language:SQL databases
PostgreSQL, MySQL, SQL Server, Oracle, etc. use SQL
MongoDB
Uses MongoDB query language (MQL) for aggregations and queries
Other databases
Druid, Presto, BigQuery each have their own dialects and features
When to use native queries
Choose native queries when you need:Database-specific features
Database-specific features
Use features unique to your database:
- PostgreSQL: JSONB operations, array functions, window functions
- MongoDB: Aggregation pipeline, MapReduce
- BigQuery: ML functions, geography operations
- MySQL: Full-text search, spatial data
Performance optimization
Performance optimization
Write queries optimized for your specific database:
- Use database-specific indexes
- Leverage query hints
- Optimize for your database’s query planner
Complex transformations
Complex transformations
Perform operations the query builder can’t express:
- Recursive queries (CTEs)
- Complex conditional logic
- Advanced aggregations
- Custom functions
SQL databases
For SQL databases (PostgreSQL, MySQL, SQL Server, Oracle, etc.), you write standard SQL with database-specific extensions.Common SQL patterns
- Basic query
- Aggregation
- Joins
- Window functions
PostgreSQL-specific features
MySQL-specific features
MongoDB queries
MongoDB uses its own query language based on JavaScript:Aggregation pipeline
You can use Metabase variables in MongoDB queries just like in SQL. Wrap variable names in double curly braces:
{{variable_name}}.Adding variables to native queries
Make your native queries interactive with variables:Configure variable
When you add a variable, Metabase shows the variables sidebar where you can set:
- Variable type (field filter, text, number, date)
- Display label
- Widget type (dropdown, search, input)
- Default value
Variable types
- Field filters
- Basic variables
- Table variables
Smart filters that connect to database fields:Benefits:
- Automatic filter UI based on field type
- Better performance with proper SQL generation
- Works with dashboard filters
Optimizing native queries
Use indexes
Ensure your WHERE clauses and JOIN conditions use indexed columns for better performance.
Limit result sets
Use LIMIT or TOP clauses to prevent accidentally returning millions of rows.
Select specific columns
Avoid SELECT * in production queries. List only the columns you need.
Test with representative data
Test queries against realistic data volumes to identify performance issues.
Use EXPLAIN
Analyze query execution plans to understand and optimize performance.
Cache expensive queries
Enable result caching for queries that run frequently but data changes infrequently.
Database-specific considerations
BigQuery
BigQuery
- Use Standard SQL syntax
- Be mindful of costs - queries scan entire columns
- Leverage partitioned tables for better performance
- Use approximate aggregation functions when exact counts aren’t needed
Snowflake
Snowflake
- Queries are case-insensitive by default
- Use warehouse-appropriate query complexity
- Leverage result caching
- Consider clustering for large tables
Redshift
Redshift
- Optimize for columnar storage
- Use DISTKEY and SORTKEY in table design
- Avoid nested loops - use hash joins
- Vacuum and analyze tables regularly
Limitations of native queries
Best practices
Start with query builder
Use the query builder first. Only switch to native queries when you hit limitations.
Document your queries
Add comments explaining:
- What the query does
- Why specific approaches were used
- Any gotchas or important notes
Add variables for reusability
Make queries flexible with variables so others can adjust parameters without editing SQL.
Converting from query builder
You can convert query builder questions to native SQL:Using native queries on dashboards
To make native queries work with dashboard filters:- Add variables to your query
- Add the question to your dashboard
- Create dashboard filters
- Map filters to your query variables
Native queries require at least one variable to connect to dashboard filters. Without variables, the question will appear on the dashboard but won’t respond to filters.
Next steps
SQL editor
Learn about the SQL editor interface and features
Query builder
Understand when to use the query builder instead
Dashboard filters
Connect native queries to dashboard filters