Because the DB driver is using pyformat for paramstyle, queries compiled by SQLAlchemy will double any percents signs in the text:
SELECT * FROM t WHERE name LIKE '%%a' AND job = '%s'
So that when running the query via:
cursor.execute(sql, ("designer",))
The parameters are applied to the SQL via:
sql = "SELECT * FROM t WHERE name LIKE '%%a' AND job = '%s'"
parameters = ("designer",)
final_query = sql % parameters
Becoming:
SELECT * FROM t WHERE name LIKE '%a' AND job = 'designer'
The problem is that if no parameters are present the function apply_parameters will return the SQL immediately, with any double percents remaining in the final query. This produces valid queries (LIKE '%%a' is valid), but for some reason the performance of these queries is much worse compared to the correct query (LIKE '%a').
This PR fixes the problem by always removing duplicate percents, even when no parameters are passed.
Because the DB driver is using
pyformat
forparamstyle,
queries compiled by SQLAlchemy will double any percents signs in the text:So that when running the query via:
The parameters are applied to the SQL via:
Becoming:
The problem is that if no parameters are present the function
apply_parameters
will return the SQL immediately, with any double percents remaining in the final query. This produces valid queries (LIKE '%%a'
is valid), but for some reason the performance of these queries is much worse compared to the correct query (LIKE '%a'
).This PR fixes the problem by always removing duplicate percents, even when no parameters are passed.