dbTalk Databases Forums  

Store images in db or folder?

comp.databases.mysql comp.databases.mysql


Discuss Store images in db or folder? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Simon
 
Posts: n/a

Default Store images in db or folder? - 09-23-2011 , 03:30 AM






Hi,

My users have 10000's of images, (if not more).

Currently all the images are stored in a sub folder,
'/user/letter/unique_filename.ext'

But I was wondering if mysql would not be slightly better at retrieving
the files, (the actual data).

The reason I ask is because I have few problems:

1- Backup is a bit all over the place currently, I could loose some/all
my images in the case of a HD failure, I would prefer to have a slave db
with all the images on it.
2- The site is growing, slowly but surely, so in the longer term it
might be better to have all the images in one table.
3- Moving to another server might help.

What do you think?

Is getting a file from the db and 'displaying' it, (serving it to the
browser), faster than getting it off the file server?

Is storing images in the db a better option for a large number of files?

Thanks

Simon

Reply With Quote
  #2  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 04:44 AM






Simon <bad (AT) example (DOT) com> wrote:

<snip>

Congrats! You warm up a "holy war" question. Please read:

http://groups.google.com/group/comp....bd1dfec0473016

Quote:
Is getting a file from the db and 'displaying' it, (serving it to the
browser), faster than getting it off the file server?
Serving a file from disk is a highly optimized operation for a web
server. Much unlike sending content that is stored as BLOB in a DBMS.

For a comparison:

http://mysqldump.azundris.com/archiv...-Database.html


XL

Reply With Quote
  #3  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 05:04 AM



Simon wrote:
Quote:
Hi,

My users have 10000's of images, (if not more).

Currently all the images are stored in a sub folder,
'/user/letter/unique_filename.ext'

But I was wondering if mysql would not be slightly better at retrieving
the files, (the actual data).

Opinions are divided.


You will probably find the files quicker, because Mysql has good indexing.

You may not retrieve them any quicker though, especially under heavy
load...a *nix operating system will have lots of access to RAM for disk
caching that MySQL may not... for example.


This was discussed extensively here a few months ago, and IIRC the
general compromises came down to better performance from flat files but
far cleaner management and backup and general tidyness from MySQL BLOB
stored images.

I have a database with about a thousand images in blobs: for sure on low
level of user activity it works not detectably worse than flat files.





Quote:
The reason I ask is because I have few problems:

1- Backup is a bit all over the place currently, I could loose some/all
my images in the case of a HD failure, I would prefer to have a slave db
with all the images on it.
2- The site is growing, slowly but surely, so in the longer term it
might be better to have all the images in one table.
3- Moving to another server might help.

What do you think?

Is getting a file from the db and 'displaying' it, (serving it to the
browser), faster than getting it off the file server?

The consensus is that its probably slightly slower. On a typical *nix
system you are using an extra process - mysqld - and a socket
connection to it.

Quote:
Is storing images in the db a better option for a large number of files?

That's where IIRC the general consensus was that the gains were to be
had - better indexing than a *nix file system.

There is also the memory issue..I cant remember exactly how Mysql
handles memory, but depending on how you access the DB you may end up
needing a lot of memory to hold images that you are currently pushing
out to userland. With *nix you have essentially the sum total of all
free RAM to do that if its simply a flat file. Mysql may not be so
fortunate.

With luck someone who knows the internals of MySQL will be able to
expand on this theme more than I can.

But unless its a high usage site, I personally prefer the neatness of
keeping all data in the DB.


Quote:
Thanks

Simon

Reply With Quote
  #4  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 05:25 AM



On 9/23/2011 4:30 AM, Simon wrote:
Quote:
Hi,

My users have 10000's of images, (if not more).

Currently all the images are stored in a sub folder,
'/user/letter/unique_filename.ext'

But I was wondering if mysql would not be slightly better at retrieving
the files, (the actual data).

The reason I ask is because I have few problems:

1- Backup is a bit all over the place currently, I could loose some/all
my images in the case of a HD failure, I would prefer to have a slave db
with all the images on it.
2- The site is growing, slowly but surely, so in the longer term it
might be better to have all the images in one table.
3- Moving to another server might help.

What do you think?

Is getting a file from the db and 'displaying' it, (serving it to the
browser), faster than getting it off the file server?

Is storing images in the db a better option for a large number of files?

Thanks

Simon
Yes, this is a religious war. Some of us have real world experience
doing this, others don't. I've been working with images and RDBMS's for
around 25 years in live environments.

A file system is just a hierarchic database, and not a very smart one at
that. 10,000's of files in one directory will slow down access as file
systems are not really made to handle huge numbers of files.
Additionally, disk space tends to fragment, slowing access even more.

A decent database can handle large numbers of images like that quite
effectively. There will be less fragmentation, access time can improve,
database integrity can be assured (you can't just delete a file from
the file system) and backups are much easier. The only down side is in
a web environment you need to call a short script to fetch and server
the file, but that can be very efficient also.

Now some databases can have some pretty crappy retrieval times, may be
inefficient on how they store the data or have other problems. Perhaps
that's why some people think it's not a good idea to store images in a
database. But with a decent database it can work quite well.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #5  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 05:28 AM



