Nate
Nate

Reputation: 28434

Should I store image paths in one table, or multiple tables based on image type?

Let's say I'm making a shopping website and need to store paths for these types of images:

Where the number of images in each category is constantly growing.

Would it be better for me to create a separate table for each type of image, or to put them all into a single table with a type column to distinguish between them?

The reason I'm thinking it might be better to put them into separate tables is that the number of product images, for example, will always be significantly higher than the number of user images. So when a user changes their avatar, or when a page is loaded where avatars are retrieved from the database, then wouldn't it save a lot of time adding/retrieving the record if the tables were separate?

Upvotes: 5

Views: 2385

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157098

Depends. If it is just an URI you could store it in the table where the actual data belongs. So for the user's avatar, use a field in the user table.

If you have more properties for the image and fields are shared amongst image types, there is no harm in saving them in the same image table.

Another thing to keep in mind is the frequency of changing the field values. If for some type rows are frequently changed or deleted / added, it might be better performance wise to group volatile data together.

Upvotes: 2

Related Questions