mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-12-11 05:44:19 -05:00
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:
parent
33abbc3278
commit
a4dce5b53d
1
changelog.d/11570.misc
Normal file
1
changelog.d/11570.misc
Normal file
@ -0,0 +1 @@
|
||||
Remove redundant `COALESCE()`s around `COUNT()`s in database queries.
|
@ -1393,7 +1393,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
|
||||
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",
|
||||
)
|
||||
|
||||
@ -1485,9 +1485,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
|
||||
"""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(
|
||||
|
@ -105,7 +105,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
|
||||
def _count_messages(txn):
|
||||
sql = """
|
||||
SELECT COALESCE(COUNT(*), 0) FROM events
|
||||
SELECT COUNT(*) FROM events
|
||||
WHERE type = 'm.room.encrypted'
|
||||
AND stream_ordering > ?
|
||||
"""
|
||||
@ -122,7 +122,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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 > ?
|
||||
@ -139,7 +139,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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 > ?
|
||||
"""
|
||||
@ -161,7 +161,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
|
||||
def _count_messages(txn):
|
||||
sql = """
|
||||
SELECT COALESCE(COUNT(*), 0) FROM events
|
||||
SELECT COUNT(*) FROM events
|
||||
WHERE type = 'm.room.message'
|
||||
AND stream_ordering > ?
|
||||
"""
|
||||
@ -178,7 +178,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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 > ?
|
||||
@ -195,7 +195,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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 > ?
|
||||
"""
|
||||
@ -231,7 +231,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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
|
||||
@ -258,7 +258,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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)
|
||||
@ -296,7 +296,7 @@ class ServerMetricsStore(EventPushActionsWorkerStore, SQLBaseStore):
|
||||
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
|
||||
|
@ -59,7 +59,7 @@ class MonthlyActiveUsersWorkerStore(SQLBaseStore):
|
||||
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
|
||||
@ -86,7 +86,7 @@ class MonthlyActiveUsersWorkerStore(SQLBaseStore):
|
||||
|
||||
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;
|
||||
|
@ -794,7 +794,7 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
|
||||
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'
|
||||
@ -819,7 +819,7 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore):
|
||||
def _count_users(txn):
|
||||
txn.execute(
|
||||
"""
|
||||
SELECT COALESCE(COUNT(*), 0) FROM users
|
||||
SELECT COUNT(*) FROM users
|
||||
WHERE appservice_id IS NULL
|
||||
"""
|
||||
)
|
||||
|
@ -390,7 +390,7 @@ class RelationsWorkerStore(SQLBaseStore):
|
||||
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
|
||||
|
@ -217,7 +217,7 @@ class RoomWorkerStore(SQLBaseStore):
|
||||
|
||||
sql = """
|
||||
SELECT
|
||||
COALESCE(COUNT(*), 0)
|
||||
COUNT(*)
|
||||
FROM (
|
||||
%(published_sql)s
|
||||
) published
|
||||
|
@ -538,7 +538,7 @@ class StatsStore(StateDeltasStore):
|
||||
|
||||
txn.execute(
|
||||
"""
|
||||
SELECT COALESCE(count(*), 0) FROM current_state_events
|
||||
SELECT COUNT(*) FROM current_state_events
|
||||
WHERE room_id = ?
|
||||
""",
|
||||
(room_id,),
|
||||
|
@ -550,7 +550,7 @@ class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase):
|
||||
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",
|
||||
)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user