🚀 How To: Optimize a SQL Query
Optimizing a SQL query in AI Query, means rewriting the SQL query in a way to help guide the query optimizer for the database. SQL query rewriting can involve breaking up complex SQL queries into smaller, simpler ones, or using subqueries to improve performance. Let's take a look at how to generate an optimized SQL query.
Steps
On the AI Query Dashboard, navigate to Query Playground
and
select the Database
.
For example purposes we will use the Demo
database.
Select Optimize Query
as the Query Type
for the Query Prompt
.
Below is the SQL Query
we want to use to generate an optimized SQL Query
:
SELECT Item, COUNT(Item) AS Count
FROM Orders
INNER JOIN Customers
ON Orders.Customer_id = Customers.Customer_id AND Customers.Country = 'USA'
GROUP BY Item
ORDER BY Count DESC
LIMIT 1;
Now paste the above SQL Query
and click ▶
to generate an optimized SQL Query
.
SELECT Item, COUNT(Item) AS Count
FROM Orders
INNER JOIN Customers
ON Orders.CustomerNumber = Customers.CustomerNumber
WHERE Customers.Country = 'USA'
GROUP BY Item
ORDER BY Count DESC
LIMIT 1;
You'll see that you get an optimized SQL Query
generated based on the SQL Query
you specified! 🫡