Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading