Sample Header Ad - 728x90

When do I need to DEALLOCATE a prepared statement in a MySQL stored procedure?

1 vote
3 answers
1793 views
My question is simple. In what situations is it better to deallocate the prepared statement and thus removing it from memory and in what situations is it better to keep it in memory? I have written a stored procedure that uses two prepared statements. One of the first few lines is to prepare the statement like so: PREPARE selectStatementByJournalEntryAndLanguageQuery FROM "INSERT INTO results(id, content, title, language_id, journalentry_id) SELECT * FROM JournalEntryTitleAndContent jetc WHERE jetc.language_id = ? AND jetc.journalentry_id = ? LIMIT 1"; PREPARE selectStatementByJournalEntryQuery FROM "INSERT INTO results(id, content, title, language_id, journalentry_id) SELECT * FROM JournalEntryTitleAndContent jetc WHERE jetc.journalentry_id = ? LIMIT 1"; Now this stored procedure and thus these prepared statements could be used many times a day by many users. In this case it is better to not call DEALLOCATE on both prepared statements, right? Because then the prepared statements remain in memory and do not have to be recompiled when someone else uses the stored procedure. But that raises a question about the two PREPARE statement lines. If I do not deallocate the prepared statements at the end and someone else starts the stored procedure, will the database then try to PREPARE the prepared statements again or will it ignore these two code lines when it detects that those prepared statements have already been prepared during another database stored procedure query session? Thank you.
Asked by Maurice (147 rep)
Mar 3, 2020, 01:40 PM
Last activity: Jul 10, 2021, 12:50 PM