Dedicated post meta field vs count of meta fields

0 views
0

I am working a integrating a post like count to my plugin. I am having trouble understanding the logic that will impact performance. I have found this post that touches on the topic a bit.

I am keeping track of the count, the IP of the user who liked the post, and the date.

Scenerio One

+---------+---------+-------------+--------------------------------------------------------+
|  col1   |   col2  |     col3    |                           col4                         |
+---------+---------+-------------+--------------------------------------------------------+
| meta id | post_id | meta_key    | meta_value                                             |
| 33      | 294     | _like_count | array( 'count' => 25, 'date_modified' => '11-20-2017') |
| 34      | 294     | _user_ip    | 192.168.1.1                                            |
| 35      | 294     | _user_ip    | 192.168.1.21                                           |
+---------+---------+-------------+--------------------------------------------------------+

I understand that is not how wordpress stores arrays, it is just to illustrate the point

Scenario Two

+---------+---------+------------+----------------------------------------------------------------------+
|  col1   |   col2  |    col3    |                                  col4                                |
+---------+---------+------------+----------------------------------------------------------------------+
| meta id | post_id | meta_key   | meta_value                                                           |
| 33      | 294     | _post_like | array( 'ip_address' => 192.168.1.1, 'date_modified' => '11-20-2017') |
| 34      | 294     | _post_like | array( 'ip_address' => 192.168.1.2, 'date_modified' => '11-20-2017') |
| 35      | 294     | _post_like | array( 'ip_address' => 192.168.1.5, 'date_modified' => '11-20-2017') |
+---------+---------+------------+----------------------------------------------------------------------+ 

Third Scenario

+---------+---------+----------------------+--------------------------------------------------------+
|  col1   |   col2  |         col3         |                           col4                         |
+---------+---------+----------------------+--------------------------------------------------------+
| meta id | post_id | meta_key             | meta_value                                             |
| 33      | 294     | _post_like_count     | array( 'count' => 25, 'date_modified' => '11-20-2017') |
| 34      | 294     | _post_like_addresses | array( 192.168.1.2, 192.168.1.3, 192.168.1.5)          |
+---------+---------+----------------------+--------------------------------------------------------+

My question is should I hold the post count in a separate meta field, like illustrated in scenario one and just update it every time a record is added?

If I use this method, I would still need to loop through each record with the meta_key user_ip and look for the IP address of the user to make sure they haven’t like the post already.

Or should I store meta field with the key _post_like and the ip address and date as the value, then just run a query to see how many records exists with that key for that post, and while running the query also return the IP addressed?

Lastly, in scenario three I store the post count in one row then in another an array of IP addresses. I think this is the best way because I will only query the database twice for every post. But how long can the array get before its self impacts performance?

$count = get_post_meta( $post_id, "_post_like_count" ); // like count single field

$count = new WP_Query( array('_post_like' => '%') ); //query to count the number of records with the key for post X

This will ultimately be used to display the post in a feed like style where it is running within a loop, the current user if they already like the post cannot like it again thus I will always need to keep track of the IP.