Currently I have a User document in mongo which has location_2dsphere index.
The number of the user document is almost 1,000,000 because I pushed it as a dump for checking performance issues.
Now I found that selecting query users using near the current user is very very slow right after I update the user location.
my business logic is simple when a user logs in, he will get a session first then update the current user location (mobile will offer the coordinates) and with the updated location, select the people near the user that the user will get the only users located near the current location wherever he last login...
In this logic, If I remove the updating location part, it is really faster and the query time is almost 0.5 or below.
but if I try to update the current user location before selecting near the user, the time dramatically increases that is almost 5~10 secs...
My python code is same as below.
location = self.location["coordinates"] if self.location else [127.0936859, 37.505808]
params = dict(location__near=location,
location__max_distance=distance * 1000, # 1000 = 1 km
location__min_distance=0 * 1000,
birthed_at__gte=self.birthed_at - (12 * ONE_YEAR_TO_SECONDS),
birthed_at__lte=self.birthed_at + (12 * ONE_YEAR_TO_SECONDS),
id__nin=nin_ids, sex=sex, available=True)
user_ids = User.list_only_user_ids(**params)
and all the indexes on user is like below
_id_ (Regular)
last_login_at (REGULAR)
location_2dsphere (GEOSPATIAL)
phone (REGULAR)
uid (REGULAR)
Is there any solution for this? Please help me guys.. I have spent already 3 days but couldn't solve it.
question from:
https://stackoverflow.com/questions/66057950/mongodb-2dsphear-index-issue 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…