I'm constructing a database for a project of mine. Currently, I have a table with images and a table with products.
product
Column | Type |
----------------+-----------+
id | integer |
images | integer[] |
display_image | integer |
...
image
Column | Type |
----------------+-----------+
id | integer |
data | text |
So the basic idea is to store the ids of all images that belong to one product in the product.images field. It will be a use case of the database to find the corresponding product to a specific image. Additionally, one image can belong to multiple products.
Now I'm not sure if this is the fastest way to get results if I query which products belong to an image and the database will hold millions of products and images. Would it be faster to have an additional field image.products where I store all the products that are associated with one image?
The query to get products for a specific image would currently look like this (I'm using PostgreSQL as the database) :
SELECT * from product where image.id = ANY(product.images)
Having another field that needs to be maintained would be a burden on the API-side. Before I do the refactoring for this I would like to hear experienced opinions, if the query mentioned above can have a performance impact later on.
Thank you in advance for your replies.