mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-12 04:15:25 +00:00
perf: fix N+1 query issue in get_models method
- Replace individual user queries with batch fetching - Use single query to fetch all required users at once - Implement O(1) user lookup with dictionary mapping - Reduce query count from 1+N to 1+1 pattern for models with base_model_id Signed-off-by: Sihyeon Jang <sihyeon.jang@navercorp.com>
This commit is contained in:
parent
22c4ef4fb0
commit
c0b3db38a5
1 changed files with 9 additions and 2 deletions
|
|
@ -175,9 +175,16 @@ class ModelsTable:
|
|||
|
||||
def get_models(self) -> list[ModelUserResponse]:
|
||||
with get_db() as db:
|
||||
all_models = db.query(Model).filter(Model.base_model_id != None).all()
|
||||
|
||||
user_ids = list(set(model.user_id for model in all_models))
|
||||
|
||||
users = Users.get_users_by_user_ids(user_ids) if user_ids else []
|
||||
users_dict = {user.id: user for user in users}
|
||||
|
||||
models = []
|
||||
for model in db.query(Model).filter(Model.base_model_id != None).all():
|
||||
user = Users.get_user_by_id(model.user_id)
|
||||
for model in all_models:
|
||||
user = users_dict.get(model.user_id)
|
||||
models.append(
|
||||
ModelUserResponse.model_validate(
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue