Skip to content

Commit

Permalink
chore: Use system.parts table to get count of rows. much cheaper (#18730
Browse files Browse the repository at this point in the history
)

* chore: Use system.parts table to get count of rows. much cheaper

* clean up another query that was doing full table scans

* is_active -> active

* added a projection to make things faster

* look at rows for sharded tables
  • Loading branch information
fuziontech authored Nov 18, 2023
1 parent 00b2630 commit dd5be88
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions posthog/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,10 @@ def pg_row_count():


CLICKHOUSE_TABLES = [
"events",
"sharded_events",
"person",
"person_distinct_id2",
"session_replay_events",
"sharded_session_replay_events",
"log_entries",
]
if not is_cloud():
Expand All @@ -540,9 +540,8 @@ def clickhouse_lag():
)
for table in CLICKHOUSE_TABLES:
try:
QUERY = (
"""select max(_timestamp) observed_ts, now() now_ts, now() - max(_timestamp) as lag from {table};"""
)
QUERY = """SELECT max(_timestamp) observed_ts, now() now_ts, now() - max(_timestamp) as lag
FROM {table}"""
query = QUERY.format(table=table)
lag = sync_execute(query)[0][2]
statsd.gauge(
Expand Down Expand Up @@ -688,9 +687,8 @@ def clickhouse_row_count():
)
for table in CLICKHOUSE_TABLES:
try:
QUERY = (
"""select count(1) freq from {table} where _timestamp >= toStartOfDay(date_sub(DAY, 2, now()));"""
)
QUERY = """SELECT sum(rows) rows from system.parts
WHERE table = '{table}' and active;"""
query = QUERY.format(table=table)
rows = sync_execute(query)[0][0]
row_count_gauge.labels(table_name=table).set(rows)
Expand Down Expand Up @@ -745,10 +743,11 @@ def clickhouse_part_count():
from posthog.client import sync_execute

QUERY = """
select table, count(1) freq
from system.parts
group by table
order by freq desc;
SELECT table, count(1) freq
FROM system.parts
WHERE active
GROUP BY table
ORDER BY freq DESC;
"""
rows = sync_execute(QUERY)

Expand Down

0 comments on commit dd5be88

Please sign in to comment.