You are on page 1of 46

More Tricks of the Universe

Masters
David G. Rathbun, Integra Solutions
Topics

Introduction
Tricks of the Masters
Wrap Up
Q&A
ƒ Please hold questions until the end of the presentation

Slide 3
Introduction 1/3
A few words about me…
Focused on BusinessObjects Solutions since 1995
ƒ Consulting / mentoring / trouble shooting
ƒ Certified BusinessObjects trainer
ƒ Primary focus on knowledge transfer and client education
Selected to present at 1996 - 2005 User Conference
ƒ 10 consecutive years as a presenter
Charter member of BOB
ƒ http://busobj.forumtopics.com

Slide 5
Introduction 2/3
A few words about Designer…
Designer has changed very little since 4.0
ƒ Initially released in 1996
ƒ Introduced contexts, aggregate awareness, multiple SQL statements
ƒ Core technology has been stable for many years
Most important component of your implementation
ƒ A bad universe = a bad implementation
ƒ Think ZEN
• Zero Defect Coding
• Efficient SQL Generation
• Narrow Project Scope
ƒ Know the rules and when to break them

Slide 6
Introduction 3/3
What has changed over the years?

Designer Evolution

Version Feature
6.0 Find and replace
Integrity check does not check cardinalities by default
List of aliases available
6.1 Parameter settings as part of the universe
Save as PDF

6.5 ANSI 92 join syntax now available


Derived tables
Index awareness

Slide 7
Topics

Introduction
Tricks of the Masters
Wrap Up
Q&A

Slide 8
Tricks of the Masters

Intelligent SQL generation


Effective use of derived tables
Working with hierarchical data

Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 9
Intelligent SQL Generation

The pitfalls of the all-in-one solution


ƒ Join loops
ƒ Fan traps
ƒ Chasm traps
Not generally present in a “one-of” solution
Impact on success
ƒ Cause incorrect SQL to be generated
Techniques demonstrated in this section are essential to
the success of your project

Slide 10
Join Loops 1/4

Loops are often present in logical data diagrams

Slide 11
Join Loops 2/4
Why are loops bad?
SQL Generator uses all available paths
SELECT
Employees.EMP_LASTNAME,
Salary.SALARY_VALUE
FROM
Salary,
Employees,
Absenses,
Lookups
WHERE
( Absenses.EMP_ID=Employees.EMP_ID )
AND ( Salary.EMP_ID=Employees.EMP_ID )
AND ( Lookups.LOOKUP_ID=Salary.LOOKUP_ID )
AND ( Lookups.LOOKUP_ID=Absenses.LOOKUP_ID )

Slide 12
Join Loops 3/4

Join loops may be addressed using several techniques


ƒ Aliases
ƒ Contexts
ƒ Shortcuts
ƒ Multiple universes
What is the “best” solution for join loops?

Slide 13
Join Loops 4/4

There is no “best” solution!


Each loop must be examined independently
Make sure you evaluate all of your options

Slide 14
When Do I Use an Alias? 1/3

More than one logical purpose for the same table


Require different objects
ƒ Types of salary
ƒ Types of absences

Slide 15
When Do I Use an Alias? 2/3

Always set cardinality on every join


ƒ One – Many
ƒ One – One
A table on the one side is a candidate for an alias

Note: You should never have a Many – Many relationship in a production database!
Slide 16
When Do I Use an Alias? 3/3
Fixed the loop!

 Demonstration
Slide 17
When Do I Use a Context? 1/3

More than one logical path between two or more tables

Slide 18
When Do I Use a Context? 2/3

A table on the many side helps identify a context

Slide 19
When Do I Use a Context? 3/3

Each context identifies a path through the structure

Slide 20
Alias versus Context

There is no “better” answer


ƒ Do you have paths or purposes?
Aliases
ƒ Require additional objects for each purpose
ƒ Generally used on tables on the outside of the universe structure
ƒ Eliminate the loop
Contexts
ƒ Do not require additional objects
ƒ Do not actually eliminate the loop
ƒ Require ongoing maintenance
Are loops the only issue?

Slide 21
Chasm Trap Definition
A chasm spans a many – one – many relationship
Each showroom has many employees
Each showroom has many annual salary figures
The number of employee records should not impact the
total salary on the report

Slide 22
Chasm Trap Example 1/3

SELECT
Showrooms.SHOWROOM_NAME,
sum(Salary_Summary_Annual.ANNUAL_SAL_TOTAL)
FROM
Salary_Summary_Annual,
Showrooms
WHERE
( Salary_Summary_Annual.SHOWROOM_ID =
Showrooms.SHOWROOM_ID )
GROUP BY
Showrooms.SHOWROOM_NAME
Showroom Name Annual Salaray
Prestige Cars $316,720.00
Prestige Motors $565,730.00
Prestige Sports Cars $461,380.00
Sum: $1,343,830.00

Slide 23
Chasm Trap Example 2/3

