Sql 如何从初始的大表创建星型模式?

Sql 如何从初始的大表创建星型模式?,sql,sql-server,database,azure,Sql,Sql Server,Database,Azure,我目前有一个包含50m行的表 Column Data Type Text1 nvarchar(60) Text2 nvarchar(115) Text3 nvarchar(100) Text4 nvarchar(50) Text5 nvarchar(17) Year INT Age_Group1 float Age_Group2 float Age_Group3 float Age_Group4 float Age_Group5 float Age_Grou

我目前有一个包含50m行的表

Column  Data Type
Text1   nvarchar(60)
Text2   nvarchar(115)
Text3   nvarchar(100)
Text4   nvarchar(50)
Text5   nvarchar(17)
Year    INT
Age_Group1  float
Age_Group2  float
Age_Group3  float
Age_Group4  float
Age_Group5  float
Age_Group6  float
Age_Group7  float
Age_Group8  float
Age_Group9  float
Age_Group10 float
Age_Group11 float
Age_Group12 float
Age_Group13 float
Age_Group14 float
Age_Group15 float
Age_Group16 float
Age_Group17 float
Age_Group18 float
Age_Group19 float
Age_Group20 float
Age_Group21 float
此数据将使用直接查询方法拉入PowerBI,因此我希望确保以最佳方式存储数据。考虑到这个表中文本的大小和数量,我想我应该为每个文本字段创建一个维度表

我心目中的剧本是:

select Text1 , row_number()  OVER (         
         ORDER BY Text1 
           )  as Text1_ID   
        into Text1_DIM      
        from (  
        select distinct Text1   
        from dbo.my_table   
            ) x ;
我想我会对每个文本字段执行此操作,然后使用以下内容创建一个新的摘要事实表:

select 
Text1_ID,
Text2_ID,
Text3_ID,
Text4_ID,
Text5_ID,
Year,
Age_Group1,
Age_Group2,
Age_Group3,
Age_Group4,
Age_Group5,
Age_Group6,
Age_Group7,
Age_Group8,
Age_Group9,
Age_Group10,
Age_Group11,
Age_Group12,
Age_Group13,
Age_Group14,
Age_Group15,
Age_Group16,
Age_Group17,
Age_Group18,
Age_Group19,
Age_Group20,
Age_Group21,
into My_Table_Fact
from My_Table y
join Text1 x1 on y.Text1 = x1.Text1
join Text2 x2 on y.Text2 = x1.Text2
join Text3 x3 on y.Text3 = x1.Text3
join Text4 x4 on y.Text4 = x1.Text4
join Text5 x5 on y.Text5 = x1.Text5
然后在PowerBI中,我将引入事实表和维度表


我想知道这是否是从一个大表创建星型架构的正确方法,这是否是最好的方法?

如果有一个表有5000万行,由字符串键入,那么,您的方法似乎是存储表的更有效的方法——假设前五个字段有很多重复值

而不是最多存储(60*2+2+115*2+2+100*2+2+50*2+2+17*2+2)=694个字节。整数列是20个字节——因此节省空间的空间很大

至于创建表本身,我建议为此使用函数:

select identity(int) as text1_id, Text1 
into Text1_DIM      
from (select distinct Text1   
      from dbo.my_table t 
     ) t
order by text1;
您还可以将所有值放入一个表中:

select identity(int) as text_id, Text
into Text1_DIM      
from (select distinct v.Text 
      from dbo.my_table t cross apply
           (values (text1), (text2), (text3), (text4), (text5)) v(text)
     ) t
order by text;

唯一需要注意的是,当你把它带进桌子时,它是有效的。但是,如果更新了该表并重新导入该表,则维度上的值可能会更改。如果它们只与一个事实表一起使用,这实际上没有什么区别。

如果一个表有5000万行,由字符串键入,那么您的方法显然是一种更有效的存储表的方法——假设前五个字段有许多重复值

而不是最多存储(60*2+2+115*2+2+100*2+2+50*2+2+17*2+2)=694个字节。整数列是20个字节——因此节省空间的空间很大

至于创建表本身,我建议为此使用函数:

select identity(int) as text1_id, Text1 
into Text1_DIM      
from (select distinct Text1   
      from dbo.my_table t 
     ) t
order by text1;
您还可以将所有值放入一个表中:

select identity(int) as text_id, Text
into Text1_DIM      
from (select distinct v.Text 
      from dbo.my_table t cross apply
           (values (text1), (text2), (text3), (text4), (text5)) v(text)
     ) t
order by text;

唯一需要注意的是,当你把它带进桌子时,它是有效的。但是,如果更新了该表并重新导入该表,则维度上的值可能会更改。如果它们仅与一个事实表一起使用,这实际上没有什么区别。

这将导致数据库中的冗余,以及如何链接不同的文本维度。此外,如果不了解底层数据及其粒度,我们就无法推荐最佳方法。如果可能,共享一些示例行。根据我的理解,
Age\u Group…
字段将包含用户年龄组的标志。我会将此信息保存在一个维度中,并将SK作为外键引用。事实上,这将导致数据库中的冗余,以及如何链接不同的文本维度。此外,如果不了解底层数据及其粒度,我们就无法推荐最佳方法。如果可能,共享一些示例行。根据我的理解,
Age\u Group…
字段将包含用户年龄组的标志。我会将此信息保存在一个维度中,并将SK作为事实表中的外键引用