Re: Default ordering option

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Cyril Champier <cyril(dot)champier(at)doctolib(dot)com>
Cc: Ian Barwick <ian(dot)barwick(at)2ndquadrant(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Default ordering option
Date: 2019-07-24 16:11:47
Message-ID: 69000113-a955-4b79-42b1-15e416a591ea@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7/24/19 8:22 AM, Cyril Champier wrote:
> Indeed, you are right, I do my test in pure sql and via ruby
> ActiveRecord, and I must had been confused,
> the behaviour is correct in sql, it must have been a cache thing in
> ActiveRecord that prevented the reordering.
> But meanwhile, I tested on our whole CI, and it took twice the normal
> time with updates to shuffle DB :(

Well there extra operations so that is to be expected. The question is
whether everything needs to be shuffled? It would seem you only need to
do that for those tests that are expecting a set order. I went back and
read your original post and am somewhat confused about that test. You said:

"Sometimes, people forgot to order their queries. Most of the time,
there is no real problem on production...."

If order is not an issue in the production code why test for it in the
test code?

>
> For the union, I speak about production code like this:
> "select count(*) from (#{directory_doctors_query_sql} union all
> #{profiles_query_sql}) as doctors"
> In the to_sql, we cannot detect that we will be injected into a union.
> So I cannot blindly add the random in the to_sql method.

The below would not be affected by an order by in any case as the
count() would be the same:

"select count(*) from (#{directory_doctors_query_sql} union all
#{profiles_query_sql}) as doctors"

If you did want to use order by random() could you not just tack it on
the end?:

"... as doctors order by random()"

>
>
>
>
>
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alexey Bashtanov 2019-07-24 16:38:01 Re: after restore the size of the database is increased
Previous Message Christopher Browne 2019-07-24 16:08:12 Re: Request for resolution || Support