trino

Optimize Trino SQL queries using TD-specific time functions for partition pruning.

21|24|Updated Oct 21, 2025
One-click install
npx skills add https://github.com/treasure-data/td-skills --skill trino
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: trino
Source: https://github.com/treasure-data/td-skills/tree/main/sql-skills/trino
Command: npx skills add https://github.com/treasure-data/td-skills --skill trino

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides expert assistance for writing, analyzing, and optimizing Trino SQL queries for Treasure Data, helping users overcome performance bottlenecks, memory issues, and complex query logic. It ensures efficient data retrieval and processing within the TD environment, saving time and compute resources.

Core Features & Use Cases

  • Performance Optimization: Guides on using time-based partition pruning (TD_INTERVAL, TD_TIME_RANGE), selecting specific columns, and leveraging approximate functions for large datasets.
  • TD-Specific Functions: Explains the effective use of Treasure Data's proprietary SQL functions like TD_INTERVAL, TD_TIME_STRING, and TD_SESSIONIZE for efficient time-series analysis.
  • Query Debugging: Offers insights into common Trino query errors and provides best practices to prevent them, such as avoiding full table scans, leading to more reliable queries.
  • Use Case: A data analyst needs to extract daily active users and their event counts for the last month from a massive event log. This skill helps them write an optimized Trino query using TD_INTERVAL and APPROX_DISTINCT, ensuring the query runs quickly and efficiently without hitting memory limits.

Quick Start

Query daily unique users for the last week

SELECT TD_TIME_STRING(time, 'd!', 'JST') as date, APPROX_DISTINCT(user_id) as unique_users FROM database_name.events WHERE TD_INTERVAL(time, '-1w', 'JST') GROUP BY 1

Frequently Asked Questions about trino

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize Trino queries for faster execution on large datasets?

Query optimization in Trino relies on partition pruning using time-based filtering. Use TD_INTERVAL to restrict queries to specific time ranges, select only required columns, and leverage APPROX_DISTINCT for large cardinality sets. This reduces data scans and memory consumption, accelerating execution.

What are TD_INTERVAL and TD_TIME_RANGE functions and when should I use them?

TD_INTERVAL and TD_TIME_RANGE are Treasure Data SQL functions that filter data by time periods efficiently. TD_INTERVAL accepts relative ranges like '-1w' for the past week; TD_TIME_RANGE handles explicit date bounds. Both enable partition pruning, reducing query cost and runtime significantly.

How do I extract daily active users and event counts from time-series data?

Combine TD_TIME_STRING to format timestamps and APPROX_DISTINCT to count unique users, grouped by date. Apply TD_INTERVAL in the WHERE clause to filter the time range. This pattern efficiently aggregates user activity without full table scans.

Can I use relative time ranges like 'last week' directly in Trino queries?

Yes, TD_INTERVAL supports relative time syntax like '-1w', '-1d', or '-1mo' to filter data relative to query execution time. Specify your timezone to ensure consistent interpretation. This eliminates manual date calculation and supports dynamic time-window analysis.

What are common mistakes when using Treasure Data time functions in Trino?

Avoid using td_scheduled_time() as the first argument to td_interval; this prevents reliable partition pruning. Ensure timestamp columns have consistent timezones across your data. Use td_scheduled_time() as a reference date elsewhere in queries to enable pattern compatibility and optimization.

How do I format timestamps for display while maintaining query performance?

Use TD_TIME_STRING to convert Unix timestamps to readable formats with timezone support. Apply it in the SELECT clause after filtering with TD_INTERVAL, so partition pruning occurs before formatting. This balances readability with efficient data retrieval.