Do we really need a table in the database to store the image file path if the images are stored on the file system?

We store images either in a database or on a file system. If we store images on the file system, we store the path to the image in the database. We did this all the time.

One of my colleagues thought of storing images in the file system, each user having a split folder named by their username to store their images without storing the file path in the database. For example, when a user submits a request to show their images, we look for a folder named after that user in the file system and show all the images in that folder. When a user uploads an image, it is saved in the file system under his / her folder. Users cannot change their username and do not have permission to change the folder name.

Is it possible to really omit the database table for the image path? Can the system be designed this way? What are the possible problems with this design?

+3


source to share


2 answers


Can we really omit the database table for the image path?

Of course you can, but (see below), I would only consider this solution for very small databases.

Can the system be designed this way?

IMO if you're not working with very little data, with fairly fixed requirements and lightweight logic around them.

What are the possible problems with this kind of design?

Here's a long text. Common questions I can think of are (at least) the following:

  • If you are using a technology such as FILESTREAM (or its equivalent, according to your environment), complex operations such as delete and rename will be handled correctly by the database engine. Doing it manually (with or without the file path stored in the column) will make your code weaker unless you carefully design such operations (initially due to concurrency). What is easier to write with a path column is a script to find orphans. This may (or not) be a problem (as you said, the user is forever), but this is something you should consider.
  • For security reasons, you can set up the storage space to be read / written but not viewed. If you don't have a path column, you need to make the storage available.
  • If you need paths in your queries , you need this column (for example, to count how many images each user has, average images per user, or prepare a list without disk access).
  • With a column like this, you'll be safer in terms of concurrency too . If one user gains access to the list (for example, reading files), someone else might decide to delete them. If access is consistent with table / record level locking, then it will work naturally, but if they are not related to each other, it should be done in your code (don't underestimate it).
  • If the storage space for images increases , you can easily add new disks, this is easy to handle if you don't have to look for the right path, but it is stored inside the DB. You can have different drives (at different speeds) for different users, and you won't pollute your code with such verbose information (the image browser sets paths that are always unaware of such business stuff). Of course, you can do it in code, but then you will put this kind of logic wherever you need to collect such a list, and here is the support for the code .
  • The path stored in the database column can be a virtual path (as a plus for what I said in point 5) and it can be resolved by data from other tables. Anything can be done in SQL and your code will just see the path. If the DB does not have such information, then this calculation must be done in code (and it will be more difficult to change it as it covers). Point again - maintainability.
  • To reuse an image for multiple users, you can simply duplicate its path (assuming it is read-only).
  • Images can be made public (to be visible to many users). This information should remain in the database, but if you don't have a file path, it will be more difficult.
  • The images may not even be saved locally , but on a different server (via HTTP). This is easy to change if you have database paths, but it's a pain if you have code like this in your presentation.


Note that dots 5 through 9 imply some kind of business logic. It can be at the database level (in SQL or with external code like .NET for MS SQL Server) or at your data access layer outside the database. The real difference here is that with a column like this, you have the flexibility to put this kind of logic where you prefer (according to your specific architecture, and also move it up if you grow layers). If you don't have such a column, you can't just put this kind of logic in the DB (and this is especially bad for small 2-tier applications).

Here's a simple requirement that will convince me to accept this path column:

Store images bigger than 10 MB on path \\ BIGDISK \ IMAGES.
Images smaller than 10 MB are on D: \ IMAGES.
Premium users always stores on D: \ IMAGES.
Trial users will store on remote server http://example.com/very_slow_storage).

You can of course do this in your code, but it's easier in SQL, and it's (more importantly) in one well-known clause not covering your view logic (to insert images, load them, get a list, to calculate statistics, to create preview ...). More importantly, it's easy to change, no need to update (and deploy / merge) config files or (worse) with code changes.

If you answer โ€œI donโ€™t careโ€ about all these points and you donโ€™t plan any changes in the future, then IMO you can delete such a column in your database.

Of course, this is a design change that you can revisit every time you have a new requirement (or when you find that your presentation code knows about details that it should really ignore, like where the image should be stored).

+2


source


The column point in your database is for metadata and for concurrency. File systems are relatively slow to search when compared to databases. So, would you rather have your web server look up a disk or database to look up files? I would prefer a database.



In addition, you can associate other metadata about the files .. (download time, user who downloaded, associated icons, permissions to access it, etc.)

+3


source







All Articles