# Query baseline

Generated: 2026-08-04T18:11:41.542Z
Database host: ep-misty-cloud-ajlx6fex-pooler.c-3.us-east-2.aws.neon.tech

## Neon slow queries (pg_stat_statements)

_pg_stat_statements extension not enabled on this database._

## EXPLAIN — top 10 slow statements

## Hot routes

### Deals listing (`/deals`)

- DB await count: **8**
- Total DB time: **2812.0 ms**
- Worst statement (504.4 ms):
```sql
SELECT GREATEST(CEIL(MAX(min_price) / 50.0) * 50, 100) AS "priceCeiling"
      FROM deals,
        (SELECT percentile_cont(0.75) WITHIN GROUP (ORDER BY min_price) AS q3,
                percentile_cont(0.25) WITHIN GROUP (ORDER BY min_price) AS q1
         FROM deals
         WHERE deal_state = 'ACTIVE' AND min_price IS NOT NULL) f
      WHERE deal_state = 'ACTIVE'
        AND min_price IS NOT NULL
        AND min_price <= f.q3 + 3 * (f.q3 - f.q1)
```
- Worst plan:
```json
[
  {
    "Plan": {
      "Node Type": "Aggregate",
      "Strategy": "Plain",
      "Partial Mode": "Simple",
      "Parallel Aware": false,
      "Async Capable": false,
      "Startup Cost": 37.26,
      "Total Cost": 37.28,
      "Plan Rows": 1,
      "Plan Width": 32,
      "Plans": [
        {
          "Node Type": "Nested Loop",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Join Type": "Inner",
          "Startup Cost": 18.29,
          "Total Cost": 37.23,
          "Plan Rows": 12,
          "Plan Width": 6,
          "Inner Unique": false,
          "Join Filter": "((deals.min_price)::double precision <= ((percentile_cont('0.75'::double precision) WITHIN GROUP (ORDER BY ((deals_1.min_price)::double precision))) + ('3'::double precision * ((percentile_cont('0.75'::double precision) WITHIN GROUP (ORDER BY ((deals_1.min_price)::double precision))) - (percentile_cont('0.25'::double precision) WITHIN GROUP (ORDER BY ((deals_1.min_price)::double precision)))))))",
          "Plans": [
            {
              "Node Type": "Aggregate",
              "Strategy": "Plain",
              "Partial Mode": "Simple",
              "Parent Relationship": "Outer",
              "Parallel Aware": false,
              "Async Capable": false,
              "Startup Cost": 18.29,
              "Total Cost": 18.3,
              "Plan Rows": 1,
              "Plan Width": 16,
              "Plans": [
                {
                  "Node Type": "Seq Scan",
                  "Parent Relationship": "Outer",
                  "Parallel Aware": false,
                  "Async Capable": false,
                  "Relation Name": "deals",
                  "Alias": "deals_1",
                  "Startup Cost": 0,
                  "Total Cost": 18.1,
                  "Plan Rows": 37,
                  "Plan Width": 6,
                  "Filter": "((min_price IS NOT NULL) AND (deal_state = 'ACTIVE'::deal_state))"
                }
              ]
            },
            {
              "Node Type": "Seq Scan",
              "Parent Relationship": "Inner",
              "Parallel Aware": false,
              "Async Capable": false,
              "Relation Name": "deals",
              "Alias": "deals",
              "Startup Cost": 0,
              "Total Cost": 18.1,
              "Plan Rows": 37,
              "Plan Width": 6,
              "Filter": "((min_price IS NOT NULL) AND (deal_state = 'ACTIVE'::deal_state))"
            }
          ]
        }
      ]
    }
  }
]
```

### Deal detail (`/deals/e2e-33cef916-96c2-42ab-ac20-9b5eb27adc2b`)

