我正在构建一个包含产品和变体的电子商务系统,每个系统都有 1 到 5 张图像存储在 Amazon S3 上。在存储 S3 URL 的地方有一个单独的图像表是否被认为是最佳实践,或者只向每个产品和变体表添加 5 个图像列是否可以接受?拥有一个单独的图像表意味着在导入时我需要执行 6 次选择然后插入(以确保产品及其每个图像不存在然后导入它们)而不是 1。而且,在检索时,我需要将 images 表连接到 products 表 5 次,以使其与产品一起返回图像,如下所示:
SELECT prd."id" AS id, prd."title" AS title, prd."description" AS description,
prd."createdAt" AS productcreatedate,
prdPic1."url" AS productpic1,
prdPic2."url" AS productpic2,
prdPic3."url" AS productpic3,
prdPic4."url" AS productpic4,
prdPic5."url" AS productpic5,
brd."name" AS brandname, brd."id" AS brandid,
cat."name" AS categoryname, cat."id" AS categoryid,
prt."name" AS partnername, prt."id" AS partnerid
FROM "Products" prd
LEFT OUTER JOIN "Pictures" prdPic1 ON prdPic1."entityId" = prd."id" AND prdPic1."entity" = '1'
AND prdPic1."sortOrder" = 1
LEFT OUTER JOIN "Pictures" prdPic2 ON prdPic2."entityId" = prd."id" AND prdPic2."entity" = '1'
AND prdPic2."sortOrder" = 2
LEFT OUTER JOIN "Pictures" prdPic3 ON prdPic3."entityId" = prd."id" AND prdPic3."entity" = '1'
AND prdPic3."sortOrder" = 3
LEFT OUTER JOIN "Pictures" prdPic4 ON prdPic4."entityId" = prd."id" AND prdPic4."entity" = '1'
AND prdPic4."sortOrder" = 4
LEFT OUTER JOIN "Pictures" prdPic5 ON prdPic5."entityId" = prd."id" AND prdPic5."entity" = '1'
AND prdPic5."sortOrder" = 5
INNER JOIN "Brands" brd ON brd."id" = prd."BrandId"
INNER JOIN "Categories" cat ON cat."id" = prd."CategoryId"
INNER JOIN "Partners" prt ON prt."id" = brd."PartnerId";
我很清楚将品牌、类别和合作伙伴标准化以减少冗余。我对图像表的价值不太清楚。在 Postgres 上解释分析说这个查询需要 3310 毫秒才能返回 22000 行。但是,我还没有在图片上创建索引,所以这不是一个公平的分析。