Hello.

I am just starting with SQLAlchemy and have a small problem: my "get" query 
is compiled every time I use it.
According to cProfile: 
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/sql/expression.py:1864(compile)
10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/sql/expression.py:1908(_compiler)
10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/sql/compiler.py:231(__init__)
10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/engine/interfaces.py:764(__init__)
10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/engine/interfaces.py:805(process)
90/10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/sql/visitors.py:73(_compiler_dispatch)
10    0.000    0.000    0.002    0.000 
/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.8.1dev-py2.7-linux-x86_64.egg/sqlalchemy/sql/compiler.py:1082(visit_select)


In case of native API, I would use prepare "command/statement", but it 
looks I am missing something obvious and cannot find similar functionality 
in the package.

Below, you can find a complete source code, which reproduce the issue.

Thank you!


import sqlalchemy 
import sqlalchemy.ext.declarative

Base = sqlalchemy.ext.declarative.declarative_base()

class Employee(Base):
    __tablename__ = 'employee'
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    name = sqlalchemy.Column(sqlalchemy.String(100), nullable=False)

def run_query(session):
    employee_by_id = session.query(Employee)
    for i in range(10):
        x = employee_by_id.get(i)
        print x.name
    session.rollback()  

if __name__ == '__main__':
    engine = sqlalchemy.create_engine('sqlite://')
    Base.metadata.create_all(engine)
    Session = sqlalchemy.orm.sessionmaker(bind=engine)
    
    session = Session()
    for i in range(10):
        session.add(Employee(id=i, name='name' + str(i)))
    session.commit()
       
    import cProfile
    cProfile.run('run_query(Session())', 'get.stats') 


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to