WebFaction
Community site: login faq

I'm running a data-driven Django site on Webfaction, backed by a postgres database. I've been using the excellent django-debug-toolbar to log SQL queries and see how long they take to execute.

I've noticed something odd. A typical query against the postgres database, with 15,000 records in it, takes just a few milliseconds. But a call to django_session (which happens on every page) takes between 1 and 3 seconds. This makes the whole site feel sluggish.

This is the call:

SELECT ••• FROM "django_session" WHERE 
("django_session"."session_key" = E'xxx' AND 
"django_session"."expire_date" > E'2013-06-13 17:55:55.200485' )

Could you suggest why this might be so slow on Webfaction? It just takes a couple of milliseconds on my local machine, and I only have one user in the database, so it's not like it's hunting through thousands of records.

asked 13 Jun '13, 12:03

AnnaPS
10711020
accept rate: 100%

edited 13 Jun '13, 12:03


Could it be that your sessions table has grown out of hand and/or isn't using the correct index? First check your table size: select count(*) from django_session;. If it looks really big, you can remove expired sessions with manage.py cleanup. I doubt that's the case, since you said your entire database has only 15,000 records, but it's worth looking at.

The next thing I'd check is whether or not your query is using the right index.

explain select * from django_session where...

What you want to see is that it's using the index django_session_session_key_like, which is based on the session key, and not something else. If it's not, check whether or not that index exists and create it if necessary. If it does exists but is not being used, then you might have to regenerate the stats on that table to let PostgreSQL know that index is the best one.

Hope this helps.

permanent link

answered 13 Jun '13, 13:09

mikey
6539
accept rate: 0%

I second this, typically slow queries on the session table is from the session table becoming to large over time.

(13 Jun '13, 21:50) johns

Thanks so much! I tried both suggestions. When I queried, there were only only 6 rows in the django_session table (though the query also ran much faster, so I will try the same query again later today). Meanwhile, the query plan said: Index Scan using django_session_pkey on django_session (cost=0.00..8.27 rows=1 width=138) Index Cond: ((session_key)::text = 'xxx'::text) Filter: (expire_date > '2013-06-14 12:57:11.63345+00'::timestamp with time zone). So that looks like a primary key index lookup, rather than an index based on the session key. Could that be the problem?

(14 Jun '13, 07:00) AnnaPS
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×2

question asked: 13 Jun '13, 12:03

question was seen: 3,717 times

last updated: 14 Jun '13, 07:00

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM