This question for my pastebin application is written in PHP.
I learned a little, although I could not find a solution that fits my needs. I have a table with this structure:
+-----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+------------------+------+-----+---------+----------------+ | id | int(12) unsigned | NO | PRI | NULL | auto_increment | | author | varchar(50) | YES | | | | | authorid | int(12) unsigned | YES | | NULL | | | project | varchar(50) | YES | | | | | timestamp | int(11) unsigned | NO | | NULL | | | expire | int(11) unsigned | NO | | NULL | | | title | varchar(25) | YES | | | | | data | longtext | NO | | NULL | | | language | varchar(50) | NO | | php | | | password | varchar(60) | NO | | NULL | | | salt | varchar(5) | NO | | NULL | | | private | tinyint(1) | NO | | 0 | | | hash | varchar(12) | NO | | NULL | | | ip | varchar(50) | NO | | NULL | | | urlkey | varchar(8) | YES | MUL | | | | hits | int(11) | NO | | 0 | | +-----------+------------------+------+-----+---------+----------------+
This is for pastebine application. Basically I want the paste changes so that if you open paste # 1234, it will show all previous versions of this paste.
I thought of three ways:
Method 1
There is a revision table with id and old_id or something else, and for each ID I would insert all the old revisions, so if my structure looks like this:
rev3: 1234 rev2: 1233 rev1: 1232
The table will show the following data:
+-------+----------+ | id | old_id | +-------+----------+ | 1234 | 1233 | | 1234 | 1232 | | 1233 | 1232 | +-------+----------+
The problem I am facing is that it enters a lot of duplicate data. And the more changes, the more data, but I need to make N inserts for each new paste in the revision table, which is not very good for large N.
Method 2
I can add child_id to the insert table at the top and just update this. And then, extracting the paste, I will continue to request db for each child_id and their child_id, etc. But the problem is that too many DB reads will be introduced every time a paste with many revisions is opened.
Method 3
It also includes a separate revision table, but for the same scenario as method 1, it will store the data as follows:
+-------+-----------------+ | id | old_id | +-------+-----------------+ | 1234 | 1233,1232 | | 1233 | 1232 | +-------+-----------------+
And when someone opens paste 1234, I will use the IN clause to get all the data of the child insert.
What is the best approach? Or is there a better approach? I am using Laravel 4 framework with Eloquent ORM.
EDIT . Can I make method 1 with oneToMany relationships? I understand that I can use