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 2 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 2
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 2 code lines when it detects that those prepared statements have already been prepared during another database stored procedure query session.