A difficult problem of database design

the problem is complicated, so make the description as simple as possible

for example, a data table that stores articles is stored in mongodb

1. There are uncertain columns, such as click rate, content, title, reply, and the number of user uid, benefits, which may be more than a dozen or hundreds. It may increase or decrease, and it is dynamic.

2, the number of storage per column is uncertain, such as replying to this field, there may be millions, or there may be only one.

3. Each field needs to be able to store the version. For example, in the content field, there may be multiple versions, version 1, version 2, and version 3. The number of versions is uncertain.

this is how I designed it

1. Data sheet: data. Store the basic information of the article. For example, the adding time of the article, the release status of the article and so on.
2. Field table: field. Store specific field content, such as reply, content, title.
3. There is also a data table: version. Store the version. The details of each version are still stored in field

current problem:

1. Storage is fine. For example, I can query the reply and content of an article.
2. There is a performance problem. For example, paging, listing 25 items on a page. Each item needs to list some data to the user in the list. Such as title, content, time and so on. About 7 fields. It adds up to 257,174 queries, which took about 10 seconds.
if you solve this problem, you may need to write a copy of this data to the data table in advance. That is, anti-paradigm. For example:

field:{
    title:"",
    content:"",
}

store such redundant data in the data table.

3, the problem of search. It seems that there are still some functional limitations and performance problems.

at present, on the whole, I feel that the design is very complex, the code is very tired, and the problems emerge one after another.
I don"t know whether there is a database design problem or that mongodb is not suitable for storing such data.
if it were you, how would you design this database

Mar.17,2021

1. There are uncertain columns-> this is not necessary, the necessary columns should be relatively fixed, if not fixed, from dozens to hundreds, how do you present the data?
you are not sure which columns there are. How do you write on the front page?
really wants to have some optional fields, which you can uniformly store in one field or another table.

2. For example, in the reply field, there may be millions-> the content of the reply cannot be the same as the table where the article is saved, right? The
reply is, of course, a separate table.
when a field needs to store multiple results, the contents of this field are usually independent of another table.

3. Although mongodb does not have as many restrictions as relational databases, I think the basic design should be based on relational databases.
when you put everything in one table, it should be faster when you query in batches, but it can be troublesome when you want to find it accurately, and it can also be troublesome to write.
for example, if the content of your reply is stored in the "reply" field in the article table, each time you add a reply, you have to check out the original data first, then take out the content of the reply data, and cycle through it to see if there is any repetition.

Menu