Documentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Stored procedures and query parameters in ClickHouse
If you’re coming from a traditional relational database, you may be looking for stored procedures and prepared statements in ClickHouse. This guide explains ClickHouse’s approach to these concepts and provides recommended alternatives.Alternatives to stored procedures in ClickHouse
ClickHouse doesn’t support traditional stored procedures with control flow logic (IF/ELSE, loops, etc.).
This is an intentional design decision based on ClickHouse’s architecture as an analytical database.
Loops are discouraged for analytical databases because processing O(n) simple queries is usually slower than processing fewer complex queries.
ClickHouse is optimized for:
- Analytical workloads - Complex aggregations over large datasets
- Batch processing - Handling large data volumes efficiently
- Declarative queries - SQL queries that describe what data to retrieve, not how to process it
User-Defined Functions (UDFs)
User-Defined Functions let you encapsulate reusable logic without control flow. ClickHouse supports two types:Lambda-based UDFs
Create functions using SQL expressions and lambda syntax:Sample data for examples
Sample data for examples
- No loops or complex control flow
- Can’t modify data (
INSERT/UPDATE/DELETE) - Recursive functions not allowed
CREATE FUNCTION for complete syntax.
Executable UDFs
For more complex logic, use executable UDFs that call external programs:Parameterized views
Parameterized views act like functions that return datasets. They’re ideal for reusable queries with dynamic filtering:Sample data for example
Sample data for example
Common use cases
- Dynamic date range filtering
- User-specific data slicing
- Multi-tenant data access
- Report templates
- Data masking
Materialized views
Materialized views are ideal for pre-computing expensive aggregations that would traditionally be done in stored procedures. If you’re coming from a traditional database, think of a materialized view as an INSERT trigger that automatically transforms and aggregates data as it’s inserted into the source table:Refreshable materialized views
For scheduled batch processing (like nightly stored procedures):External orchestration
For complex business logic, ETL workflows, or multi-step processes, it’s always possible to implement logic outside ClickHouse, using language clients.Using application code
Here’s a side-by-side comparison showing how a MySQL stored procedure translates to application code with ClickHouse:- MySQL Stored Procedure
- ClickHouse Application Code
Key differences
- Control flow - MySQL stored procedure uses
IF/ELSE,WHILEloops. In ClickHouse, implement this logic in your application code (Python, Java, etc.) - Transactions - MySQL supports
BEGIN/COMMIT/ROLLBACKfor ACID transactions. ClickHouse is an analytical database optimized for append-only workloads, not transactional updates - Updates - MySQL uses
UPDATEstatements. ClickHouse prefersINSERTwith ReplacingMergeTree or CollapsingMergeTree for mutable data - Variables and state - MySQL stored procedures can declare variables (
DECLARE v_discount). With ClickHouse, manage state in your application code - Error handling - MySQL supports
SIGNALand exception handlers. In application code, use your language’s native error handling (try/catch)
Using workflow orchestration tools
- Apache Airflow - Schedule and monitor complex DAGs of ClickHouse queries
- dbt - Transform data with SQL-based workflows
- Prefect/Dagster - Modern Python-based orchestration
- Custom schedulers - Cron jobs, Kubernetes CronJobs, etc.
- Full programming language capabilities
- Better error handling and retry logic
- Integration with external systems (APIs, other databases)
- Version control and testing
- Monitoring and alerting
- More flexible scheduling
Alternatives to prepared statements in ClickHouse
While ClickHouse doesn’t have traditional “prepared statements” in the RDBMS sense, it provides query parameters that serve the same purpose: safe, parameterized queries that prevent SQL injection.Syntax
There are two ways to define query parameters:Method 1: using SET
Example table and data
Example table and data
Method 2: using CLI parameters
Parameter syntax
Parameters are referenced using:{parameter_name: DataType}
parameter_name- The name of the parameter (without theparam_prefix)DataType- The ClickHouse data type to cast the parameter to
Data type examples
Tables and sample data for example
Tables and sample data for example
- Strings & Numbers
- Dates & Times
- Arrays
- Maps
- Identifiers
For use of query parameters in language clients, refer to the documentation for the specific language client you’re interested in.
Limitations of query parameters
Query parameters are not general text substitutions. They have specific limitations:- They’re primarily intended for SELECT statements - the best support is in SELECT queries
- They work as identifiers or literals - they can’t substitute arbitrary SQL fragments
- They have limited DDL support - they’re supported in
CREATE TABLE, but not inALTER TABLE
Security best practices
Always use query parameters for user input:MySQL protocol prepared statements
ClickHouse’s MySQL interface includes minimal support for prepared statements (COM_STMT_PREPARE, COM_STMT_EXECUTE, COM_STMT_CLOSE), primarily to enable connectivity with tools like Tableau Online that wrap queries in prepared statements.
Key limitations:
- Parameter binding isn’t supported - You can’t use
?placeholders with bound parameters - Queries are stored but not parsed during
PREPARE - Implementation is minimal and designed for specific BI tool compatibility
Summary
ClickHouse alternatives to stored procedures
| Traditional Stored Procedure Pattern | ClickHouse Alternative |
|---|---|
| Simple calculations and transformations | User-Defined Functions (UDFs) |
| Reusable parameterized queries | Parameterized Views |
| Pre-computed aggregations | Materialized Views |
| Scheduled batch processing | Refreshable Materialized Views |
| Complex multi-step ETL | Chained materialized views or external orchestration (Python, Airflow, dbt) |
| Business logic with control flow | Application code |
Use of query parameters
Query parameters can be used for:- Preventing SQL injection
- Parameterized queries with type safety
- Dynamic filtering in applications
- Reusable query templates
Related documentation
CREATE FUNCTION- User-Defined FunctionsCREATE VIEW- Views including parameterized and materialized- SQL Syntax - Query Parameters - Complete parameter syntax
- Cascading Materialized Views - Advanced materialized view patterns
- Executable UDFs - External function execution