A
A
Artem00712017-04-13 00:43:33
PHP
Artem0071, 2017-04-13 00:43:33

How do you count the total number of friends in VK?

How does VK count the total number of friends so quickly?
Well, how to optimize this process?
If there is a table with users and a separate one with friends connections (user-user)
After all, then for each user, from the user, you will have to take more of his users and compare if the first user has a friend-user-user
. You will also have to do a loop in a loop and each iteration to db
How can this process be simplified?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2017-04-13
@LiguidCool

Conditionally 2 tables with friends of both users are taken and compared. The number of matches is "common" friends. What is the actual problem? Joins and IN() to help.

S
Stanislav Makarov, 2017-04-13
@Nipheris

Graph database
Such DBMSs are architecturally adapted to execute queries like "is vertex A reachable from B?" or "what vertices are reachable from A in two transitions?". See an example from the documentation for Cypher, a query language for Neo4j.

A
Alyosha, 2017-04-13
@GIBRID21

Probably just a "number of friends" variable that is updated when the list of friends is updated. And you don’t need to count friends each time you request a page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question