Google Groups Home Help | Sign in
Request for comments: prepared statements in ActiveRecord
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 37 - Collapse all   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Hongli Lai  
View profile
 More options May 14 2007, 12:36 pm
From: Hongli Lai <hongli...@gmail.com>
Date: Mon, 14 May 2007 09:36:38 -0700
Local: Mon, May 14 2007 12:36 pm
Subject: Request for comments: prepared statements in ActiveRecord
I'm working on long-awaited support for prepared statements in
ActiveRecord. Before I finish my work, I would like to know whether
the Ruby on Rails core team accepts my design, and whether they will
review my patch (once it is finished) seriously. I have documented the
design at: http://izumi.plan99.net/blog/?p=35

Please comment on this.
Thank you.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mislav Marohnić  
View profile
 More options May 14 2007, 1:15 pm
From: "Mislav Marohnić" <mislav.maroh...@gmail.com>
Date: Mon, 14 May 2007 19:15:49 +0200
Local: Mon, May 14 2007 1:15 pm
Subject: Re: [Rails-core] Request for comments: prepared statements in ActiveRecord

On 5/14/07, Hongli Lai <hongli...@gmail.com> wrote:

> I have documented the design at: http://izumi.plan99.net/blog/?p=35

Nicely done! But am I to understand StatementBuilder code is going to
replace (cleanup) all the string hacks currently involved in building SQL
queries? Will the usage of prepared statements in MySQL/PostgreSQL become
implicit with this? Also, did you follow the "ActiveRecord refactoring"
thread [1]? Did Zach [2][3] really do it?

[1]
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/...
[2] http://www.continuousthinking.com/
[3]
http://groups.google.com/groups/profile?enc_user=WZYiNRUAAADUiWRahsdn...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hongli Lai  
View profile
 More options May 14 2007, 1:31 pm
From: Hongli Lai <hongli...@gmail.com>
Date: Mon, 14 May 2007 10:31:36 -0700
Local: Mon, May 14 2007 1:31 pm
Subject: Re: Request for comments: prepared statements in ActiveRecord
On May 14, 7:15 pm, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:

> Nicely done! But am I to understand StatementBuilder code is going to
> replace (cleanup) all the string hacks currently involved in building SQL
> queries? Will the usage of prepared statements in MySQL/PostgreSQL become
> implicit with this?

Thanks. :) The use of prepared statements in MySQL/PostgreSQL will
completely replace the argument escaping stuff. Database adapters that
don't support prepared statements (or haven't implemented support yet)
will silently fallback to argument escaping, as is done now.

> Also, did you follow the "ActiveRecord refactoring"
> thread [1]? Did Zach [2][3] really do it?

> [1]http://groups.google.com/group/rubyonrails-core/browse_thread/thread/...
> [2]http://www.continuousthinking.com/
> [3]http://groups.google.com/groups/profile?enc_user=WZYiNRUAAADUiWRahsdn...

I didn't follow any of these threads, but I have read the
ActiveRecord::Extensions website in the past.

This prepared statements support is entirely my own work, and is not
related to Zach's work.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mislav Marohnić  
View profile
 More options May 14 2007, 2:25 pm
From: "Mislav Marohnić" <mislav.maroh...@gmail.com>
Date: Mon, 14 May 2007 20:25:59 +0200
Local: Mon, May 14 2007 2:25 pm
Subject: Re: [Rails-core] Re: Request for comments: prepared statements in ActiveRecord

On 5/14/07, Hongli Lai <hongli...@gmail.com> wrote:

> The use of prepared statements in MySQL/PostgreSQL will
> completely replace the argument escaping stuff.

So, every query ever issued will become a prepared statement. Isn't this
going to lead to unnecessary overhead? I mean, there are *a lot* of various
SELECT statements generated by even a simple Rails application. Does it
really make sense to prepare every one of them? Having backends cache the
execution plan for rare SELECTs can possibly have undesirable overhead.

Also, how will ActiveRecord know how to reuse these statements? For example:

  a = Author.find(1)
  post = Author.posts.first
  post.author

The first and the last query are different to AR, yet they take a same shape
("SELECT * FROM authors WHERE authors.id = ?") and can both be represented
by a single prepared statement. With your solution, will AR know how to
reuse the prepared statement in different places, or will it try to prepare
it twice?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hongli Lai  
View profile
 More options May 14 2007, 2:40 pm
From: Hongli Lai <hongli...@gmail.com>
Date: Mon, 14 May 2007 11:40:41 -0700
Local: Mon, May 14 2007 2:40 pm
Subject: Re: Request for comments: prepared statements in ActiveRecord
On May 14, 8:25 pm, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:

The current implementation immediately removes prepared statements
after using them once, so they will be reparsed twice. Right now I'm
trying to make it actually work and getting the framework in place.
Optimization can be done later (and I'm fairly confident that it can
be optimized).
In the future I can add caching. The cache will have a limited size,
and items can be removed based on least-recently-used or some other
criteria, similar to how CPU caches work.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mislav Marohnić  
View profile
 More options May 14 2007, 3:07 pm
From: "Mislav Marohnić" <mislav.maroh...@gmail.com>
Date: Mon, 14 May 2007 21:07:28 +0200
Local: Mon, May 14 2007 3:07 pm
Subject: Re: [Rails-core] Re: Request for comments: prepared statements in ActiveRecord

On 5/14/07, Hongli Lai <hongli...@gmail.com> wrote:

> The current implementation immediately removes prepared statements
> after using them once

Doesn't that negate the whole idea of having prepared statements in the
database in the first place? I don't think Rails core team would accept the
patch that slows down ActiveRecord just for the sake of using a database
feature (in a wrong way).

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hongli Lai  
View profile
 More options May 14 2007, 3:27 pm
