Skip to content

Commit 3fe4890

Browse files
authored
Merge pull request #2574 from IntersectMBO/chore/optimize-connection-pool-and-drep-list-loading
chore: optimize the db connection and drep list sql
2 parents 9e2f035 + 3d80160 commit 3fe4890

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

govtool/backend/app/Main.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Data.Monoid (mempty)
2626
import Data.OpenApi (OpenApi, Server (Server), _openApiServers,
2727
_serverDescription, _serverUrl, _serverVariables,
2828
servers)
29-
import Data.Pool (createPool)
29+
import Data.Pool (createPool, Pool)
3030
import Data.Proxy
3131
import Data.String (fromString)
3232
import Data.String.Conversions (cs)
@@ -36,7 +36,7 @@ import qualified Data.Text.IO as Text
3636
import qualified Data.Text.Lazy as LazyText
3737
import qualified Data.Text.Lazy.Encoding as LazyText
3838

39-
import Database.PostgreSQL.Simple (close, connectPostgreSQL)
39+
import Database.PostgreSQL.Simple (close, connectPostgreSQL, Connection)
4040

4141
import Network.Wai
4242
import Network.Wai.Handler.Warp
@@ -65,6 +65,15 @@ import VVA.Types (AppEnv (..),
6565
AppError (CriticalError, InternalError, NotFoundError, ValidationError),
6666
CacheEnv (..))
6767

68+
-- Function to create a connection pool with optimized settings
69+
createOptimizedConnectionPool :: BS.ByteString -> IO (Pool Connection)
70+
createOptimizedConnectionPool connectionString = createPool
71+
(connectPostgreSQL connectionString) -- Connection creation function
72+
close -- Connection destruction function
73+
1 -- Number of stripes (sub-pools)
74+
60 -- Idle timeout (seconds)
75+
50 -- Maximum number of connections per stripe
76+
6877
proxyAPI :: Proxy (VVAApi :<|> SwaggerAPI)
6978
proxyAPI = Proxy
7079

@@ -125,7 +134,10 @@ startApp vvaConfig sentryService = do
125134
, dRepListCache
126135
, networkMetricsCache
127136
}
128-
connectionPool <- createPool (connectPostgreSQL (encodeUtf8 (dbSyncConnectionString $ getter vvaConfig))) close 1 10 60
137+
138+
let connectionString = encodeUtf8 (dbSyncConnectionString $ getter vvaConfig)
139+
connectionPool <- createOptimizedConnectionPool connectionString
140+
129141
let appEnv = AppEnv {vvaConfig=vvaConfig, vvaCache=cacheEnv, vvaConnectionPool=connectionPool }
130142
server' <- mkVVAServer appEnv
131143
runSettings settings server'

