BLOBs in MySQL: Binary Laughable OBjects?!

Submitted by gwolf on Thu, 09/13/2007 - 08:00
Bah, MySQL keeps insisting on being a fun way to waste your time.
One of my clients hosts its systems at Dreamhost, a quite nice hosting company, which has... Well, a couple of strange details :-/
Anyway, people who work with databases know that a BLOB (or its equivalent) is the right datatype for storing images or, in general, files, right? After all, BLOB is just an acronym for Binary Large OBject. And if somebody has a BLOB field in the DB and searches on it, most RDBMS-bound programmers will at very least chuckle at a design flaw - BLOBs are just to be stored and retrieved, that's it.
Well, the system I wrote for this client uses a BLOB field - Ok, to be clear: I just declared it as a :binary in the corresponding migration. That should do the trick, and should allow me to keep my system RDBMS-independent, right? So I can still work on my development system using nice-and-trusty PostgreSQL
Anyway... My client said some documents were being corrupted. And that is Not NiceTM... Documents were being truncated at 63Kb. The bug was consistent across the three instances of my system in Dreamhost, but could not be reproduced at my machines - It didn't take too long to find out it must be the DB (and that's partly because I don't really trust MySQL :) ).
Turns out that, in MySQL speak, there are four types of blobs: TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. Forgoodnessake, WHY?! The stupidiest one is the tinyblob: A Binary Large OBject limited to 8 bits (255 characters). Regular blobs are 16 bit long (65535 characters - Aha! That's your 63.99Kb limit!). Mediumblobs are 24 bits (16 million), and longblobs are 32 bits (4000 million). And, of course, the blob datatype is not large at all.
Bah.
Anyway... At least this is an open and known issue already to the Rails people, and I do expect them to change the migration equivalencies to something saner.
Bah again.
( categories: )
Sam Morris's picture

MySQL: laughable database

Nice to see MySQL continuing the fine tradition of truncating your data rather than doing the right thing and throwing an error back to the application...
garaged's picture

Re: BLOBs in MySQL: Binary Laughable OBjects?!

from mysql manual :) "If strict SQL mode is not enabled and you assign a value to a BLOB or TEXT column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. For truncation of non-space characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode. See Section 5.2.6, «SQL Modes»."
Gunnar's picture

Re: Re: BLOBs in MySQL: Binary Laughable OBjects?!

Yes, very nice - but why isn't this the default? MySQL is often seen as "a RDBMS for those who don't care about RDBMSs". We should not have to specially configure it to complain about our mistakes! :-/ (specially not if living under a hosted environment)
skoob's picture

Re: BLOBs in MySQL: Binary Laughable OBjects?!

It's usually better to let relational databases do what they do best: store relationships between data. For huge blobs of binary data, it better to use a database that has been designed for that type of usage (aka as a file system). If you need to store relationships between files and other data, just store the filename in the db. (But you're right. Silently truncating data is unacceptable.)