- DB await count: **13**
- Total DB time: **2074.5 ms**
- Worst statement (173.0 ms):
```sql
select "deal_sku_id", "min_qty", "discount_percent" from "sku_qty_tiers" where "sku_qty_tiers"."deal_sku_id" in ($1) order by "sku_qty_tiers"."sort_order" asc
```
- Worst plan:
```json
[
  {
    "Plan": {
      "Node Type": "Sort",
      "Parallel Aware": false,
      "Async Capable": false,
      "Startup Cost": 13.74,
      "Total Cost": 13.76,
      "Plan Rows": 6,
      "Plan Width": 28,
      "Sort Key": [
        "sort_order"
      ],
      "Plans": [
        {
          "Node Type": "Bitmap Heap Scan",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Relation Name": "sku_qty_tiers",
          "Alias": "sku_qty_tiers",
          "Startup Cost": 4.2,
          "Total Cost": 13.67,
          "Plan Rows": 6,
          "Plan Width": 28,
          "Recheck Cond": "(deal_sku_id = 'bff26584-738f-4d1c-b9c3-bcc71cb8efce'::uuid)",
          "Plans": [
            {
              "Node Type": "Bitmap Index Scan",
              "Parent Relationship": "Outer",
              "Parallel Aware": false,
              "Async Capable": false,
              "Index Name": "sku_qty_tiers_deal_sku_idx",
              "Startup Cost": 0,
              "Total Cost": 4.2,
              "Plan Rows": 6,
              "Plan Width": 0,
              "Index Cond": "(deal_sku_id = 'bff26584-738f-4d1c-b9c3-bcc71cb8efce'::uuid)"
            }
          ]
        }
      ]
    }
  }
]
```

### Cart (`/api/cart`)

