为什么查询时间在sqlite3数据库中波动?

时间:2019-02-19 13:46:39

标签: python-3.x sqlite sqlalchemy

我正在测试使用sqlalchemy和原始SQLite3查询存储在外部硬盘驱动器中的数据库之间的时间差,以便了解哪一个时间最合适,并找到了结果随时间波动:

Here I'm repeatedly executing the same block of queries in both Raw sqlite3(green) and sqlalchemy.

每个步骤使用的代码是:

def time_attack_alchemy():
    import sqlalchemy as sql
    from pydave4vm.addons.maindb import Base, ActiveRegion, Observations

    ###################################################################
    #creating the engine
    engine = sql.create_engine('sqlite:////.../main.db')
    Base.metadata.bind = engine
    #binding the engine
    DBSession = sql.orm.sessionmaker(bind = engine)
    #binding the object methods
    session = DBSession()

    # Getting all entries of the activeregion table.
    s = sql.select([ActiveRegion])
    results = session.execute(s).fetchall()

    ###################################################################
    # Getting all the observation ids of a given region.
    s = sql.select([Observations.OBS_id]).where(Observations.ar_id == 1)
    results = session.execute(s).fetchall()


    ###################################################################
    # Getting a image of a region given its id.
    s = sql.select([Observations.mean_bx]).where(Observations.OBS_id == 1)
    results = session.execute(s).fetchall()

    session.close()
    return

和:

def time_attack_rawsql():
    import sqlite3

    # Making the connection to the db on the hardrive.
    connection = sqlite3.connect('/Volumes/chicrala/databases/main.db')

    # Creating the cursor.
    cursor = connection.cursor()

    # Getting all entries of the activeregion table.
    cursor.execute("SELECT * FROM ActiveRegion")
    results = cursor.fetchall()

    # Getting all the observation ids of a given region.cursor.execute("SELECT OBS_id FROM Observations WHERE ar_id=1")
    results = cursor.fetchall()

    # Getting a image of a region given its id.
    cursor.execute("SELECT mean_bx FROM Observations WHERE OBS_id=1")
    results = cursor.fetchall()

    # Closing the connection.
    connection.close()

    return

那么为什么这些结果在每个步骤中都稳定地振荡?这是硬盘中固有的吗?还是可以将查询时间降低到下限?

谢谢。

0 个答案:

没有答案