php - How do I efficiently compare users? -
i have users can "like" categories. instance, may have 2 users:
john likes apples, oranges, pears
bob likes apples, oranges, pie, cake
they both apples, oranges
this isn't issue 2 users, when imagine scaling thousands of users, thousands of likes, there major efficiency concerns.
i need able compare user other users, , determine likes have in common.
i have tried array_intersect, not scale. need mysql solution.
how efficiently return users share same likes, , likes shared?
users +-------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | name | varchar(16) | no | | null | | +-------+-------------+------+-----+---------+----------------+ categories +-------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | name | varchar(32) | no | | null | | +-------+-------------+------+-----+---------+----------------+ likes +-------------+---------+------+-----+---------+-------+ | field | type | null | key | default | | +-------------+---------+------+-----+---------+-------+ | user_id | int(11) | no | mul | null | | | category_id | int(11) | no | mul | null | | +-------------+---------+------+-----+---------+-------+
function find_intersect($likes1, $likes2){ sort($likes1); sort($likes2); $intersect = array(); $i = 0; $j = 0; while ($i < count($likes1) , $j < count($likes2)){ if ($likes1[$i] == $likes2[$j]){ array_push($intersect, $likes1[$i]); $i++; $j++; } else if ($likes1[$i] < $likes2[$j]) $i++; else $j++; } return $intersect; }
above drummed , should efficient way of finding intersection of 2 arrays. agree @danfarrell though in mysql or database way more efficient in managing information when comes thousands of users.
Comments
Post a Comment