Skip to main content

prepared_statements

🚀 Leverage Prepared Statements for Repeated Queries​

Rails automatically caches prepared statements, but you can manually manage them for highly repetitive queries to shave off planning time. Use connection.prepare and connection.exec_prepared when you need fine-grained control over statement reuse and parameter binding.

conn = ActiveRecord::Base.connection.raw_connection
conn.prepare('find_user_by_email', 'SELECT * FROM users WHERE email = $1')
result = conn.exec_prepared('find_user_by_email', ['bob@example.com'])

This low-level approach reduces round-trip overhead and can significantly speed up tight loops of similar queries.