index_caching.rb

lib/sequel/extensions/index_caching.rb
Last Update: 2023-08-17 13:01:31 -0700

The index_caching extension adds a few methods to Sequel::Database that make it easy to dump information about database indexes to a file, and load it from that file. Loading index information from a dumped file is faster than parsing it from the database, so this can save bootup time for applications with large numbers of index.

Basic usage in application code:

DB = Sequel.connect('...')
DB.extension :index_caching
DB.load_index_cache('/path/to/index_cache.dump')

# load model files

Then, whenever database indicies are modified, write a new cached file. You can do that with bin/sequel‘s -X option:

bin/sequel -X /path/to/index_cache.dump postgres://...

Alternatively, if you don’t want to dump the index information for all tables, and you don’t worry about race conditions, you can choose to use the following in your application code:

DB = Sequel.connect('...')
DB.extension :index_caching
DB.load_index_cache?('/path/to/index_cache.dump')

# load model files

DB.dump_index_cache?('/path/to/index_cache.dump')

With this method, you just have to delete the index dump file if the schema is modified, and the application will recreate it for you using just the tables that your models use.

Note that it is up to the application to ensure that the dumped index cache reflects the current state of the database. Sequel does no checking to ensure this, as checking would take time and the purpose of this code is to take a shortcut.

The index cache is dumped in Marshal format, since it is the fastest and it handles all ruby objects used in the indexes hash. Because of this, you should not attempt to load from an untrusted file.

Related module: Sequel::IndexCaching