Prepared Statements and the Statement Cache

Prepared statements are inherently cached for a single session. That is, if a prepared statement is executed many times (or if a single cursor is opened many times), the same prepared query plan is used repeatedly by that session.

If a session prepares a statement and then executes it many times, its performance is essentially unaffected by using the SQL statement cache, because the statement is optimized just once, during the PREPARE statement.

If other sessions also prepare that same statement, however, or if the first session prepares the statement several times, then the statement cache usually provides a direct performance benefit, because the database server only calculates the query plan once. Of course, the original session might gain a small benefit from the statement cache, even if it prepares the statement only once, because other sessions use less memory, and the database server does less work for the other sessions.