Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary data in database, blob vs compressed base64

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?

like image 421
Tyler Liu Avatar asked Nov 21 '11 10:11

Tyler Liu


People also ask

Which is better blob or Base64?

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 .

Is Base 64 and blob same?

2 Answers. base64 encoding: A binary-to-text encoding scheme whereby an arbitrary sequence of bytes is converted to a sequence of printable ASCII characters, as described in RFC4648. binary large object (BLOB): A discrete packet of data that is stored in a database and is treated as a sequence of uninterpreted bytes.

What is the difference between binary and Base64?

Base64 image representation can be directly placed within html to render an image. Binary takes up less space. And benefits from greater network effects and standardization. E.g. if you want to use amazon simple secure storage S3 you have to store a binary file.

Which is the best way to download binary data in JSON *?

The JSON format natively doesn't support binary data. The binary data has to be escaped so that it can be placed into a string element (i.e. zero or more Unicode chars in double quotes using backslash escapes) in JSON. An obvious method to escape binary data is to use Base64.


1 Answers

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.

like image 91
Tyler Liu Avatar answered Sep 18 '22 13:09

Tyler Liu