Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit b968187

Browse files
committed
statsが重そうなやつ
1 parent d1ab38f commit b968187

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

webapp/go/stats_handler.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,18 @@ func getUserStatisticsHandler(c echo.Context) error {
9696
for _, user := range users {
9797
var reactions int64
9898
query := `
99-
SELECT COUNT(*) FROM users u
100-
INNER JOIN livestreams l ON l.user_id = u.id
99+
SELECT COUNT(*) FROM livestreams
101100
INNER JOIN reactions r ON r.livestream_id = l.id
102-
WHERE u.id = ?`
101+
WHERE l.user_id = ?`
103102
if err := tx.GetContext(ctx, &reactions, query, user.ID); err != nil && !errors.Is(err, sql.ErrNoRows) {
104103
return echo.NewHTTPError(http.StatusInternalServerError, "failed to count reactions: "+err.Error())
105104
}
106105

107106
var tips int64
108107
query = `
109-
SELECT IFNULL(SUM(l2.tip), 0) FROM users u
110-
INNER JOIN livestreams l ON l.user_id = u.id
108+
SELECT IFNULL(SUM(l2.tip), 0) FROM livestreams
111109
INNER JOIN livecomments l2 ON l2.livestream_id = l.id
112-
WHERE u.id = ?`
110+
WHERE l.user_id = ?`
113111
if err := tx.GetContext(ctx, &tips, query, user.ID); err != nil && !errors.Is(err, sql.ErrNoRows) {
114112
return echo.NewHTTPError(http.StatusInternalServerError, "failed to count tips: "+err.Error())
115113
}

webapp/sql/initdb.d/10_schema.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ CREATE TABLE `livestreams` (
4646
`playlist_url` VARCHAR(255) NOT NULL,
4747
`thumbnail_url` VARCHAR(255) NOT NULL,
4848
`start_at` BIGINT NOT NULL,
49-
`end_at` BIGINT NOT NULL
49+
`end_at` BIGINT NOT NULL,
50+
INDEX `idx_livestreams_user_id` (`user_id`)
5051
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
5152

5253
-- ライブ配信予約枠
@@ -117,5 +118,6 @@ CREATE TABLE `reactions` (
117118
`livestream_id` BIGINT NOT NULL,
118119
-- :innocent:, :tada:, etc...
119120
`emoji_name` VARCHAR(255) NOT NULL,
120-
`created_at` BIGINT NOT NULL
121+
`created_at` BIGINT NOT NULL,
122+
INDEX `idx_reactions_livestream_id` (`livestream_id`)
121123
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

0 commit comments

Comments
 (0)