P
P
Pavel2015-03-02 21:36:08
Database design
Pavel, 2015-03-02 21:36:08

What is an efficient way to implement a user's favorites/cart in a database?

There is a user table that has a favorites field. (for example, articles in favorites)
Articles are stored conditionally in the articles table (id, name, desctiption, date, etc.)
The task is to implement an individual list of Favorites for the user, that is, each user should have a Favorites section whose are stored in the database. We need an efficient way.
If you understand what I'm talking about, you don't have to read further)
I gave below 3 options for solving the problem, but I don't like them. I'm not looking for a discussion of the ways below (although welcome), but rather the proven best practices (or just good) of your solution to the problem, or provide a direction for researching the issue.
I must say right away that the last method seems to be the most effective. The second method is very attractive, but it gives a small spike (due to the use of table names as data). And the first one is very inefficient (in my opinion) and is written "for show".
So far, there are 3 ways to implement this idea:
1. make a favorite table that will contain all the necessary data of the Favorite (name, description, picture or, conditionally, just the id of the desired entry in the articles table) + nickname of the user who added it to favorites. The result is a complete mess - the table will contain all the favorite articles of all users, and the selection will be made by (probably long) parsing by the user's nickname.
2. user table: the favorite field contains the name of the personal table Favorite for the user (random, for example, 8hafhq2ij). To get the data, you will have to get the table name and then all its data (8hafhq2ij).
3. in the user table: in the favorite field, simply write down the id of the desired articles in the format "1 2 18 21 212", and then just pull it out and throw it into an array, which can be used to get specific articles by their id in the articles table.
PS: the number of users is assumed to be from 10k with the active use of the add function.
I have an understanding of database theory, but in practice there was nothing more serious than an "electronic journal"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Optimus, 2015-03-02
@ProgramCodePav

So can a separate favorites table with the fields id, id_articles, id_user Selection
: select all id_articles where id_user = 13
For example, the basket can be done by transferring articles to a separate table with the old id saved, or you can simply hide them by entering the "visibility" field 1 and 0. If we clean the basket - all articles where vision=0 are deleted from the database for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question