Skip to content

Commit 6efc0a7

Browse files
committed
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 b7877b6 commit 6efc0a7

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

things/database.py

+9-7
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,16 +501,12 @@ 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+
self.connection.row_factory = row_factory or dict_factory
505+
cursor = self.connection.cursor()
506+
504507
cursor.execute(sql_query, parameters)
505508

506509
result = cursor.fetchall()
507-
connection.close()
508510
return result
509511

510512

0 commit comments

Comments
 (0)