0

I am creating an e-commerce store in Django and am currently trying to decide between implementing product images as an entity of their own then linking to the product by a foreign key or putting image fields as attributes in the product entity.

In the answers I've seen to this question, most people recommend implementing a separate table for images and I would prefer doing this because then, I will not have a limit to the number of images I can add to a product.

My concern however is, will this come at a huge speed cost for the customers using my website i.e. for my product listing page, I would have to, for each product, filter (query) the images corresponding to the particular product from a table of several images. I assume it would be faster if I make the images columns in the actual product table.

I would give a number of rows in the images table but, I have no idea how many images I will wind up having for the store.

7
  • 1
    Are you using MySQL or Microsoft SQL Server? Tag you question with only the appropriate DBMS.
    – Dan Guzman
    Commented Nov 12, 2021 at 10:56
  • Only testing the design with your data and your workload can tell which is faster. Some people suggest that images and such should be stored outside the database entirely, with only paths to them stored in the database; this is also an option to consider.
    – mustaccio
    Commented Nov 12, 2021 at 13:19
  • Sorry. I'm using Django, currently with SQLite but for production will do either PostgreSQL or MySQL. Commented Nov 12, 2021 at 13:34
  • Thanks @mustaccio. I'll work on some Django fixtures to simulate the production environment and, perhaps, do some benchmarking to see potential areas for performance improvements. I guess I was just hoping that there was a quick and dirty answer. Commented Nov 12, 2021 at 13:38
  • "I assume it would be faster if I make the images columns in the actual product table." - no it would not.
    – user1822
    Commented Nov 12, 2021 at 15:07

2 Answers 2

0

If those images will be delivered to web pages, it is better to store the URL (or the key part of the URL) in the database table. Have the image in a file. That way, the HTML <img src=...> can be generated by your code and very easily be fetched by the user's browser.

The limit, of course, is disk space where you store the images. And there is no need to have the images on the same server. Nor do all the images need to be in a single server.

Building the web page does not have to touch the image. That is handled asynchronously later.

Most image formats (jpg, etc) are already compressed. It is wasteful to compress them again.

1
  • Hi Rick. Really appreciate the insights! Thanks a ton. Will definitely try this out. Commented Nov 13, 2021 at 12:06
-1

“ I will not have a limit to the number of images I can add to a product.” Then you need a new table or to store all images in one row ( zipping could work).

You’re going to index this table on the lookup key so any additional overhead is going to be small. Remember that images are going to be large objects and unlikely to be stored in the same page as the other data anyway so there’ll be additional lookups for both options either way.

3
  • Thanks Andrew. I'll definitely give the one-row technique a go; I don't know how simple it will be with Django but sounds promising to shave some time off my queries. Commented Nov 12, 2021 at 13:42
  • I’m not suggesting a single row will shave any meaningful time off. My opinion is that a table especially for it will will do fine and will be easier to manage Commented Nov 12, 2021 at 14:02
  • Awesome, thanks. Yeah I've opted for the separate table solution seeing as everyone is recommending that. Commented Nov 12, 2021 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.