MySQL的 - 的Base64 VS BLOB [英] MySQL - Base64 vs BLOB
问题描述
为了简单起见,假设我开发移动应用Instagram的一样。用户可以从服务器下载图片,并上传自己的形象。目前,服务器存储在MySQL数据库为BLOB所有图像(在现实中,只是小缩略图)。看来传送图像时最常用的方法是使用Base64编码,这给我留下了两个选项:
For the sake of simplicity, suppose I'm developing a mobile app like Instagram. Users can download images from the server, and upload images of their own. Currently the server stores all images (in reality, just small thumbnails) in a MySQL database as BLOBs. It seems that the most common way to transfer images is by using Base64 encoding, which leaves me with two options:
- 服务器存储所有图片为BLOB。要上传图像,连接客户端codeS入Base64编码字符串,然后将其发送到服务器。服务器德codeS图像返回成二进制格式,并将其存储在数据库中的BLOB。当客户端请求的图像,服务器重新连接codeS图像为Base64字符串,并将其发送给客户,谁再德codeS回二进制文件显示。
- 服务器存储所有图像为Base64字符串。要上传图像,连接客户端codeS入Base64编码字符串,并将其发送给服务器。服务器确实没有编码或解码,而只是存储在数据库中的字符串。当客户端请求的图像,为Base64字符串返回给客户端,那么谁去codeS它的显示。
显然,选项#1需要在服务器上显著更多的处理,图像必须是EN codeD /德codeD与每一个请求。这使我对选项#2瘦,但一些研究已经表明,存储Base64编码字符串MySQL是不是直接存储图像的BLOB效率要低得多,一般气馁。
Clearly, option #1 requires significantly more processing on the server, as images must be encoded/decoded with every single request. This makes me lean toward option #2, but some research has suggested that storing Base64 string in MySQL is much less efficient than storing the image directly as BLOB, and is generally discouraged.
我肯定不会遇到这种情况的第一人,所以没有任何人有使这项工作的最佳方法建议?
I'm certainly not the first person to encounter this situation, so does anybody have suggestions on the best way to make this work?
推荐答案
JSON假设UTF8,因此与图像不兼容,除非它们是连接在某种程度上codeD。
JSON assumes utf8, hence is incompatible with images unless they are encoded in some way.
Base64是几乎完全8/6倍,体积大二进制(BLOB)。有人可能会说,这是很容易负担得起。 3000 字节
变成约4000 字节
。
Base64 is almost exactly 8/6 times as bulky as binary (BLOB). One could argue that it is easily affordable. 3000 bytes
becomes about 4000 bytes
.
每个人的应的能够接受任意8位codeS,但不是每个人都这样。基本64可以是用于不具有处理8位数据的最简单的和整体最佳折衷。
Everyone should be able to accept arbitrary 8-bit codes, but not everybody does. Base-64 may be the simplest and overall best compromise for not having to deal with 8-bit data.
由于这些是小,我想将它们存储在一个表中,而不是一个文件。我会,但是,它们存储在一个单独的表和加入
通过适当的 ID
当你需要他们。这使得不需要以运行图像更快,因为它们不跨过所述BLOB的查询。
Since these are "small", I would store them in a table, not a file. I would, however, store them in a separate table and JOIN
by an appropriate id
when you need them. This allows queries that don't need the image to run faster because they are not stepping over the BLOBs.
从技术上说,文本字符集ASCII COLLATE ascii_bin
会做,但 BLOB
使得它更加清晰,有没有真正任何可用的文本在列。
Technically, TEXT CHARACTER SET ascii COLLATE ascii_bin
would do, but BLOB
makes it clearer that there is not really any usable text in the column.
这篇关于MySQL的 - 的Base64 VS BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!