Database Denormalisation
Add redundant data to the database to optimise queries and remove some joins.
For example, you have
- Posts table
- Comments table
For each post, you want to display all the list post number of associated comments. Doing this will need at least 2 query
- One query to list the post
- One query per post to count how many comments
- ...
Which means several queries at once. Now what we can do is to add another number of comments
field in the Posts
table
Doing this you only need 1 query to list the post, and don't need to touch the Comments
table as number of commenrts
are already de-normalised to the Posts
table
Now there are some costs
- Disk and memory as store the redundant data
- Each time add and remove a comment, have to
- save/delete the comment
- update the corresponding number in the
Posts
table
Note: this is good if your blog has a lot more people reading than writing comments (not so bad)