1

I have a model with a lot of entries (more than 12,513,262) and they are supposed to increase exponentially. But the problem is due to this large no. entries querying is taking a lot of time is there a way to increase performance using indices etc.

My query is like this:

MyModel.objects.all().order_by('-timestamp')[0:50]

and its taking a lot of time to execute.

2
  • excuse me, what is the model name? just curious. Commented Nov 2, 2011 at 21:17
  • @Glycerine, of course! Now, I understand why about 12513262 entries ;) Commented Nov 30, 2011 at 16:21

1 Answer 1

4

do you have an index on the timestamp field?

if you use south (for database migrations, you should definitely look into it if you aren't already), you can just add db_index=True to your field and migrate. Otherwise you can run

./manage.py sqlindexes MyApp

to show the sql statement adding the index. (which you need to run manually, e.g. using

./manage.py dbshell
Sign up to request clarification or add additional context in comments.

2 Comments

i don t have any index yet can i create one now if yes how to do that ?
If you haven't been using South, you'll need to create the index manually with the appropriate SQL. That will vary based on what DB you're using. Ask Google: "create index [my database brand]"

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.