binary data in database, blob vs compressed base64

狂风中的少年 提交于 2019-12-30 04:01:24

问题


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

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 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. as blob
  2. as compressed base64

My Questions is: Which way is better and why?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/8210293/binary-data-in-database-blob-vs-compressed-base64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!