Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
balance_tracker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
balance_tracker
Commits
58a4e8d9
Commit
58a4e8d9
authored
1 month ago
by
Michal Zander
Committed by
Dan Notestein
1 month ago
Browse files
Options
Downloads
Patches
Plain Diff
Optimization of daily aggregation
parent
36518079
No related branches found
No related tags found
1 merge request
!169
Merge develop
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/aggregated_history.sql
+30
-18
30 additions, 18 deletions
backend/aggregated_history.sql
with
30 additions
and
18 deletions
backend/aggregated_history.sql
+
30
−
18
View file @
58a4e8d9
...
@@ -188,6 +188,18 @@ RETURN QUERY (
...
@@ -188,6 +188,18 @@ RETURN QUERY (
ROW_NUMBER
()
OVER
(
ORDER
BY
date
)
AS
row_num
ROW_NUMBER
()
OVER
(
ORDER
BY
date
)
AS
row_num
FROM
date_series
FROM
date_series
),
),
get_daily_aggregation
AS
MATERIALIZED
(
SELECT
bh
.
account
,
bh
.
nai
,
bh
.
balance
,
bh
.
min_balance
,
bh
.
max_balance
,
bh
.
source_op_block
,
bh
.
updated_at
FROM
balance_history_by_day
bh
WHERE
bh
.
account
=
_account_id
AND
bh
.
nai
=
_coin_type
),
balance_records
AS
(
balance_records
AS
(
SELECT
SELECT
ds
.
date
,
ds
.
date
,
...
@@ -197,10 +209,7 @@ RETURN QUERY (
...
@@ -197,10 +209,7 @@ RETURN QUERY (
bh
.
max_balance
,
bh
.
max_balance
,
bh
.
source_op_block
bh
.
source_op_block
FROM
add_row_num_to_series
ds
FROM
add_row_num_to_series
ds
LEFT
JOIN
balance_history_by_day
bh
ON
LEFT
JOIN
get_daily_aggregation
bh
ON
ds
.
date
=
bh
.
updated_at
bh
.
account
=
_account_id
AND
bh
.
nai
=
_coin_type
AND
ds
.
date
=
bh
.
updated_at
),
),
filled_balances
AS
(
filled_balances
AS
(
WITH
RECURSIVE
agg_history
AS
(
WITH
RECURSIVE
agg_history
AS
(
...
@@ -219,11 +228,8 @@ RETURN QUERY (
...
@@ -219,11 +228,8 @@ RETURN QUERY (
bh
.
min_balance
,
bh
.
min_balance
,
bh
.
max_balance
,
bh
.
max_balance
,
bh
.
source_op_block
bh
.
source_op_block
FROM
balance_history_by_day
bh
FROM
get_daily_aggregation
bh
WHERE
WHERE
bh
.
updated_at
<
ds
.
date
bh
.
account
=
_account_id
AND
bh
.
nai
=
_coin_type
AND
bh
.
updated_at
<
ds
.
date
ORDER
BY
bh
.
updated_at
DESC
ORDER
BY
bh
.
updated_at
DESC
LIMIT
1
LIMIT
1
)
prev_balance
ON
TRUE
)
prev_balance
ON
TRUE
...
@@ -284,6 +290,18 @@ RETURN QUERY (
...
@@ -284,6 +290,18 @@ RETURN QUERY (
ROW_NUMBER
()
OVER
(
ORDER
BY
date
)
AS
row_num
ROW_NUMBER
()
OVER
(
ORDER
BY
date
)
AS
row_num
FROM
date_series
FROM
date_series
),
),
get_montly_aggregation
AS
MATERIALIZED
(
SELECT
bh
.
account
,
bh
.
nai
,
bh
.
balance
,
bh
.
min_balance
,
bh
.
max_balance
,
bh
.
source_op_block
,
bh
.
updated_at
FROM
balance_history_by_month
bh
WHERE
bh
.
account
=
_account_id
AND
bh
.
nai
=
_coin_type
),
balance_records
AS
(
balance_records
AS
(
SELECT
SELECT
ds
.
date
,
ds
.
date
,
...
@@ -293,10 +311,7 @@ RETURN QUERY (
...
@@ -293,10 +311,7 @@ RETURN QUERY (
bh
.
max_balance
,
bh
.
max_balance
,
bh
.
source_op_block
bh
.
source_op_block
FROM
add_row_num_to_series
ds
FROM
add_row_num_to_series
ds
LEFT
JOIN
balance_history_by_month
bh
ON
LEFT
JOIN
get_montly_aggregation
bh
ON
ds
.
date
=
bh
.
updated_at
bh
.
account
=
_account_id
AND
bh
.
nai
=
_coin_type
AND
ds
.
date
=
bh
.
updated_at
),
),
filled_balances
AS
(
filled_balances
AS
(
WITH
RECURSIVE
agg_history
AS
(
WITH
RECURSIVE
agg_history
AS
(
...
@@ -315,11 +330,8 @@ RETURN QUERY (
...
@@ -315,11 +330,8 @@ RETURN QUERY (
bh
.
min_balance
,
bh
.
min_balance
,
bh
.
max_balance
,
bh
.
max_balance
,
bh
.
source_op_block
bh
.
source_op_block
FROM
balance_history_by_month
bh
FROM
get_montly_aggregation
bh
WHERE
WHERE
bh
.
updated_at
<
ds
.
date
bh
.
account
=
_account_id
AND
bh
.
nai
=
_coin_type
AND
bh
.
updated_at
<
ds
.
date
ORDER
BY
bh
.
updated_at
DESC
ORDER
BY
bh
.
updated_at
DESC
LIMIT
1
LIMIT
1
)
prev_balance
ON
TRUE
)
prev_balance
ON
TRUE
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment