I have 2 tables:
Order
id: Serial
date: timestamp
item: varchar(255)
user_id: UNSIGNED INT References to users
Users:
id: UNSIGNED INT
email: varchar(255)
type: ENUM(CUSTOMER,ADMIN)
And I want to fetch for a whole year of 2022 the customer orders:
select
orders.id,
orders.item,
orders.date,
customer.name,
from
orders join users on orders.user_id = users.user_id
where
users.type = 'CUSTOMER' and date_part('year',orders.date) = 2022
But an alternate approach is to use subqueries:
select
orders1.id,
orders1.item,
orders1.date,
customer.name,
from
(select * from orders where date_part('year',order.date) = 2022) as orders1
join (select * from users where users.type = 'CUSTOMER') as customers
on orders1.user_id = customers.user_id
But when is recomended the first approach and when is recomended the second one? What I want to achieve is faster data retrieval from the database upon the internet. Does table size is a parameter that I need to consider as well when I write select queries?