Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Remove redundant COALESCE()s around COUNT()s in database queries #11570

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.d/11570.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove redundant `COALESCE()`s around `COUNT()`s in database queries.
6 changes: 2 additions & 4 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ async def prune_staged_events_in_room(
count = await self.db_pool.simple_select_one_onecol(
table="federation_inbound_events_staging",
keyvalues={"room_id": room_id},
retcol="COALESCE(COUNT(*), 0)",
retcol="COUNT(*)",
desc="prune_staged_events_in_room_count",
)

Expand Down Expand Up @@ -1485,9 +1485,7 @@ async def _get_stats_for_federation_staging(self):
"""Update the prometheus metrics for the inbound federation staging area."""

def _get_stats_for_federation_staging_txn(txn):
txn.execute(
"SELECT coalesce(count(*), 0) FROM federation_inbound_events_staging"
)
txn.execute("SELECT count(*) FROM federation_inbound_events_staging")
(count,) = txn.fetchone()

txn.execute(
Expand Down
18 changes: 9 additions & 9 deletions synapse/storage/databases/main/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def count_daily_e2ee_messages(self):

def _count_messages(txn):
sql = """
SELECT COALESCE(COUNT(*), 0) FROM events
SELECT COUNT(*) FROM events
WHERE type = 'm.room.encrypted'
AND stream_ordering > ?
"""
Expand All @@ -122,7 +122,7 @@ def _count_messages(txn):
like_clause = "%:" + self.hs.hostname

sql = """
SELECT COALESCE(COUNT(*), 0) FROM events
SELECT COUNT(*) FROM events
WHERE type = 'm.room.encrypted'
AND sender LIKE ?
AND stream_ordering > ?
Expand All @@ -139,7 +139,7 @@ def _count_messages(txn):
async def count_daily_active_e2ee_rooms(self):
def _count(txn):
sql = """
SELECT COALESCE(COUNT(DISTINCT room_id), 0) FROM events
SELECT COUNT(DISTINCT room_id) FROM events
WHERE type = 'm.room.encrypted'
AND stream_ordering > ?
"""
Expand All @@ -161,7 +161,7 @@ async def count_daily_messages(self):

def _count_messages(txn):
sql = """
SELECT COALESCE(COUNT(*), 0) FROM events
SELECT COUNT(*) FROM events
WHERE type = 'm.room.message'
AND stream_ordering > ?
"""
Expand All @@ -178,7 +178,7 @@ def _count_messages(txn):
like_clause = "%:" + self.hs.hostname

sql = """
SELECT COALESCE(COUNT(*), 0) FROM events
SELECT COUNT(*) FROM events
WHERE type = 'm.room.message'
AND sender LIKE ?
AND stream_ordering > ?
Expand All @@ -195,7 +195,7 @@ def _count_messages(txn):
async def count_daily_active_rooms(self):
def _count(txn):
sql = """
SELECT COALESCE(COUNT(DISTINCT room_id), 0) FROM events
SELECT COUNT(DISTINCT room_id) FROM events
WHERE type = 'm.room.message'
AND stream_ordering > ?
"""
Expand Down Expand Up @@ -231,7 +231,7 @@ def _count_users(self, txn, time_from):
Returns number of users seen in the past time_from period
"""
sql = """
SELECT COALESCE(count(*), 0) FROM (
SELECT COUNT(*) FROM (
SELECT user_id FROM user_ips
WHERE last_seen > ?
GROUP BY user_id
Expand All @@ -258,7 +258,7 @@ def _count_r30_users(txn):
thirty_days_ago_in_secs = now - thirty_days_in_secs

sql = """
SELECT platform, COALESCE(count(*), 0) FROM (
SELECT platform, COUNT(*) FROM (
SELECT
users.name, platform, users.creation_ts * 1000,
MAX(uip.last_seen)
Expand Down Expand Up @@ -296,7 +296,7 @@ def _count_r30_users(txn):
results[row[0]] = row[1]

sql = """
SELECT COALESCE(count(*), 0) FROM (
SELECT COUNT(*) FROM (
SELECT users.name, users.creation_ts * 1000,
MAX(uip.last_seen)
FROM users
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/databases/main/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def get_monthly_active_count(self) -> int:
def _count_users(txn):
# Exclude app service users
sql = """
SELECT COALESCE(count(*), 0)
SELECT COUNT(*)
FROM monthly_active_users
LEFT JOIN users
ON monthly_active_users.user_id=users.name
Expand All @@ -86,7 +86,7 @@ async def get_monthly_active_count_by_service(self) -> Dict[str, int]:

def _count_users_by_service(txn):
sql = """
SELECT COALESCE(appservice_id, 'native'), COALESCE(count(*), 0)
SELECT COALESCE(appservice_id, 'native'), COUNT(*)
FROM monthly_active_users
LEFT JOIN users ON monthly_active_users.user_id=users.name
GROUP BY appservice_id;
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def _count_daily_user_type(txn):
yesterday = int(self._clock.time()) - (60 * 60 * 24)

sql = """
SELECT user_type, COALESCE(count(*), 0) AS count FROM (
SELECT user_type, COUNT(*) AS count FROM (
SELECT
CASE
WHEN is_guest=0 AND appservice_id IS NULL THEN 'native'
Expand All @@ -819,7 +819,7 @@ async def count_nonbridged_users(self):
def _count_users(txn):
txn.execute(
"""
SELECT COALESCE(COUNT(*), 0) FROM users
SELECT COUNT(*) FROM users
WHERE appservice_id IS NULL
"""
)
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def _get_thread_summary_txn(
latest_event_id = row[0]

sql = """
SELECT COALESCE(COUNT(event_id), 0)
SELECT COUNT(event_id)
FROM event_relations
INNER JOIN events USING (event_id)
WHERE
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _count_public_rooms_txn(txn):

sql = """
SELECT
COALESCE(COUNT(*), 0)
COUNT(*)
FROM (
%(published_sql)s
) published
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def _fetch_current_state_stats(txn):

txn.execute(
"""
SELECT COALESCE(count(*), 0) FROM current_state_events
SELECT COUNT(*) FROM current_state_events
WHERE room_id = ?
""",
(room_id,),
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def test_prune_inbound_federation_queue(self):
self.store.db_pool.simple_select_one_onecol(
table="federation_inbound_events_staging",
keyvalues={"room_id": room_id},
retcol="COALESCE(COUNT(*), 0)",
retcol="COUNT(*)",
desc="test_prune_inbound_federation_queue",
)
)
Expand Down