Parameter Reuse Optimization for SQL Template Literals
Current Behavior
When using SQL template literals with Kysely, identical parameters are assigned new parameter indices each time they appear in the query, even when the values are the same. This leads to unnecessary parameter duplication in the compiled query.
For example:
const terms = ['test', 'example'];
const query = sql<any>`
select *
from some_table
where
column1 = any(${terms}::text[]) -- Becomes $1
or column2 = any(${terms}::text[]) -- Becomes $2
`;
Compiles to:
select * from some_table where column1 = any($1::text[]) or column2 = any($2::text[])
Parameters: [['test', 'example'], ['test', 'example']]
Expected Behavior
When the same parameter value is used multiple times in a query, it would be more efficient to reuse the same parameter index:
select * from some_table where column1 = any($1::text[]) or column2 = any($1::text[])
Parameters: [['test', 'example']]
This optimization would:
- Reduce the number of parameters passed to the database
- Potentially improve query planning as the database can recognize the parameters are identical
- Make the generated SQL more concise
Parameter Reuse Optimization for SQL Template Literals
Current Behavior
When using SQL template literals with Kysely, identical parameters are assigned new parameter indices each time they appear in the query, even when the values are the same. This leads to unnecessary parameter duplication in the compiled query.
For example:
Compiles to:
Expected Behavior
When the same parameter value is used multiple times in a query, it would be more efficient to reuse the same parameter index:
This optimization would: