From 0e7e3d85ffb8e49a64ee5854f0749ed2e8c919c1 Mon Sep 17 00:00:00 2001 From: jurriaan Date: Fri, 7 Jun 2024 17:25:14 +0200 Subject: [PATCH] fix(hubble): improve max height performance --- ...393065ea4d48c45767e9efbf17b09b8cf1493.json | 22 ------------------- ...6f3c912498e8b77c17c46063153e63e9f7b6.json} | 6 ++--- ...9c0757b54cd2e8f467827e08c951553da20d6.json | 22 +++++++++++++++++++ hubble/src/eth.rs | 11 +++------- hubble/src/tm.rs | 9 +++++++- 5 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 hubble/.sqlx/query-5bb8cc7883163790bbbd234b5f4393065ea4d48c45767e9efbf17b09b8cf1493.json rename hubble/.sqlx/{query-bf9f287afde0338ee6a09c1b9268e6c4d38292c17098eca23070dec007f066fb.json => query-816fdbdb9ddad93052392f3670176f3c912498e8b77c17c46063153e63e9f7b6.json} (57%) create mode 100644 hubble/.sqlx/query-aac17724ad7b68938c6820c8b479c0757b54cd2e8f467827e08c951553da20d6.json diff --git a/hubble/.sqlx/query-5bb8cc7883163790bbbd234b5f4393065ea4d48c45767e9efbf17b09b8cf1493.json b/hubble/.sqlx/query-5bb8cc7883163790bbbd234b5f4393065ea4d48c45767e9efbf17b09b8cf1493.json deleted file mode 100644 index a7ae5811e4..0000000000 --- a/hubble/.sqlx/query-5bb8cc7883163790bbbd234b5f4393065ea4d48c45767e9efbf17b09b8cf1493.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT height \n FROM \"v0\".\"logs\" \n WHERE chain_id = $1 \n ORDER BY height DESC \n NULLS LAST \n LIMIT 1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "height", - "type_info": "Int4" - } - ], - "parameters": { - "Left": [ - "Int4" - ] - }, - "nullable": [ - false - ] - }, - "hash": "5bb8cc7883163790bbbd234b5f4393065ea4d48c45767e9efbf17b09b8cf1493" -} diff --git a/hubble/.sqlx/query-bf9f287afde0338ee6a09c1b9268e6c4d38292c17098eca23070dec007f066fb.json b/hubble/.sqlx/query-816fdbdb9ddad93052392f3670176f3c912498e8b77c17c46063153e63e9f7b6.json similarity index 57% rename from hubble/.sqlx/query-bf9f287afde0338ee6a09c1b9268e6c4d38292c17098eca23070dec007f066fb.json rename to hubble/.sqlx/query-816fdbdb9ddad93052392f3670176f3c912498e8b77c17c46063153e63e9f7b6.json index 251e1150b4..18e8acd657 100644 --- a/hubble/.sqlx/query-bf9f287afde0338ee6a09c1b9268e6c4d38292c17098eca23070dec007f066fb.json +++ b/hubble/.sqlx/query-816fdbdb9ddad93052392f3670176f3c912498e8b77c17c46063153e63e9f7b6.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT height FROM \"v0\".blocks WHERE chain_id = $1 ORDER BY time DESC NULLS LAST LIMIT 1", + "query": "SELECT MAX(height) height FROM \"v0\".blocks WHERE chain_id = $1", "describe": { "columns": [ { @@ -15,8 +15,8 @@ ] }, "nullable": [ - false + null ] }, - "hash": "bf9f287afde0338ee6a09c1b9268e6c4d38292c17098eca23070dec007f066fb" + "hash": "816fdbdb9ddad93052392f3670176f3c912498e8b77c17c46063153e63e9f7b6" } diff --git a/hubble/.sqlx/query-aac17724ad7b68938c6820c8b479c0757b54cd2e8f467827e08c951553da20d6.json b/hubble/.sqlx/query-aac17724ad7b68938c6820c8b479c0757b54cd2e8f467827e08c951553da20d6.json new file mode 100644 index 0000000000..06b23bf81b --- /dev/null +++ b/hubble/.sqlx/query-aac17724ad7b68938c6820c8b479c0757b54cd2e8f467827e08c951553da20d6.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT MAX(height) height FROM \"v0\".\"logs\" WHERE chain_id = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "height", + "type_info": "Int4" + } + ], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [ + null + ] + }, + "hash": "aac17724ad7b68938c6820c8b479c0757b54cd2e8f467827e08c951553da20d6" +} diff --git a/hubble/src/eth.rs b/hubble/src/eth.rs index fa13ad4c7b..7bc3b87493 100644 --- a/hubble/src/eth.rs +++ b/hubble/src/eth.rs @@ -74,18 +74,13 @@ impl Config { .get_inner_logged(); let current = sqlx::query!( - r#"SELECT height - FROM "v0"."logs" - WHERE chain_id = $1 - ORDER BY height DESC - NULLS LAST - LIMIT 1"#, + r#"SELECT MAX(height) height FROM "v0"."logs" WHERE chain_id = $1"#, chain_id.db ) .fetch_optional(&pool) .await? .map(|block| { - if block.height == 0 { + if block.height.unwrap_or(0) == 0 { info!( self.start_height, "no block found, starting at configured start height, or 0 if not defined" @@ -96,7 +91,7 @@ impl Config { self.start_height, block.height, "block found, starting max(start_height, block_height + 1)" ); - (block.height + 1).max(self.start_height.unwrap_or_default()) + (block.height.unwrap_or(0) + 1).max(self.start_height.unwrap_or_default()) } }) .unwrap_or(self.start_height.unwrap_or_default()) as u64; diff --git a/hubble/src/tm.rs b/hubble/src/tm.rs index c83ebf0007..ad6586481e 100644 --- a/hubble/src/tm.rs +++ b/hubble/src/tm.rs @@ -156,7 +156,14 @@ where let chain_id = postgres::fetch_or_insert_chain_id(pool, chain_id) .await? .get_inner_logged(); - let height = sqlx::query!("SELECT height FROM \"v0\".blocks WHERE chain_id = $1 ORDER BY time DESC NULLS LAST LIMIT 1", chain_id.db).fetch_optional(pool).await?.map(|block| block.height + 1).map(|h| Height::from(h as u32)); + let height = sqlx::query!( + r#"SELECT MAX(height) height FROM "v0".blocks WHERE chain_id = $1"#, + chain_id.db + ) + .fetch_optional(pool) + .await? + .map(|block| block.height.unwrap_or(0) + 1) + .map(|h| Height::from(h as u32)); Ok((chain_id, height)) }