Hi all again,

Trying to deploy polymorphic inheritance, I am now hit by what may be
another bug: it seems like getting polymorphic objects may be 100
times slower than regular ones.

I reproduce the problem in the copy-pastable script below, using a
single table polymorphic inheritance scenario. On my machine, the
output of this script gives me:

    The non-polymorphic loop took 0.082 seconds
    The polymorphic loop took 7.916 seconds
    The polymorphic loop was 96 times slower

I know that there may be *some* overhead in using polymorphism, but 96
times slower sounds way too much, so I am suspecting some form of bug.

Is this a known issue? Is there a work around?

Thank you for your help!


### BEGIN: polymorphic_inheritance_slowness_test.py ###
from sqlalchemy import *

db = create_engine('sqlite://', echo=False)
__meta__ = BoundMetaData(db)

person_table = Table("persons", __meta__,
        Column("id", Integer, primary_key=True),
        Column("ptype", String(80)), # Polymorphic ID field
        Column("name", String(80)),
        )

class Person(object):
    def __init__(self, name):
        self.name = name

class Employee(Person): pass

__meta__.create_all()

person_mapper = mapper(Person, person_table,
                       polymorphic_on=person_table.c.ptype)
mapper(Employee, person_table, inherits=person_mapper,
                       polymorphic_identity='EMPLOYEE')


person_insert = person_table.insert()
# Alice is just a Person (non-polymorphic)
person_insert.execute(id=1, ptype=None, name='alice')
# Bob is an Employee (polymorphic)
person_insert.execute(id=2, ptype='EMPLOYEE', name='bob')

session = create_session()
query = session.query(Person)

# Now timing loops getting our 2 objects:
from time import time
start1 = time()
# Non-polymorphic get loop
for i in range(5000):
    dummy = query.get(1)

time1 = time() - start1

start2 = time()
# Polymorphic get loop
for i in range(5000):
    dummy = query.get(2)

time2 = time() - start2

print """The non-polymorphic loop took %0.3f seconds
The polymorphic loop took %0.3f seconds
The polymorphic loop was %i times slower""" % (time1, time2, time2/
time1)

### END ###


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to