govtool/backend/sql/list-dreps.sql

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
WITH DRepDistr AS (
22
SELECT
3-
*,
3+
drep_distr.*,
44
ROW_NUMBER() OVER (PARTITION BY drep_hash.id ORDER BY drep_distr.epoch_no DESC) AS rn
55
FROM
66
drep_distr
77
JOIN drep_hash ON drep_hash.id = drep_distr.hash_id
88
),
99
DRepActivity AS (
1010
SELECT
11-
drep_activity AS drep_activity,
12-
epoch_no AS epoch_no
11+
drep_activity,
12+
epoch_no
1313
FROM
1414
epoch_param
1515
WHERE
@@ -26,7 +26,7 @@ SELECT
2626
encode(va.data_hash, 'hex'),
2727
dr_deposit.deposit,
2828
DRepDistr.amount,
29-
(DRepActivity.epoch_no - Max(coalesce(block.epoch_no, block_first_register.epoch_no))) <= DRepActivity.drep_activity AS active,
29+
(DRepActivity.epoch_no - COALESCE(block.epoch_no, block_first_register.epoch_no)) <= DRepActivity.drep_activity AS active,
3030
encode(dr_voting_anchor.tx_hash, 'hex') AS tx_hash,
3131
newestRegister.time AS last_register_time,
3232
COALESCE(latestDeposit.deposit, 0),
@@ -50,17 +50,17 @@ FROM
5050
FROM
5151
drep_registration dr
5252
WHERE
53-
dr.deposit IS NOT NULL) AS dr_deposit ON dr_deposit.drep_hash_id = dh.id
54-
AND dr_deposit.rn = 1
55-
JOIN (
53+
dr.deposit IS NOT NULL
54+
) AS dr_deposit ON dr_deposit.drep_hash_id = dh.id AND dr_deposit.rn = 1
55+
JOIN (
5656
SELECT
5757
dr.id,
5858
dr.drep_hash_id,
5959
dr.deposit,
6060
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
6161
FROM
62-
drep_registration dr) AS latestDeposit ON latestDeposit.drep_hash_id = dh.id
63-
AND latestDeposit.rn = 1
62+
drep_registration dr
63+
) AS latestDeposit ON latestDeposit.drep_hash_id = dh.id AND latestDeposit.rn = 1
6464
LEFT JOIN (
6565
SELECT
6666
dr.id,
@@ -70,9 +70,11 @@ FROM
7070
tx.hash AS tx_hash
7171
FROM
7272
drep_registration dr
73-
JOIN tx ON tx.id = dr.tx_id) AS dr_voting_anchor ON dr_voting_anchor.drep_hash_id = dh.id
74-
AND dr_voting_anchor.rn = 1
75-
LEFT JOIN (
73+
JOIN tx ON tx.id = dr.tx_id
74+
WHERE
75+
dr.deposit IS NOT NULL AND dr.deposit >= 0
76+
) AS dr_voting_anchor ON dr_voting_anchor.drep_hash_id = dh.id AND dr_voting_anchor.rn = 1
77+
LEFT JOIN (
7678
SELECT
7779
dr.id,
7880
dr.drep_hash_id,
@@ -82,36 +84,42 @@ FROM
8284
FROM
8385
drep_registration dr
8486
JOIN tx ON tx.id = dr.tx_id
85-
WHERE dr.deposit is not null
86-
AND dr.deposit >= 0) AS dr_non_deregister_voting_anchor ON dr_non_deregister_voting_anchor.drep_hash_id = dh.id
87-
AND dr_non_deregister_voting_anchor.rn = 1
87+
) AS dr_non_deregister_voting_anchor ON dr_non_deregister_voting_anchor.drep_hash_id = dh.id AND dr_non_deregister_voting_anchor.rn = 1
8888
LEFT JOIN (
8989
SELECT
9090
dr.id,
9191
dr.drep_hash_id,
9292
dr.voting_anchor_id,
9393
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
9494
FROM
95-
drep_registration dr) AS second_to_newest_drep_registration ON second_to_newest_drep_registration.drep_hash_id = dh.id
96-
AND second_to_newest_drep_registration.rn = 2
97-
LEFT JOIN DRepDistr ON DRepDistr.hash_id = dh.id
98-
AND DRepDistr.rn = 1
95+
drep_registration dr
96+
) AS second_to_newest_drep_registration ON second_to_newest_drep_registration.drep_hash_id = dh.id AND second_to_newest_drep_registration.rn = 2
97+
LEFT JOIN DRepDistr ON DRepDistr.hash_id = dh.id AND DRepDistr.rn = 1
9998
LEFT JOIN voting_anchor va ON va.id = dr_voting_anchor.voting_anchor_id
10099
LEFT JOIN voting_anchor non_deregister_voting_anchor ON non_deregister_voting_anchor.id = dr_non_deregister_voting_anchor.voting_anchor_id
101100
LEFT JOIN (
102-
SELECT fetch_error as message, voting_anchor_id
103-
FROM off_chain_vote_fetch_error
104-
WHERE fetch_time = (
105-
SELECT max(fetch_time)
106-
FROM off_chain_vote_fetch_error)
107-
GROUP BY fetch_error, voting_anchor_id
101+
SELECT
102+
fetch_error as message,
103+
voting_anchor_id
104+
FROM
105+
off_chain_vote_fetch_error
106+
WHERE
107+
fetch_time = (
108+
SELECT
109+
max(fetch_time)
110+
FROM
111+
off_chain_vote_fetch_error
112+
)
113+
GROUP BY
114+
fetch_error,
115+
voting_anchor_id
108116
) AS fetch_error ON fetch_error.voting_anchor_id = va.id
109117
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = va.id
110-
LEFT JOIN off_chain_vote_drep_data on off_chain_vote_drep_data.off_chain_vote_data_id = off_chain_vote_data.id
118+
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = off_chain_vote_data.id
111119
CROSS JOIN DRepActivity
112-
LEFT JOIN voting_procedure AS voting_procedure ON voting_procedure.drep_voter = dh.id
113-
LEFT JOIN tx AS tx ON tx.id = voting_procedure.tx_id
114-
LEFT JOIN block AS block ON block.id = tx.block_id
120+
LEFT JOIN voting_procedure ON voting_procedure.drep_voter = dh.id
121+
LEFT JOIN tx ON tx.id = voting_procedure.tx_id
122+
LEFT JOIN block ON block.id = tx.block_id
115123
LEFT JOIN (
116124
SELECT
117125
block.time,
@@ -122,16 +130,16 @@ FROM
122130
JOIN tx ON tx.id = dr.tx_id
123131
JOIN block ON block.id = tx.block_id
124132
WHERE
125-
NOT (dr.deposit < 0)) AS newestRegister ON newestRegister.drep_hash_id = dh.id
126-
AND newestRegister.rn = 1
133+
NOT (dr.deposit < 0)
134+
) AS newestRegister ON newestRegister.drep_hash_id = dh.id AND newestRegister.rn = 1
127135
LEFT JOIN (
128136
SELECT
129137
dr.tx_id,
130138
dr.drep_hash_id,
131139
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id ASC) AS rn
132140
FROM
133-
drep_registration dr) AS dr_first_register ON dr_first_register.drep_hash_id = dh.id
134-
AND dr_first_register.rn = 1
141+
drep_registration dr
142+
) AS dr_first_register ON dr_first_register.drep_hash_id = dh.id AND dr_first_register.rn = 1
135143
LEFT JOIN tx AS tx_first_register ON tx_first_register.id = dr_first_register.tx_id
136144
LEFT JOIN block AS block_first_register ON block_first_register.id = tx_first_register.block_id
137145
WHERE
@@ -142,6 +150,8 @@ WHERE
142150
off_chain_vote_drep_data.given_name ILIKE ?
143151
)
144152
GROUP BY
153+
block_first_register.epoch_no,
154+
block.epoch_no,
145155
dh.raw,
146156
second_to_newest_drep_registration.voting_anchor_id,
147157
dh.view,
@@ -163,4 +173,4 @@ GROUP BY
163173
off_chain_vote_drep_data.motivations,
164174
off_chain_vote_drep_data.qualifications,
165175
off_chain_vote_drep_data.image_url,
166-
off_chain_vote_drep_data.image_hash
176+
off_chain_vote_drep_data.image_hash;

0 commit comments

Comments
 (0)