数据库中的二进制数据,blob 与压缩的 base64 [英] binary data in database, blob vs compressed base64

查看:18
本文介绍了数据库中的二进制数据,blob 与压缩的 base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库中有一个名为blob的列类型,用于存储二进制数据.

There is a column type named blob in database, and it is used to store binary data.

但我经常看到的解决方案是压缩二进制数据,然后将二进制数据转换为 base64,并将 base64 字符串作为 varchar 或文本存储在数据库中.

But more often than not, I see solutions which compress binary data, then convert binary data to base64, and store base64 string as varchar or text in database.

Python 代码示例:

Python code example:

import zlib, base64
base64_str = base64.b64encode(zlib.compress(binary_data, 9))

所以有两种方法可以将二进制数据存入数据库:

So there are two ways to store binary data into database:

  1. 作为blob
  2. 作为压缩的 base64

我的问题是:哪种方式更好,为什么?

My Questions is: Which way is better and why?

推荐答案

看来我得回答我自己的问题了.大多数时候,将压缩的 base64 存储到数据库中并不是一个好主意.它比存储 blob 更复杂.而且大多数时候二进制小于base64字符串.

It seems that I have to answer my own question. Most of the time, storing compressed base64 into database is not a good idea. It is more complex than storing blob. And most of the time binary is smaller than base64 string.

我只发现压缩 base64 有用的一种情况:您无法更改表架构,并且只有文本列,因此您必须将二进制数据存储到该表中.唯一可能的方法是将二进制转换为 base64 字符串.

I only find one case that compressed base64 is useful: you can't alter the table schema, and there are only text columns, thus you have to store binary data into that table. The only possible way is to convert binary to base64 string.

这篇关于数据库中的二进制数据,blob 与压缩的 base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