You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use SQL instead of python to retrieve image count, asset count and board cover image.
This reduces the number of SQL queries needed to list all boards. Previously, we did `1 + 2 * board_count` queries::
- 1 query to get the list of board records
- 1 query per board to get its total count
- 1 query per board to get its cover image
Then, on the frontend, we made two additional network requests to get each board's counts:
- 1 request (== 1 SQL query) for image count
- 1 request (== 1 SQL query) for asset count
All of this information is now retrieved in a single SQL query, and provided via single network request.
As part of this change, `BoardRecord` now includes `image_count`, `asset_count` and `cover_image_name`. This makes `BoardDTO` redundant, but removing it is a deeper change...
-- This query retrieves board records, joining with the board_images and images tables to get image counts and cover image names.
21
+
-- It is not a complete query, as it is missing a GROUP BY or WHERE clause (and is unterminated).
22
+
SELECT b.board_id,
23
+
b.board_name,
24
+
b.created_at,
25
+
b.updated_at,
26
+
b.archived,
27
+
-- Count the number of images in the board, alias image_count
28
+
COUNT(
29
+
CASE
30
+
WHEN i.image_category in ('general') -- "Images" are images in the 'general' category
31
+
AND i.is_intermediate = 0 THEN 1 -- Intermediates are not counted
32
+
END
33
+
) AS image_count,
34
+
-- Count the number of assets in the board, alias asset_count
35
+
COUNT(
36
+
CASE
37
+
WHEN i.image_category in ('control', 'mask', 'user', 'other') -- "Assets" are images in any of the other categories ('control', 'mask', 'user', 'other')
38
+
AND i.is_intermediate = 0 THEN 1 -- Intermediates are not counted
39
+
END
40
+
) AS asset_count,
41
+
-- Get the name of the the most recent image in the board, alias cover_image_name
42
+
(
43
+
SELECT bi.image_name
44
+
FROM board_images bi
45
+
JOIN images i ON bi.image_name = i.image_name
46
+
WHERE bi.board_id = b.board_id
47
+
AND i.is_intermediate = 0 -- Intermediates cannot be cover images
48
+
ORDER BY i.created_at DESC -- Sort by created_at to get the most recent image
49
+
LIMIT 1
50
+
) AS cover_image_name
51
+
FROM boards b
52
+
LEFT JOIN board_images bi ON b.board_id = bi.board_id
53
+
LEFT JOIN images i ON bi.image_name = i.image_name
0 commit comments