我尝试在我的Ruby-on-Rails项目中设置Sqlite3的日志模式。
ruby-on-rails似乎使用sqlite的默认日志模式,即'delete',因为当我更新数据库时,我在'db‘文件夹中看到了一个日志文件,当更新完成时,它被删除了。我希望将jouranl mode设置为"WAL“或"memory”。我尝试了Sqlite命令行
PRAGMA main.journal_mode=WAL但它不会影响ruby-on-rails中的应用程序。
最后,我通过修改sqlite3_adapter.rb的源代码实现了这个功能
我更改了文件中的一个函数: activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb
def configure_connection
# here are original codes
execute("PRAGMA journal_mode = WAL", "SCHEMA")
end因为SQLite3Adapter的初始化调用了configure_connection
这听起来并不是很好的解决方案,尽管它是有效的。在Ruby-on-Rails(版本是5.1.4)中,有没有更好的方法来设置Sqlite3的日志模式?例如配置选项
发布于 2019-05-24 13:46:52
自从我不得不这样做已经有一段时间了,但是你应该能够使用初始化器,这样你就不需要修补源代码了。
将类似以下内容放入config/initializers/configure_sqlite_journal.rb中
if c = ::ActiveRecord::Base.connection
c.execute 'PRAGMA journal_mode = WAL'
end应该做你想做的事
发布于 2020-08-01 23:37:58
我在这里找到了一个更好的答案:Speed up your Rails sqlite database for large dataset,更高的性能和配置:
代码(我把它放到config/initializers/speedup_sqlite3.rb中):
if ::ActiveRecord::Base.connection_config[:adapter] == 'sqlite3'
if c = ::ActiveRecord::Base.connection
# see http://www.sqlite.org/pragma.html for details
# Page size of the database. The page size must be a power of two between 512 and 65536 inclusive
c.execute 'PRAGMA main.page_size=4096;'
# Suggested maximum number of database disk pages that SQLite will hold in memory at once per open database file
c.execute 'PRAGMA main.cache_size=10000;'
# Database connection locking-mode. The locking-mode is either NORMAL or EXCLUSIVE
c.execute 'PRAGMA main.locking_mode=EXCLUSIVE;'
# Setting of the "synchronous" flag, "NORMAL" means sync less often but still more than none
c.execute 'PRAGMA main.synchronous=NORMAL;'
# Journal mode for database, WAL=write-ahead log
c.execute 'PRAGMA main.journal_mode=WAL;'
# Storage location for temporary tables, indices, views, triggers
c.execute 'PRAGMA main.temp_store = MEMORY;'
end
endhttps://stackoverflow.com/questions/56286225
复制相似问题