Topic: Databases, Images, and RMagick

Is it a good idea to upload images into a database as blobs? I've done a quick play around and it very slow on my machine. although If I use just ruby to use RMagick and its quick enough. but in Rails its very slow. Is this normal? I understand there is more things going on in Rails but this kinda slowdown is painful.

--

Re: Databases, Images, and RMagick

If your storing more than 2 or 3 images I'd put them in the file system or something like Amazon S3. Look into attachment_fu which is easy to use and will give you the option to upload to database, filesystem or S3. Personally, I use the file system but our latest app is using S3.

I also use ImageMagick with minimagick for resizing/manipulating images. MM just uses command line IM so its easier on memory.

Last edited by shadow (2007-05-27 05:19:09)

Re: Databases, Images, and RMagick

Images should definitely not go in the database.

I've used S3 and it's great ... although you may need to look at the cost structure for your application.

You can just store images on the server ... you can track a reference to the image path in the database but server the image direct from the FS.

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Databases, Images, and RMagick

How does one integrate Amazon S3 services into a Rails app?  Maybe this may not even be the right question, so please correct me.  I guess what I am trying to say is I don't know how the S3 service works and how to start uploading/downloading from the S3 storage area.

Re: Databases, Images, and RMagick

I'd check out attachment_fu, from the sounds of it. Haven't used it myself.

S3 is basically a big bucket that can store files. Files are stored with a unique key and then a simple get will retrieve them.

S3 has a pretty simple HTTP and XML interface to access it. You need to sign-up and get a developer key, but other than that it's really quite straight forward.

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Databases, Images, and RMagick

I looked it up last night.  Definitely seems straight forward.  I will definitely look into this as well.

Thanks for everyone's input on this matter.

Re: Databases, Images, and RMagick

tobyhede wrote:

Images should definitely not go in the database.

Why is this? If performance is an issue, wouldn't caching take care of this? And if large db backups are an issue, it's easy to exclude the one "db_files" table if required (multiple models can share one parent Image class, which writes to one db_files table).

I'm storing images in the database because I want to keep the files and data distinctly separate, however I'd really like to know why you strongly discourage it...

Zubin

Re: Databases, Images, and RMagick

It is performance. Databases are designed for large numbers of small queries. Once you start using Blobs you can hit bottlenecks really fast ... a single query may now be a 100k image rather than 1k of text, all needing to be streamed across the connection.

That said, if its working for you, do it. Worry about it if you hit a problem, because storing images in the database can make life a lot easier ..  especially backup.

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Databases, Images, and RMagick

I think a good solution to storing images in the database is to determine a maximum image size and then resize to that size before storing, that way a user can't upload that 10 megapixel picture of their cat and frag your db. i.e. set 640x480 as the max and fit it to that without distorting.

http://www.brianthecoder.com

Rails, Javascript, Actionscript and More.