我正在使用django 2.2.13和django-pgcrypto-fields 2.5.2。此外,我还使用email作为身份验证方法。email存储为pgcrypto字段。大约有10000活跃用户。当用户尝试登录时,需要很长时间(8-9秒)。我试着从shell登录,这也需要很长时间。
from django.contrib.auth import authenticate
user = authenticate(email='john@gmail.com', password='secret')身份验证函数的执行时间几乎为7-8秒。
user = authenticate(username='john', password='secret')当我尝试使用username进行身份验证时,它将在1秒内执行。
from app.models import User
user = User.objects.filter(email=email).first()上面的查询也需要很长时间才能执行(7-8秒)。我试着索引电子邮件列,但结果是一样的。如何加快对pgcrypto字段的身份验证和筛选查询?
发布于 2022-09-08 05:03:29
我有个解决办法。我通过last_login缩小了用户列表,并覆盖了django-allauth的_authenticate_by_email方法。
class CustomAuthenticationBackend(AuthenticationBackend):
def _authenticate_by_email(self, **credentials):
email = credentials.get('email', credentials.get('username'))
if email:
startdate= timezone.now() - timedelta(days=CACHED_AUTHENTICATION_DAY)
user = User.objects.filter(last_login__gte=startdate, email=email).first()
if user:
if self._check_password(user, credentials["password"]):
return user
return None
for user in filter_users_by_email(email):
if self._check_password(user, credentials["password"]):
return user
return None因此,如果CACHED_AUTHENTICATION_DAY设置为30/60天,那么活动用户将能够非常快地登录。
https://stackoverflow.com/questions/72642457
复制相似问题