On Tue, 2008-07-08 at 08:13 -0700, ToSH wrote:
> Hi,
> 
> I would need to do a query like
> SELECT * FROM mytable WHERE match (a) against("+AAA" in boolean mode)
> and match (b against("+BBB" in boolean mode)
> and match (a,b) against("+AAA +BBB" in boolean mode)
> 
> If I would omit the last line (which seems to be redundant) I could do
> this easily with the Django framework by
> s.filter(a__search='AAA', b__search='BBB')
> 
> However it turned out that doing the query with the last line is far
> more efficient for MySQL. By querying (a,b) it can query a combined
> index which returns only very few rows which then are combined with
> the other two AND statements. If I omit the combined index, the temp
> table (for doing the AND statement) is far bigger. The speed gain is
> about factor 100 - sometimes even more (yes: my tables are quiet large
> and it is noticeable)
> 
> How would I do this in Django?

cursor.execute(my_custom_sql)

Seriously! Django is designed to cover maybe 80% of what SQL does. The
common stuff. And it provides you with access the the database cursor so
that you can do raw queries when you need to. Django isn't a replacement
for SQL, it's an adjunct. Particularly if you want to take advantages of
things that are very specific to one or other backend (esp. MySQL with
all the myriad of storage engine options), talking to the database is
the right idea.

You might be able to pull of something with the extra(where=....) method
on a queryset, too, although that can become a bit fragile in complex
situations.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to