SELECT
Showrooms.SHOWROOM_NAME,
Employees.EMP_LASTNAME,
sum(Salary_Summary_Annual.ANNUAL_SAL_TOTAL)
FROM
Employees,
Salary_Summary_Annual,
Showrooms
WHERE
( Employees.SHOWROOM_ID=Showrooms.SHOWROOM_ID )
AND (
Salary_Summary_Annual.SHOWROOM_ID=Showrooms.SHOWROOM_I
D )
GROUP BY
Showrooms.SHOWROOM_NAME,
Employees.EMP_LASTNAME

Slide 24
Chasm Trap Example 3/3

A chasm trap inflates the number of rows


ƒ X rows on the left
ƒ Y rows on the right
ƒ X * Y rows in the combined set
Can you prevent a query writer from using both objects?
ƒ No
Can you still get the correct answer?
ƒ Yes

Slide 25
Context Chasm Trap Solution 1/2

Set up contexts for each side of the chasm


BusinessObjects automatically generates separate SQL

Slide 26
Context Chasm Trap Solution 2/2

You can prevent users from spanning a chasm

Slide 27
Intelligent SQL Generation Review

The pitfalls of the all-in-one solution


ƒ Join loops
ƒ Fan traps (not discussed today)
ƒ Chasm traps
Not generally present in a “one-of” solution
Impact on success
ƒ Cause incorrect SQL to be generated
Designer gives us the tools to handle these issues

Slide 28
Tricks of the Masters

Intelligent SQL generation


Effective use of derived tables
Working with hierarchical data

Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 29
Effective Use of Derived Tables

Another option for aliases


Replace database views
Denormalize data
Create summary data
Eliminate intermediate joins
… all this and more

Slide 30
Creating A Derived Table 1/2

Insert + Derived Tables…

Enter desired SQL code


Select Models.MODEL_ID, Models.MODEL_NAME,
Models.MODEL_PRICE, Models.MODEL_COST,
Makers.MAKER_NAME, Styles.STYLE_NAME
FROM Models, Makers, Styles
WHERE Models.STYLE_ID = Styles.STYLE_ID
AND Models.MAKER_ID = Makers.MAKER_ID

Slide 31
Creating A Derived Table 2/2

This example shows


denormalized data
Could be done with a view
Are there better uses for
this technique?

Slide 32
Summary Data

Database views are static


