Re: Default ordering option - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Default ordering option
Date
Msg-id 69000113-a955-4b79-42b1-15e416a591ea@aklaver.com
Whole thread Raw
In response to Re: Default ordering option  (Cyril Champier <cyril.champier@doctolib.com>)
List 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.klaver@aklaver.com



pgsql-general by date:

Previous
From: Cyril Champier
Date:
Subject: Re: Default ordering option
Next
From: Alexey Bashtanov
Date:
Subject: Re: after restore the size of the database is increased