开发者

Retrieving images - MySQL based or file system based, which one is faster?

开发者 https://www.devze.com 2023-01-31 19:50 出处:网络
I\'d like to know if it\'s faster/b开发者_如何学Goetter to retrieve image(s) for a particular product or blog post or whatever in a website by getting the path/filename from the database along with th

I'd like to know if it's faster/b开发者_如何学Goetter to retrieve image(s) for a particular product or blog post or whatever in a website by getting the path/filename from the database along with the rest of the data, or if I should just create a folder with the product id and use glob to iterate over it or just use the id as filename in case of a single image, for example.

What are the advantages/disadvantages to these approaches?


It's definitely faster to use the paths/name methods (- 1 database calls; composing a string is not a time/memory consuming operation). By the way, in case you'll have different images for the same product id, you might create a foreach loop that looks in the given folder for pic1.png, pic2.png [...] picn.png.

There is a problem, though: changing the images will become a more convoluted process - instead of just uploading new ones and updating the DB, you will have to delete the previous (or previous set) with relative thumbnails if any before uploading the new ones; that will also mean the image change process won't be reversible unless you store the old ones in a backup directory, etc.

So, (you plan to change them often || your users will change them often) ? database : paths


Images..

  1. Imagine they are in the database.
  2. Imagine yourself caching them to improve performance.
  3. Imagine you put them in a file in order to cache them.

Ding ding ding. Images in file on harddrive.


Storing images using a database record that holds a path to the actual image file allows you to do much more, such as store an alt text column, or any other useful information in the database as well. You will of course need to create a good content management system around that idea in order to be able to make fast and easy changes to already uploaded images.

I usually keep one table for all of my images that has the id, filename, alt_text, width, and height, then reference that table in all of my other tables that might need images attached. I also store extra stuff like resize method information, so that the next time they upload an image for that same field, you can grab and set the default values automatically for any resizing information they need to enter.

You may also want to store the image's alignment, although I usually put this in to the table that links to the image table. This allows for you to set it up so that users may easily re-use already uploaded images in other areas of the website without uploading a second copy of that image.

0

精彩评论

暂无评论...
验证码 换一张
取 消