[BangPypers] Getting a random record from Database . The best strategy.

s|s supr.e.etsethi at gmail.com
Thu Oct 3 19:59:32 CEST 2013


An ideal situation is to use predictable test interfaces. Hence ideally you
should have sequence of inserts and updates and selects at end of which you
should be able to verify that database logic is consistent with your
expectations.

But if random records query is a requirement, at outset you can view
sqlalchemy db cursor as an iterator thus you can use **choice** and
**sample** routines from random library module. The merit to this approach
is simplicity in case test records are not very many. To optimize on this
approach further, you can use randint(1, <number of rows>) and use returned
value as primary key search parameter in your select statement. With this
approach you can make two fast queries and get a random row of which count
query would often be cached.

regards

Supreet


On Thu, Oct 3, 2013 at 10:11 PM, Amit Sethi <amit.pureenergy at gmail.com>wrote:

> Hi ,  I am not sure if this question will be considered sufficiently
> python related. If its not than I am sorry I will take this somewhere
> else
>
> I am setting up some smoke tests for which I am fetching some records
> from the db.
>
> I am using sqlalchemy for this right now.
> My real question when fetching such a random record from a db for some
> reason.
> What is the best strategy.
>
> Should I use some  kind of randomizer in the database tool.
> Or should I use something in ORM
> Or should I make the decision for finding the random value in the
> python or the language I am working with and then fetch the record
> using the tools I am using.
>
>
> Thanks
> Amit
>
> --
> A-M-I-T S|S
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Supreet Sethi
Ph UK: +447859172473
Ph IN: +919811143517
Ph Skype: d_j_i_n_n
Profile: http://www.google.com/profiles/supreet.sethi
Twt: http://twitter.com/djinn


More information about the BangPypers mailing list