Я тестировал разницу во времени между запросами к базе данных, которая хранится на внешнем жестком диске , с использованием sqlalchemy и необработанного SQLite3, чтобы определить, какие из них дадут мне лучшие времена, и нашел результаты.колебаться во времени:
![Here I'm repeatedly executing the same block of queries in both Raw sqlite3(green) and sqlalchemy.](https://i.stack.imgur.com/csEIc.png)
Код, используемый для каждого шага:
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
Так почему же эти результаты постоянно колеблются на каждом этапе?Это что-то присуще жесткому диску?Или я могу уменьшить время запроса до нижней границы?
Спасибо.