Skip to main content

Optimize Queries

When adding metrics or validated queries, clean SQL helps Qluent understand your intent. The optimize feature rewrites messy SQL into a clear, readable format—while keeping the same results.

Use case

Use optimize when:

  • You're saving a query from a failed query as a new metric or example
  • You have working SQL but it's hard to read
  • You want Qluent to better understand the query's structure

What it does

  • Reformats SQL into CTEs for readability
  • Adds comments explaining what each part does
  • Improves table aliases (t1customers)
  • Verifies the optimized query returns the same data

Example

Before:

SELECT t1.name,sum(t2.amt) FROM customers t1 JOIN orders t2 ON t1.id=t2.cust_id WHERE t2.status='completed' GROUP BY t1.name ORDER BY sum(t2.amt) DESC

After:

-- Total order amount per customer
WITH customer_orders AS (
SELECT
customers.name AS customer_name,
orders.amt AS order_amount
FROM customers
JOIN orders ON customers.id = orders.cust_id
WHERE orders.status = 'completed'
)
SELECT
customer_name,
SUM(order_amount) AS total_amount
FROM customer_orders
GROUP BY customer_name
ORDER BY total_amount DESC

How to use

When creating or editing metrics/validated queries:

  1. Write or paste your SQL
  2. Click Optimize
  3. Review the optimized version
  4. Accept or keep original

Not all queries can be optimized. Include an ORDER BY clause so results can be compared between original and optimized versions.