Saving images: files or blobs?
Asked Answered
A

12

27

When you save your images (supose you have lots of them) do you store then as blobs in your Database, or as files? Why?

Duplicate of: Storing Images in DB - Yea or Nay?

Associationism answered 28/8, 2009 at 14:44 Comment(0)
G
30

I usually go with storing them as files, and store the path in the database. To me, it's a much easier and more natural approach than pushing them into the database as blobs.

One argument for storing them in the database: much easier to do full backups, but that depends on your needs. If you need to be able to easily take full snapshots of your database (including the images), then storing them as blobs in the database is probably the way to go. Otherwise you have to pair your database backup with a file backup, and somehow try to associate the two, so that if you have to do a restore, you know which pair to restore.

Gusman answered 28/8, 2009 at 14:45 Comment(4)
if you have a web app, files are the way to go, you build the HTML with the path info from the database that points to the file that is stored where the client browser will pull it down from.Bucaramanga
I'm building an offline application. I've found that storing the blob allows for easy replication of the database. Just a thought!Barlow
I wrote a small shipping system and stuck the images into the database. Once it got to a large size the database backups became very problematic (30,000 images). Eventually the database and any query returning the images ground to a halt. The database engine SQL server had serious issues and had to clear the query cache to fix it. In the end I eventually refactored it out. Adding a bit of disk management was worth it and I am never sticking an image into a SQL Server database again.Language
So if files are better, then how do i secure them? Again, im storing very sensitive information ss#, bank statements, tax returns, etc. That's more my questions. i don't mind using files if i can get the level of security i need. Based on comments below, the best way to get the security i need is blob even at the cost of speed. Please advise.Downcast
Y
15

It depends on the size of the image.

Microsoft Research has an interesting document on the subject

Yoicks answered 28/8, 2009 at 14:48 Comment(2)
The summary of that document was that the file system (ntfs) tends to perform better with files greater than 1mb and the db performs better with files less than 1mb (smaller the better).Sextuple
That the paper is from 2006, that's 11 years ago by now. So today things might be different. And actually, the conclusion is: "(...) if objects are larger than 1 MiB on average, NTFS has a clear advantage over SQL Server. If the objects are under 256 KiB, the database has a clear advantage. Inside this range, it depends ..."Darden
S
12

I've tried to use the db (SQL Server and MySQL) to store medium (< 5mb) files, and what I got was tons of trouble.

1) Some DBs (SQL Server Express) have size limits;

2) Some DBs (MySQL) become mortally slow;

3) When you have to display a list of object, if you inadvertedly do SELECT * FROM table, tons of data will try to go up and down from the db, resulting in a deadly slow response or memory fail;

4) Some frontends (ruby ActiveRecord) have very big troubles handling blobs.

Just use files. Don't store them all in the same directory, use some technique to put them on several dirs (for instance, you could use last two chars of a GUID or last two digits of an int id) and then store the path on db.

Stubble answered 28/8, 2009 at 14:51 Comment(1)
You sold me on SELECT * FROM table -- I use this periodically for testing purposes and that alone could cause me a massive headache. Files for me!Libriform
T
6

The performance hit of a database server is a moot issue. If you need the performance benefits of a file system, you simply cache it there on the first request. Subsequent requests can then be served directly from the file system by a direct link (which, in case of a web app, you could rewrite the HTML with before flushing the output buffer).

This provides the best of both worlds:

  • The authoritative store is the database, keeping transactional and referential integrity
  • You can deploy all user data by simply deploying the database
  • Emptying this cache (e.g. by adding a web server) would only cause a temporary performance hit while it is refilled automatically.

There is no need to constantly hammer the database for things that won't change all the time, but the important thing is that the user data is all there and not scattered around different places, making multi-server operation and deployment a total mess. I'm always advocating the "database as the user data store, unless" approach, because it is better architecturally, and not necessarily slower with effective caching.

