The given SQL statement is using a CTE (Common Table Expression) to find customers who rented in the last month but not in the current month. Here's the formatted SQL statement:
WITH
last_month_rentals AS (
SELECT DISTINCT
customer_id
FROM
rental
WHERE
rental_date >= date_trunc('month', current_date - interval '1 month')
AND rental_date < date_trunc('month', current_date)
),
this_month_rentals AS (
SELECT DISTINCT
customer_id
FROM
rental
WHERE
rental_date >= date_trunc('month', current_date)
AND rental_date < date_trunc('month', current_date + interval '1 month')
)
SELECT
c.*
FROM
customer c
JOIN last_month_rentals lmr ON c.customer_id = lmr.customer_id
LEFT JOIN this_month_rentals tmr ON c.customer_id = tmr.customer_id
WHERE
tmr.customer_id IS NULL;
Here are some suggestions to optimize the SQL statement for a Postgres database:
EXISTS
and NOT EXISTS
instead of JOIN
and LEFT JOIN
:SELECT
c.*
FROM
customer c
WHERE
EXISTS (
SELECT 1
FROM rental
WHERE
customer_id = c.customer_id
AND rental_date >= date_trunc('month', current_date - interval '1 month')
AND rental_date < date_trunc('month', current_date)
)
AND NOT EXISTS (
SELECT 1
FROM rental
WHERE
customer_id = c.customer_id
AND rental_date >= date_trunc('month', current_date)
AND rental_date < date_trunc('month', current_date + interval '1 month')
);
rental
table for the customer_id
and rental_date
columns to speed up the query:CREATE INDEX idx_rental_customer_id_rental_date ON rental (customer_id, rental_date);
rental
and customer
tables to ensure the query planner has up-to-date statistics:ANALYZE rental;
ANALYZE customer;
These optimizations should help improve the performance of the SQL statement on a Postgres database.