Fiche boutique : requête « boutiques similaires » réécrite en 2 temps indexés (le LEFT JOIN DISTINCT prenait ~5 min sur les grosses catégories à 347 k produits — 0,5 s maintenant)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 changed file +32 −9
modified
fabrika/web.py
+32 −9
@@ -189,15 +189,38 @@ def store_detail(store_id: str): | ||
| 189 | 189 | c["label"] = CATEGORIES.get(c["key"], (c["key"], []))[0] |
| 190 | 190 | # boutiques similaires : même catégorie dominante ou même région, avec produits |
| 191 | 191 | topcat = s["category_breakdown"][0]["key"] if s["category_breakdown"] else None |
| 192 | − s["similar"] = q(con, """ | |
| 193 | − SELECT DISTINCT st.id, st.name, st.region, st.origin_class, st.logo_url, | |
| 194 | − st.product_count | |
| 195 | − FROM stores st | |
| 196 | − LEFT JOIN products p ON p.store_id=st.id AND p.active=1 AND p.listing_status='published' AND p.category=? | |
| 197 | − WHERE st.id<>? AND st.product_count>0 | |
| 198 | − AND (p.uid IS NOT NULL OR (st.region<>'' AND st.region=?)) | |
| 199 | − ORDER BY (st.region=?) DESC, st.product_count DESC LIMIT 8""", | |
| 200 | − (topcat, store_id, s.get("region") or "", s.get("region") or "")) | |
| 192 | + # Boutiques similaires en 2 temps INDEXÉS (l'ancien LEFT JOIN DISTINCT | |
| 193 | + # prenait plusieurs minutes sur les grosses catégories — constaté le | |
| 194 | + # 2026-08-19 après le passage à 347 k produits publiés) : | |
| 195 | + # 1) boutiques ayant des produits publiés dans la catégorie dominante | |
| 196 | + # (idx_products_cat), 2) complément par région. | |
| 197 | + region = s.get("region") or "" | |
| 198 | + similar, seen_ids = [], {store_id} | |
| 199 | + if topcat: | |
| 200 | + for r in q(con, """ | |
| 201 | + SELECT st.id, st.name, st.region, st.origin_class, st.logo_url, | |
| 202 | + st.product_count | |
| 203 | + FROM stores st JOIN ( | |
| 204 | + SELECT store_id FROM products | |
| 205 | + WHERE category=? AND active=1 AND listing_status='published' | |
| 206 | + GROUP BY store_id) pc ON pc.store_id = st.id | |
| 207 | + WHERE st.id<>? AND st.product_count>0 | |
| 208 | + ORDER BY (st.region=?) DESC, st.product_count DESC LIMIT 8""", | |
| 209 | + (topcat, store_id, region)): | |
| 210 | + if r["id"] not in seen_ids: | |
| 211 | + seen_ids.add(r["id"]) | |
| 212 | + similar.append(r) | |
| 213 | + if len(similar) < 8 and region: | |
| 214 | + for r in q(con, """ | |
| 215 | + SELECT id, name, region, origin_class, logo_url, product_count | |
| 216 | + FROM stores WHERE id<>? AND product_count>0 AND region=? | |
| 217 | + ORDER BY product_count DESC LIMIT 8""", (store_id, region)): | |
| 218 | + if r["id"] not in seen_ids: | |
| 219 | + seen_ids.add(r["id"]) | |
| 220 | + similar.append(r) | |
| 221 | + if len(similar) >= 8: | |
| 222 | + break | |
| 223 | + s["similar"] = similar[:8] | |
| 201 | 224 | return s |
| 202 | 225 | finally: |
| 203 | 226 | con.close() |
| 204 | 227 | |