Should PDO prepared statements be freed up after use? And if so, how? Specifically I'm asking about MySQL - how can you, and should you, call DEALLOCATE PREPARE
though PDO. (Edit: To clarify, this question is not referring to emulated prepares, but real prepares. )
Also - will this free the results set (when large)?
Explanation:
I have seen code along the lines of
$stmnt = $db->prepare($sql);
$stmnt->execute($aParams);
$stmnt = null;
which led me to wondering what this does, when, and if f unset($stmnt);
would be different?
The manual indicates that
When the query is prepared, the database will analyze, compile and optimize its plan for executing the query. [...] By using a prepared statement the application avoids repeating the analyze/compile/optimize cycle.
which tends to suggest you should unallocate the statement, and MySQL has the capability. So,
- Can you call
DEALLOCATE PREPARE
, and how - Should you do it?
- And can anyone confirm that setting statement to null (or unsetting the statement) will do the same as "free_result" for mysql_ and mysqli_?
- Does it happen immediately, or does it wait for garbage collector to kick in?
For completeness, another SO question referring to "free_result" and "close" functions for mysqli_()
suggests that freeing the statement actually adds time (unless you have large memory usage and need the space). But "free_result" is different from freeing the SQL server from having the prepared statment cached.