I’m trying to display a user’s shopping cart with the items listed. What I want to avoid is – 2, 3, or 10 years down the road having the query results take a long time to display results after the db_cartitems becomes 10,000 items long.
When organizing the database table structure, should I have a table listing all products previously ordered and then GROUP BY the cart token (I created a rand() number to make sure the cart items are uniquely tied to each other per cart build).
Or is there a better way? Or am I overly concerned? Are the response times fairly decent for say 50,000 ordered items listed to search through?
The merchant might get 50 or less orders a day and I fear not planning ahead for growth.
What you are asking is whether you should denormalize your data so that it queries more efficiently. I would recommend reading up a bit on the topic of database normalization to get more background on these kinds of decisions.
To answer you question, you should (generally) always start out with tables being normalized, and consider denormalizing if you start running into performance problems down the road. I would not be concerned with what will happen 2, 3 or 10 years down the road, as you probably have plenty of problems to worry about today.
Moving from a normalized model to a denormalized one is not difficult, plus you may have other options for improving performance if you run into problems (e.g. archiving old data).