Having said that, a good reason to use the file system as the authoritative store would be when you really need to use external independent tools for accessing it, e.g. SFTP and whatnot.

Tarazi answered 2/7, 2011 at 16:50 Comment(1)
Though I still use file based system myself personally, I think this is a really good answer.Hearsay
H
3

Given that you might want to save an image along with a name, brief description, created date, created by, etc., you might find it better to save in a database. That way, everything is together. If you saved this same info and stored the image as a file, you would have to retrieve the whole "image object" from two places...and down the road, you might find yourself having syncing issues (some images not being found). Hopefully this makes sense.

Homiletic answered 28/8, 2009 at 14:49 Comment(0)
B
2

If I'm running on one web server and will only ever be running on one web server, I store them as files. If I'm running across multiple webheads, I put the reference instance of the image in a database BLOB and cache it as a file on the webheads.

Balance answered 28/8, 2009 at 14:48 Comment(0)
S
2

By saving you mean to use them to show in a webpage or something like that? If it's the case, the better option will be to use files, if you use a database it will be constantly hammered with the request for photos. And it's a situation that doesn't scale too well.

Spaghetti answered 28/8, 2009 at 14:50 Comment(1)
No images. These are just transaction related documents so nothing that would be pulled each time someone visited the site like a logo or something. These are client documents and the only time they would be pulled down would be when either the professional or the client is tryign to view them.Downcast
M
2

The question is, does your application handle BLOBS or other files like other application data? Do your users upload images alongside other data? If so, then you ought to store the BLOBs in the database. It makes it easier to back up the database and, in the event of a problem, to recover to a transactionally consistent state.

But if you mean images which are part of the application infratstructure rather than user data then probably the answer is, No.

Matsu answered 28/8, 2009 at 21:10 Comment(0)
M
2

I would suggest to go for File systems. First, let's discuss why not Blob? So to answer that, we need to think what advantages DB provides us over File system?

  1. Mutability: We can modify the data once stored. Not Applicable in case of images. Images are just a series of 1s and 0s. Whenever we changes an image, it wouldn't be a matter of few 1s and 0s altered and hence, modifying the same image content doesn't make sense. It's better to delete the old one, and store new.
  2. Indexing: We can create indexes for faster searching. But it doesn't apply on images as images are just 1s and 0s and we can't index that.

Then why File systems?

  1. Faster access: If we are storing images in Blob inside our DB, then a query to fetch the complete record (select *) will result in a very poor performance of the query as a lots and lots of data will be going to and from the DB. Instead if we just store the URL of images in DB and store images in a distributed file system (DFS), it will be much faster.
  2. Size limit: If DBs are storing images, a lot and lot of images then it might face performance issues and also, reach its memory limit (few DBs do have it).
Midwifery answered 3/11, 2020 at 16:17 Comment(0)
G
1

Blobs can be heavy on the db/scripts, why not just store paths. The only reason we've ever used blobs is if it needs to be merge replicated or super tight security for assets (as in cant pull image unless logged in or something)

Gaynellegayner answered 28/8, 2009 at 14:49 Comment(1)
To you point, i need "super tight security." So i think a blob for secure documents like tax returns would be best, no? Then all non secure items like images, etc., could be in a file folder?Downcast
C
1

Using file System is better as the basic feature you would be provided with while storing images as a blob would be 1. mutability which is not needed for an image as we won't be changing the binary data of images, we will be removing images as whole only 2. Indexed searching :which is not needed for image as the content of images can't be indexed and indexed searching searches the content of the BLOB.

Using file system is beneficial here because 1. its cheaper 2. Using CDN for fast access

hence one way forward could be to store the images as a file and provide its path in database

Combustor answered 20/10, 2019 at 9:55 Comment(0)
D
0

Blob and file system are old now Why not go for S3 bucket to store images as in production we are using them,and storing the path of them in db

Distil answered 30/8, 2023 at 0:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.