简体   繁体   English

MySQL数据库设计-存储图像-单个表或多个表

[英]MySQL Database design - Storing Images - Single table or multiple tables

I am currently working in a product where different types of images like product images, user profile pictures, logo etc. are there. 我目前正在使用一种产品,其中有不同类型的图像,例如产品图像,用户个人资料图片,徽标等。 I need a database with good query performance. 我需要一个具有良好查询性能的数据库。

I got two DB designs in mind. 我想到了两个数据库设计。

OPTION 1. - Storing all images in a single table with id,title, url_full, url_thumb , status and timestamp field 选项1.-将所有图像存储在具有id,title,url_full,url_thumb,status和timestamp字段的单个表中

Advantages 好处

  1. I can use single ImageModel file to insert delete / update data. 我可以使用单个ImageModel文件插入删除/更新数据。 So there will be no multiple logic for image storage. 因此,图像存储将没有多重逻辑。 It is just a single logic, "storing in a single table". 这只是一个逻辑,“存储在单个表中”。 So whenever image has to be saved, I can call the method of ImageModel 因此,每当必须保存图像时,我都可以调用ImageModel的方法

Disadvantages 缺点

  1. If there are lot of product images and less user images, the user image querying will become slow due to the huge number of products. 如果产品图像很多而用户图像较少,则由于产品数量巨大,用户图像查询将变慢。

OPTION 2. - Storing different type of images in different tables with id,title, url_full, url_thumb , status and timestamp field 选项2-在具有id,title,url_full,url_thumb,status和timestamp字段的不同表中存储不同类型的图像

Advantages 好处

  1. Increased number of records in one section won't affect the query speed of other 一个部分中增加的记录数不会影响另一部分的查询速度

Disadvantages 缺点

  1. Has to write separate model files / functions for each image type. 必须为每种图像类型编写单独的模型文件/功能。
  2. Whenever image has to be stored, type needs to be specified. 每当必须存储图像时,都需要指定类型。

My question is , which is the better approach. 我的问题是,这是更好的方法。 Is the advantages and disadvantages a real concern.Also if there are any other advantages / disadvantages, please list. 优点和缺点是真正的问题。如果还有其他优点/缺点,请列出。 Or if there are any other god db designs, please suggest. 或者,如果还有其他上帝数据库设计,请提出建议。

Please answer based on the practical scenario where there are lots of products and users. 请根据实际情况回答,那里有很多产品和用户。

This began as a long comment so I decided to post it as an answer. 这是从很长的评论开始的,所以我决定将其发布为答案。 Storing different types of images in different tables sounds like a bad idea to me. 对我来说,在不同的表中存储不同类型的图像听起来是个坏主意。 For one thing, how will that design scale if, for example, new types of categories appear later? 一方面,例如,如果以后出现新类型的类别,该设计将如何扩展? Would you then be able to cope with adding an arbitrary number of new image tables? 这样一来,您就能应付任意数量的新图像表了吗? Also, querying all images would require either a series of joins or unions, which could be costly. 同样,查询所有图像将需要一系列的联接或联合,这可能会很昂贵。

You mentioned the following advantage to a multi image table schema: 您提到了多图像表模式的以下优点:

Increased number of records in one section won't affect the query speed of other 一个部分中增加的记录数不会影响另一部分的查询速度

If you use a single image table with an index on the type column, then increasing the number of records of one type won't necessarily increase querying for images of a second type. 如果将单个图像表与在type列上的索引一起使用,则增加一种类型的记录数不一定会增加对第二种类型的图像的查询。 And here is a disadvantage to a single image table which you gave: 这是您提供的单个图像表的缺点:

If there are lot of product images and less user images, the user image querying will become slow due to the huge number of products. 如果产品图像很多而用户图像较少,则由于产品数量巨大,用户图像查询将变慢。

It is true that adding more records will generally slow down querying. 的确,添加更多记录通常会降低查询速度。 However, having an appropriate index on the type should greatly diminish this problem. 但是,在类型上具有适当的索引将大大减少此问题。

A single image table with appropriate indices seems much better. 具有适当索引的单个图像表似乎要好得多。

How will you deliver the images? 您将如何交付图像? If it is via <img src=http:...> then there is only one answer: separate files. 如果通过<img src=http:...>进行,则只有一个答案:单独的文件。

Yes, thumbnails can be done by other mechanisms, such as converting to base64 and including in the img tag. 是的,缩略图可以通过其他机制完成,例如转换为base64并包含在img标签中。 This might be best if you have a lot of thumbnails on the page. 如果页面上有很多缩略图,这可能是最好的。

But having the thumbnails in the same table as the columns you query on could be bad for performance. 但是将缩略图与要查询的列放在同一表中可能会降低性能。 So we need to see the queries and the table schema (as it stands so far). 因此,我们需要查看查询和表架构(到目前为止)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM