Skip to content

Commit

Permalink
speed up host & pathname migration by using UNION. perform VACUUM for…
Browse files Browse the repository at this point in the history
… SQLite migrations.
  • Loading branch information
dannyvankooten committed Nov 14, 2018
1 parent 00d2657 commit da6ffde
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- +migrate Up
INSERT IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_page_stats;
INSERT IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_referrer_stats;
INSERT INTO hostnames(name) SELECT hostname FROM daily_page_stats UNION SELECT hostname FROM daily_referrer_stats;

-- +migrate Down

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- +migrate Up
INSERT IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_page_stats;
INSERT IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_referrer_stats;
INSERT INTO pathnames(name) SELECT pathname FROM daily_page_stats UNION SELECT pathname FROM daily_referrer_stats;

-- +migrate Down

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- +migrate Up
INSERT INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_page_stats;
INSERT INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_referrer_stats ON CONFLICT(name) DO NOTHING;
INSERT INTO hostnames(name) SELECT hostname FROM daily_page_stats UNION SELECT hostname FROM daily_referrer_stats;

-- +migrate Down

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- +migrate Up
INSERT INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_page_stats ON CONFLICT(name) DO NOTHING; ;
INSERT INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_referrer_stats ON CONFLICT(name) DO NOTHING; ;
INSERT INTO pathnames(name) SELECT pathname FROM daily_page_stats UNION SELECT pathname FROM daily_referrer_stats;

-- +migrate Down

4 changes: 4 additions & 0 deletions pkg/datastore/sqlstore/migrations/sqlite3/15_vacuum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- +migrate Up notransaction
VACUUM;

-- +migrate Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- +migrate Up
INSERT OR IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_page_stats;
INSERT OR IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_referrer_stats;
-- +migrate Up
INSERT INTO hostnames(name) SELECT hostname FROM daily_page_stats UNION SELECT hostname FROM daily_referrer_stats;

-- +migrate Down

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- +migrate Up
INSERT OR IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_page_stats;
INSERT OR IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_referrer_stats;
INSERT INTO pathnames(name) SELECT pathname FROM daily_page_stats UNION SELECT pathname FROM daily_referrer_stats;

-- +migrate Down

4 changes: 4 additions & 0 deletions pkg/datastore/sqlstore/migrations/sqlite3/25_vacuum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- +migrate Up notransaction
VACUUM;

-- +migrate Down
2 changes: 1 addition & 1 deletion pkg/datastore/sqlstore/sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func New(c *Config) *sqlstore {
log.Printf("Connected to %s database: %s on %s", c.Driver, c.Dbname(), c.Host)
}

// run migrations
// apply database migrations (if any)
db.Migrate()

return db
Expand Down

0 comments on commit da6ffde

Please sign in to comment.