0% found this document useful (0 votes)
65 views6 pages

Eversql Report Query 60923672

This document provides the original and optimized SQL queries along with recommendations to optimize the query performance. The recommendations are to avoid comparing columns of different types, correlated subqueries, unnecessary columns, and subqueries. It also recommends creating several indexes on key columns to improve join performance. The optimized query applies these recommendations to improve efficiency.

Uploaded by

Muhammad Faiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Eversql Report Query 60923672

This document provides the original and optimized SQL queries along with recommendations to optimize the query performance. The recommendations are to avoid comparing columns of different types, correlated subqueries, unnecessary columns, and subqueries. It also recommends creating several indexes on key columns to improve join performance. The optimized query applies these recommendations to improve efficiency.

Uploaded by

Muhammad Faiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction

This optimization report was generated by EverSQL, your new personal and automatic DBA. The
report includes the original SQL query, the optimized SQL query, and the indexes we recommend
to create to optimize this query. Please make sure you test the recommendations for both
correctness and performance before applying any changes.

To optimize this query, we recommend to ...


Avoid Comparing Columns From Different Types - Joining or filtering using columns of
different types/lengths/signedness in the same condition may cause performance
degradation. The database will have to perform a cast for each of these values before
performing the comparison, which may also prevent the use of an index for these columns.
Make sure to alter the column types so that common comparisons will be done between two
columns of the same type.
Avoid Correlated Subqueries - A correlated subquery is a subquery that contains a
reference (column: film_id) to a table that also appears in the outer query. Usually correlated
queries can be rewritten with a join clause, which is the best practice. The database optimizer
handles joins much better than correlated subqueries. Therefore, rephrasing the query with a
join will allow the optimizer to use the most efficient execution plan for the query.
Avoid Selecting Unnecessary Columns - Avoid selecting all columns with the '*' wildcard,
unless you intend to use them all. Selecting redundant columns may result in unnecessary
performance degradation.
Avoid Subqueries - We advise against using subqueries as they are not optimized well by
the optimizer. Therefore, it's recommended to join a newly created temporary table that holds
the data, which also includes the relevant search index.
Create Optimal Indexes - Indexing recommendations are pending in the indexing tab above.
These indexes are an integral part of this optimization effort and should be created before
testing the execution duration of the optimized query.

Original Query
WITH t AS (SELECT
temp_cust_film_loc.*,
film_lang.language_id,
film_lang.`name`
FROM
(SELECT
temp_cust_film.*,
city.city,
city.city_id,
country.country_id
FROM
(SELECT
temp_film.*,
inventory.inventory_id,
rental.rental_id,
customer.customer_id,
customer.first_name,
customer.address_id,
customer.last_name,
payment.payment_id,
payment.amount,
payment.payment_date
FROM
(SELECT
film.film_id,
film.title,
category.`name`,
COUNT(film_actor.actor_id) num_of_actors
FROM
film
JOIN
film_actor
ON film.film_id = film_actor.film_id
JOIN
film_category
ON film.film_id = film_category.film_id
JOIN
category
ON category.category_id = film_category.category_id
GROUP BY
film.film_id,
category.`name`) AS temp_film,
payment,
inventory,
rental,
customer
WHERE
temp_film.film_id = inventory.film_id
AND inventory.inventory_id = rental.inventory_id
AND rental.customer_id = customer.customer_id
AND customer.customer_id = payment.customer_id) AS temp_cust_film, address, city, country
WHERE
address.address_id = temp_cust_film.address_id
AND city.city_id = address.city_id
AND country.country_id = city.country_id) AS temp_cust_film_loc,
(SELECT
film.film_id,
"language".language_id,
"language".`name`
FROM
film
JOIN
"language"
ON film.language_id = "language".language_id) AS film_lang
WHERE
temp_cust_film_loc.film_id = film_lang.film_id
ORDER BY
temp_cust_film_loc.title) SELECT
*
FROM
t

Indexing recommendations
These indexes are an integral part of this optimization effort and should be created before testing
the execution duration of the optimized query.

CREATE INDEX address_idx_address_id_city_id ON address (address_id,city_id);


CREATE INDEX category_idx_category_id ON category (category_id);
CREATE INDEX city_idx_city_id_country_id ON city (city_id,country_id);
CREATE INDEX country_idx_country_id ON country (country_id);
CREATE INDEX customer_idx_customer_id ON customer (customer_id);
CREATE INDEX film_category_idx_film_id ON film_category (film_id);
CREATE INDEX inventory_idx_film_id_inventory_id ON inventory (film_id,inventory_id);
CREATE INDEX language_idx_language_id ON language (language_id);
CREATE INDEX rental_idx_inventory_id_customer_id ON rental (inventory_id,customer_id);

Optimized Query
WITH t AS (SELECT
temp_cust_film_loc.*,
film_lang.language_id,
film_lang.`name`
FROM
(SELECT
temp_cust_film.*,
city.city,
city.city_id,
country.country_id
FROM
(SELECT
temp_film.*,
inventory.inventory_id,
rental.rental_id,
customer.customer_id,
customer.first_name,
customer.address_id,
customer.last_name,
payment.payment_id,
payment.amount,
payment.payment_date
FROM
(SELECT
film.film_id,
film.title,
category.`name`,
COUNT(film_actor.actor_id) num_of_actors
FROM
film
JOIN
film_actor
ON film.film_id = film_actor.film_id
JOIN
film_category
ON film.film_id = film_category.film_id
JOIN
category
ON category.category_id = film_category.category_id
GROUP BY
film.film_id,
category.`name`) AS temp_film,
payment,
inventory,
rental,
customer
WHERE
temp_film.film_id = inventory.film_id
AND inventory.inventory_id = rental.inventory_id
AND rental.customer_id = customer.customer_id
AND customer.customer_id = payment.customer_id) AS temp_cust_film, address, city, country
WHERE
address.address_id = temp_cust_film.address_id
AND city.city_id = address.city_id
AND country.country_id = city.country_id) AS temp_cust_film_loc,
(SELECT
film.film_id,
"language".language_id,
"language".`name`
FROM
film
JOIN
"language"
ON film.language_id = "language".language_id) AS film_lang
WHERE
temp_cust_film_loc.film_id = film_lang.film_id
ORDER BY
temp_cust_film_loc.title) SELECT
*
FROM
t

You might also like