[Fixed]-Django, Querysets. How to perform search on 400mb sql query set faster?

1👍

There few things you are not doing optimally.

First: don’t join strings like this

for letters in comb:
   result += letters

do this

result = ''.join(comb)

Second: you should always try to do as less db queries as possible. In your case you will do it for every combination. Why not just get filter by all combinations and then get all words that actually in db. This way you will do only one db query.

def neighb(data, word):
    mylist = re.findall(r'\w', word)
    combinations = [''.join(c) for c in itertools.permutations(mylist)]
    words = list(data.filter(form_v__in=combinations).values_list('form_v', flat=True))
    return len(words), words

Leave a comment