[object Object]

← back to AsSeenInMovies

asseeninmovies: /spotted — full parallelization of all 5 queries (rows / total / cats / stats / top-films) via Promise.all; was 4 sequential awaits before Promise.all on the last 2. Cold 1.3s → 543ms, warm 3-7ms.

40fa8ddb488032b86f6ce3fe4662f3c8a90f2685 · 2026-05-12 18:00:39 -0700 · SteveStudio2

Files touched

Diff

commit 40fa8ddb488032b86f6ce3fe4662f3c8a90f2685
Author: SteveStudio2 <stevestudio2@SteveStacStudio.lan>
Date:   Tue May 12 18:00:39 2026 -0700

    asseeninmovies: /spotted — full parallelization of all 5 queries (rows / total / cats / stats / top-films) via Promise.all; was 4 sequential awaits before Promise.all on the last 2. Cold 1.3s → 543ms, warm 3-7ms.
---
 server.js | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/server.js b/server.js
index d5fc12f..f795d46 100644
--- a/server.js
+++ b/server.js
@@ -256,19 +256,21 @@ app.get('/spotted', async (req, res) => {
     let where = `s.verified_at IS NOT NULL AND (verified_by IS NULL OR verified_by NOT LIKE 'rejected%')`;
     if (cat) { params.push(cat); where += ` AND s.product_category = $${params.length}`; }
     params.push(limit, offset);
-    const r = await pool.query(`
-      SELECT s.id, s.product_name, s.product_category, s.brand, s.scene_description,
-             s.scene_timecode, s.shop_url, s.image_url, s.image_source,
-             m.id AS movie_id, m.title AS movie_title, m.release_year, m.poster_url, m.poster_source
-        FROM asim_spotted s JOIN asim_movies m ON m.id=s.movie_id
-       WHERE ${where}
-       ORDER BY s.verified_at DESC, s.id DESC
-       LIMIT $${params.length-1} OFFSET $${params.length}`, params);
-    const totalR = await pool.query(`SELECT count(*) AS n FROM asim_spotted s WHERE ${where}`, params.slice(0, params.length - 2));
-    const total = parseInt(totalR.rows[0].n, 10);
-    const totalPages = Math.max(1, Math.ceil(total / limit));
-    const catsR = await pool.query(`SELECT product_category, count(*) AS n FROM asim_spotted WHERE verified_at IS NOT NULL AND (verified_by IS NULL OR verified_by NOT LIKE 'rejected%') AND product_category IS NOT NULL GROUP BY 1 ORDER BY 2 DESC`);
-    const [statsR, topMoviesR] = await Promise.all([
+    const countParams = params.slice(0, params.length - 2);
+    // All 5 queries are independent — fire them in parallel so the wall
+    // time = max of any one, not the sum (was 4 awaits sequential ≈ 1.3s
+    // cold; parallel keeps it bounded by the slowest query).
+    const [r, totalR, catsR, statsR, topMoviesR] = await Promise.all([
+      pool.query(`
+        SELECT s.id, s.product_name, s.product_category, s.brand, s.scene_description,
+               s.scene_timecode, s.shop_url, s.image_url, s.image_source,
+               m.id AS movie_id, m.title AS movie_title, m.release_year, m.poster_url, m.poster_source
+          FROM asim_spotted s JOIN asim_movies m ON m.id=s.movie_id
+         WHERE ${where}
+         ORDER BY s.verified_at DESC, s.id DESC
+         LIMIT $${params.length-1} OFFSET $${params.length}`, params),
+      pool.query(`SELECT count(*) AS n FROM asim_spotted s WHERE ${where}`, countParams),
+      pool.query(`SELECT product_category, count(*) AS n FROM asim_spotted WHERE verified_at IS NOT NULL AND (verified_by IS NULL OR verified_by NOT LIKE 'rejected%') AND product_category IS NOT NULL GROUP BY 1 ORDER BY 2 DESC`),
       pool.query(`
         SELECT
           (SELECT count(*) FROM asim_spotted WHERE verified_at IS NOT NULL AND (verified_by IS NULL OR verified_by NOT LIKE 'rejected%')) AS placements,
@@ -283,6 +285,8 @@ app.get('/spotted', async (req, res) => {
          ORDER BY count(*) DESC, m.title
          LIMIT 6`)
     ]);
+    const total = parseInt(totalR.rows[0].n, 10);
+    const totalPages = Math.max(1, Math.ceil(total / limit));
     const gs = statsR.rows[0] || {};
     const topMovies = topMoviesR.rows;
     const qs = (overrides = {}) => {

← 992f471 asseeninmovies: /spotted — 'Most-Spotted Films' rail (top 6  ·  back to AsSeenInMovies  ·  asseeninmovies: /movie/:id + /person/:id — parallelize credi 08671ef →