Search code examples
rubypostgresqlpg

Ruby PG Gem `exec_params` vs `exec_prepared`


What are the differences between Connection#exec_params and Connection#exec_prepared? It seems like they both require the same kinds of input and they both perform the same action. Why would someone choose one versus the other?


Solution

  • You can use exec_prepared to optimize SQL query, see documentation.

    exec_params lets you execute specified SQL query with binded parameters. exec_prepared lets you execute prepared (parsed, optimized etc) SQL query specified by its string identificator/name which you have got earlier.

    If you make a lot of similar SQL select queries and difference is parameter values, you can make it effectively by preparing SQL statement once (you receive it identifier) and execute it with different parameters multiple times.