wow, OK i am a supreme idiot for missing what was wrong with this one, ive been focusing on the attributes package for a couple of days now and i have a really nice rewrite waiting to go for that....but it has nothing to do with this issue !   its just the save-update cascade rule firing off for all those backreferences you have there, from the appended child down to whatever children it might have, and then the backref goes off and it goes in the other direction all the way up the tree to the parent item, causing an exponential slowdown, all totally unnecessarily since everything is already in the session.

changeset 1623 commits a check for if the attached item is already in the session, in which case the save-update cascade operation just doesnt happen.  speed is now more or less identical to 0.1.7.

On Jun 7, 2006, at 10:38 PM, Robert Taylor wrote:

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 mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to