When using the NodeJS mysql2 library, I am trying to understand the differences between Connection.execute and Connection.query.
As I understand it, query prepares the statement locally and then makes one call to the database. execute sends the query without parameters to the database to be prepared then sends the parameters separately to be executed with the prepared statement.
Where my understanding really falls apart is how or if the statement gets cached when using Connection.execute. Does calling Connection.execute twice in a row with the same query have any benefit?
In function B1 below, the statement gets re-used, which gives a performance improvement. However in function B2, does the statement get re-used, or are there four separate network calls being made to the database? In the case there is no re-use of the statement in B2, would B3 be the fastest option?
async function B1(connection) { const statement = await connection.prepare("SELECT 1 + ? + ?"); const result1 = await statement.execute([1, 2]); const result2 = await statement.execute([3, 4]); await statement.close(); return [result1, result2]; } async function B2(connection) { const result1 = await connection.execute("SELECT 1 + ? + ?", [1, 2]); const result2 = await connection.execute("SELECT 1 + ? + ?", [3, 4]); return [result1, result2]; } async function B3(connection) { const result1 = await connection.query("SELECT 1 + ? + ?", [1, 2]); const result2 = await connection.query("SELECT 1 + ? + ?", [3, 4]); return [result1, result2]; }