psycopg2 faster way to retrieve last x records

Luis P. Mendes lupe at localhost.localdomain
Wed Nov 8 01:30:47 CET 2006


Hi,

I would like to know if there is a better way to do what I'm already doing
as stated in the following example, when using psycopg2 with PostgresQL.
.......................
nr_bars_before = 200
tabela = 'seconds'
sqlString = "SELECT * FROM " + tabela + " ORDER BY tempounix;"
curs = self.conn.cursor()
curs.execute(sqlString)
try:
	while 1:
	curs.scroll(1,mode='relative')
except: pass
curs.scroll(-int(math.fabs(nr_bars_before)),mode='relative')
row = curs.fetchone()
curs.close()
.......................

What I need is to get the last 200
records from the table, each couple minutes.  As stated,
what I do is to go all the way through the table records until the end,
then going back 200 in order to select all of them (those 200) forward
down to the last one.

But it takes a lot of time to do it.  I mean some seconds.  And it brings
some 'heavy' work on disk.  The table 'seconds' has 540000+ lines right
now. 

Can I do something different in order to have a lighter load on the system
and a quicker response?

Luis P. Mendes



More information about the Python-list mailing list