MySQL 将图像直接存储在数据库中还是作为 base64 数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9722603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:33:41  来源:igfitidea点击:

Storing image in database directly or as base64 data?

mysqldatabaseimageimagemagickbase64

提问by Googlebot

The common method to store images in a database is to convert the image to base64data before storing the data. This process will increase the size by 33%. Alternatively it is possible to directly store the image as a BLOB; for example:

在数据库中存储图像的常用方法是base64在存储数据之前将图像转换为数据。此过程将使大小增加 33%。或者,可以直接将图像存储为BLOB; 例如:

$image = new Imagick("image.jpg");
$data = $image->getImageBlob();
$data = $mysqli->real_escape_string($data);
$mysqli->query("INSERT INTO images (data) VALUES ('$data')");

and then display the image with

然后显示图像

<img src="data:image/jpeg;base64,' .  base64_encode($data)  . '" />

With the latter method, we save 1/3 storage space. Why is it more common to store images as base64in MySQL databases?

使用后一种方法,我们节省了 1/3 的存储空间。为什么像base64在 MySQL 数据库中那样存储图像更常见?

UPDATE:There are many debates about advantages and disadvantages of storing images in databases, and most people believe it is not a practical approach. Anyway, here I assume we store image in database, and discussing the best method to do so.

更新:关于在数据库中存储图像的优缺点有很多争论,大多数人认为这不是一种实用的方法。无论如何,在这里我假设我们将图像存储在数据库中,并讨论这样做的最佳方法。

回答by Marcus Adams

I contend that images (files) are NOT usually stored in a database base64 encoded. Instead, they are stored in their raw binary form in a binary (blob) column (or file).

我认为图像(文件)通常不存储在 base64 编码的数据库中。相反,它们以原始二进制形式存储在二进制(blob)列(或文件)中。

Base64 is only used as a transport mechanism, not for storage. For example, you can embed a base64 encoded image into an XML document or an email message.

Base64 仅用作传输机制,不用于存储。例如,您可以将 base64 编码的图像嵌入到 XML 文档或电子邮件消息中。

Base64 is also stream friendly. You can encode and decode on the fly (without knowing the total size of the data).

Base64 也是流友好的。您可以即时编码和解码(无需知道数据的总大小)。

While base64 is fine for transport, do not store your images base64 encoded.

虽然 base64 适合传输,但不要存储 base64 编码的图像

Base64 provides no checksum or anything of any value for storage.

Base64 不提供校验和或任何存储值。

Base64 encoding increases the storage requirement by 33% over a raw binary format. It also increases the amount of data that must be read from persistent storage, which is still generally the largest bottleneck in computing. It's generally faster to read less bytes and encode them on the fly. Only if your system is CPU bound instead of IO bound, and you're regularly outputting the image in base64, then consider storing in base64.

Base64 编码比原始二进制格式增加了 33% 的存储要求。它还增加了必须从持久存储读取的数据量,这通常仍然是计算中的最大瓶颈。读取更少的字节并即时对其进行编码通常会更快。仅当您的系统受 CPU 限制而不是 IO 限制,并且您经常以 base64 格式输出图像时,才考虑存储在 base64 中。

Inline images (base64 encoded images embedded in HTML) are a bottleneck themselves--you're sending 33% more data over the wire, and doing it serially (the web browser has to wait on the inline images before it can finish downloading the page HTML).

内联图像(嵌入在 HTML 中的 base64 编码图像)本身就是一个瓶颈——您通过线路发送的数据增加了 33%,并且是连续发送的(Web 浏览器必须等待内联图像才能完成页面下载HTML)。

If you still wish to store images base64 encoded, please, whatever you do, make sure you don't store base64 encoded data in a UTF8 column then index it.

如果您仍然希望存储 base64 编码的图像,无论您做什么,请确保不要将 base64 编码的数据存储在 UTF8 列中,然后对其进行索引。

回答by user1252434

  • Pro base64:the encoded representation you handle is a pretty safe string. It contains neither control chars nor quotes. The latter point helps against SQL injection attempts. I wouldn't expect any problem to just add the value to a "hand coded" SQL query string.

  • Pro BLOB:the database manager software knows what type of data it has to expect. It can optimize for that. If you'd store base64 in a TEXT field it might try to build some index or other data structure for it, which would be really nice and useful for "real" text data but pointless and a waste of time and space for image data. And it is the smaller, as in number of bytes, representation.

  • Pro base64:您处理的编码表示是一个非常安全的字符串。它既不包含控制字符也不包含引号。后一点有助于防止 SQL 注入尝试。我不希望将值添加到“手工编码”SQL 查询字符串中会有任何问题。

  • Pro BLOB:数据库管理器软件知道它需要什么类型的数据。它可以为此进行优化。如果您将 base64 存储在 TEXT 字段中,它可能会尝试为其构建一些索引或其他数据结构,这对于“真实”文本数据来说非常好且有用,但对于图像数据来说毫无意义且浪费时间和空间。它是较小的表示,如字节数。

回答by msmfsd

I recommend looking at modern databases like NoSQL and also I agree with user1252434's post. For instance I am storing a few < 500kb PNGs as base64 on my Mongo db with binary set to true with no performance hit at all. Mongo can be used to store large files like 10MB videos and that can offer huge time saving advantages in metadata searches for those videos, see storing large objects and files in mongodb.

我建议查看像 NoSQL 这样的现代数据库,并且我同意user1252434的帖子。例如,我将一些 < 500kb PNG 作为 base64 存储在我的 Mongo db 上,二进制设置为 true,根本没有性能损失。Mongo 可用于存储像 10MB 视频这样的大文件,并且可以在这些视频的元数据搜索中提供巨大的节省时间优势,请参阅在 mongodb 中存储大对象和文件