我在一个名为“”的表中有一个名为"Description“的列,其中包含由分隔的多个食品项目名称,例如鸡肉,汤,面包,可乐
如何从列中提取每一项并创建多行。例如,目前它就像{FoodID, FoodName, Description}
==> {123,Meal,“鸡肉,汤,面包,可乐”}
我需要的是
{FoodID, FoodName, Description}
==> {123,膳食,鸡肉},{123,膳食,汤},{123,膳食,面包}等。
在Redshift中,我首先将"description“列拆分为
select FoodID, FoodName, Description,
SPLIT_PART(Description, ',',1) AS Item1,
SPLIT_PART(Description, ',',1) AS Item2,
SPLIT_PART(Description, ',',1) AS Item3,.....till Item10
FROM Food
考虑到最多可以有10个项目,因此可以使用Item10。将这些列的Item1转换为Item10以存储为行的最佳方法是什么?我尝试了UNION ALL,但考虑到巨大的数据负载,它需要更长的时间。
发布于 2018-06-07 22:30:56
你的问题是answered,这里是专门针对Redshift的。您只需要将您的查询映射到那里提供的示例查询。您的查询将如下所示。
select (row_number() over (order by true))::int as n into numbers from food limit 100;
这将创建numbers表。
您的查询将变为:
select foodId, foodName, split_part(Description,',',n) as descriptions from food cross join numbers where split_part(Description,',',n) is not null and split_part(Description,',',n) != '';
现在,回到你最初关于性能的问题。
考虑到巨大的数据负载,需要更长的时间。
考虑到high read
和seldom write
的典型数据仓库用例,您应该在stagging
表中保留您提到的典型食品数据,比如stg_food
。
您应该使用以下类型的查询一次性插入到实际的food
表中,如下所示。
insert into food select foodId, foodName, split_part(Description,',',n) as descriptions from stg_food cross join numbers where split_part(Description,',',n) is not null and split_part(Description,',',n) != '';
这将编写一次,并使您的select查询更快。
https://stackoverflow.com/questions/50733755
复制相似问题