Home > Net >  Does latest Mysql and Postgres prepare every query automatically?
Does latest Mysql and Postgres prepare every query automatically?

Time:09-16

I come across this in sqlx docs:


On most databases, statements will actually be prepared behind the scenes whenever a query is executed. However, you may also explicitly prepare statements for reuse elsewhere with sqlx.DB.Prepare():


Although I can't find proof that databases actually prepare every query. So is it true, should I use prepare manually?

CodePudding user response:

MySQL and PostgreSQL definitely don't prepare every query. You can execute queries directly, without doing a prepare & execute sequence.

The Go code in the sqlx driver probably does this, but it's elective, done to make it simpler to code the Go interface when you pass args.

You don't need to use the Prepare() func manually, unless you want to reuse the prepared query, executing it multiple times with different args.

  • Related