An article contains more than one picture, so is it better to save the picture field in the database with one-to-many or with the json field?

an article contains multiple pictures. Is it better for the database to use one-to-many or json fields to save picture fields? Or is it stored in some other way?

for example, in moments, if you store it as one-to-many:

           
friend_circle     pictures
id                id
content           address  //string
                  friend_circle_id

suppose there are 9 pictures in a moments. When there are 1 million moments, the picture table has 9 million items of data. Each time you query a circle of friends, you have to go to the 9 million pictures list to go through it. Is this very inefficient?

json data can be stored above mysql5.7. If you use json to store pictures of each moments:

          
friend_circle     
id               
content 
address  //json:["1":"picture01.jpg","2":"picture02.jpg","3":"picture03.jpg"]

json storage will not traverse such a large picture table, but the parsing of json may take some time. In the case of a large amount of data, I feel that this method has more advantages, but I am not sure, so please help me analyze it. Thank you

Sep.08,2021

there is no need to store this relationship in the database, it's nice to maintain the relationship with json, and you don't have to waste database resources


you can save it anyway. However, I still suggest that when the user uploads the image, rename the image directly, and then save the id of the image to the database. After that, the client displays the picture, binds the full path of the picture, and does not have to find it in the database.

Menu