Suppose I am building a website for an online store and have a database of products. All these products have their own pages, with their own respective URLs.
If the URL can always be derived from other data in the database (let’s pretend every product can be found at http://example.com/%5B PRODUCT.CATEGORY ]?prod=[ SHA1(PRODUCT.NAME) ]), would it be wise to store the URL in another field of the table?
On one hand, it is duplicate data (it can easily be derived from other existing fields in the table). On the other hand, these URLs are used everywhere and it seems wasteful re-deriving them every single time they are needed.
Is there a best-practice for this scenario?
You should not store ANY urls in the database.
You should create the urls at runtime based on the data you are looking for.
The page that displays the product can ignore any fields it doestn’ need.
You should also use an ID field not product.name. Unless you are trying to hide certain products from users, there is no reason to sha1 the key.
Remember using the primary key to lookup a record is the fastest way possible to get that record.