是电子商务网站的图像表最佳做法吗?

时间:2013-02-12 14:28:35

标签: postgresql amazon-s3 database-schema

我正在构建一个包含产品和变体的电子商务系统,其中每个产品都有1到5个存储在Amazon S3上的图像。是否最好在我存储S3 URL的情况下使用单独的图像表,或者只为每个产品和变体表添加5个图像列?有一个单独的图像表意味着在导入时我需要做6个SELECTS然后INSERTS(以确保产品及其每个图像都不存在然后导入它们)而不是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上的Explain Analyze说这个查询需要3310毫秒才能返回22000行。但是,我还没有在图片上创建索引,所以这不是一个公平的分析。

1 个答案:

答案 0 :(得分:0)

在这种情况下,我想我会添加text [] type(text of text)的单列。通过这种方式,您可以获得所需的图像,并且当您为产品添加更多图像时,您不必在地狱中结束。