ssas 层次结构_分析服务(SSAS)多维设计技巧–关系和层次结构

ssas 层次结构

We have already discussed quite some design tips for building Analysis Services (SSAS) Multidimensional cubes and dimensions:

我们已经讨论了很多有关构建Analysis Services(SSAS)多维数据集和维度的设计技巧:

In this last part of the series we’ll look at attribute relationships and hierarchies in a dimension. As with the previous articles, the examples are created using the AdventureWorks 2014 Enterprise sample OLAP cube.

在本系列的最后一部分,我们将研究维度中的属性关系和层次结构。 与前面的文章一样,这些示例是使用AdventureWorks 2014 Enterprise示例OLAP cube创建的

层次结构 (Hierarchies)

Hierarchies are a useful tool in SSAS to reduce complexity between attributes and guide users into a certain drill-down behavior. In fact, it’s the first design tip SSAS gives you after creating a new dimension:

层次结构是SSAS中的有用工具,可以降低属性之间的复杂性,并指导用户进行某种向下钻取行为。 实际上,这是SSAS在创建新尺寸之后为您提供的第一个设计技巧:

We can find some examples in the Reseller dimension:

我们可以在转销商维度中找到一些示例:

In the browser, we can inspect the Reseller Bank hierarchy:

在浏览器中,我们可以检查转销商银行层次结构:

When building a Pivot table in Excel, it becomes clear how a hierarchy provides us with a clear drill-down path:

在Excel中构建数据透视表时,很清楚层次结构如何为我们提供清晰的向下钻取路径:

To avoid any confusion between the hierarchy levels and the attributes those levels originate from, it’s a good idea to hide the original attributes. For example, if you have a hierarchy with the levels Year – Month – Day and an attribute called Month, it might be confusing; why are there two month attributes? This is also a design tip provided by SSAS:

为了避免层次结构级别和这些级别所源自的属性之间的混淆,最好隐藏原始属性。 例如,如果您的层次结构的级别为Year – Month – Day,并且名为Month的属性可能会造成混淆; 为什么有两个月的属性? 这也是SSAS提供的设计技巧:

You can combine different attributes with each other, SSAS allows you to create hierarchies in all directions:

您可以相互组合不同的属性,SSAS允许您在各个方向上创建层次结构:

However, not all hierarchies might lead to optimal performance. SSAS warns you about this (see the yellow triangle with exclamation mark):

但是,并非所有层次结构都可能导致最佳性能。 SSAS为此警告您(请参阅带有感叹号的黄色三角形):

We’ll come back on this in the section about attribute relationships.

我们将在关于属性关系的部分中再次讨论这一点。

亲子层次 (Parent-child hierarchies)

A parent-child hierarchy is a special type of hierarchy. It’s number of levels can vary depending on the data. The best practices are quite simple: try to avoid them. Larger parent-child hierarchies have terrible performance and they are hard to secure. If you really need a parent-child structure, you can try to flatten the relationship into a pre-defined set of levels and use the HideMemberIf property to create a ragged hierarchy. You can read all about it in the article User-Defined Hierarchies – Ragged Hierarchies.

父子层次结构是一种特殊的层次结构。 它的级别数可以根据数据而有所不同。 最佳做法非常简单:尽量避免使用。 较大的父子层次结构的性能很差,而且很难保证。 如果确实需要父子结构,则可以尝试将关系展平为一组预先定义的级别,并使用HideMemberIf属性创建一个参差不齐的层次结构。 您可以在《 用户定义的层次结构-参差不齐的层次结构 》一文中阅读所有相关内容。

属性关系 (Attribute Relationships)

Attribute relationships are one of the most important concepts in SSAS dimensions. They tell the cube how the data is structured and they are vital to query performance. In the Reseller dimension, we can find the following relationships:

属性关系是SSAS维度中最重要的概念之一。 它们告诉多维数据集数据的结构,对查询性能至关重要。 在转销商维度中,我们可以找到以下关系:

This is the default behavior: every attribute is related to the key attribute (Reseller in this example). There are two kinds of relationships: flexible and rigid. Flexible relationships can change over time. For example, a reseller might change banks. Rigid relationships are fixed; they cannot change. Here, the relationship Reseller – Business Type is rigid (as indicated by the black arrow). This means that the business type will never change for a reseller. If it does, the processing of the dimension will fail if you use Process Update.

这是默认行为:每个属性都与键属性(在此示例中为Reseller)相关。 有两种关系: 柔性刚性 。 灵活的关系会随着时间而改变。 例如,经销商可能会更换银行。 刚性关系是固定的; 他们无法改变。 在这里,“经销商-业务类型”关系是固定的(如黑色箭头所示)。 这意味着,对于经销商,业务类型将永远不会改变。 如果确实如此,则在使用Process Update时 ,维度的处理将失败。