- DB await count: **4**
- Total DB time: **1399.2 ms**
- Worst statement (774.5 ms):
```sql
select "cart_line"."id", "cart_line"."variant_id", "cart_line"."qty", "cart"."updated_at", "deal_skus"."deal_id", "deals"."title", "deals"."vendor_id", "vendors"."display_name", "deal_skus"."discounted_price", "deal_skus"."original_price", "deals"."commission_rate", "deals"."max_per_user", "deals"."stock_remaining", "deals"."deal_state", "deals"."window_end", "deal_images"."url" from "cart_line" inner join "cart" on "cart_line"."cart_id" = "cart"."id" inner join "deal_skus" on "deal_skus"."id" = "cart_line"."variant_id" inner join "deals" on "deals"."id" = "deal_skus"."deal_id" inner join "vendors" on "vendors"."id" = "deals"."vendor_id" left join "deal_images" on ("deal_images"."deal_id" = "deals"."id" and "deal_images"."is_primary" = $1) where "cart_line"."cart_id" = $2
```
- Worst plan:
```json
[
  {
    "Plan": {
      "Node Type": "Nested Loop",
      "Parallel Aware": false,
      "Async Capable": false,
      "Join Type": "Left",
      "Startup Cost": 1.77,
      "Total Cost": 16.3,
      "Plan Rows": 1,
      "Plan Width": 242,
      "Inner Unique": false,
      "Plans": [
        {
          "Node Type": "Nested Loop",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Join Type": "Inner",
          "Startup Cost": 1.5,
          "Total Cost": 15.71,
          "Plan Rows": 1,
          "Plan Width": 181,
          "Inner Unique": true,
          "Plans": [
            {
              "Node Type": "Nested Loop",
              "Parent Relationship": "Outer",
              "Parallel Aware": false,
              "Async Capable": false,
              "Join Type": "Inner",
              "Startup Cost": 1.23,
              "Total Cost": 14.94,
              "Plan Rows": 1,
              "Plan Width": 166,
              "Inner Unique": true,
              "Plans": [
                {
                  "Node Type": "Nested Loop",
                  "Parent Relationship": "Outer",
                  "Parallel Aware": false,
                  "Async Capable": false,
                  "Join Type": "Inner",
                  "Startup Cost": 1.09,
                  "Total Cost": 14.5,
                  "Plan Rows": 1,
                  "Plan Width": 71,
                  "Inner Unique": false,
                  "Plans": [
                    {
                      "Node Type": "Hash Join",
                      "Parent Relationship": "Outer",
                      "Parallel Aware": false,
                      "Async Capable": false,
                      "Join Type": "Inner",
                      "Startup Cost": 1.09,
                      "Total Cost": 9.2,
                      "Plan Rows": 1,
                      "Plan Width": 79,
                      "Inner Unique": true,
                      "Hash Cond": "(deal_skus.id = cart_line.variant_id)",
                      "Plans": [
                        {
                          "Node Type": "Seq Scan",
                          "Parent Relationship": "Outer",
                          "Parallel Aware": false,
                          "Async Capable": false,
                          "Relation Name": "deal_skus",
                          "Alias": "deal_skus",
                          "Startup Cost": 0,
                          "Total Cost": 7.46,
                          "Plan Rows": 246,
                          "Plan Width": 43
                        },
                        {
                          "Node Type": "Hash",
                          "Parent Relationship": "Inner",
                          "Parallel Aware": false,
                          "Async Capable": false,
                          "Startup Cost": 1.07,
                          "Total Cost": 1.07,
                          "Plan Rows": 1,
                          "Plan Width": 52,
                          "Plans": [
                            {
                              "Node Type": "Seq Scan",
                              "Parent Relationship": "Outer",
                              "Parallel Aware": false,
                              "Async Capable": false,
                              "Relation Name": "cart_line",
                              "Alias": "cart_line",
                              "Startup Cost": 0,
                              "Total Cost": 1.07,
                              "Plan Rows": 1,
                              "Plan Width": 52,
                              "Filter": "(cart_id = '7e13810a-2b5d-4528-92f6-717059eb0ed7'::uuid)"
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "Node Type": "Seq Scan",
                      "Parent Relationship": "Inner",
                      "Parallel Aware": false,
                      "Async Capable": false,
                      "Relation Name": "cart",
                      "Alias": "cart",
                      "Startup Cost": 0,
                      "Total Cost": 5.29,
                      "Plan Rows": 1,
                      "Plan Width": 24,
                      "Filter": "(id = '7e13810a-2b5d-4528-92f6-717059eb0ed7'::uuid)"
                    }
                  ]
                },
                {
                  "Node Type": "Index Scan",
                  "Parent Relationship": "Inner",
                  "Parallel Aware": false,
                  "Async Capable": false,
                  "Scan Direction": "Forward",
                  "Index Name": "deals_pkey",
                  "Relation Name": "deals",
                  "Alias": "deals",
                  "Startup Cost": 0.14,
                  "Total Cost": 0.44,
                  "Plan Rows": 1,
                  "Plan Width": 95,
                  "Index Cond": "(id = deal_skus.deal_id)"
                }
              ]
            },
            {
              "Node Type": "Index Scan",
              "Parent Relationship": "Inner",
              "Parallel Aware": false,
              "Async Capable": false,
              "Scan Direction": "Forward",
              "Index Name": "vendors_pkey",
              "Relation Name": "vendors",
              "Alias": "vendors",
              "Startup Cost": 0.27,
              "Total Cost": 0.75,
              "Plan Rows": 1,
              "Plan Width": 31,
              "Index Cond": "(id = deals.vendor_id)"
            }
          ]
        },
        {
          "Node Type": "Index Scan",
          "Parent Relationship": "Inner",
          "Parallel Aware": false,
          "Async Capable": false,
          "Scan Direction": "Forward",
          "Index Name": "deal_images_deal_idx",
          "Relation Name": "deal_images",
          "Alias": "deal_images",
          "Startup Cost": 0.27,
          "Total Cost": 0.58,
          "Plan Rows": 1,
          "Plan Width": 93,
          "Index Cond": "(deal_id = deals.id)",
          "Filter": "is_primary"
        }
      ]
    }
  }
]
```

### Checkout (`/api/deals/33cef916-96c2-42ab-ac20-9b5eb27adc2b/checkout-context`)

- DB await count: **6**
- Total DB time: **960.0 ms**
- Worst statement (165.8 ms):
```sql
select "id", "vendor_id", "deal_type", "title", "description", "category_id", "category", "is_voucher", "window_start", "window_end", "pickup_start", "pickup_end", "pickup_address", "special_instructions", "deal_state", "rejection_reason", "rejection_detail", "commission_rate", "is_personal_deal", "personal_deal_for_user_id", "approved_at", "approved_by", "content_hash", "appeal_status", "appeal_reason", "appeal_at", "appeal_decided_at", "sla_alerted_at", "max_per_user", "created_at", "sold_out_at", "sold_out_gold_expires_at", "source_language", "translation_status", "metadata", "min_price", "max_price", "max_discount_percent", "stock_remaining", "return_window_days", "is_returnable", "is_physical" from "deals" where ("deals"."id" = $1 and "deals"."deal_state" = $2) limit $3
```
- Worst plan:
```json
[
  {
    "Plan": {
      "Node Type": "Limit",
      "Parallel Aware": false,
      "Async Capable": false,
      "Startup Cost": 0.14,
      "Total Cost": 8.16,
      "Plan Rows": 1,
      "Plan Width": 683,
      "Plans": [
        {
          "Node Type": "Index Scan",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Scan Direction": "Forward",
          "Index Name": "deals_pkey",
          "Relation Name": "deals",
          "Alias": "deals",
          "Startup Cost": 0.14,
          "Total Cost": 8.16,
          "Plan Rows": 1,
          "Plan Width": 683,
          "Index Cond": "(id = '33cef916-96c2-42ab-ac20-9b5eb27adc2b'::uuid)",
          "Filter": "(deal_state = 'ACTIVE'::deal_state)"
        }
      ]
    }
  }
]
```

