PHP / SQLite - Copying a table from disk to memory

后端 未结 6 1771
日久生厌
日久生厌 2020-12-30 10:36

I have a sqlite3 database on my harddrive (file.db) with 5 tables. I\'d like to copy 3 of these tables to an in-memory database (:memory:).

Is there a simple way to

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 11:38

    Using the method outlined by VolkerK roughly doubled the performance of my code when using a ~150Mb sqlite database.

    Loading the database into an in-memory sqlite db didn't require any other changes to my existing code.

    My use case was batch processing data so I didn't have to deal with the problems Wez Furlong highlights.

    Reading the data into memory was surprisingly fast. The whole 150Mb was loaded into memory from SSD in less than two seconds. It seemed too good to be true so I checked and rechecked the data.

    Many thanks to VolkerK for a magic solution!

    I also found that when I indexed the in-memory tables my queries executed three times faster

    //adapting VolkerK's example...
    //loop through tables from the local sqlite db 
    $tables = array('companies', 'directors',  'previous_names');
    foreach($tables as $table){
        //load each table into memory
        $pdo->exec("CREATE TABLE $table AS SELECT * FROM filedb.$table");
    
        //index each table on the relevant columns
        $pdo->exec("CREATE INDEX IF NOT EXISTS `".$table."_company_number` 
                    ON $table (`company_number`);");
    }
    

提交回复
热议问题