I’d like to be able to return a thumbnail (first image of a set) with my product data – what is the best way to accomplish this (performance)?
I have the following tables:
products (product data)
products_images (relation between products and images)
images (image data)
So for every product, it’d return the first image of a set associated with that product.
Two things:
- I’m trying to get the first image but the order depends on an ‘order’
field. - If no image found just return null for image data but still
get product.
I’m thinking of doing a subquery since I don’t know how to about ordering and limiting image results in a join.
What do you suggest?
Edit:
The image order field is present in the products_images table. That table has the following fields (product_id, image_id, order)
An index on
(product_id, order, image_id)in tableproducts_imageswould be useful for the subquery to run faster.