Trying to optimise a huge and slow request

65 阅读1分钟

image.png

Shame. My bad code are very slow. But logic is very easy.

image.png

First, let me test nltk parse token speed.

image.png

It is very fast so must sql slow.

Try to filter and find value by my self.

image.png

We parse database record to dict. Only 2 sql left and faster but we not satisfied!

ChatGpt tell me a better plan to implement:

image.png

image.png

First time request it waste me 6s.

image.png

Second time request it is very fast but it show no cache was used.Why ?

image.png


Let us learn ChatGpt's code.

Word.objects.filter(word__in=tokens)

This code used in to generate a sql IN str list. Why SQL is so fast?

image.png

Ok. We use what we learned to refactor old code:

image.png

Much better but ChatGpt's code is more faster.


Thanks for reading!