First I was using this request:
Model.object.get(name='something', date=somedatavariable)
But then I needed to extend my query and change it to this:
Model.objects.get(name__icontains="something", date__range(start_date,end_date))
Suddenly my database takes 5 times longer than with the original query. What’s going on here, and how can I make it faster?
A case-insensitive wildcard search (
name__icontains='something') will DEFINITELY be a more expensive DB query than a case-sensitive exact match (name='something'). 5 times slower doesn’t sound unreasonable, and it will vary heavily based on any and all of the following:The last one there is very tricky. Doing full-text indexing for a database is very hard to get right, very easy to get wrong, and not even supported by all database engines.
The same goes for
datevs.date__range: you’re using a quick, simple, easy-to-index exact match in one, and an inexact match in the other.The bottom line: if you don’t need inexact matches, then don’t use them. They’re expensive DB operations and WILL take significantly longer.