Remove unused `consider_intermediate_updates` flag..optimizing query
[rapid-gossip-sync-server] / src / lookup.rs
index 8018e5ca19d62a5b564b9f40a777f2aad18100b3..0534c1c8a7e82922bc240eab0be214f06d87eec8 100644 (file)
@@ -201,7 +201,7 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
        }
 }
 
-pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Client, last_sync_timestamp: u32, consider_intermediate_updates: bool) {
+pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Client, last_sync_timestamp: u32) {
        let start = Instant::now();
        let last_sync_timestamp_object = SystemTime::UNIX_EPOCH.add(Duration::from_secs(last_sync_timestamp as u64));
 
@@ -209,14 +209,17 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli
        // there was an update in either direction that happened after the last sync (to avoid
        // collecting too many reference updates)
        let reference_rows = client.query("
-               SELECT DISTINCT ON (short_channel_id, direction) id, direction, blob_signed
-               FROM channel_updates
-               WHERE seen < $1 AND short_channel_id IN (
+               SELECT id, direction, blob_signed FROM channel_updates
+               WHERE id IN (
+                       SELECT DISTINCT ON (short_channel_id, direction) id
+                       FROM channel_updates
+                       WHERE seen < $1
+                       ORDER BY short_channel_id ASC, direction ASC, seen DESC
+               ) AND short_channel_id IN (
                        SELECT DISTINCT ON (short_channel_id) short_channel_id
                        FROM channel_updates
                        WHERE seen >= $1
                )
-               ORDER BY short_channel_id ASC, direction ASC, seen DESC
                ", &[&last_sync_timestamp_object]).await.unwrap();
 
        println!("Fetched reference rows ({}): {:?}", reference_rows.len(), start.elapsed());
@@ -250,18 +253,11 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli
        // (to calculate the set of mutated fields for snapshotting, where intermediate updates may
        // have been omitted)
 
-       let mut intermediate_update_prefix = "";
-       if !consider_intermediate_updates {
-               intermediate_update_prefix = "DISTINCT ON (short_channel_id, direction)";
-       }
-
-       let query_string = format!("
-               SELECT {} id, direction, blob_signed, seen
+       let intermediate_updates = client.query("
+               SELECT id, direction, blob_signed, seen
                FROM channel_updates
                WHERE seen >= $1
-               ORDER BY short_channel_id ASC, direction ASC, seen DESC
-               ", intermediate_update_prefix);
-       let intermediate_updates = client.query(&query_string, &[&last_sync_timestamp_object]).await.unwrap();
+               ", &[&last_sync_timestamp_object]).await.unwrap();
        println!("Fetched intermediate rows ({}): {:?}", intermediate_updates.len(), start.elapsed());
 
        let mut previous_scid = u64::MAX;