Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set SQLite PRAGMA statements with SQLAlchemy

I would like SQLAlchemy to put the SQLite .journal file in-memory to speed up performance. I have tried this:

sqlite_db_engine = create_engine('sqlite:///%s' % str(dbname), connect_args = {'PRAGMA     journal_mode':'MEMORY', 'PRAGMA synchronous':'OFF', 'PRAGMA temp_store':'MEMORY', 'PRAGMA cache_size':'5000000'})

db = sqlite_db_engine.connect()

and this:

sqlite_db_engine = create_engine('sqlite:///%s' % str(dbname))

db = sqlite_db_engine.connect()
db.execute("PRAGMA journal_mode = MEMORY")
db.execute("PRAGMA synchronous = OFF")
db.execute("PRAGMA temp_store = MEMORY")
db.execute("PRAGMA cache_size = 500000")

With no luck. For long transactions I can still see the .journal file being created on the disk. Is there another way to set this?

*note I have no problem doing it with the built-in python sqlite module

like image 327
tomc Avatar asked Mar 12 '12 17:03

tomc


2 Answers

How about using events:

from sqlalchemy.engine import Engine
from sqlalchemy import event

@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
    cursor = dbapi_connection.cursor()
    cursor.execute("PRAGMA journal_mode=WAL")
    cursor.close()

See http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#foreign-key-support

like image 114
kalu Avatar answered Oct 09 '22 12:10

kalu


Basically you should be able to rewrite the examples about foreignkey to achieve what you want. Take a look at https://stackoverflow.com/a/7831210/1890086

engine = create_engine(database_url)

def _fk_pragma_on_connect(dbapi_con, con_record):
    dbapi_con.execute('PRAGMA journal_mode = MEMORY')
    # ...

from sqlalchemy import event
event.listen(engine, 'connect', _fk_pragma_on_connect)
like image 25
Elrond Avatar answered Oct 09 '22 11:10

Elrond