# Active Record Prepare SQL

**URL:** <https://www.ruby-forum.com/t/active-record-prepare-sql/141604>\
**Category:** Rails\
**Created:** [June 27, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604 "2008-06-27T16:17:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [June 27, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604/1 "2008-06-27T16:17:47Z")

</div>

Would it be possiblein the future to see Active Record generate  
Prepare and Bind SQL statements rather than passing literals to the DB?

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [June 27, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604/2 "2008-06-27T18:27:13Z")

</div>

On Fri, Jun 27, 2008 at 7:17 AM, Jorg L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Would it be possiblein the future to see Active Record generate  
> Prepare and Bind SQL statements rather than passing literals to the DB?

Yes. Why?

jeremy

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [June 27, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604/3 "2008-06-27T20:36:59Z")

</div>

On Jun 27, 11:26 am, “Jeremy K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Fri, Jun 27, 2008 at 7:17 AM, Jorg L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Would it be possiblein the future to see Active Record generate  
> > Prepare and Bind SQL statements rather than passing literals to the DB?
> 
> Yes. Why?
> 
> jeremy

Because, at least in DB2, you have much lower cost preparing select c1  
from t1 where c1 = ? rather than c1 = ‘Hi’  
DB2 caches the queries that come in and all that use the ? are  
equivalent thus there is no new overhead in determining the access  
method. With literals, unless they are identical each literal will  
cause more overhead.

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [June 28, 2008, 12:11am UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604/4 "2008-06-28T00:11:21Z")

</div>

Well I did ask about the ibm\_db adapter first but since the SQL is  
generated in active record the adapter wouldn’t know what was a  
parameter originally. Also, I checked and Oracle and SQL Server work  
the same way. Since the cost of preparing a literal is 10x preparing  
out of cache in all three major databases it seems like a worthwhile  
addition.

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [June 28, 2008, 3:41am UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604/5 "2008-06-28T03:41:17Z")

</div>

Thanks for the reply Jorg.

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [June 27, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/active-record-prepare-sql/141604/6 "2008-06-27T22:34:08Z")

</div>

Just to satisfy my own curiosity…does this need to be included in  
ActiveRecord. Couldn’t this be implemented in the DB2 adapter?

Thanks guys.  
Mike