Derived tables can include prompts
SELECT Departments.DEPT_ID, Departments.DEPT_NAME,
Lookups.LOOKUP_TEXT AS Salary_type,
sum(Salary.SALARY_VALUE) as Total_salary
FROM Departments, Employees, Salary, Lookups
WHERE Departments.DEPT_ID=Employees.DEPT_ID
AND Employees.EMP_ID=Salary.EMP_ID
AND Salary.LOOKUP_ID=Lookups.LOOKUP_ID
AND Lookups.LOOKUP_TYPE='SAL'
GROUP BY Departments.DEPT_ID, Departments.DEPT_NAME,
Lookups.LOOKUP_TEXT
HAVING sum(Salary.SALARY_VALUE) >= @Prompt('Enter
Department Salary Threshhold','N',,,)

 Demonstration
Slide 33
Tricks of the Masters

Intelligent SQL generation


Effective use of derived tables
Working with hierarchical data

Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 34
Hierarchical Data 1/2

Hierarchical data is not uncommon


ƒ Company organizational chart
ƒ Manufacturing BOM (Bill of Materials)
ƒ Referral tracking
Hierarchical data is recursive
ƒ A table joins to itself
Employee ID Last Name Manager ID
ƒ Depth is unlimited 101 Noakes
ƒ Depth is unknown 102 Ferrerez 101
103 Field 102
SQL is not recursive
104 Fraser 101
How can we solve this? 105 Snow 101
106 Speed 105

Slide 35
Hierarchical Data 2/2
Some typical questions
Who do I work for?
Who are my direct reports?
ƒ Who reports directly to me in the organization
Who are my indirect reports?
ƒ Who works for someone that works for me
What is my level in the hierarchy?
ƒ Show my parents in the tree
ƒ Show my children in the tree

Slide 36
Alias Solution 1/2
One solution
Use aliases to represent the hierarchical relationships

 Demonstration
Slide 37
Alias Solution 2/2
The problems
Unknown depth
ƒ Each new level requires an alias
ƒ Adding new employees should not require universe maintenance
Varying depth
ƒ An organization tree is not always balanced
ƒ Different employees have different depths
ƒ Outer joins are used to allow for missing data
ƒ Outer joins can cause performance issues
ƒ Not all databases support outer join chains (sequences of outer joins)

Slide 38
Creating New Tables 1/2
Another solution
Create a new table to flatten out the structure
ƒ Employee ID
ƒ Manager ID
ƒ Top Level ID
Each person exists as a top level ID
ƒ Who works for me?
Each person exists as an Employee ID
ƒ Who do I work for?
ƒ What is my level?

Slide 39
Creating New Tables 2/2
The problems
This table has to be created / populated / updated
Stores each potential manager as a top level
ƒ Inflates the amount of data
Have to change objects if I change questions
ƒ Who works for me?
ƒ Who do I work for?
ƒ What is my level in the hierarchy?

Slide 40
Tree Traversal Data 1/5
Store the tree traversal path in the original table
Assign each person (node) a left and right value
Value is determined by tree traversal order

1 Noakes 16

2 Ferrerez 3 4 Fraser 5 6 Snow 7 8 Pickworth 15


Forest Porter Duckworth
Note: This is not the complete tree 9 10 11 12 13 14

Tree traversal means walk around the outside edge


ƒ Number each node each time you pass the left or right side
ƒ Store those values in the original source table

Slide 41
Tree Traversal Data 2/5
Using left and right data to build the hierarchy
ID Full Name Left Tree Right Tree Name Descendants Parents Level
101 Noakes, Nicholas 1 54 Noakes, Nicholas 26 0 1
102 Ferrerez, Ferdinand 2 5 Ferrerez, Ferdinand 1 1 2
103 Field, Felicity 3 4 Field, Felicity 0 2 3
104 Fraser, Frank 6 7 Fraser, Frank 0 1 2
105 Snow, Sara 8 13 Snow, Sara 2 1 2
106 Speed, Sonya 9 10 Speed, Sonya 0 2 3
107 Spencer, Steve 11 12 Spencer, Steve 0 2 3
108 Helen, Harrison 14 15 Helen, Harrison 0 1 2
109 Thomas, Tom 16 19 Thomas, Tom 1 1 2
110 Thatcher, Terry 17 18 Thatcher, Terry 0 2 3
111 Davis, Diana 20 21 Davis, Diana 0 1 2
201 Pickworth, Paul 22 39 Pickworth, Paul 8 1 2
202 Forest, Florence 23 28 Forest, Florence 2 2 3
203 Brown, Bella 24 25 Brown, Bella 0 3 4
204 Porter, Pete 29 34 Hilary, Hibbs 0 3 4
205 Irving, Ira 30 31 Porter, Pete 2 2 3
206 Bailey, Ben 32 33 Irving, Ira 0 3 4
207 Duckworth, Dave 35 38 Bailey, Ben 0 3 4
208 Ince, Ian 36 37 Duckworth, Dave 1 2 3
209 Hilary, Hibbs 26 27 Ince, Ian 0 3 4

 Demonstration
Slide 42
Tree Traversal Data 3/5

Inserts and updates must adjust left and right values

1 Noakes 18

2 Ferrerez 3 Fraser 5 6 Snow 7 8 Pickworth 17


Forest Porter Duckworth
9 12 13 14 15 16
New
10 11

Update table set left = left + 2 where left >= 10;


Update table set right = right + 2 where right >= 10;
Insert (name, left, right) values (‘New’, 10, 11);

Slide 43
Tree Traversal Data 4/5
Revisiting derived tables
Including the following derived table in our universe…
SELECT A.EMP_ID,
(A.RIGHT_TREE-A.LEFT_TREE-1)/2 AS Number_of_descendants,
COUNT(B.EMP_ID) AS Employee_Level,
COUNT(B.EMP_ID)-1 AS Number_of_parents
FROM Employees A, Employees B
WHERE B.LEFT_TREE <= A.LEFT_TREE AND B.RIGHT_TREE >= A.RIGHT_TREE
GROUP BY A.EMP_ID, (A.RIGHT_TREE-A.LEFT_TREE-1)/2

… enables us to answer the required questions


ƒ Who do I work for?
ƒ Who are my direct reports?
ƒ Who are my indirect reports?
ƒ What is my level in the hierarchy?

Slide 44
Tree Traversal Data 5/5

Who do I work for?


ƒ Left < My Left and Right > My Right and Level = My Level - 1
Who are my direct descendants?
ƒ Left > My Left and Right < My Right and Level = My Level + 1
Who are my indirect descendants?
ƒ Left > My Left and Right < My Right and Level > My Level + 1
Who are all of my descendants?
ƒ Left > My Left and Right < My Right
What is my level in the hierarchy?
ƒ Provided directly by the derived table
Create a complete tree
ƒ Display all records order by LEFT indent by LEVEL

Slide 45
Hierarchical Data Recap

There are various solutions for handling hierarchies


ƒ Aliases
ƒ Extra tables
ƒ Tree traversal
Tree traversal data seems to provide the best solution
ƒ Sample data included one hierarchy only
ƒ If multiple hierarchies are present add the TREE_ID to each node
ƒ Assign TREE_ID during the initial tree traversal process
Database views may be substituted for derived tables
ƒ Reminder: Derived tables were added in Designer 6.5

Slide 46
Tricks of the Masters

Intelligent SQL generation


ƒ Resolve loops
ƒ Resolve chasm traps
ƒ Resolve fan traps
Effective use of derived tables
ƒ Denormalize data
ƒ Summarize data
ƒ Include prompts
Working with hierarchical data
ƒ Number each node according to its position in the tree path
ƒ Use that information to answer typical hierarchical questions

Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 47
Q&A

Questions?
Contact information
ƒ David G. Rathbun
ƒ Integra Solutions
ƒ www.IntegraSolutions.net

Slide 48

You might also like