On 7/1/06, Michael Glaesemann <[EMAIL PROTECTED]> wrote:
On Jul 2, 2006, at 8:21 , Coda Hale wrote:
> If there is no
> existing :order option in the find itself, the ORDER BY clause begins
> with the primary key of the model being searched, which is the default
> SQL order.

I can't speak to the correctness of the rest of the patch (though it
seems pretty nifty to me :), this assumption is false. There is no
guaranteed order in SQL without an explicit ORDER BY clause. Any
ordering you may see in cases without an ORDER BY clause is an
implementation artifact and can easily vary with the dbms and even
the query itself.

Well, I'm working with MySQL, which returns records by order of
primary key. (I think.) Does anyone know of any Rails-supported RDBMS
which doesn't do this?

I'm not familiar enough with the ActiveRecord code to know whether or
not ActiveRecord adds a default ORDER BY #{table_name}.#
{primary_key}, in which case what is specified by the SQL standard is
pretty much moot so feel free to ignore all of this :). I'd be
surprised if ActiveRecord *does* add a default ORDER BY as this could
affect performance.

It doesn't add an ORDER BY clause unless one is provided, and I'd
imagine that adding an ORDER BY clause to a JOIN statement would add
minimal overhead. For a straight SELECT, though, I totally agree.

 From what I see, this is just a small part of the patch though, and
it can easily be amended by removing c136-140.

This would introduce an unintended side effect, namely that the
following two pieces of code would not return the same results:

 Author.find(:all).map(&:id) != Author.find(:all, :include =>
[:posts]).map(&:id)

The array of Authors would be ordered by the default RDBMS order in
the first instance, and by the :order option of Author's post
association in the second. Ideally, it would be nice to nail this down
so that both eagerly- and non-eagerly-loaded associations work the
same, but I'm personally in favor of trading the weirdness I have to
deal with for the one I can ignore. Some people may have a different
perspective. ;-)

Can any database ninjas within earshot give any advice about the most
polite way of doing this, or is prepending the first-order model's
primary key to the ORDER BY clause a reasonable thing to do?

Also, can anyone tell me what this patch does off of MySQL, regarding
unit tests? I'd run PostgreSQL myself, but I'm humbled by my inability
to configure the damn thing.

--
Coda Hale
http://blog.codahale.com
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to