I have two tables in PostgreSQL 10.12 database:
waste_card
(id (PK), user_id, manufacture_date, transfer_date, address_id, card_type, .. (and other card_specific_firelds)wastes
(user_id, waste_card_id(FK), amount, ... (other wastes_specific_fields)
I need to frequently list and count user wastes (2 separated API endpoints) by address and manufacture_date or transfer_date
Now to list items I fire 2 queries: 1- load user cards then 2- load wastes
SELECT waste_cards.*
FROM waste_cards
WHERE waste_cards.user_id = $1
AND (waste_cards.manufacture_date < '$2')
AND (waste_cards.address_id = $3)
LIMIT $4
SELECT wastes.*
FROM wastes
WHERE wastes.waste_card_id IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
To just counting amount I fire:
SELECT SUM(wastes.amount)
FROM waste_cards
LEFT OUTER JOIN wastes
ON wastes.waste_card_id = waste_cards.id
WHERE waste_cards.user_id = $1
AND (waste_cards.manufacture_date < '$2')
AND (waste_cards.address_id = $3)
Should I add [manufacture_date, transfer_date, address_id] fields to wastes to speed up queries to be like:
SELECT SUM(amount)
FROM wastes
WHERE user_id = $1
AND address_id = $2
AND manufacture_date < '$3'
I'm on very beginning with this system. Queries are less than 150 ms. Today its about 1K users with 100-200 cards each and 2-5 wastes on each card. Not much yet but we want to add other 10-15K users next month. So I want to ask if this db schema is correct or should I change it before go live on production.