limitby without orderby - why primary ids of tables are added as orderby

22 views
Skip to first unread message

Tarun Arora

unread,
Sep 22, 2011, 3:43:58 AM9/22/11
to web...@googlegroups.com
I have a requirement, just to have 50 random records from table without any order. I don't want to use any orderby condition as number of records in tables is very large and any ordering is over-head cost when I need random records. A query like db().select(db.person.name, limitby=(0,50)) automatically applies order by clause to the query so resultant query becomes db().select(db.person.name, orderby=db.person.id, limitby(0,50)).

What is the rational behind appending orderby clause automatically ? Is there anyway to bypass this?

P.S. I have some other params which will make sure, records are not repeated when query is run multiple times without any ordering.

Bruno Rocha

unread,
Sep 22, 2011, 6:55:56 AM9/22/11
to web...@googlegroups.com
use this:

db().select(db.person.name, orderby="<random>", limitby=(0,50))


On Thu, Sep 22, 2011 at 4:43 AM, Tarun Arora <tarunaro...@gmail.com> wrote:
db().select(db.person.name, orderby=db.person.id, limitby(0,50))



--



--
Bruno Rocha
[ Aprenda a programar: http://CursoDePython.com.br ]
[ O seu aliado nos cuidados com os animais: http://AnimalSystem.com.br ]
[ Consultoria em desenvolvimento web: http://www.blouweb.com ]

Tarun Arora

unread,
Sep 22, 2011, 7:07:11 AM9/22/11
to web...@googlegroups.com
any orderby is over-head (small or big). Also limit with order by is not cost-efficient since limiting is done after ordering rows, resulting in fetching of all rows.

For time being, I am using executesql to execute my query.

Tarun Arora

unread,
Sep 22, 2011, 7:20:26 AM9/22/11
to web...@googlegroups.com
http://www.depesz.com/index.php/2007/09/16/my-thoughts-on-getting-random-row/ explains alternate approaches than using order by random().
 
"this is very good way to get truly random records. there is no “preference” for specific rows. the major drawback is speed. or lack of it actually."
 
Reply all
Reply to author
Forward
0 new messages