Skip to content

Commit 4591d4d

Browse files
committed
fix: optimize SQL queries for token usage and top questions with improved username handling
1 parent 2bcb43e commit 4591d4d

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
2-
SELECT
3-
SUM(application_chat_record.message_tokens + application_chat_record.answer_tokens) as "token_usage",
4-
COALESCE(application_chat.asker->>'username', '游客') as "username"
5-
FROM
6-
application_chat_record application_chat_record
7-
LEFT JOIN application_chat application_chat ON application_chat."id" = application_chat_record.chat_id
8-
${default_sql}
1+
SELECT SUM(application_chat_record.message_tokens + application_chat_record.answer_tokens) as "token_usage",
2+
MAX(COALESCE(application_chat.asker ->>'username', '游客')) as "username"
3+
FROM application_chat_record application_chat_record
4+
LEFT JOIN application_chat application_chat ON application_chat."id" = application_chat_record.chat_id
5+
${default_sql}
96
GROUP BY
10-
COALESCE(application_chat.asker->>'username', '游客')
7+
application_chat.chat_user_id
118
ORDER BY
129
"token_usage" DESC

apps/application/sql/top_questions.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
SELECT COUNT(application_chat_record."id") AS chat_record_count,
2-
COALESCE(application_chat.asker ->>'username', '游客') AS username
1+
SELECT COUNT(application_chat_record."id") AS chat_record_count,
2+
MAX(COALESCE(application_chat.asker ->>'username', '游客')) as "username"
33
FROM application_chat_record application_chat_record
44
LEFT JOIN application_chat application_chat ON application_chat."id" = application_chat_record.chat_id
55
${default_sql}
66
GROUP BY
7-
COALESCE (application_chat.asker->>'username', '游客')
7+
application_chat.chat_user_id
88
ORDER BY
99
chat_record_count DESC,
1010
username ASC

0 commit comments

Comments
 (0)