Skip to content

Commit

Permalink
1.6.1dev: use sorted() instead of ORDER BY session.sid in order t…
Browse files Browse the repository at this point in the history
…o avoid filesort caused by indexing only a prefix of column values on MySQL (closes #13634)

git-svn-id: http://trac.edgewall.org/intertrac/log:/branches/1.6-stable@17760 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed Dec 16, 2023
1 parent 0c96c81 commit a62e8b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions trac/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,18 @@ def get_known_users(self, as_dict=False):

@cached
def _known_users(self):
return self.db_query("""
SELECT DISTINCT s.sid, n.value, e.value
# Use sorted() instead of "ORDER BY s.sid" in order to avoid filesort
# caused by indexing only a prefix of column values on MySQL.
users = self.db_query("""
SELECT s.sid, n.value, e.value
FROM session AS s
LEFT JOIN session_attribute AS n ON (n.sid=s.sid
AND n.authenticated=1 AND n.name = 'name')
LEFT JOIN session_attribute AS e ON (e.sid=s.sid
AND e.authenticated=1 AND e.name = 'email')
WHERE s.authenticated=1 ORDER BY s.sid
WHERE s.authenticated=1
""")
return sorted(users, key=lambda u: u[0])

@cached
def _known_users_dict(self):
Expand Down
9 changes: 6 additions & 3 deletions trac/web/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,12 @@ def _get_list(self, sids):
sids = {self._split_sid(sid)
for sid in sids
if sid not in ('anonymous', 'authenticated', '*')}
# Use sort() instead of "ORDER BY s.sid, s.authenticated" in order to
# avoid filesort caused by indexing only a prefix of column values on
# MySQL.
rows = self.env.db_query("""
SELECT DISTINCT s.sid, s.authenticated, s.last_visit,
n.value, e.value, h.value
SELECT s.sid, s.authenticated, s.last_visit, n.value, e.value,
h.value
FROM session AS s
LEFT JOIN session_attribute AS n
ON (n.sid=s.sid AND n.authenticated=s.authenticated
Expand All @@ -476,8 +479,8 @@ def _get_list(self, sids):
LEFT JOIN session_attribute AS h
ON (h.sid=s.sid AND h.authenticated=s.authenticated
AND h.name='default_handler')
ORDER BY s.sid, s.authenticated
""")
rows.sort(key=lambda u: (u[0], u[1]))
for sid, authenticated, last_visit, name, email, handler in rows:
if all_anon and not authenticated or all_auth and authenticated \
or (sid, authenticated) in sids:
Expand Down

0 comments on commit a62e8b0

Please sign in to comment.