From 236745daf0e528567e1b4e68f990b6f7e83fc5c8 Mon Sep 17 00:00:00 2001 From: Young Hahn Date: Sun, 22 Jan 2012 17:29:30 -0500 Subject: [PATCH] Aggressive performance opts. --- bin/mbrekey | 15 +++++++++------ lib/mbtiles.js | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bin/mbrekey b/bin/mbrekey index 918ddce..69a8fc0 100755 --- a/bin/mbrekey +++ b/bin/mbrekey @@ -19,6 +19,8 @@ var filename = argv._[0]; var db = new sqlite3.Database(filename); +db.exec("PRAGMA journal_mode = OFF; PRAGMA locking_mode = EXCLUSIVE; PRAGMA cache_size = 400000; PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY;"); + var tables = {}; db.all("SELECT tbl_name FROM sqlite_master WHERE TYPE = 'table'", function(err, map) { if (err) throw err; @@ -35,13 +37,14 @@ db.run('CREATE INDEX "temp_tile_id_idx" ON "map" ("tile_id")', function(err) { console.warn('Created temporary index.'); db.run('CREATE TEMP TABLE "tile_hash_id" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "hash" TEXT UNIQUE NOT NULL)', function(err) { if (err) throw err; - db.run('INSERT INTO tile_hash_id (hash) SELECT tile_id FROM images', function(err) { + db.run('REPLACE INTO tile_hash_id (hash) SELECT tile_id FROM images', function(err) { if (err) throw err; db.get('SELECT MAX(id) AS max FROM tile_hash_id', function(err, max) { if (err) throw err; max = max.max; console.warn('Starting tile update... (%d total)', max); - for (var i = 1; i < max; i += 1000) { + + for (var i = 1; i < max; i += 10000) { tileQueue.add(i); } }); @@ -58,13 +61,13 @@ tileQueue.on('empty', function() { db.run('CREATE TEMP TABLE "grid_hash_id" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "hash" TEXT UNIQUE NOT NULL)', function(err) { if (err) throw err; - db.run('INSERT INTO grid_hash_id (hash) SELECT grid_id FROM grid_utfgrid', function(err) { + db.run('REPLACE INTO grid_hash_id (hash) SELECT grid_id FROM grid_utfgrid', function(err) { if (err) throw err; db.get('SELECT MAX(id) AS max FROM grid_hash_id', function(err, max) { if (err) throw err; max = max.max; console.warn('Starting grid update... (%d total)', max); - for (var i = 1; i < max; i += 1000) { + for (var i = 1; i < max; i += 10000) { gridQueue.add(i); } }); @@ -77,7 +80,7 @@ gridQueue.on('empty', deleteTempKey); var changedTiles = 0; function changeTileID(start, done) { - db.all('SELECT id, hash FROM tile_hash_id WHERE id >= ? AND id < ?', start, start + 1000, function(err, rows) { + db.all('SELECT id, hash FROM tile_hash_id WHERE id >= ? AND id < ?', start, start + 10000, function(err, rows) { if (err) throw err; db.serialize(function() { db.run('BEGIN'); @@ -98,7 +101,7 @@ function changeTileID(start, done) { var changedGrids = 0; function changeGridID(start, done) { - db.all('SELECT id, hash FROM grid_hash_id WHERE id >= ? AND id < ?', start, start + 1000, function(err, rows) { + db.all('SELECT id, hash FROM grid_hash_id WHERE id >= ? AND id < ?', start, start + 10000, function(err, rows) { if (err) throw err; db.serialize(function() { db.run('BEGIN'); diff --git a/lib/mbtiles.js b/lib/mbtiles.js index b5080d5..0902cf0 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -392,7 +392,7 @@ MBTiles.prototype.startWriting = function(callback) { if (!this._isWritable) { this._isWritable = 1; this._clearCaches(); - this._db.run('PRAGMA synchronous=OFF', callback); + this._db.exec('PRAGMA journal_mode = OFF; PRAGMA locking_mode = EXCLUSIVE; PRAGMA synchronous=OFF; PRAGMA cache_size = 400000;', callback); } else { this._isWritable++; return callback(null); @@ -505,7 +505,7 @@ MBTiles.prototype.stopWriting = function(callback) { this._commit(function(err) { if (err) return callback(err); if (!mbtiles._isWritable) { - mbtiles._db.run('PRAGMA synchronous=NORMAL', callback); + mbtiles._db.exec('PRAGMA journal_mode = DELETE; PRAGMA locking_mode = NORMAL; PRAGMA synchronous=NORMAL; PRAGMA cache_size=2000;', callback); } else { return callback(null); }