Flash Protection: Minimize Database Hit

In my application, I used flask-security to add authentication and authorization topics. SQLAlchemy is also used as a data provider (with MySQL as a backend). The application is working fine.

Then I did some MySQL tracing, and the log shows me that for every requested URL in the library of security flags, two requests to the database are sent:

  • select ... from user where user.id = 'the user identifier'
  • select ... from role, roles_users ...

I think this is a performance issue and I like to minimize these queries. I do not know if there is a configuration function that I am missing.

+4
source share
2 answers

(, Redis SQL ), , . , cookie . - Redis , .

JOIN, . , + .

+1

, -Login user_loader. , . , .

Flask-Security user_loader.

def _user_loader(user_id):
    return _security.datastore.find_user(id=user_id)

SQL-.

, () SQL-, sqlalchemy:

import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

, , , , SQL-Lite ( Flask-Login, Flask-Security .. ). ~0 latency.

0

Source: https://habr.com/ru/post/1548233/


All Articles