Good strategy for copying a "sliding window" of data from a table?

Posted by chiborg on Stack Overflow See other posts from Stack Overflow or by chiborg
Published on 2010-05-27T09:58:56Z Indexed on 2010/05/27 10:01 UTC
Read the original article Hit count: 280

Filed under:
|
|
|
|

I have a MySQL table from a third-party application that has millions of rows and only one index - the timestamp of each entry. Now I want to do some heavy self-joins and queries on the data using fields other than the timestamp. Doing the query on the original table would bring the database to a crawl, adding indexes to the table is not an option. Additionally, I only need entries that are newer than one week.

My current strategy for doing the queries efficiently is to use a separate table (aux_table) that has the necessary indexes. My questions are: Is there another way to do the queries? and if not, How do I update the data in the indexed table efficiently?

So far I have found two approaches for updating aux_table:

  1. Truncate aux_table and insert the desired data from the original table. Not very efficient because all the indexes must be re-crated.
  2. Check for the biggest timestamp in aux_table and insert all entries with a greater or equal timestamp from the original table. Occasionally drop older entries. Only copying entries with greater timestamp leads to dropped entries (because of entries with same timestamp that were inserted into the original table after the last update).

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql