I have a query simply like this:
SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(ARRAY['key1', 'key2', ...])
According to the article 100x faster Postgres performance by changing 1 line, using VALUES
instead of ARRAY
can improve the performance. I've also tried the solution and the query has better performance:
SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(VALUES('key1'), ('key2'), ...)
But I am passing the keyList
via Dapper:
SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(@keyList)
and it translete the query with array:
SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY($1)
My question is how can I force Dapper to use the query with VALUES
?
And this is the implementation roughly:
var query = @"SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(@keyList)";
var keyList = new[] { "key1", "key2", ... };
List<int> list;
using (var connection = new NpgsqlConnection(connectionString))
{
list = await connection.QueryAsync<int>(query, new {keyList});
}