I have a Rails/ActiveRecord server utilizing Postgres via the native gem.
I was wondering if/how Postgres reuses query plans.
For example let’s say my app has stuff like this:
ActiveRecord::Base.execute("select foo from t where id=5")
ActiveRecord::Base.execute("select foo from t where id=7")
Will Posgres know that the 2nd SQL is almost identical to the 1st and to reuse existing query plans?
I’m not looking for an answer of “just use models!” or what not; I’m just wondering what happens in the database.
I’m not sure if it is right , from my personal opinion , If use a bind variable in your application,
then PostgreSQL Server will reuse the esisting query plan without parsed,rewritten the plan again。
Another way is that use PREPARE statement in your appplication, When the PREPARE statement is executed,
the specified statement is parsed, rewritten, and planned. When an EXECUTE command is subsequently issued,
the prepared statement need only be executed.Thus, the parsing, rewriting, and planning stages are only
performed once, instead of every time the statement is executed
More information about “PREPARED statemnet” please vist
http://www.postgresql.org/docs/9.0/static/sql-prepare.html