Abhi
Abhi

Reputation: 115

Django query optimization

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.

Upvotes: 1

Views: 387

Answers (1)

second
second

Reputation: 28637

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

Upvotes: 4

Related Questions