Add query optimization scripts and results

This commit is contained in:
2024-11-17 14:47:57 +01:00
parent aac9358de5
commit 05e2863bfd
4 changed files with 83 additions and 0 deletions

9
sql/cancel_order.sql Normal file
View File

@@ -0,0 +1,9 @@
-- query optimization - task 3
-- noqa: disable=CP03
create procedure CancelOrder (in TargetOrderID int)
begin
delete from Orders
where OrderID = TargetOrderID;
select concat("Order ", TargetOrderID, if(row_count() = 1, " is cancelled", " doesn't exist")) as Confirmation;
end

12
sql/get_max_quantity.sql Normal file
View File

@@ -0,0 +1,12 @@
-- query optimization - task 1
-- changes:
-- interpreted quantity as the number of items in an order, because I assumed that Little lemon wants
-- orders à la carte instead of fixed menus
create procedure GetMaxQuantity () -- noqa: disable=CP03
select count(OI.OrderItemID) as MaxQuantityInOrder
from MenuItems as MI
inner join OrderItems as OI on MI.MenuItemID = OI.MenuItemID
group by OI.OrderID
order by MaxQuantityInOrder desc
limit 1

15
sql/get_order_detail.sql Normal file
View File

@@ -0,0 +1,15 @@
-- query optimization - task 2
-- changes:
-- interpreted quantity as the number of items in an order, because I assumed that Little lemon wants
-- orders à la carte instead of fixed menus
prepare GetOrderDetail from "
select
O.OrderID,
count(OI.MenuItemID) as Quantity,
O.BillAmount as Cost
from Orders as O
inner join OrderItems as OI on O.OrderID = OI.OrderID
inner join Bookings as B on O.BookingID = B.BookingID
where B.CustomerID = ?;
";

View File

@@ -0,0 +1,47 @@
# Task 1
> delimiter $
> source ./get_max_quantity.sql
> delimiter ;
> call GetMaxQuantity();
+--------------------+
| MaxQuantityInOrder |
+--------------------+
| 15 |
+--------------------+
# Task 2
> source ./get_order_detail.sql
> set @customerID = 1
> execute GetOrderDetail using @customerID
+---------+----------+-------+
| OrderID | Quantity | Cost |
+---------+----------+-------+
| 4 | 3 | 29.00 |
+---------+----------+-------+
# Task 3
> delimiter $
> source ./cancel_order.sql
> delimiter ;
> call CancelOrder(2)
+----------------------+
| Confirmation |
+----------------------+
| Order 2 is cancelled |
+----------------------+
> select * from `Orders`
+---------+---------------------+---------------------+------------+-----------+
| OrderID | Started | Finished | BillAmount | BookingID |
+---------+---------------------+---------------------+------------+-----------+
| 1 | 2024-11-14 18:05:00 | 2023-10-01 19:25:00 | 82.50 | 1 |
| 3 | 2024-11-14 19:10:00 | 2023-10-03 20:30:00 | 31.00 | 3 |
| 4 | 2024-11-14 19:35:00 | 2023-10-04 20:50:00 | 29.00 | 4 |
| 5 | 2024-11-14 19:40:00 | 2023-10-05 20:55:00 | 20.00 | 5 |
| 6 | 2024-11-14 19:40:00 | 2023-10-06 20:45:00 | 150.50 | 6 |
+---------+---------------------+---------------------+------------+-----------+
> call CancelOrder(2)
+-----------------------+
| Confirmation |
+-----------------------+
| Order 2 doesn't exist |
+-----------------------+