Skip to content

Commit

Permalink
Merge pull request #600 from mixpanel/jared-sql-optimizations
Browse files Browse the repository at this point in the history
create indexes and enable WAL journal_mode
  • Loading branch information
jaredmixpanel authored Apr 28, 2023
2 parents ce587c0 + 98ca3be commit 5b9fca0
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions Sources/MPDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,24 @@ class MPDB {
close()
} else {
Logger.info(message: "Successfully opened connection to database at path: \(dbPath)")
createTables()
if let db = connection {
let pragmaString = "PRAGMA journal_mode=WAL;"
var pragmaStatement: OpaquePointer?
if sqlite3_prepare_v2(db, pragmaString, -1, &pragmaStatement, nil) == SQLITE_OK {
if sqlite3_step(pragmaStatement) == SQLITE_ROW {
let res = String(cString: sqlite3_column_text(pragmaStatement, 0))
Logger.info(message: "SQLite journal mode set to \(res)")
} else {
logSqlError(message: "Failed to enable journal_mode=WAL")
}
} else {
logSqlError(message: "PRAGMA journal_mode=WAL statement could not be prepared")
}
sqlite3_finalize(pragmaStatement)
} else {
reconnect()
}
createTablesAndIndexes()
}
}
}
Expand Down Expand Up @@ -108,10 +125,34 @@ class MPDB {
}
}

private func createTables() {
private func createIndexFor(_ persistenceType: PersistenceType) {
if let db = connection {
let tableName = tableNameFor(persistenceType)
let indexName = "idx_\(persistenceType)_time"
let createIndexString = "CREATE INDEX IF NOT EXISTS \(indexName) ON \(tableName) (time);"
var createIndexStatement: OpaquePointer?
if sqlite3_prepare_v2(db, createIndexString, -1, &createIndexStatement, nil) == SQLITE_OK {
if sqlite3_step(createIndexStatement) == SQLITE_DONE {
Logger.info(message: "\(indexName) index created")
} else {
logSqlError(message: "\(indexName) index creation failed")
}
} else {
logSqlError(message: "CREATE statement for index \(indexName) could not be prepared")
}
sqlite3_finalize(createIndexStatement)
} else {
reconnect()
}
}

private func createTablesAndIndexes() {
createTableFor(PersistenceType.events)
createIndexFor(PersistenceType.events)
createTableFor(PersistenceType.people)
createIndexFor(PersistenceType.people)
createTableFor(PersistenceType.groups)
createIndexFor(PersistenceType.groups)
}

func insertRow(_ persistenceType: PersistenceType, data: Data, flag: Bool = false) {
Expand Down

0 comments on commit 5b9fca0

Please # to comment.