From: Hongli Lai <hongli...@gmail.com>
Date: Mon, 14 May 2007 12:27:33 -0700
Local: Mon, May 14 2007 3:27 pm
Subject: Re: Request for comments: prepared statements in ActiveRecord
On May 14, 9:07 pm, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:

> Doesn't that negate the whole idea of having prepared statements in the
> database in the first place? I don't think Rails core team would accept the
> patch that slows down ActiveRecord just for the sake of using a database
> feature (in a wrong way).

Actually, no. One of the problems with quoting string manually is
excessive memory usage in Rails. For example, if you're uploading a 1
MB photo to your database, then Rails has to quote a 1 MB string,
which is very slow and results in more than 1 MB memory usage because
of string overheads. Using prepared statements will remove this
problem.
And I'm not sure that using prepared statements (and immediately
deleting them) really makes things slower. Why would it be? After all,
if you're not using prepared statements then the database will have to
reparse each statement every time anyway. This will need more
benchmarking.
The prepared statement caching that I mentioned should be trivial to
implement. It's on my todo list.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alexey Verkhovsky  
View profile
 More options May 14 2007, 4:09 pm
From: "Alexey Verkhovsky" <alexey.verkhov...@gmail.com>
Date: Mon, 14 May 2007 14:09:06 -0600
Local: Mon, May 14 2007 4:09 pm
Subject: Re: [Rails-core] Re: Request for comments: prepared statements in ActiveRecord
On 5/14/07, Hongli Lai <hongli...@gmail.com> wrote:

> And I'm not sure that using prepared statements (and immediately
> deleting them) really makes things slower. Why would it be?

It'll be somewhat slower than just throwing a SQL statement at the
database because it adds extra synchronous communication associated
with preparing the statement. In most cases it'll be negligible.

From past experiences, I would expect to see a noticeable, if not
huge, gain from caching prepared statements. At least, with Oracle.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mislav Marohnić  
View profile
 More options May 14 2007, 4:23 pm
From: "Mislav Marohnić" <mislav.maroh...@gmail.com>
Date: Mon, 14 May 2007 22:23:17 +0200
Local: Mon, May 14 2007 4:23 pm
Subject: Re: [Rails-core] Re: Request for comments: prepared statements in ActiveRecord

On 5/14/07, Alexey Verkhovsky <alexey.verkhov...@gmail.com> wrote:

> From past experiences, I would expect to see a noticeable, if not
> huge, gain from caching prepared statements. At least, with Oracle.

I'd dare to say that Oracle isn't exactly very popular in Rails world. The
majority uses Postgres and MySQL, and they would see a noticable gain only
when these prepared statements were reused for multiple queries.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alexey Verkhovsky  
View profile
 More options May 14 2007, 4:35 pm
From: "Alexey Verkhovsky" <alexey.verkhov...@gmail.com>
Date: Mon, 14 May 2007 14:35:05 -0600
Local: Mon, May 14 2007 4:35 pm
Subject: Re: [Rails-core] Re: Request for comments: prepared statements in ActiveRecord
On 5/14/07, Mislav Marohnić <mislav.maroh...@gmail.com> wrote:

> On 5/14/07, Alexey Verkhovsky <alexey.verkhov...@gmail.com> wrote:
> I'd dare to say that Oracle isn't exactly very popular in Rails world.

It is in my corner of the woods. Let me not mention that evil E-word again :)

> and they would see a noticable gain only when these prepared statements
> were reused for multiple queries.

It's the same with Oracle. Prepared statement is merely a way to avoid
hitting SQL parser and query optimizer.

--
Alex


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hongli Lai  
View profile
 More options May 14 2007, 4:41 pm
From: Hongli Lai <hongli...@gmail.com>
Date: Mon, 14 May 2007 13:41:45 -0700
Local: Mon, May 14 2007 4:41 pm
Subject: Re: Request for comments: prepared statements in ActiveRecord
On May 14, 10:35 pm, "Alexey Verkhovsky" <alexey.verkhov...@gmail.com>
wrote:

> On 5/14/07, Mislav Marohnić <mislav.maroh...@gmail.com> wrote:

> > On 5/14/07, Alexey Verkhovsky <alexey.verkhov...@gmail.com> wrote:
> > I'd dare to say that Oracle isn't exactly very popular in Rails world.

> It is in my corner of the woods. Let me not mention that evil E-word again :)

> > and they would see a noticable gain only when these prepared statements
> > were reused for multiple queries.

> It's the same with Oracle. Prepared statement is merely a way to avoid
> hitting SQL parser and query optimizer.

That's all good and well but right now I have my hands full on making
it *work* (I have a ton of unit test failures to fix). I promise you I
will take care of performance once this actually works.

Are there any other discussion points left?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mislav Marohnić  
View profile
 More options May 14 2007, 5:06 pm
From: "Mislav Marohnić" <mislav.maroh...@gmail.com>
Date: Mon, 14 May 2007 23:06:03 +0200
Local: Mon, May 14 2007 5:06 pm
Subject: Re: [Rails-core] Re: Request for comments: prepared statements in ActiveRecord

On 5/14/07, Hongli Lai <hongli...@gmail.com> wrote:

> Are there any other discussion points left?

Yes. In your place I would contact Zach to get an update on his progress. If
he really did some refactoring like he said, it is most likely that he made
some statement builder class himself. Picking up where he left off in this
area would be a boost.

I wish you luck with the project! Hopefully your cleanup will finally make
AR::Base (and related files) SQL-free; connection adapters and "statements
builders" should handle that logic.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael A. Schoen  
View profile
 More options May 14 2007, 7:06 pm
From: &quo