Skip to content

Commit 0ed2f9d

Browse files
mbhuttonAlexanderWillner
authored andcommitted
fix: Improve query performance by using a single connection
Use one connection for the lifetime of the process rather than creating a new connection for each query. This cuts execution time roughtly in half for queries across large Things databases.
1 parent 681cb8b commit 0ed2f9d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

things/database.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class Database:
168168
"""
169169

170170
debug = False
171+
connection: sqlite3.Connection
171172

172173
# pylint: disable=R0913
173174
def __init__(self, filepath=None, print_sql=False):
@@ -181,6 +182,11 @@ def __init__(self, filepath=None, print_sql=False):
181182
if self.print_sql:
182183
self.execute_query_count = 0
183184

185+
# "ro" means read-only
186+
# See: https://sqlite.org/uri.html#recognized_query_parameters
187+
uri = f"file:{self.filepath}?mode=ro"
188+
self.connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101
189+
184190
# Test for migrated database in Things 3.15.16+
185191
# --------------------------------
186192
assert self.get_version() > 21, (
@@ -495,17 +501,14 @@ def execute_query(self, sql_query, parameters=(), row_factory=None):
495501
print(prettify_sql(sql_query))
496502
print()
497503

498-
# "ro" means read-only
499-
# See: https://sqlite.org/uri.html#recognized_query_parameters
500-
uri = f"file:{self.filepath}?mode=ro"
501-
connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101
502-
connection.row_factory = row_factory or dict_factory
503-
cursor = connection.cursor()
504-
cursor.execute(sql_query, parameters)
504+
with self.connection:
505+
# Using context manager to keep queries in separate transactions,
506+
# see https://docs.python.org/3/library/sqlite3.html#sqlite3-connection-context-manager
507+
self.connection.row_factory = row_factory or dict_factory
508+
cursor = self.connection.cursor()
509+
cursor.execute(sql_query, parameters)
505510

506-
result = cursor.fetchall()
507-
connection.close()
508-
return result
511+
return cursor.fetchall()
509512

510513

511514
# Helper functions

0 commit comments

Comments
 (0)