Hi All,

I'm in the process of upgrading an application that was using SA 0.1.5 to SA 0.2.2.  One section of the code that is structurally similar to the following code is now running many times slower under .2 than it was with .1. On my fast laptop with 2G RAM this test code takes 52 seconds to create 1000 rows, most of which is spent in the nested loops creating the objects. I'm using Postgres 8.1.4 under Win XP.

Perhaps my approach is flawed? I'd be glad to hear of a better way to do this sort of thing, if anyone has any ideas.

Best,
Bob


import sqlalchemy.mods.threadlocal
from sqlalchemy import *

dbEngine = create_engine('postgres://[EMAIL PROTECTED]:5432/mydb')
metadata = BoundMetaData(dbEngine)

t1s = Table( 't1s', metadata,
    Column( 'id', Integer, primary_key=True))

t2s = Table( 't2s', metadata,
    Column( 'id', Integer, primary_key=True),
    Column( 't1id', Integer, ForeignKey("t1s.id"), nullable=True ))

t3s = Table( 't3s', metadata,
    Column( 'id', Integer, primary_key=True),
    Column( 't2id', Integer, ForeignKey("t2s.id"), nullable=True ))

t4s = Table( 't4s', metadata,
    Column( 'id', Integer, primary_key=True),
    Column( 't3id', Integer, ForeignKey("t3s.id"), nullable=True ))

class T1( object ): pass
class T2( object ): pass
class T3( object ): pass
class T4( object ): pass

assign_mapper( T1, t1s )
assign_mapper( T2, t2s )      
assign_mapper( T3, t3s )      
assign_mapper( T4, t4s )      

T1.mapper.add_property( 't2s', relation(T2, backref="t1"))
T2.mapper.add_property ( 't3s', relation(T3, backref="t2"))
T3.mapper.add_property( 't4s', relation(T4, backref="t3"))

metadata.drop_all()
metadata.create_all()

print "start"       
o1 = T1()
for i2 in range(10):
    o2 = T2()
    o1.t2s.append( o2 )
   
    for i3 in range( 10 ):
        o3 = T3()
        o2.t3s.append( o3 )
       
        for i4 in range( 10 ):
            o3.t4s.append ( T4() )
            print i2, i3, i4
           
print "flushing"
objectstore.flush()

print "done"
           



_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to