### Vendor dashboard (`/api/vendor/dashboard`)

- DB await count: **11**
- Total DB time: **2495.1 ms**
- Worst statement (494.8 ms):
```sql
select "id", "aggregate_type", "aggregate_id", "event_type", "dedupe_key", "payload", "created_at", "processed_at", "failed_at", "retry_count", "retry_limit", "last_error", "dead_at" from "outbox" where ("outbox"."aggregate_type" = $1 and "outbox"."aggregate_id" = $2 and "outbox"."event_type" in ($3, $4, $5, $6, $7, $8, $9, $10, $11)) order by "outbox"."created_at" desc limit $12
```
- Worst plan:
```json
[
  {
    "Plan": {
      "Node Type": "Limit",
      "Parallel Aware": false,
      "Async Capable": false,
      "Startup Cost": 0.3,
      "Total Cost": 8.33,
      "Plan Rows": 1,
      "Plan Width": 406,
      "Plans": [
        {
          "Node Type": "Index Scan",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Scan Direction": "Backward",
          "Index Name": "outbox_aggregate_feed_idx",
          "Relation Name": "outbox",
          "Alias": "outbox",
          "Startup Cost": 0.3,
          "Total Cost": 8.33,
          "Plan Rows": 1,
          "Plan Width": 406,
          "Index Cond": "((aggregate_type = 'vendor'::text) AND (aggregate_id = 'bd771295-1523-4637-b4e3-9d1736e4fe92'::uuid))",
          "Filter": "(event_type = ANY ('{vendor.deal.tier_passed,vendor.deal.critical_window,vendor.purchase.redeemed,vendor.purchase.refunded,vendor.review.received,vendor.account.approved,vendor.account.frozen,vendor.account.unfrozen,vendor.account.rejected}'::text[]))"
        }
      ]
    }
  }
]
```

### Admin lists (`/api/admin/deals?limit=20`)

- DB await count: **4**
- Total DB time: **627.9 ms**
- Worst statement (162.4 ms):
```sql
SELECT
      COUNT(*) FILTER (WHERE deal_state = 'ACTIVE')::int AS "totalActive",
      COUNT(*) FILTER (
        WHERE deal_state = 'ACTIVE'
          AND window_end IS NOT NULL
          AND window_end > NOW()
          AND window_end <= NOW() + INTERVAL '48 hours'
      )::int AS "expiringIn48h",
      COUNT(*) FILTER (
        WHERE deal_state = 'ACTIVE'
          AND stock_remaining IS NOT NULL
          AND stock_remaining < 5
      )::int AS "lowStock",
      COUNT(*) FILTER (WHERE deal_state = 'PENDING_APPROVAL')::int AS "pendingApproval"
    FROM deals
```
- Worst plan:
```json
[
  {
    "Plan": {
      "Node Type": "Aggregate",
      "Strategy": "Plain",
      "Partial Mode": "Simple",
      "Parallel Aware": false,
      "Async Capable": false,
      "Startup Cost": 26.16,
      "Total Cost": 26.18,
      "Plan Rows": 1,
      "Plan Width": 16,
      "Plans": [
        {
          "Node Type": "Seq Scan",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Relation Name": "deals",
          "Alias": "deals",
          "Startup Cost": 0,
          "Total Cost": 17.48,
          "Plan Rows": 248,
          "Plan Width": 16
        }
      ]
    }
  }
]
```

