2

I've got a postgresql-query that returns 120 rows {integer, boolean, integer, varchar(255), varchar(255), bigint, text} in about 70ms when done in the database running psql.

Using python/django with django.db.connection.cursor.execute() it takes 10s to run, on the same machine.

I've tried putting all the rows into an array, and a single string (18k characters, but returning only the first 500 takes the same time) so there is only one row returned but with no gain.

Any ideas as to why there is such a dramatic slowdown in running a query from within python and in the db?

EDIT

I had to increase the work_mem to get the function running timely in psql. Other functions/queries don't show the same pattern, the difference between psql and python is only a few milliseconds.

EDIT

Cutting down the work_mem to 1MB shows similar numbers in psql and the django shell. Could it be that django is not going by the memory set in work_mem?

EDIT

Ugh. The problem was that the work_mem set in psql is not valid globally, if I set the memory in the function, the call is timely. I suppose setting this in the configuration file would work globally.

5
  • How are measuring this? Also what are your database settings (eg. do they say "localhost", or include full host name)? Any other details regarding the environment, connection, Django installation? Commented Sep 24, 2012 at 15:46
  • I'm using explain analyze in psql and timeit.Timer in python. Other queries I've run so far don't show this pattern. The server is running locally so there is no connection, ie localhost, environment is osx mountain lion and I'm using django 1.3. Commented Sep 24, 2012 at 15:57
  • It shouldnt be so slow ... we regularly do fetches that are much bigger than that with no where near that kind of time... Commented Sep 24, 2012 at 16:09
  • I agree, this takes way to long..:) Commented Sep 25, 2012 at 9:19
  • Yes. Setting it to False does not have any apparent effect. Commented Sep 25, 2012 at 10:47

1 Answer 1

1

If the timing between "in situ" queries and psql queries differs much then the first and usual suspect is this: If the framework uses prepared statements, then you have to check the timing in psql using prepared statements too. For example:

prepare foo as select * from sometable where intcolumn = $1;
execute foo(42);

If the timing of the execute is in the same ballpark as your in situ query, then you can explain and explain analyse the execute line.

If the timing is not in the same ballpark you have to look for something else.

Sign up to request clarification or add additional context in comments.

1 Comment

The timing is still the same even though the query is prepared. I've created a function to run the it but it has the same results whether or not the query is run directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.