Disclaimer: I am newbie in database world
I wonder: How do you solve the problem of an always-growing table?
I mean, suppose I would like to know “the last added item”, then I do
SELECT * FROM planetHistory WHERE name=”earth” ORDER BY date DESC
LIMIT 1;
That seems to be a bad idea because it would take more and more time as table grows.
What come to my mind is to “prepare” the design of database for the usual future queries. For this example the database could include a table with a single field and a instance:
+-----------------+
|table "lastAdded"|
+-----------------+
| 3242 |
+-----------------+
That would store the last added, so before doing the insert I should read that field increment it by one and then write it.
It sound weird, but it seems worse for me to order 1 Terabyte of data “just to know which is the last” that’s weirder
Ordering by an indexed column does this kind of optimization ‘under the hood’ for you.
Let the DB designers worry about optimizing query logic, just make sure you utilize the tools they give you like INDEXes. It is their job to ensure queries, even over millions of records, are performant enough.
Doing this kind of ‘optimization’ yourself may cause several other problems:
Monitor your app/DB so that you can pro-actively address performance problems but don’t solve them before you know there is a problem. Especially when it comes to DBs, they are built to be as fast as possible; when they aren’t fast, it is usually because of something we do which is silly.