In a date dimension, you can typically set all relationships to rigid, as we can expect the calendar structure to never change.

在日期维度中,通常可以将所有关系设置为刚性,因为我们可以预期日历结构永远不会改变。

You can read the attribute relationships of the date dimension as follows: at the lowest level at the left we have the individual dates (which are the key attribute of the dimension). Dates roll up into months. Months roll up into quarters, quarters roll up into semesters and semesters finally roll up into years at the right.

您可以按以下方式读取日期维度的属性关系:在左侧的最低层,我们有各个日期(它们是维度的关键属性)。 日期累积到几个月。 几个月累加成季度,季度累加成学期,而学期最后累加成年。

For attribute relationships to function, data must be of a certain quality. There can be no duplicates (watch out for white space and different casing/accents) and a member cannot have multiple parents. For example, you cannot create an attribute relationship between month and year if you have the following data:

为了使属性关系起作用,数据必须具有一定的质量。 不能有重复项(注意空格和不同的大写字母/重音符号),并且成员不能有多个父母。 例如,如果您具有以下数据,则不能在月份和年份之间创建属性关系:

The problem here is that January is not unique. It appears in the years 2016 & 2017, which means it has multiple parents. If you would see the month January, you wouldn’t be able to tell from which year it is. The solution is simple: you either create a different month attribute that contains the year as well, to make it unique. For example: January, 2017. Or you can configure the existing attribute to have a composite key:

这里的问题是一月不是唯一的。 它出现在2016年和2017年,这意味着它有多个父母。 如果您看到一月,您将无法确定是哪一年。 解决方案很简单:您可以创建一个也包含年份的不同的month属性,以使其具有唯一性。 例如: 2017年1月 。 或者,您可以将现有属性配置为具有复合键:

Keep in mind SSAS won’t stop you from creating attribute relationships that aren’t supported by the underlying data. When processing the dimension however, you will be confronted with several error messages. Let’s illustrate with an example. Suppose we created the following attribute relationship between business type and Last Order Year in the Reseller dimension.

请记住,SSAS不会阻止您创建基础数据不支持的属性关系。 但是,在处理尺寸时,您将遇到一些错误消息。 让我们用一个例子来说明。 假设我们在“经销商”维度中在业务类型和“最后订单年”之间创建了以下属性关系。

When we process the dimension, we receive the following error:

处理尺寸时,会出现以下错误:

自然层次 (Natural hierarchies)

When you create a hierarchy and you create attribute relationships between the levels of the hierarchy, you have created a natural hierarchy. Such a hierarchy will be optimized for performance (for example when creating aggregations or a query plan). If you don’t have matching attribute relationships for the hierarchy, the hierarchy will be unbalanced and not optimized for performance. SSAS will warn you for this situation as discussed in the previous section.

创建层次结构并在层次结构的各个级别之间创建属性关系时,就创建了自然层次结构。 这样的层次结构将针对性能进行优化(例如,在创建聚合或查询计划时)。 如果该层次结构没有匹配的属性关系,则该层次结构将不平衡并且不会针对性能进行优化。 如上一节中所述,SSAS将针对这种情况警告您。

排序属性 (Sorting Attributes)

When there’s an attribute relationship between two attributes, you can use the child attribute to sort the parent attribute. A typical example is sorting the months. If you do not apply any sorting, months would be visualized in the following order:

当两个属性之间存在属性关系时,可以使用child属性对父属性进行排序。 一个典型的例子是对月份进行排序。 如果不进行任何排序,则月份将按以下顺序显示:

You can solve this issue in two ways:

您可以通过两种方式解决此问题:

  1. Assign the month number as the key column of the months, and the month name as the name column.

    将月份号指定为月份的关键列,并将月份名称指定为名称列。
  2. Or you can define an attribute relationship between Month and the Month Number attributes.

    或者,您可以定义月份和月份编号属性之间的属性关系。

With solution 1, you simply need to make sure the sorting is set to Key.

对于解决方案1,您只需要确保将排序设置为Key即可

Now the months will be sorted according to the Key column (1 = January, 2 = February and so on). For the other solution, we need to add an attribute relationship between Month of Year and Month Number:

现在,将根据“关键字”列对月份进行排序(1 =一月,2 =二月,依此类推)。 对于其他解决方案,我们需要在“月份的月份”和“月份的编号”之间添加属性关系:

Now we can change the OrderBy property of Month of Year to AttributeKey. The OrderByAttribute should be set to Month Number.

现在,我们可以将“月份的月份”的OrderBy属性更改为AttributeKey 。 OrderByAttribute应该设置为月数。

