Main Content

Images in a database? How big is a database? (MySQL)

Archive - Originally posted on "The Horse's Mouth" - 2009-05-28 07:58:10 - Graham Ellis

The discussion came up yesterday as to whether images should be stored in plain files or in a database, with one of my delegates saying that he had been strongly advised against a database.

That advise is typically going to be correct for a handful of images ... but when you get on to thousands in a single 'directory', the directory structure of operating systems is not best set up to serve all the individual files and a database solution is the best. The database solution also allows you to hold descriptions and other information easily with the images.

"So how big is your image database?" Well - there's just over 4000 images in it, and I guessed that the disc space occupied was about a quarter of a gigabyte on the server. As my data is stored using MyISAM tables, I was able to take a look in the evening and see that my estimate was remarkable close:

-rw-rw---- 1 mysql mysql      8674 2009-03-09 17:24 im_library.frm
-rw-rw---- 1 mysql mysql 243813840 2009-05-27 21:23 im_library.MYD
-rw-rw---- 1 mysql mysql 158720 2009-05-27 21:23 im_library.MYI


All the images from the database are 'fed' via a PHP script which initially checks for a 'real' file and if it finds it, saves it into the database keyed by the file name, and deletes the real file - so it's very easy to add images. My regular database backup backs up all my images. And mod_rewrite is used to send all requests ending in .jpg to the script.