Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize this MySQL table?

This is for an upcoming project. I have two tables - first one keeps tracks of photos, and the second one keeps track of the photo's rank

Photos:
+-------+-----------+------------------+ 
| id    | photo     | current_rank     |
+-------+-----------+------------------+ 
| 1     | apple     | 5                |
| 2     | orange    | 9                |
+-------+-----------+------------------+

The photo rank keeps changing on a regular basis, and this is the table that tracks it:

Ranks:
+-------+-----------+----------+-------------+ 
| id    | photo_id  | ranks    | timestamp   |
+-------+-----------+----------+-------------+
| 1     | 1         | 8        | *           |
| 2     | 2         | 2        | *           |
| 3     | 1         | 3        | *           |
| 4     | 1         | 7        | *           |
| 5     | 1         | 5        | *           |
| 6     | 2         | 9        | *           |
+-------+-----------+----------+-------------+ * = current timestamp

Every rank is tracked for reporting/analysis purpose. [Edit] Users will have access to the statistics on demand.

I talked to someone who has experience in this field, and he told me that storing ranks like above is the way to go. But I'm not so sure yet.

The problem here is data redundancy. There are going to be tens of thousands of photos. The photo rank changes on a hourly basis (many times- within minutes) for recent photos but less frequently for older photos. At this rate the table will have millions of records within months. And since I do not have experience in working with large databases, this makes me a little nervous.

I thought of this:

Ranks:
+-------+-----------+--------------------+
| id    | photo_id  | ranks              |
+-------+-----------+--------------------+
| 1     | 1         | 8:*,3:*,7:*,5:*    |
| 2     | 2         | 2:*,9:*            |
+-------+-----------+--------------------+ * = current timestamp

That means some extra code in PHP to split the rank/time (and sorting), but that looks OK to me.

Is this a correct way to optimize the table for performance? What would you recommend?

like image 743
Yeti Avatar asked Jun 05 '10 13:06

Yeti


1 Answers

The first one. Period.

Actually you'll lose much more. A timestamp stored in the int column will occupy only 4 bytes of space.

While the same timestamp stored in the string format will take 10 bytes.

like image 198
Your Common Sense Avatar answered Sep 23 '22 08:09

Your Common Sense