I currently have 3 tables: locations
, products
, and stock
, which are pretty self-explanatory:
The fields of stock
are location_id
, product_id
, and quantity
, which is great... until I add a new location (or product, but less so, since the number of locations is normally quite small).
In production, we have 20k+ products, which means that if a new location is created, 200k+ records must be created in stock, which seems quite ridiculous to me. I'm sure it's not that bad in reality, but it still feels like something that should be avoided.
So, is there any better way to do this, or is this the normal best practice?
location
have to do with how manyproducts
you have?...unless you're storing every possibleproduct
andlocation
combination in thestock
table, with 0quantity
for whenever you add a newlocation
. If so, that's silly and probably not necessary. But based on the schema you've provided, as far as I'm concerned, adding a newlocation
should only result in 1 new row.INNER JOIN
returns no rows for the result when any one of the tables is missing the desired row.LEFT JOIN
will returnNULL
, which can be turned into0
. So, even if you need0
, you don't necessarily need "rows with 0s". Let's see theSELECT
if you want to discuss further.