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_tools 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 tools listing Signed-off-by: Sihyeon Jang <sihyeon.jang@navercorp.com>
This commit is contained in:
parent
22c4ef4fb0
commit
03d1d2a88b
1 changed files with 9 additions and 2 deletions
|
|
@ -144,9 +144,16 @@ class ToolsTable:
|
||||||
|
|
||||||
def get_tools(self) -> list[ToolUserModel]:
|
def get_tools(self) -> list[ToolUserModel]:
|
||||||
with get_db() as db:
|
with get_db() as db:
|
||||||
|
all_tools = db.query(Tool).order_by(Tool.updated_at.desc()).all()
|
||||||
|
|
||||||
|
user_ids = list(set(tool.user_id for tool in all_tools))
|
||||||
|
|
||||||
|
users = Users.get_users_by_user_ids(user_ids) if user_ids else []
|
||||||
|
users_dict = {user.id: user for user in users}
|
||||||
|
|
||||||
tools = []
|
tools = []
|
||||||
for tool in db.query(Tool).order_by(Tool.updated_at.desc()).all():
|
for tool in all_tools:
|
||||||
user = Users.get_user_by_id(tool.user_id)
|
user = users_dict.get(tool.user_id)
|
||||||
tools.append(
|
tools.append(
|
||||||
ToolUserModel.model_validate(
|
ToolUserModel.model_validate(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue