Skip to content

Commit 3a464bd

Browse files
committed
Some additional indices were added to database for faster access to 'ledger' table (new DB schema isn't activated yet).
1 parent 7c0e084 commit 3a464bd

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

jal/db/operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def __init__(self, operation_data=None, opart=None):
704704
else:
705705
self._icon = JalIcon[JalIcon.BUY]
706706
self._oname = self.tr("Buy")
707-
if self._opart is not None and self._opart == self.PART_PROFIT: # FIXME this query slows down UI a lot - make db index? or change
707+
if self._opart is not None and self._opart == self.PART_PROFIT:
708708
profit = self._read("SELECT amount FROM ledger WHERE otype=:otype AND oid=:oid AND opart=:opart AND book_account=:book",
709709
[(":otype", self._otype), (":oid", self._oid), (":opart", self._opart), (":book", BookAccount.Incomes)])
710710
try:

jal/jal_init.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ CREATE TABLE ledger (
182182
category_id INTEGER REFERENCES categories (id) ON DELETE NO ACTION ON UPDATE NO ACTION, -- category that is related with transaction. No FK enforcement as table is cleaned by categories_after_delete trigger
183183
tag_id INTEGER REFERENCES tags (id) ON DELETE NO ACTION ON UPDATE NO ACTION -- tag that is related with transaction. No FK enforcement as table is cleaned by tags_after_delete trigger
184184
);
185+
DROP INDEX IF EXISTS ledger_by_operation;
186+
CREATE INDEX ledger_by_operation ON ledger (otype, oid, opart, book_account);
187+
DROP INDEX IF EXISTS ledger_by_time;
188+
CREATE INDEX ledger_by_time ON ledger (timestamp, asset_id, account_id);
185189

186190
-- Table: ledger_totals to keep last accumulated amount value for each transaction
187191
DROP TABLE IF EXISTS ledger_totals;

jal/updates/jal_delta_58.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BEGIN TRANSACTION;
2+
--------------------------------------------------------------------------------
3+
PRAGMA foreign_keys = OFF;
4+
--------------------------------------------------------------------------------
5+
DROP INDEX IF EXISTS ledger_by_operation;
6+
CREATE INDEX ledger_by_operation ON ledger (otype, oid, opart, book_account);
7+
DROP INDEX IF EXISTS ledger_by_time;
8+
CREATE INDEX ledger_by_time ON ledger (timestamp, asset_id, account_id);
9+
--------------------------------------------------------------------------------
10+
-- Set new DB schema version
11+
UPDATE settings SET value=58 WHERE name='SchemaVersion';
12+
COMMIT;

0 commit comments

Comments
 (0)