I want to make one thing happen. Here let's say I have a lot of news. You can put likes to them. Each user, or IP can put one like. Here's how to write this in the database?

There is an idea to create a table. There are ip, user, news columns in it - the IP from which they liked, the user who liked, if the login and the likes of the likes were liked, respectively. I don’t know if this is theoretically true or not, but considering ... if, for example, there are 100 news items, and each has 100 likes, then this is already 10,000 lines in the database table. Surely because the check put the user like or not, will take quite a lot of time.

So tell me, please, how best to do?

  • your only option that comes to mind. - Ilya Chizhanov

3 answers 3

It is not necessary to look at the position of how to store and whether the database will cope, but from the position what data will be needed.

For example. Take the news. Those. over time, the news gets old and the visitor returns to the old news, information about likes is only needed by other users.

  1. From the point of view of the user who liked this news, if he opens it again - it’s enough to keep this information in cookies, over time this information is not so important, well, he will give it to him a week later, so be it.
  2. From the point of view of other users who do not need information about who exactly liked this news. Enough to keep the number of likes for the news. One news - one record.

Another example. Articles that are searched by search at any time and what was in the past century is also important and interesting, especially for closed communities. Here the table is clear (article, user), the question remains with the indices.

  1. From the point of view of the user, you need to know which articles he likes, in order to avoid re-likes (cheating, etc.). Need an index by user.
  2. From the point of view of other users, again, information about the number of likes is basically needed. And about who exactly likes - additional information that is issued on additional request. For an article, again, storing the aggregation of likes does not hurt, so as not to make select count . Well, the index in the table (article, user) for the article.

    option offhand. table, 2 columns: user id | News ID - Stores All Liked News IDs

    or: go news | Id users who liked her

    • Yeah, one million likes and write gone. With each request will be a crawl of the entire table. - lampa
    • million ... billion :) what kind of news should these be there? in high-load systems, technologies like hadoop are used .. although for simple sites and usual techniques are suitable .. - Gorets
    • @Gorets but I still need to clarify about the indexes. - lampa

    Table with two columns:

     +-------------------+ | news_id | user_id | +-------------------+ 

    On both columns by index. For unauthorized, you need to ban articles like.