What's the efficient or best way on updating a multiple tags or categories?
Currently I have posts and posts_meta tables.
I have this structure on posts_meta table:
+----+---------+----------+------------+---------------------+---------------------+
| id | post_id | meta_key | meta_value | created_at | last_update_at |
+----+---------+----------+------------+---------------------+---------------------+
| 94 | 111 | tags | art | 2016-07-05 15:01:05 | 2016-07-05 15:01:05 |
| 95 | 111 | tags | books | 2016-07-05 15:01:05 | 2016-07-05 15:01:05 |
| 96 | 111 | tags | comics | 2016-07-05 15:01:05 | 2016-07-05 15:01:05 |
+----+---------+----------+------------+---------------------+---------------------+
Currently what I was thinking is when I update the tags on a single post, I delete the current tags and then insert the new ones.
What I think the problem on that is when I update a post without changing the tags, the function will still going to happen(deleting the current tags and inserting new ones). And I think that is not efficient.
Another solution I thought was just updating the current tags row by just changing the meta_value
.
So from:
+----+---------+----------+------------+---------------------+---------------------+
| id | post_id | meta_key | meta_value | created_at | last_update_at |
+----+---------+----------+------------+---------------------+---------------------+
| 94 | 111 | tags | art | 2016-07-05 15:01:05 | 2016-07-05 15:01:05 |
+----+---------+----------+------------+---------------------+---------------------+
to:
+----+---------+----------+------------+---------------------+---------------------+
| id | post_id | meta_key | meta_value | created_at | last_update_at |
+----+---------+----------+------------+---------------------+---------------------+
| 94 | 111 | tags | magazine | 2016-07-05 15:01:05 | 2016-07-05 15:01:05 |
+----+---------+----------+------------+---------------------+---------------------+
But another problem here is that, what if the current post have 3 tags and the update has 4 new tags or vice versa. I know we can do some workaround for that but, not sure if that's efficient or worth to do it for just a simple update.
Now I'm really not sure if this is how I should do it.
Any suggestions?
Thanks