The Natural Philosopher <tnp (AT) invalid (DOT) invalid> wrote:

Quote:
Is storing images in the db a better option for a large number of files?

That's where IIRC the general consensus was that the gains were to be
had - better indexing than a *nix file system.
This really depends on the file system. If the file system in use is
conceptually younger than - lets say - 10 years, then there is a big
chance that it uses a tree structure to organize files. And then
there will be no difference in performance if you ask the file system
to locate a file by name or if you ask the database to locate a BLOB
by key.

The real difference is, that a database can have multiple keys where
a file system has only one. OTOH you can get the same in a file system
too - by using (symbolic) links.

And of course even with old file systems, where directories were lists,
it was no problem to have many files and still be performant. The
canonical solution is a hashed directory structure.

I.e. file foobar.jpg is located in (root)/f/o/foobar.jpg


XL

Reply With Quote
  #6  
Old   
Ignoramus14736
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 11:09 AM



I have done both and I HIGHLY prefer storing images in a database. A
lot of things become a lot easier and more natural. Backup, access,
fsck, bulk operations, etc etc etc.

If the process is a little slower, I personally do not care. I had to
deal with filesystems with millions of objects, and all I want to say
is NOT AGAIN.

i

Reply With Quote
  #7  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 11:11 AM



Ignoramus14736 wrote:
Quote:
I have done both and I HIGHLY prefer storing images in a database. A
lot of things become a lot easier and more natural. Backup, access,
fsck, bulk operations, etc etc etc.

If the process is a little slower, I personally do not care. I had to
deal with filesystems with millions of objects, and all I want to say
is NOT AGAIN.

i
Personally I agree. Unless its a facebook type app with millions of
users, the practical aspects of management far outweigh the alleged
performance hits IMHO.

Reply With Quote
  #8  
Old   
Jacek Krysztofik
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 11:28 AM



Quote:
I've been working with images and RDBMS's for
around 25 years in live environments.
Yeah... installing A/C.


Quote:
file systems are not really made to handle huge numbers of files.
Oh FFS.

Quote:
~ gibberish ~
Missed you.

Reply With Quote
  #9  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 01:41 PM



Axel Schwenke wrote:

Quote:
Simon <bad (AT) example (DOT) com> wrote:
snip

Congrats! You warm up a "holy war" question. Please read:


http://groups.google.com/group/comp....bd1dfec0473016

Is getting a file from the db and 'displaying' it, (serving it to the
browser), faster than getting it off the file server?

Serving a file from disk is a highly optimized operation for a web
server. Much unlike sending content that is stored as BLOB in a DBMS.

For a comparison:

http://mysqldump.azundris.com/archiv...Images-From-A-
Database.html

I have had first-hand experience of this recently when a less experienced
colleague actually stored dynamically generated PDF documents in BLOBs in a
MySQL database table (I think the version was 5.1, and the engine was
InnoDB) for a WIMP application. (Not only that, but for reasons well beyond
me he also base64-encoded the PDF data beforehand, increasing the footprint
of each file by about 33%.)

After a month or so the table storing the PDF documents generated by about
500 different users reached 2177 records and the formidable size of 1.7
GiB(!), and response times of the Web application for retrieving a single
PDF document by primary key (indexed!) went up to 2 minutes(!).

After I extracted the data to the filesystem, with the table only keeping
the filenames (which took about 2 *hours* to complete), response times of
the application went down to about 2 *seconds* again.

So I can only strongly emphasize this: Avoid storing files in BLOBs!

There has also been a Microsoft study on this in 2006 CE, finding that due
to fragmentation which filesystems appear to handle better than databases,
the break-even point for storing a file in a database is at 256 KiB or even
lower, depending on the DBMS:

<ftp://ftp.research.microsoft.com/pub/tr/TR-2006-45.pdf>

I have found it referred at

<http://mysqldatabaseadministration.b...01/i-will-not-
blob.html>


HTH

--
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Reply With Quote
  #10  
Old   
Ignoramus14736
 
Posts: n/a

Default Re: Store images in db or folder? - 09-23-2011 , 02:07 PM



On 2011-09-23, The Natural Philosopher <tnp (AT) invalid (DOT) invalid> wrote:
Quote:
Simon wrote:
Hi,

My users have 10000's of images, (if not more).

Currently all the images are stored in a sub folder,
'/user/letter/unique_filename.ext'

But I was wondering if mysql would not be slightly better at retrieving
the files, (the actual data).


Opinions are divided.


You will probably find the files quicker, because Mysql has good indexing.

You may not retrieve them any quicker though, especially under heavy
load...a *nix operating system will have lots of access to RAM for disk
caching that MySQL may not... for example.


This was discussed extensively here a few months ago, and IIRC the
general compromises came down to better performance from flat files but
far cleaner management and backup and general tidyness from MySQL BLOB
stored images.

I have a database with about a thousand images in blobs: for sure on low
level of user activity it works not detectably worse than flat files.

I have a database with 473,374 images, works great. I am now building a
database that will hold 200,000,000 images or so.

Storing them in files would mean that I will have to reimplement many
database features on a filesystem.

i

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.