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

`COUNT()` never returns `NULL`. A `COUNT(*)` over 0 rows is 0 and a
`COUNT(NULL)` is also 0.
This commit is contained in:
Sean Quah 2021-12-14 12:34:30 +00:00 committed by GitHub
parent 33abbc3278
commit a4dce5b53d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 20 additions and 21 deletions

1
changelog.d/11570.misc Normal file
View File

@ -0,0 +1 @@
Remove redundant `COALESCE()`s around `COUNT()`s in database queries.

View File

@ -1393,7 +1393,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
count = await self.db_pool.simple_select_one_onecol( count = await self.db_pool.simple_select_one_onecol(
table="federation_inbound_events_staging", table="federation_inbound_events_staging",
keyvalues={"room_id": room_id}, keyvalues={"room_id": room_id},
retcol="COALESCE(COUNT(*), 0)", retcol="COUNT(*)",
desc="prune_staged_events_in_room_count", desc="prune_staged_events_in_room_count",
) )
@ -1485,9 +1485,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
"""Update the prometheus metrics for the inbound federation staging area.""" """Update the prometheus metrics for the inbound federation staging area."""
def _get_stats_for_federation_staging_txn(txn): def _get_stats_for_federation_staging_txn(txn):
txn.execute( txn.execute("SELECT count(*) FROM federation_inbound_events_staging")
"SELECT coalesce(count(*), 0) FROM federation_inbound_events_staging"
)
(count,) = txn.fetchone() (count,) = txn.fetchone()
txn.execute( txn.execute(

View File

@ -105,7 +105,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
def _count_messages(txn): def _count_messages(txn):
sql = """ sql = """
SELECT COALESCE(COUNT(*), 0) FROM events SELECT COUNT(*) FROM events
WHERE type = 'm.room.encrypted' WHERE type = 'm.room.encrypted'
AND stream_ordering > ? AND stream_ordering > ?
""" """
@ -122,7 +122,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
like_clause = "%:" + self.hs.hostname like_clause = "%:" + self.hs.hostname
sql = """ sql = """
SELECT COALESCE(COUNT(*), 0) FROM events SELECT COUNT(*) FROM events
WHERE type = 'm.room.encrypted' WHERE type = 'm.room.encrypted'
AND sender LIKE ? AND sender LIKE ?
AND stream_ordering > ? AND stream_ordering > ?
@ -139,7 +139,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
async def count_daily_active_e2ee_rooms(self): async def count_daily_active_e2ee_rooms(self):
def _count(txn): def _count(txn):
sql = """ sql = """
SELECT COALESCE(COUNT(DISTINCT room_id), 0) FROM events SELECT COUNT(DISTINCT room_id) FROM events
WHERE type = 'm.room.encrypted' WHERE type = 'm.room.encrypted'
AND stream_ordering > ? AND stream_ordering > ?
""" """
@ -161,7 +161,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
def _count_messages(txn): def _count_messages(txn):
sql = """ sql = """
SELECT COALESCE(COUNT(*), 0) FROM events SELECT COUNT(*) FROM events
WHERE type = 'm.room.message' WHERE type = 'm.room.message'
AND stream_ordering > ? AND stream_ordering > ?
""" """
@ -178,7 +178,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
like_clause = "%:" + self.hs.hostname like_clause = "%:" + self.hs.hostname
sql = """ sql = """
SELECT COALESCE(COUNT(*), 0) FROM events SELECT COUNT(*) FROM events
WHERE type = 'm.room.message' WHERE type = 'm.room.message'
AND sender LIKE ? AND sender LIKE ?
AND stream_ordering > ? AND stream_ordering > ?
@ -195,7 +195,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
async def count_daily_active_rooms(self): async def count_daily_active_rooms(self):
def _count(txn): def _count(txn):
sql = """ sql = """
SELECT COALESCE(COUNT(DISTINCT room_id), 0) FROM events SELECT COUNT(DISTINCT room_id) FROM events
WHERE type = 'm.room.message' WHERE type = 'm.room.message'
AND stream_ordering > ? AND stream_ordering > ?
""" """
@ -231,7 +231,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
Returns number of users seen in the past time_from period Returns number of users seen in the past time_from period
""" """
sql = """ sql = """
SELECT COALESCE(count(*), 0) FROM ( SELECT COUNT(*) FROM (
SELECT user_id FROM user_ips SELECT user_id FROM user_ips
WHERE last_seen > ? WHERE last_seen > ?
GROUP BY user_id GROUP BY user_id
@ -258,7 +258,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
thirty_days_ago_in_secs = now - thirty_days_in_secs thirty_days_ago_in_secs = now - thirty_days_in_secs
sql = """ sql = """
SELECT platform, COALESCE(count(*), 0) FROM ( SELECT platform, COUNT(*) FROM (
SELECT SELECT
users.name, platform, users.creation_ts * 1000, users.name, platform, users.creation_ts * 1000,
MAX(uip.last_seen) MAX(uip.last_seen)
@ -296,7 +296,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
results[row[0]] = row[1] results[row[0]] = row[1]
sql = """ sql = """
SELECT COALESCE(count(*), 0) FROM ( SELECT COUNT(*) FROM (
SELECT users.name, users.creation_ts * 1000, SELECT users.name, users.creation_ts * 1000,
MAX(uip.last_seen) MAX(uip.last_seen)
FROM users FROM users

View File

@ -59,7 +59,7 @@ class MonthlyActiveUsersWorkerStore(SQLBaseStore):
def _count_users(txn): def _count_users(txn):
# Exclude app service users # Exclude app service users
sql = """ sql = """
SELECT COALESCE(count(*), 0) SELECT COUNT(*)
FROM monthly_active_users FROM monthly_active_users
LEFT JOIN users LEFT JOIN users
ON monthly_active_users.user_id=users.name ON monthly_active_users.user_id=users.name
@ -86,7 +86,7 @@ class MonthlyActiveUsersWorkerStore(SQLBaseStore):
def _count_users_by_service(txn): def _count_users_by_service(txn):
sql = """ sql = """
SELECT COALESCE(appservice_id, 'native'), COALESCE(count(*), 0) SELECT COALESCE(appservice_id, 'native'), COUNT(*)
FROM monthly_active_users FROM monthly_active_users
LEFT JOIN users ON monthly_active_users.user_id=users.name LEFT JOIN users ON monthly_active_users.user_id=users.name
GROUP BY appservice_id; GROUP BY appservice_id;

View File

@ -794,7 +794,7 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
yesterday = int(self._clock.time()) - (60 * 60 * 24) yesterday = int(self._clock.time()) - (60 * 60 * 24)
sql = """ sql = """
SELECT user_type, COALESCE(count(*), 0) AS count FROM ( SELECT user_type, COUNT(*) AS count FROM (
SELECT SELECT
CASE CASE
WHEN is_guest=0 AND appservice_id IS NULL THEN 'native' WHEN is_guest=0 AND appservice_id IS NULL THEN 'native'
@ -819,7 +819,7 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
def _count_users(txn): def _count_users(txn):
txn.execute( txn.execute(
""" """
SELECT COALESCE(COUNT(*), 0) FROM users SELECT COUNT(*) FROM users
WHERE appservice_id IS NULL WHERE appservice_id IS NULL
""" """
) )

View File

@ -390,7 +390,7 @@ class RelationsWorkerStore(SQLBaseStore):
latest_event_id = row[0] latest_event_id = row[0]
sql = """ sql = """
SELECT COALESCE(COUNT(event_id), 0) SELECT COUNT(event_id)
FROM event_relations FROM event_relations
INNER JOIN events USING (event_id) INNER JOIN events USING (event_id)
WHERE WHERE

View File

@ -217,7 +217,7 @@ class RoomWorkerStore(SQLBaseStore):
sql = """ sql = """
SELECT SELECT
COALESCE(COUNT(*), 0) COUNT(*)
FROM ( FROM (
%(published_sql)s %(published_sql)s
) published ) published

View File

@ -538,7 +538,7 @@ class StatsStore(StateDeltasStore):
txn.execute( txn.execute(
""" """
SELECT COALESCE(count(*), 0) FROM current_state_events SELECT COUNT(*) FROM current_state_events
WHERE room_id = ? WHERE room_id = ?
""", """,
(room_id,), (room_id,),

View File

@ -550,7 +550,7 @@ class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase):
self.store.db_pool.simple_select_one_onecol( self.store.db_pool.simple_select_one_onecol(
table="federation_inbound_events_staging", table="federation_inbound_events_staging",
keyvalues={"room_id": room_id}, keyvalues={"room_id": room_id},
retcol="COALESCE(COUNT(*), 0)", retcol="COUNT(*)",
desc="test_prune_inbound_federation_queue", desc="test_prune_inbound_federation_queue",
) )
) )