We use a MariaDB database (with stored procedures) and pass params via PHP middleware.
When I execute the following prepared statement directly in a client like DBeaver against a view containing nested queries with GROUP BY and JSON_ARRAYAGG, it takes approximately 1.5 seconds to return one record.
SET @x = 'foo';
SET @y = 'bar';
PREPARE test FROM 'SELECT * FROM db.v_view WHERE x = ? AND y = ?';
EXECUTE test USING @x, @y
Then when I try with casting:
SET @x = 'foo';
SET @y = 'bar';
PREPARE test FROM 'SELECT * FROM
db.v_view WHERE x = CAST(? AS CHAR CHARACTER SET utf8mb4) AND
y = CAST(? AS CHAR CHARACTER SET utf8mb4)';
EXECUTE test USING @x, @y;
This only takes 0.3 seconds to get the same record.
From what I can gather it should have something to do with MariaDB's optimizer but I didn't find any similar case online or any documentation on why a cast (or lack thereof) would cause this behavior especially since the cast itself doesn't change anything for my params as far as I can see. They both have the same charsets (utf8mb4) with and without the cast so the performance drop shouldn't be caused by a mismatch.