> ## Documentation Index
> Fetch the complete documentation index at: https://docs.probalytics.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Tips & Best Practices

> Performance optimization for Probalytics queries

Performance tips and common mistakes to avoid.

## Filter by ORDER BY Fields

Tables are sorted by their ORDER BY columns. Filtering on these makes queries **much faster**.

**markets**: `ORDER BY (platform, id)`
**fills**: `ORDER BY (platform, market_id, timestamp, id)`
**orderbook\_snapshots**: `ORDER BY (market_id, outcome.id, timestamp, hash, state, continuity, path_index)`

Use the leftmost applicable sort-key columns and a time bound whenever possible. Time bounds reduce work even when the time column is not part of the leading sort-key prefix.

### Examples

```sql theme={null}
-- Slow: No ORDER BY filters
SELECT * FROM markets WHERE title LIKE '%election%';

-- Better: Use the sort-key prefix and bound the requested period
SELECT * FROM markets
WHERE platform = 'POLYMARKET'
  AND created_at >= '2024-01-01'
  AND title LIKE '%election%';
```

## Always Filter markets by market\_type

Kalshi combo markets (`market_type = 'PARLAY'`) are over 90% of the `markets` table. Any query that does not filter `market_type` reads them all.

```sql theme={null}
-- Slow: Reads 200M+ combo rows you do not want
SELECT count() FROM markets WHERE platform = 'KALSHI';

-- Fast: Skips the combos
SELECT count() FROM markets WHERE platform = 'KALSHI' AND market_type = 'BINARY';
```

The same applies to joins. A bare `JOIN markets` loads the whole table into memory before matching; filter it in a subquery first.

```sql theme={null}
-- Slow: Builds a hash table from every market
SELECT f.price, m.title
FROM fills f
JOIN markets m ON f.market_id = m.id
WHERE f.platform = 'KALSHI' AND f.timestamp >= now() - INTERVAL 1 DAY;

-- Fast: Join only the markets you need
SELECT f.price, m.title
FROM fills f
JOIN (
    SELECT id, title
    FROM markets
    WHERE platform = 'KALSHI' AND market_type = 'BINARY'
) m ON f.market_id = m.id
WHERE f.platform = 'KALSHI' AND f.timestamp >= now() - INTERVAL 1 DAY;
```

```sql theme={null}
-- Slow: Scans all fills
SELECT * FROM fills WHERE price > 0.9;

-- Fast: Filter by ORDER BY columns
SELECT * FROM fills
WHERE platform = 'POLYMARKET'
  AND market_id = 'your-market-uuid-here'
  AND timestamp >= '2024-01-01'
  AND price > 0.9;
```

## Common Mistakes

### Always Use LIMIT

Avoid loading millions of rows unintentionally—it's slow and wastes resources.

```sql theme={null}
-- Bad: Loads massive result set
SELECT * FROM fills;

-- Good: Quick exploration
SELECT * FROM fills LIMIT 100;
```

### Don't Overuse FINAL

`FINAL` forces immediate deduplication and is **significantly slower**. Use it only for counts/aggregations where exact results matter.

```sql theme={null}
-- Slow: FINAL for exploration
SELECT * FROM markets FINAL LIMIT 100;

-- Fast: Skip FINAL for data browsing
SELECT * FROM markets LIMIT 100;

-- Use FINAL for accurate counts
SELECT count() FROM markets FINAL;
```

### Select Only What You Need

```sql theme={null}
-- Bad: Transfers 20+ columns unnecessarily
SELECT * FROM fills;

-- Good: Only needed columns
SELECT timestamp, normalized_price, size, taker_side FROM fills;
```

## Working with large datasets

* Filter by date and other sorting-key columns.
* Aggregate in ClickHouse before returning rows.
* Split large raw-data reads into smaller time ranges.

```sql theme={null}
SELECT
    toStartOfHour(timestamp) AS hour,
    avg(price) AS average_price
FROM fills
WHERE timestamp >= now() - INTERVAL 30 DAY
GROUP BY hour
ORDER BY hour;
```

## Quick Tips

**Percentiles**

```sql theme={null}
SELECT quantile(0.95)(price) as p95_price
FROM fills
WHERE market_id = 'your-market-uuid-here';
```

**Cardinality estimates**

```sql theme={null}
SELECT uniq(taker_id) as unique_takers FROM fills;
```

**Unnest outcomes array**

```sql theme={null}
SELECT outcome.name
FROM markets
ARRAY JOIN outcomes AS outcome
WHERE platform = 'POLYMARKET';
```

**Best bid/ask from orderbook**

```sql theme={null}
SELECT
    bids[1].price as best_bid,
    asks[1].price as best_ask,
    asks[1].price - bids[1].price as spread
FROM orderbook_snapshots
WHERE market_id = 'your-market-uuid-here'
  AND outcome.id = 'your-outcome-uuid-here'
  AND state = 'VERIFIED'
ORDER BY timestamp DESC, path_index DESC, indexed_at DESC
LIMIT 1;
```

When `continuity` is `RESET`, use that row as a fresh starting point. Do not forward-fill from the preceding row.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.