Hacker News new | ask | show | jobs
by greenSunglass 1249 days ago
I've been working on a project with flask + sqlalchemy. I have those sql queries returning a bit of rows (up to 20k) and that are quite slow. SQLalchemy does not seem to support caching the results and I've started to use flask-caching[1] with redis using the @cache.memoize() decorator.

Just wondering if I am taking the right path or if there is better alternative.

[1]https://flask-caching.readthedocs.io

3 comments

> flask-caching[1] with redis using the @cache.memoize() decorator.

> Just wondering if I am taking the right path or if there is better alternative.

Yes, this can be a fine solution to slow queries and is used very often in many kinds of web applications.

However... 20k rows is not a very big number for a modern dB. If the db query is really the slow part then you should investigate why - ensure the relevant sql queries are written properly, and that the relevant tables are indexed properly for the queries that you are running on them.

I've been using nginx for caching when the response isn't user-specific. Pretty painless to set up and doesn't bloat the codebase.

(But it's mostly for small personal projects, so grain of salt and all.)

https://www.nginx.com/blog/nginx-caching-guide/

Indexed?

If complex, what does Sqlalchemy output as the SQL? Could you optimise the query? If quite complex, optimise the tables by redesigning them?

Was the move to allow Redis to cache persistently across requests? Does it do this?

Are you timing each function to look for slowness?