1

我不知从哪里来了一些奇怪的开销。我正在运行一个 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

我有 16 毫秒的开销。SQL 查询只需要 1.8 毫秒即可执行。

有人能帮助我吗?

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

4

0 回答 0