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

# Trino best practices

> Best practices for optimizing query performance in Trino.

The Trino best practices page describes several ways to optimize Trino queries.

## Use `EXPLAIN` for query plans

It's useful to inspect the query plan before running heavy queries.
`EXPLAIN` shows how Trino handles joins, filters, and table scans.

```sql theme={null}
EXPLAIN SELECT * FROM employees1 WHERE salary > 5000;
```

For deeper insight, a JSON format reveals scan ranges, cost estimates, and pushdown behavior.

```sql theme={null}
EXPLAIN (FORMAT JSON)
SELECT department_id, AVG(salary)
FROM employees1
GROUP BY department_id;
```

## Push down predicates

Write filters so you can push it down into Iceberg or Hive metadata.
This reduces I/O and avoids unnecessary S3 reads.

* **Bad example**:

  ```sql theme={null}
  WHERE CAST(start_date AS DATE) = DATE '2025-01-15'
  ```

* **Good example**:

  ```sql theme={null}
  WHERE start_date = DATE '2025-01-15'
  ```

## Prune columns

Select only the required columns. This helps minimize shuffles and reduces the
amount of data pulled from S3.

* **Bad example**:

  ```sql theme={null}
  SELECT * FROM employees1;
  ```

* **Good example**:

  ```sql theme={null}
  SELECT employee_id, salary FROM employees1;
  ```

## Optimize joins

As [previously described](#join-optimization-hint), use join hints when table
sizes are predictable. Broadcast a small dimension table to speed up the join,
and apply partitioning for large fact-to-dimension joins.

* **Bad example**:

  ```sql theme={null}
  SELECT /*+ PARTITIONED(f, dim) */ ...
  ```

* **Good example**:

  ```sql theme={null}
  SELECT /*+ BROADCAST(dim) */ ...
  ```

## Limit large scans

Apply filters and `LIMIT` clauses during early data exploration to avoid
scanning entire Iceberg tables.

* **Bad example**:

  ```sql theme={null}
  SELECT * FROM employees1;
  ```

* **Good example**:

  ```sql theme={null}
  SELECT *
  FROM employees1
  WHERE salary > 3000
  LIMIT 100;
  ```

## Pre-compute aggregations with CTAS or materialized views

For dashboards or recurring workloads, pre-compute heavy aggregations using
Create Table as Select (CTAS) or materialized views. This reduces the load on
Trino and improves Superset's responsiveness.

```sql theme={null}
CREATE TABLE analytics.employee_salary_by_department AS
SELECT department_id,
       COUNT(*) AS employee_count,
       SUM(salary) AS total_salary
FROM employees1
GROUP BY department_id;
```

## Additional resources

* To get an overview of Trino, refer to the [Trino in NexusOne](./trino-in-nx1) page.
* To learn practical ways to use Trino in the NexusOne environment, refer to the
  [Trino hands-on examples](./trino-hands-on-examples) page.
* For more details about Trino, refer to the [Trino](https://trino.io/docs/current/) official documentation.
* If you are using the NexusOne portal and want to learn how to launch Trino, refer to the
  [Launch a hosted app](/documentation/platform/tasks/launch-a-hosted-app) page.
