やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

SQLite3でTEMPの保存先を指定する

 一時テーブルやインデックスをメモリに保存する等。

成果物

情報源

temp保存先をインメモリにしたい

PRAGMA temp_store=2;

 これが必要。これがないときはファイル保存される。以下、しくみ。

SQLITE_TEMP_STORE

 コンパイルオプションのひとつ。

SQLITE_TEMP_STORE 意味
0 常に一時ファイルを使用する
1 デフォルトでファイルを使用するが、PRAGMA temp_storeによるオーバーライドを許可する
2 デフォルトでメモリを使用するが、PRAGMA temp_storeによるオーバーライドを許可する
3 常にメモリを使用する

 デフォルトは1詳細

PRAGMA temp_store

 SQLITE_TEMP_STORE1,2のいずれかのときのみ有効。

PRAGMA temp_store 保存先(temp表, index)
0 SQLITE_TEMP_STOREに従う
1 ファイル
2 メモリ

PRAGMA temp_store_directory = 'ディレクトリ名'

 保存パスを設定する。sqlite3_temp_directoryでも可。非推奨らしい。

全パターン

SQLITE_TEMP_STORE PRAGMA temp_store 保存先(temp表, index)
0 * ファイル
1 0 ファイル
1 1 ファイル
1 2 メモリ
2 0 メモリ
2 1 ファイル
2 2 メモリ
3 * メモリ

3.29.0のSQLITE_TEMP_STORE値を調べた

grep SQLITE_TEMP_STORE ./*
./Makefile.msc:# SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to
./Makefile.msc:TCC = $(TCC) -DSQLITE_TEMP_STORE=1
./Makefile.msc:RCC = $(RCC) -DSQLITE_TEMP_STORE=1
./sqlite3.c:#ifdef SQLITE_TEMP_STORE
./sqlite3.c:  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
./sqlite3.c:** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
./sqlite3.c:#ifndef SQLITE_TEMP_STORE
./sqlite3.c:# define SQLITE_TEMP_STORE 1
./sqlite3.c:** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
./sqlite3.c:#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
./sqlite3.c:** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
./sqlite3.c:      if( SQLITE_TEMP_STORE==0
./sqlite3.c:       || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
./sqlite3.c:       || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
./sqlite3.c:** parameter) and the compile time value of SQLITE_TEMP_STORE. The
./sqlite3.c:**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
./sqlite3.c:#if SQLITE_TEMP_STORE==1
./sqlite3.c:#if SQLITE_TEMP_STORE==2
./sqlite3.c:#if SQLITE_TEMP_STORE==3
./sqlite3.c:#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
grep: ./tea: ディレクトリです

 -DSQLITE_TEMP_STORE=1# define SQLITE_TEMP_STORE 1からみて1であると思われる。

 つまり、temp保存先をインメモリにしたいときは以下。

PRAGMA temp_store=2;

 これがないときはファイル保存される。もうこれは~/.sqlitercに設定してもいいレベルか。

~/.sqliterc

~/.sqliterc

PRAGMA temp_store=2;

 ~/.sqlitercファイルにて、起動時にTEMP保存先をインメモリに指定する。

 -- Loading resources from /home/*/.sqlitercの表示がウザいときは以下。

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux

前回まで