I'm trying to use prepared statements with python to improve query performance, but my prepared statements are slower (by a factor of ~7.5 no matter how many iterations I set in my benchmark script).
In essence what I do for regular queries:
cursor = connection.cursor() cursor.execute("INSERT into test (id,name) VALUES (1,'Value 1')") ... multiple inserts ... connection.commit()
And for prepared statements:
cursor = connection.cursor(prepared=True) sql = "INSERT into test (id,name) VALUES (%s,%s)" cursor.execute(sql, (1, 'Value 1')) ... multiple inserts ... connection.commit()
- The script runs 2 loops with same number of iterations
- 1st loop runs with the default
.cursor()while 2nd with
.cursor(prepared=True)as per above
And here are the results:
As you can see
Why are my prepared statements slower than regular queries? Is it an issue with the python
mysql.connector driver or do I need to tweak something in my MySQL config to fix this?