views:

30

answers:

2

First i was using this request:

Model.object.get(name='somethink', date=somedatavariable)

But then I needed to extend my query and change it to this:

Model.objects.get(name__icontains="somethink", 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: 

I doubt if anyone can guess, how exactly your database behaved :-) But you can use this middleware:

http://djangosnippets.org/snippets/161/

and check, what kind of sql queries were made and how much time they took.

gruszczy
Knowing exactly how long the queries took is a good thing, though honestly I prefer to do that kind of profiling in a unit test where I can isolate variables. What the queries look like is fairly straightforward, though, and the root of the OP's slowdown is pretty clear.
Gabriel Hurley
+2  A: 

A case-insensitive wildcard search (name__icontains='somethink') will DEFINITELY be a more expensive DB query than a case-sensitive exact match (name='somethink'). 5 times slower doesn't sound unreasonable, and it will vary heavily based on any and all of the following:

  • Database Engine (MySQL, PostgreSQL, SQLite, etc.)
  • Number of records in the DB
  • Volume of text in each name field that it has to search through
  • Whether or not there's an appropriate index the DB can use for that column

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 date vs. 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.

Gabriel Hurley
Maybe I can somehow reconfigure MySQL DB. I dont like how it work. I think pretty long time. But in Task Manager it show that MySQL use only 25 mb. Can I extend my memory for MsSQL?
Pol
It's not a memory issue. MySQL's memory-management is fine. You need to understand more about how database indexing and collation work. Or for a quick "I don't want to understand, just give me a solution..." then I recommend this Google search: http://www.google.com/search?q=django+mysql+full-text+indexing
Gabriel Hurley
No! I want understand. Give me some good doc
Pol