Skip to content

Commit

Permalink
Optimize queries (#2264)
Browse files Browse the repository at this point in the history
* Optimize queries

Signed-off-by: deniallugo <[email protected]>

* Remove distincts

Signed-off-by: deniallugo <[email protected]>
  • Loading branch information
Deniallugo committed Jul 29, 2022
1 parent ce0b36b commit 680dfa8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 49 deletions.
11 changes: 11 additions & 0 deletions core/lib/storage/migrations/2022-07-05-090548_new-indexes/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP INDEX IF EXISTS ix_tx_filters_tx_hash_address;
CREATE INDEX IF NOT EXISTS tokens_symbol_index on public.tokens (symbol);
DROP INDEX IF EXISTS tokens_symbol_lower_idx;
DROP INDEX IF EXISTS ix_executed_transactions_failed_at;
CREATE INDEX IF NOT EXISTS executed_transactions_tx_hash_idx
ON "executed_transactions" USING hash (tx_hash);
DROP INDEX IF EXISTS ix_prover_job_queue_job_type_last_block;
DROP INDEX IF EXISTS aggregate_operations_action_type_to_block_true_idx;
DROP INDEX IF EXISTS aggregate_operations_action_type_to_block_false_idx;
DROP INDEX IF EXISTS aggregate_operations_action_type_to_block_idx;
DROP INDEX IF EXISTS ix_prover_job_queue_job_status_updated_at ;
11 changes: 11 additions & 0 deletions core/lib/storage/migrations/2022-07-05-090548_new-indexes/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE INDEX IF NOT EXISTS ix_tx_filters_tx_hash_address ON public.tx_filters(tx_hash, address);
DROP INDEX IF EXISTS tokens_symbol_index;
DROP INDEX IF EXISTS tokens_symbol_idx;
CREATE INDEX IF NOT EXISTS tokens_symbol_lower_idx ON public.tokens (lower(symbol));
CREATE INDEX IF NOT EXISTS ix_executed_transactions_failed_at ON executed_transactions ( created_at ) WHERE ( success = false );
DROP INDEX IF EXISTS executed_transactions_tx_hash_idx;
CREATE INDEX IF NOT EXISTS ix_prover_job_queue_job_type_last_block ON prover_job_queue ( job_type, last_block );
CREATE INDEX IF NOT EXISTS ix_prover_job_queue_job_status_updated_at ON prover_job_queue ( job_status, updated_at );
CREATE INDEX IF NOT EXISTS aggregate_operations_action_type_to_block_true_idx ON public.aggregate_operations ( action_type, to_block ) where ( confirmed is distinct from false );
CREATE INDEX IF NOT EXISTS aggregate_operations_action_type_to_block_false_idx ON public.aggregate_operations ( action_type, to_block ) where ( confirmed is distinct from true );
CREATE INDEX IF NOT EXISTS aggregate_operations_action_type_to_block_idx ON public.aggregate_operations ( action_type, to_block );
42 changes: 21 additions & 21 deletions core/lib/storage/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3628,6 +3628,27 @@
]
}
},
"76385fe94faaff36649e7f2e8b59cbfad7b656dd0c1fd823939b2e70a2278685": {
"query": "UPDATE prover_job_queue SET (job_status, updated_at, updated_by) = ($1, now(), 'server_clean_idle')\n WHERE job_status = $2 AND (now() - INTERVAL '120 seconds') >= updated_at RETURNING id",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int4"
}
],
"parameters": {
"Left": [
"Int4",
"Int4"
]
},
"nullable": [
false
]
}
},
"76ac37f173ae27687dbb0eb261a5ab9920fd2185e50a476c00315a874dd6b75c": {
"query": "UPDATE prover_job_queue\n SET (updated_at, job_status, updated_by) = (now(), $1, 'server_finish_job')\n WHERE id = $2 AND job_type = $3",
"describe": {
Expand Down Expand Up @@ -8893,27 +8914,6 @@
]
}
},
"faefd7d582000800f48eddcc1e933a876f5b0cd556f0dc45c6241eb72da97894": {
"query": "UPDATE prover_job_queue SET (job_status, updated_at, updated_by) = ($1, now(), 'server_clean_idle')\n WHERE job_status = $2 and (now() - updated_at) >= interval '120 seconds' RETURNING id",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int4"
}
],
"parameters": {
"Left": [
"Int4",
"Int4"
]
},
"nullable": [
false
]
}
},
"fd16aadbd04d4a48332d59c77290a588f1a33922418b55a08c656a44ff75b8e8": {
"query": "SELECT * FROM account_balance_updates WHERE block_number = $1",
"describe": {
Expand Down
52 changes: 25 additions & 27 deletions core/lib/storage/src/chain/operations_ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,29 +1188,28 @@ impl<'a, 'c> OperationsExtSchema<'a, 'c> {
};

let token_query = if token.is_some() {
"AND token = $2"
"AND a.token = $2"
} else {
""
};

let query = format!(
r#"
SELECT DISTINCT
executed_priority_operations.sequence_number,
executed_priority_operations.tx_hash,
operation as op,
block_number,
created_at,
SELECT
b.sequence_number,
b.tx_hash,
b.operation as op,
b.block_number,
b.created_at,
true as success,
Null as fail_reason,
eth_hash,
priority_op_serialid,
block_index,
b.eth_hash,
b.priority_op_serialid,
b.block_index,
Null::bigint as batch_id
FROM tx_filters
INNER JOIN executed_priority_operations
ON tx_filters.tx_hash = executed_priority_operations.tx_hash
WHERE address = $1 {} {}
FROM executed_priority_operations as b
WHERE EXISTS ( SELECT 1 FROM tx_filters as a WHERE a.tx_hash = b.tx_hash AND a.address = $1 {} )
{}
"#,
token_query, query_direction
);
Expand Down Expand Up @@ -1245,29 +1244,28 @@ impl<'a, 'c> OperationsExtSchema<'a, 'c> {
};

let token_query = if token.is_some() {
"AND token = $2"
"AND a.token = $2"
} else {
""
};

let query = format!(
r#"
SELECT DISTINCT
SELECT
txs.sequence_number,
txs.tx_hash,
tx as op,
block_number,
created_at,
success,
fail_reason,
txs.tx as op,
txs.block_number,
txs.created_at,
txs.success,
txs.fail_reason,
Null::bytea as eth_hash,
Null::bigint as priority_op_serialid,
block_index,
batch_id
FROM tx_filters
INNER JOIN executed_transactions txs
ON tx_filters.tx_hash = txs.tx_hash
WHERE address = $1 {} {}
txs.block_index,
txs.batch_id
FROM executed_transactions as txs
WHERE EXISTS ( SELECT 1 FROM tx_filters as a WHERE a.tx_hash = txs.tx_hash AND a.address = $1 {} )
{}
"#,
token_query, query_direction
);
Expand Down
2 changes: 1 addition & 1 deletion core/lib/storage/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a, 'c> ProverSchema<'a, 'c> {
let start = Instant::now();
let result = sqlx::query!(
"UPDATE prover_job_queue SET (job_status, updated_at, updated_by) = ($1, now(), 'server_clean_idle')
WHERE job_status = $2 and (now() - updated_at) >= interval '120 seconds' RETURNING id",
WHERE job_status = $2 AND (now() - INTERVAL '120 seconds') >= updated_at RETURNING id",
ProverJobStatus::Idle.to_number(),
ProverJobStatus::InProgress.to_number(),
)
Expand Down

0 comments on commit 680dfa8

Please sign in to comment.