I have a large table (several million rows) in a MySQL (version 5.0) database that has data inserted into it at a very frequent rate.
There are also less frequent select statements done against the database that only required the latest data (i.e. the newest 10000 rows), is there a way to create a table that would keep a copy of data from the main table and would dynamically add new rows whenever a new row was added to the main table (I can delete the old data periodically so that’s not too much of an issue) without causing much of an impact to the main table.
If possible I would also like to Index rows in the 2nd table slightly differently.
It sounds like you should use triggers.
You’ll need three triggers on the large table:
For example:
It’s fairly straightforward, though the triggers can get unwieldy if the tables have a lot of columns. You may want to consider the performance characteristics of MyISAM vs InnoDB; InnoDB may offer better overall performance depending on what sorts of queries you are running.
You don’t want to use views; views in MySQL are not materialized, so you won’t generally get any performance benefit (which is what it sounds like you want) and you won’t be able to index the view differently from the table(s) on which the view is based.