Skip to content

Commit a84609c

Browse files
committed
query cost optimalization
1 parent 6067f56 commit a84609c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

hedera-balances/native.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ def get_global_start(client, project, dataset, max_batch_size):
1414

1515
logger.info(f"Looking for latest ingested balance")
1616
# Perform a query.
17+
latest_balance_partition_query = """
18+
SELECT PARSE_TIMESTAMP("%Y%m%d", MAX(partition_id)) AS latest_partition
19+
FROM `{project}.{dataset}.INFORMATION_SCHEMA.PARTITIONS`
20+
WHERE table_name = "native_token_balance" AND partition_id != "__NULL__"
21+
"""
22+
query_job = client.query(latest_balance_partition_query) # API request
23+
rows = query_job.result() # Waits for query to finish
24+
25+
row = next(rows)
26+
latest_partition = row.latest_partition
27+
1728
latest_balance_query = f"""
18-
SELECT MAX(created) AS latest FROM `{project}.{dataset}.native_token_balance` WHERE created >= "2019-01-01"
29+
SELECT MAX(created) AS latest FROM `{project}.{dataset}.native_token_balance` WHERE created >= "{latest_partition}"
1930
"""
2031

2132
query_job = client.query(latest_balance_query) # API request

scripts/balances-loop.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -e
44
set -x
55

6-
START_DATE="2025-08-20"
6+
START_DATE="2025-09-09"
77
INCREMENT="+1 day"
88

99
PROJECT_ID="hedera-etl-bq"
@@ -15,11 +15,15 @@ export GOOGLE_CLOUD_PROJECT="$PROJECT_ID"
1515

1616
## MAIN PROGRAM
1717
while true; do
18+
if [ -e stop_balances_loop ]; then
19+
echo "Poison pill detected. Good bye"
20+
exit 0
21+
fi
1822
LATEST_TABLE="hedera_technical.balance_latest_$(date -d "${START_DATE} -1 day" +"%Y%m%d")"
1923
echo "Running balances for date ${START_DATE}"
20-
WINDOW_IN_MINUTES=5
21-
POLLING_WAIT=1
22-
python3 -m hedera-balances -p hedera-etl-bq -d hedera_restricted -x hedera_technical -w $POLLING_WAIT -m $WINDOW_IN_MINUTES -t native -e START_DATE -l $LATEST_TABLE
24+
WINDOW_IN_MINUTES=10
25+
POLLING_WAIT=300
26+
python3 -m hedera-balances -p hedera-etl-bq -d hedera_restricted -x hedera_technical -w $POLLING_WAIT -m $WINDOW_IN_MINUTES -t native -e $START_DATE -l $LATEST_TABLE
2327
START_DATE=$(date -d "${START_DATE} +1 day" --iso)
2428
sleep 10s
2529
done

0 commit comments

Comments
 (0)