After processing the dimension, we can see the months are correctly sorted.

处理完维度后,我们可以看到月份已正确排序。

会员属性 (Member Properties)

Every attribute that has an attribute relationship with another attribute is a member property of that other attribute. This means there’s a direct relationship between those attributes. Let’s look at an example.

与另一个属性具有属性关系的每个属性都是该另一个属性的成员属性。 这意味着这些属性之间存在直接关系。 让我们来看一个例子。

In the date dimension, we have the attribute Calendar Quarter.

在日期维度中,我们具有日历季度属性。

It has attribute relationships with the following attributes:

它具有具有以下属性的属性关系:

  • Calendar Quarter of Year

    日历季度
  • Calendar Semester of Year

    年度日历学期
  • Calendar Semester

    日历学期
  • Fiscal Quarter

    财政季度

These attributes are also member properties of Calendar Quarter. We can easily check this in the dimension browser:

这些属性也是“日历季度”的成员属性。 我们可以在维度浏览器中轻松检查此内容:

Selecting items from the list would put them side by side with their parent attribute:

从列表中选择项目将使它们与它们的父属性并排放置:

SSAS can do this because it knows there’s a 1:1 relationship between the attribute and its member properties, thanks to the attribute relationships. Now, member properties become interesting for attributes that aren’t useful for direct analysis, because it wouldn’t make much sense to do so. Examples are phone numbers, email addresses, remarks etc. However, sometimes they are useful if you need more information about a certain member. For example, in an analysis with our top 5 worst performing sales persons, you might want to include the email addresses so you can easily contact them.

由于属性关系,SSAS知道属性与其成员属性之间存在1:1的关系,因此可以这样做。 现在,成员属性对于对直接分析无用的属性变得很有趣,因为这样做没有多大意义。 例如电话号码,电子邮件地址,备注等。但是,如果您需要有关某个特定成员的更多信息,有时它们很有用。 例如,在对我们表现最好的5名销售人员的分析中,您可能希望包括电子邮件地址,以便您轻松与他们联系。

Using such attributes directly in an analysis can cause performance problems. For example, suppose you have 30,000 customers each with a unique email address. If you create a pivot table with customers and their emails on the row axis, Excel will be slow because it will try to calculate subtotals for each customer and its email. This isn’t necessary because there’s a 1:1 relationship. We can avoid this by using the property AttributeHierarchyEnabled. When this is set to False, you won’t be able to use this attribute directly. You can only use it as a member property. Since SSAS knows there’s a 1:1 relationship, there will be no subtotals in Excel and performance will be much faster.

在分析中直接使用此类属性可能会导致性能问题。 例如,假设您有30,000个客户,每个客户都有一个唯一的电子邮件地址。 如果您在行轴上创建一个带有客户及其电子邮件的数据透视表,Excel将会变慢,因为它会尝试计算每个客户及其电子邮件的小计。 这是没有必要的,因为存在1:1的关系。 我们可以通过使用AttributeHierarchyEnabled属性来避免这种情况。 当将此设置为False时,您将无法直接使用此属性。 您只能将其用作成员属性。 由于SSAS知道存在1:1的关系,因此Excel中不会有小计,并且性能会更快。

In the Reseller dimension, we can see some attributes have been disabled (notice the gray icons).

在转销商维度中,我们可以看到一些属性已被禁用(请注意灰色图标)。

In Excel, we can see member properties in the tooltip:

在Excel中,我们可以在工具提示中看到成员属性:

You can also add member properties by right-clicking an attribute and selecting an item from the list Show Properties in Report.

您还可以通过右键单击一个属性,然后从“ 在报表显示属性 ”列表中选择一个项目来添加成员属性。

This will add the member property next to the parent attribute:

这会将member属性添加到parent属性旁边:

Notice the member property is not visible in the pivot table fields and there are no subtotals. Reporting Services also supports the use of member properties, but Power BI Desktop (currently) does not.

请注意,成员属性在数据透视表字段中不可见,并且没有小计。 Reporting Services还支持成员属性的使用,但Power BI Desktop(当前)不支持。

结论 (Conclusion)

In this article, we went over some best practices for hierarchies and relationships in SSAS dimensions. We showed why attribute relationships are important, why we would need hierarchies and how we can improve performance with member properties.

在本文中,我们介绍了有关SSAS维度中的层次结构和关系的一些最佳实践。 我们展示了为什么属性关系很重要,为什么我们需要层次结构以及如何使用成员属性提高性能。

Previous articles in this series

本系列以前的文章

参考链接 (Reference Links)

翻译自: https://www.sqlshack.com/analysis-services-ssas-multidimensional-design-tips-relations-hierarchies/

ssas 层次结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值