On Mon, Jul 11, 2011 at 3:06 PM, Cal Leeming [Simplicity Media Ltd] < cal.leem...@simplicitymedialtd.co.uk> wrote:
> > > On Mon, Jul 11, 2011 at 3:04 PM, Cal Leeming [Simplicity Media Ltd] < > cal.leem...@simplicitymedialtd.co.uk> wrote: > >> >> >> On Mon, Jul 11, 2011 at 2:57 PM, Michel30 <forerunn...@gmail.com> wrote: >> >>> Hi all, >>> >>> I have a basic search function that uses Q objects. >>> After profiling it I found that the actual (mysql) database query >>> finishes in fractions of seconds but the iterating after this can take >>> up to 50 seconds per 10.000 results. >>> >>> I have been trying to speed it up but I have had not much results.. >>> >>> My query is this one: >>> >>> found_entries = model.objects.filter((Q-objects), >>> obsolete=0).order_by('-version','docid') >>> >>> So far so good, but then I need a dictionary to retrieve only unique >>> 'documentid's'. >>> >> >> You could do: >> >> # grab all results >> _res = >> model.objects.filter((Q-objects),obsolete=0).order_by('-version','docid').values() >> # re-map them into (id, obj) >> _res = map(lambda x: [x.docid, x], _res) >> > Sorry, small adjustment: # re-map them into (id, obj), model(**x) will remap the values into new ORM objects _res = map(lambda x: [x.get('docid'), model(**x)], _res) # wrap in a dict(), which uses index position 0 as the key, and index >> position 1 as the value >> _res = dict(_res) >> > > Just tested the same principle, and it seems to work fine. It uses last > object found as the final choice if dups are found. > > >>> _res = [ [1,2], [1,3], [2,4], [2,5] ] > >>> map(lambda x: [x[0], x[1]], _res) > [[1, 2], [1, 3], [2, 4], [2, 5]] > >>> map(lambda x: [x[0], x[1]], _res) > [[1, 2], [1, 3], [2, 4], [2, 5]] > >>> dict(map(lambda x: [x[0], x[1]], _res)) > {1: 3, 2: 5} > >>> > > > > >> >> Let me know if this give you the results you need. >> >> >>> rev_dict = {} >>> >>> This is the part that hurts: >>> >>> for d in found_entries: >>> rev_dict[d.documentid] = d >>> >>> And then some more sorting and filtering: >>> >>> filtered_entries = rev_dict.values() >>> filtered_entries.sort(key=lambda d: d.revisiondate, reverse=True) >>> >>> Does anyone have some better ideas to achieve this? >>> >>> Thanks >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Django users" group. >>> To post to this group, send email to django-users@googlegroups.com. >>> To unsubscribe from this group, send email to >>> django-users+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/django-users?hl=en. >>> >>> >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.