# Faster queries

Optimize your query performance to get faster results when exploring your logs and metrics.

## Running the same query repeatedly?

Extract values as **unaggregated fields** in **Sources → Your source → Metrics** tab instead of querying logs each time. This converts your frequently-accessed log data into time series metrics for much faster retrieval.

Learn more about [Extracting metrics from logs](https://betterstack.com/docs/logs/dashboards/logs-to-metrics/).

## Understanding your data types

Our infrastructure handles **wide events** (logs) and **time series** (metrics) differently, with metrics offering much faster query performance. Learn more about [Wide events vs. time series](https://betterstack.com/docs/logs/wide-events-time-series/).

If you're frequently running the same queries, consider whether your use case would benefit from [converting log data to metrics](https://betterstack.com/docs/logs/dashboards/logs-to-metrics/).

### When to use metrics vs. logs

**Use metrics when**

- Running the same query repeatedly.
- Need sub-second query performance.
- Working with numerical data and aggregations.
- Building dashboards and alerts.

**Use logs when**

- Performing ad-hoc exploration.
- Need full context and details.
- Debugging specific issues.
- Working with unstructured data.

## Optimizing ad-hoc log queries

Quickly analyze logs in **Live tail → Explore** using Log filtering, Drag and Drop, or custom SQL. Read more about all features of [Explore](https://betterstack.com/docs/logs/using-logtail/explore-logs/).

For faster queries on your **wide events** (logs), try these optimization techniques:

### 1. Narrow your time range

Shorter time frames significantly reduce the amount of data processed.

![Narrowing the time range](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/0180de4c-9f93-4eb5-30f5-9cff5d115900/orig =2166x878)

### 2. Query specific sources

Instead of searching across all sources, target the specific source containing your data.

- Select individual sources in the source dropdown.
- Avoid querying All sources when possible.

![Querying a single source](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/0a66e4c6-4a06-40db-3a21-7f1014e3fa00/lg1x =2166x878)

### 3. Use sampling for exploration

Enable Sampling to work with a representative subset of your data while developing and testing queries.

If using a Log SQL query, use the `{{sampling}}` variable (`Float32` between 0 and 1) to extrapolate `sum` and `count` aggregated values from the sampled subset of data to approximate an unsampled query.

```SQL
SELECT
  {{time}} AS time,
  COUNT(*) / {{sampling}} AS count
FROM {{source}}
GROUP BY time
ORDER BY time ASC
```

![Using sampling](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/b5e07771-d7d1-42f3-a1e9-cb3549983f00/public =1708x1430)

### 4. Request additional compute

For consistently slow queries on large datasets, we can add more compute power to your cluster:

- Share a slow Live tail link with our support team.
- We'll analyze your data volume and query performance.
- Small adjustments are often available at no charge.
- Larger performance improvements for very large datasets may require a custom cluster for an additional cost.

## Custom clusters for high performance

For applications requiring consistently fast queries over large datasets and long time periods, we can provision dedicated compute resources:

- **Tailored setup**: Custom cluster sized for your specific needs.
- **Dedicated compute**: No resource sharing with other workloads.
- **Faster speeds**: Optimized for your query patterns and data volume.
- **Additional cost**: Comes with dedicated infrastructure pricing.

Contact our support team at hello@betterstack.com to discuss custom cluster options for your use case 📩

## Getting help

Generally speaking, we can make querying as fast as needed through query optimization or infrastructure scaling. If you're experiencing slow query performance:

1. Try the optimization techniques above.
2. Share a slow Live tail link with our support team using the in-app chat or at hello@betterstack.com.
3. Describe your performance requirements and use case.

**We're happy to help find the right balance of performance and cost for your needs** 🚀

![Better Stack support team](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/e690f898-5c3d-4ca0-c008-ad820bff3700/public =2170x2134)