diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index 6c3743cb0..fce8e480e 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -302,3 +302,20 @@ async def m010_create_installed_extensions_table(db): ); """ ) + + +async def m011_optimize_balances_view(db): + """ + Make the calculation of the balance a single aggregation + over the payments table instead of 2. + """ + await db.execute("DROP VIEW balances") + await db.execute( + """ + CREATE VIEW balances AS + SELECT wallet, SUM(amount - abs(fee)) AS balance + FROM apipayments + WHERE (pending = false AND amount > 0) OR amount < 0 + GROUP BY wallet + """ + )