#30717: Session model has a useless LIKE index
------------------------------------------------+------------------------
               Reporter:  Ran Benita            |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  contrib.sessions      |        Version:  master
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 The Session model has a CharField primary key, `session_key`. As a primary
 key, it is indexed, but as a CharField, it has two indexes created, one
 for equality matches and one for LIKE matches. Sessions are not (and
 should not) be matched with LIKE, so the index is unused. It will be
 better to remove it -- will reduce space usage and slightly reduce INSERT
 and DELETE overhead.

 ----

 I use PostgreSQL, version 11. I checked whether the index is used with the
 following query:

 {{{
 select * from pg_stat_all_indexes where indexrelname like
 'django_session_session_key_%_like';
 }}}

 Surprisingly, it is used, every time a user logs out. For some reason,
 postgres decides to use it on DELETE (and not for SELECT):

 {{{
 # explain delete from django_session where session_key = '1000';
                                                          QUERY PLAN
 
-----------------------------------------------------------------------------------------------------------------------------
  Delete on django_session  (cost=0.43..8.45 rows=1 width=6)
    ->  Index Scan using django_session_session_key_1136ae038ed61e94_like
 on django_session  (cost=0.43..8.45 rows=1 width=6)
          Index Cond: ((session_key)::text = '1000'::text)
 }}}

 However, when the index is dropped, the pkey index is used instead:

 {{{
 # drop index django_session_session_key_1136ae038ed61e94_like;
 DROP INDEX
 # explain delete from django_session where session_key = '1001';
                                            QUERY PLAN
 
------------------------------------------------------------------------------------------------
  Delete on django_session  (cost=0.43..8.45 rows=1 width=6)
    ->  Index Scan using django_session_pkey on django_session
 (cost=0.43..8.45 rows=1 width=6)
          Index Cond: ((session_key)::text = '1001'::text)
 }}}

 so the LIKE index is not actually helpful.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30717>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.445090fddca38d47a8a6dc85c569fc89%40djangoproject.com.

Reply via email to