-
Notifications
You must be signed in to change notification settings - Fork 0
Stored procedure #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: streamline_api_endpoint
Are you sure you want to change the base?
Conversation
76ca1e1
to
3da6af9
Compare
Hmmm I don't think this is really beneficial to performance. It basically rebuilds a query with psql, including some N+1 sub-queries. I read the file name of the migration and my question would be: Why not use a real materialized view for this? Next to a real table, this is probably the most performant solution. |
I've considered materialized views initially, but the problem here is that we do not need view, but something that needs to loop over rows. |
I've rebased this on the |
The problem with this solution is that it will always be a sequential scan on the I think the best solution in terms of performance will be to persist every point of the accumulation. And every time you add another |
The sub-selects are currently run on every INSERT INTO asset_stats(paging_token, asset_code, asset_issuer, asset_type, created_at, issued, transferred, accounts_with_trustline, accounts_with_payments, payments)
VALUES ($1, $2, $3, $4, $5,
(SELECT COALESCE(SUM(amount), 0) FROM effects WHERE type='account_debited' AND account=$6),
(SELECT COALESCE(SUM(amount), 0) FROM effects WHERE type='account_debited' AND account!=$6),
(SELECT COUNT(DISTINCT account) FROM effects WHERE type='trustline_created' AND account!=$6),
(SELECT COUNT(DISTINCT account) FROM effects WHERE type='account_debited' AND account!=$6),
(SELECT COUNT(*) FROM effects WHERE type='account_debited' AND account!=$6)
) It would be better to pass the new Effect into |
In order to support the generic helper functions for transactions and usual connections, we used to use the generic interface and do manual type-checks and conversions. However, as sqlx.DB and sqlx.Tx share the same method signatures, I created a new interface that contains all used methods and was able to replace the usage of the generic interface{} with the new one.
Use a stored procedure to generate
asset_stats
on the fly from theeffects
table. I'm not sure whether this is actually good or bad for performance (less writes, but probably a bit more calculations).My guess is, that CPU is way less of a bottleneck than IO, therefore I'd go with this version.
This pull-request also adds a change (which we probably should backport in case we decide against this) to not include transfers and account stats from the asset issuer account. I'd argue that the issuer should be ignored when counting payments and involved accounts of customers.