How to efficiently maintain daily stock for more than 1000 products in MySQL?

250 views Asked by At

I'm having 1000+ Products. I need to maintain the stock on the daily basis.

Product Table Schema

CREATE TABLE Product (
    ProductId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Name VARCHAR(30) NOT NULL,
)

Stock Table Schema

CREATE TABLE StockInfo (
    StockInfoId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    StockDate Date NOT NULL,
    ProductId INT NOT NULL,
    OpeningStock INT NOT NULL,
    Purchase INT DEFAULT 0,
    Sales INT DEFAULT 0,
    ClosingStock INT DEFAULT 0
)

Each day the table StockInfo gets updated. The column OpeningStock is updated by cron jobs, the logic is:

last days ClosingStock = OpeningStock + Purchase - Sales

Todays OpeningStock = last day ClosingStock

This approach consumes larger rows and hence the performance is impacted.

Note: Every day each and every product has at least one sale and purchase

Kindly assist me to redesign this logic.

0

There are 0 answers