On Mon, 2010-07-05 at 00:59 -0700, Simon Litchfield wrote: > I'd be happy to use raw(); but then you lose len(), slicing, > pagination, filter chaining, etc. My problem came about because admin > change lists were unusably slow on large tables. With_hints allowed a > simple monkey patch to admin, dropping 2s per page down to ~0.01s.
So the underlying problem is: how to rewrite a query (whether generated by my code, someone else's code, or a mixture) so that it performs better on my database. There will always be third party code which generates queries that are really bad for performance, depending on your project or your database. This also applies to tickets like http://code.djangoproject.com/ticket/11604 This makes me think that what we need is a mechanism to do an equivalent to your monkey patching that is easier and more general. Some kind of "replace the auto-generated SQL with this SQL" method could be really useful. Could this be solved by writing your own cursor wrapper which checks the SQL against a list and rewrites as necessary? (You could set it up quickly like this: http://chris-lamb.co.uk/2010/06/01/appending-request-url-sql-statements-django/ That still involves monkey patching, but it is possible to do the same thing by writing a database backend). The major drawback is fragility - doing a replacement at this level could easily break, for example if you added a field to a model. Adequate testing could catch this if your cursor wrapper had a debug mode that could report what was replaced. If we tried to do the replacement at a higher level, then you run into the problem of how to recognise a query and replace it, which could be a much more expensive operation if we are trying to compare objects of type django.db.models.sql.query.Query, for example, and you also might have the problem of not having full access to SQL statements. However, if this method does work, if would be good to document how to do it, and provide some better builtin hooks if necessary. Luke -- "We may not return the affection of those who like us, but we always respect their good judgement." -- Libbie Fudim Luke Plant || http://lukeplant.me.uk/ -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.