Store image files or urls in MySQL database? What's better?

Possible duplicate:
Saving images in DB - Yea or Nay?
Images in the database and file system

I am developing a web application using RIA technologies (Flex + PHP + MySQL + Ajax), and now I have a dilemma about image files.

I use some images in my Flex application, so I think that "it can be awesome if I store them in a database and then retrieve from it, it still should be easier to support the process." But here is my dilemma:

Should I store the physical URL of my images or would it be better if I save the image directly?

For example, if my Cars table looks like this:

ID (standalone) | Source (text)

or how is it?

ID (standalone) | Image (longblob or blob)

I know that there are cool people who can answer me this question, explaining to me what is better and why :)

+6
source share
6 answers

I personally recommend storing images in a database. Of course, these are both advantages and disadvantages.

Benefits of storing BLOB data in a database:

  • It’s easier to save BLOB data with the rest of the elements in a row.
  • BLOB data is backed up using a database. Having a single storage system can facilitate administration.
  • BLOB data can be accessed through XML support in MySQL, which can return a basic 64-encoded representation of data in an XML stream.
  • MySQL full-text search (FTS) operations can be performed on columns containing fixed or variable-sized data (including Unicode). You can also perform FTS operations on formatted text data contained in image fields, such as Microsoft Word or Microsoft Excel documents.

Disadvantages of storing BLOB data in a database:

Take a close look at which resources can be better stored on the file system rather than the database. Good examples are images that are usually referenced via HTTP HREF. This is because:

  • Retrieving an image from a database results in significant overhead compared to using a file system.
  • SAN-based disk storage is usually more expensive than storage on disks used in web server farms.
+6
source

Generally, you don't want your databases to be small, so they work better (and backups are better). Therefore, if you can only save the link to the file system (path + file name) or the URL in the database, this will be better.

+4
source

This is probably a matter of personal preference.

Generally, it is best to keep the database small. However, when you come to corporate applications, they regularly add images directly to the database. If you put them in the file system, db and your file system may go out of sync.

Larger CMS will regularly place these files in db. However, keep in mind that this requires a larger database when everything grows ...

When you save only the URL and name, make sure that they do not change in the future.

With files stored in a database, you can make security easier and you don’t have to worry about duplicate file names.

+3
source

I used to store the path in the URL, but then adding an additional web server to the mix turned out to be less ideal. First, you need to share where the images are stored. We used NFS, and after a while it became slow. We tried to sync files from one web server to another, but the process became cumbersome.

Having said that, I would save them in the database. Since then, I have moved all of my image / file storage to MongoDB. I know that this does not satisfy your needs, but we all tried (even S3), and we were not happy with other solutions. If we had to, I would definitely throw them in MySQL.

+1
source

I would save the URL, this is less data, which means a smaller database and faster data collection from it;)

0
source

Personally, I always saved the URL.

There is no real reason not to store the image directly in the database, but there are advantages to not storing it in the database.

You get more flexibility if you do not save the image in the database. You can easily move it and just update the URL in the file. So, if you want to transfer the image from your web server to a service like Flickr or Amazon Web Services, it will be as simple as updating the link to new files. It also gives you easy access to content delivery networks so that images are delivered to end users faster.

0
source

Source: https://habr.com/ru/post/901802/


All Articles