来自python函数调用的奇怪开销

时间:2013-02-07 18:01:54

标签: python mysql performance optimization sqlalchemy

我有一些奇怪的开销来自无处。我正在运行python脚本并使用SQLAlchemy进行一些SQL查询。这是我的问题:

def _create_connection(name):
    connection = get_connection(name)
    cstring = "mysql+mysqldb://login:passwd@host/database?charset=utf8"
    return create_engine(cstring)

def _get_table(engine, table):
    metadata = MetaData(engine)
    return Table(table, metadata, autoload=True)

class A(object):
    def requests_statuses(self, user_id):
        friendship_request_t = _get_table(self._db, "friendship_request")
        user_t = _get_table(self._db, "user")

        a = datetime.now()

        s = select([user_t.c.user_id, friendship_request_t.c.status],
                   from_obj=[friendship_request_t.join(
                    user_t,
                    friendship_request_t.c.user_id_a==user_t.c.user_id)]).\
            where(friendship_request_t.c.user_id_b==bindparam('fr_user_id_b'))
        results = self._db.execute(s, fr_user_id_b=user_id).fetchall()

        b = datetime.now()

        print "interval: " + unicode(b - a)

        return results

class B(object):
    def friend_requests_statuses(self):
        a_obj = A()

        print "begin: " + unicode(datetime.now())
        results = a_obj.requests_statuses(self.id)
        print "end  : " + unicode(datetime.now())

运行此程序时:

b_obj = B()
b.friend_requests_statuses()

我有这个输出:

begin: 2013-02-07 18:57:40.058834
interval: 0:00:00.001852
end  : 2013-02-07 18:57:40.076525

我有16ms的开销。 SQL查询只花了1.8ms来执行。

有人能帮助我吗?

退出A.requests_statuses方法时,“开销”是否可能来自垃圾收集?

0 个答案:

没有答案