how to resolve loops
TRANSCRIPT
More Tricks of the Universe Masters
More Tricks of the Universe Masters
David G. Rathbun, Integra Solutions
Slide 3
IntroductionTricks of the MastersWrap UpQ&A
Please hold questions until the end of the presentation
Topics
Slide 5
Introduction 1/3
Focused on BusinessObjects Solutions since 1995Consulting / mentoring / trouble shootingCertified BusinessObjects trainerPrimary focus on knowledge transfer and client education
Selected to present at 1996 - 2005 User Conference10 consecutive years as a presenter
Charter member of BOBhttp://busobj.forumtopics.com
A few words about me…
Slide 6
Introduction 2/3
Designer has changed very little since 4.0Initially released in 1996Introduced contexts, aggregate awareness, multiple SQL statementsCore technology has been stable for many years
Most important component of your implementationA bad universe = a bad implementationThink ZEN
• Zero Defect Coding• Efficient SQL Generation• Narrow Project Scope
Know the rules and when to break them
A few words about Designer…
Slide 7
Introduction 3/3
ANSI 92 join syntax now availableDerived tablesIndex awareness
6.5
Parameter settings as part of the universeSave as PDF
6.1
Find and replaceIntegrity check does not check cardinalities by defaultList of aliases available
6.0FeatureVersion
Designer Evolution
What has changed over the years?
Slide 8
IntroductionTricks of the MastersWrap UpQ&A
Topics
Slide 9
Intelligent SQL generationEffective use of derived tablesWorking with hierarchical data
Tricks of the Masters
Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 10
Intelligent SQL Generation
The pitfalls of the all-in-one solutionJoin loopsFan trapsChasm traps
Not generally present in a “one-of” solutionImpact on success
Cause incorrect SQL to be generated
Techniques demonstrated in this section are essential to the success of your project
Slide 11
Join Loops 1/4
Loops are often present in logical data diagrams
Slide 12
Join Loops 2/4
SQL Generator uses all available paths
Why are loops bad?
SELECTEmployees.EMP_LASTNAME,Salary.SALARY_VALUE
FROMSalary,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 13
Join Loops 3/4
Join loops may be addressed using several techniquesAliasesContextsShortcutsMultiple universes
What is the “best” solution for join loops?
Slide 14
Join Loops 4/4
There is no “best” solution!Each loop must be examined independentlyMake sure you evaluate all of your options
Slide 15
When Do I Use an Alias? 1/3
More than one logical purpose for the same tableRequire different objects
Types of salaryTypes of absences
Slide 16
When Do I Use an Alias? 2/3
Always set cardinality on every joinOne – ManyOne – 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 17
When Do I Use an Alias? 3/3Fixed the loop!
Demonstration
Slide 18
When Do I Use a Context? 1/3
More than one logical path between two or more tables
Slide 19
When Do I Use a Context? 2/3
A table on the many side helps identify a context
Slide 20
When Do I Use a Context? 3/3
Each context identifies a path through the structure
Slide 21
Alias versus Context
There is no “better” answerDo you have paths or purposes?
AliasesRequire additional objects for each purposeGenerally used on tables on the outside of the universe structureEliminate the loop
ContextsDo not require additional objectsDo not actually eliminate the loopRequire ongoing maintenance
Are loops the only issue?
Slide 22
Chasm Trap DefinitionA chasm spans a many – one – many relationship
Each showroom has many employeesEach showroom has many annual salary figuresThe number of employee records should not impact the total salary on the report
Slide 23
Chasm Trap Example 1/3
SELECTShowrooms.SHOWROOM_NAME,sum(Salary_Summary_Annual.ANNUAL_SAL_TOTAL)
FROMSalary_Summary_Annual,Showrooms
WHERE( Salary_Summary_Annual.SHOWROOM_ID =
Showrooms.SHOWROOM_ID )GROUP BY
Showrooms.SHOWROOM_NAME
Showroom Name Annual SalarayPrestige Cars $316,720.00Prestige Motors $565,730.00Prestige Sports Cars $461,380.00
Sum: $1,343,830.00
Slide 24
Chasm Trap Example 2/3
SELECTShowrooms.SHOWROOM_NAME,Employees.EMP_LASTNAME,sum(Salary_Summary_Annual.ANNUAL_SAL_TOTAL)
FROMEmployees,Salary_Summary_Annual,Showrooms
WHERE( Employees.SHOWROOM_ID=Showrooms.SHOWROOM_ID )AND (
Salary_Summary_Annual.SHOWROOM_ID=Showrooms.SHOWROOM_ID )GROUP BY
Showrooms.SHOWROOM_NAME, Employees.EMP_LASTNAME
Slide 25
Chasm Trap Example 3/3
A chasm trap inflates the number of rowsX rows on the leftY rows on the rightX * 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 26
Context Chasm Trap Solution 1/2
Set up contexts for each side of the chasmBusinessObjects automatically generates separate SQL
Slide 27
Context Chasm Trap Solution 2/2
You can prevent users from spanning a chasm
Slide 28
Intelligent SQL Generation Review
The pitfalls of the all-in-one solutionJoin loopsFan traps (not discussed today)Chasm traps
Not generally present in a “one-of” solutionImpact on success
Cause incorrect SQL to be generated
Designer gives us the tools to handle these issues
Slide 29
Intelligent SQL generationEffective use of derived tablesWorking with hierarchical data
Tricks of the Masters
Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 30
Effective Use of Derived Tables
Another option for aliasesReplace database viewsDenormalize dataCreate summary dataEliminate intermediate joins… all this and more
Slide 31
Insert + Derived Tables…
Enter desired SQL code
Creating A Derived Table 1/2
Select Models.MODEL_ID, Models.MODEL_NAME, Models.MODEL_PRICE, Models.MODEL_COST, Makers.MAKER_NAME, Styles.STYLE_NAMEFROM Models, Makers, StylesWHERE Models.STYLE_ID = Styles.STYLE_IDAND Models.MAKER_ID = Makers.MAKER_ID
Slide 32
Creating A Derived Table 2/2
This example shows denormalized dataCould be done with a viewAre there better uses for this technique?
Slide 33
Summary Data
Database views are staticDerived tables can include promptsSELECT Departments.DEPT_ID, Departments.DEPT_NAME, Lookups.LOOKUP_TEXT AS Salary_type, sum(Salary.SALARY_VALUE) as Total_salaryFROM Departments, Employees, Salary, LookupsWHERE Departments.DEPT_ID=Employees.DEPT_IDAND Employees.EMP_ID=Salary.EMP_IDAND Salary.LOOKUP_ID=Lookups.LOOKUP_IDAND Lookups.LOOKUP_TYPE='SAL'GROUP BY Departments.DEPT_ID, Departments.DEPT_NAME, Lookups.LOOKUP_TEXTHAVING sum(Salary.SALARY_VALUE) >= @Prompt('EnterDepartment Salary Threshhold','N',,,)
Demonstration
Slide 34
Intelligent SQL generationEffective use of derived tablesWorking with hierarchical data
Tricks of the Masters
Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 35
Hierarchical Data 1/2
Hierarchical data is not uncommonCompany organizational chartManufacturing BOM (Bill of Materials)Referral tracking
Hierarchical data is recursiveA table joins to itselfDepth is unlimitedDepth is unknown
SQL is not recursiveHow can we solve this?
Employee ID Last Name Manager ID101 Noakes102 Ferrerez 101103 Field 102104 Fraser 101105 Snow 101106 Speed 105
Slide 36
Hierarchical Data 2/2
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 treeShow my children in the tree
Some typical questions
Slide 37
Alias Solution 1/2
Use aliases to represent the hierarchical relationships
One solution
Demonstration
Slide 38
Alias Solution 2/2
Unknown depthEach new level requires an aliasAdding new employees should not require universe maintenance
Varying depthAn organization tree is not always balancedDifferent employees have different depthsOuter joins are used to allow for missing dataOuter joins can cause performance issuesNot all databases support outer join chains (sequences of outer joins)
The problems
Slide 39
Creating New Tables 1/2
Create a new table to flatten out the structureEmployee IDManager IDTop Level ID
Each person exists as a top level IDWho works for me?
Each person exists as an Employee IDWho do I work for?What is my level?
Another solution
Slide 40
Creating New Tables 2/2
This table has to be created / populated / updatedStores each potential manager as a top level
Inflates the amount of data
Have to change objects if I change questionsWho works for me?Who do I work for?What is my level in the hierarchy?
The problems
Slide 41
Tree Traversal Data 1/5
Assign each person (node) a left and right valueValue is determined by tree traversal order
Tree traversal means walk around the outside edgeNumber each node each time you pass the left or right sideStore those values in the original source table
Store the tree traversal path in the original table
Noakes
Ferrerez Fraser Snow Pickworth
Forest Porter Duckworth
5 6
9 10
7 8 15
11 12 13 14
161
2 3 4
Note: This is not the complete tree
Slide 42
Tree Traversal Data 2/5
ID Full Name Left Tree Right Tree101 Noakes, Nicholas 1 54102 Ferrerez, Ferdinand 2 5103 Field, Felicity 3 4104 Fraser, Frank 6 7105 Snow, Sara 8 13106 Speed, Sonya 9 10107 Spencer, Steve 11 12108 Helen, Harrison 14 15109 Thomas, Tom 16 19110 Thatcher, Terry 17 18111 Davis, Diana 20 21201 Pickworth, Paul 22 39202 Forest, Florence 23 28203 Brown, Bella 24 25204 Porter, Pete 29 34205 Irving, Ira 30 31206 Bailey, Ben 32 33207 Duckworth, Dave 35 38208 Ince, Ian 36 37209 Hilary, Hibbs 26 27
Name Descendants Parents LevelNoakes, Nicholas 26 0 1 Ferrerez, Ferdinand 1 1 2 Field, Felicity 0 2 3 Fraser, Frank 0 1 2 Snow, Sara 2 1 2 Speed, Sonya 0 2 3 Spencer, Steve 0 2 3 Helen, Harrison 0 1 2 Thomas, Tom 1 1 2 Thatcher, Terry 0 2 3 Davis, Diana 0 1 2 Pickworth, Paul 8 1 2 Forest, Florence 2 2 3 Brown, Bella 0 3 4 Hilary, Hibbs 0 3 4 Porter, Pete 2 2 3 Irving, Ira 0 3 4 Bailey, Ben 0 3 4 Duckworth, Dave 1 2 3 Ince, Ian 0 3 4
Using left and right data to build the hierarchy
Demonstration
Slide 43
Tree Traversal Data 3/5
Inserts and updates must adjust left and right values
Noakes
Ferrerez Fraser Snow Pickworth
Forest Porter Duckworth
5 6
9
10
7 8 17
11
14 15 16
181
2 3
New12 13
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 44
Tree Traversal Data 4/5
Including the following derived table in our universe…
… enables us to answer the required questionsWho do I work for?Who are my direct reports?Who are my indirect reports?What is my level in the hierarchy?
Revisiting derived tables
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 BWHERE B.LEFT_TREE <= A.LEFT_TREE AND B.RIGHT_TREE >= A.RIGHT_TREEGROUP BY A.EMP_ID, (A.RIGHT_TREE-A.LEFT_TREE-1)/2
Slide 45
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 treeDisplay all records order by LEFT indent by LEVEL
Slide 46
Hierarchical Data Recap
There are various solutions for handling hierarchiesAliasesExtra tablesTree traversal
Tree traversal data seems to provide the best solutionSample data included one hierarchy onlyIf multiple hierarchies are present add the TREE_ID to each nodeAssign TREE_ID during the initial tree traversal process
Database views may be substituted for derived tablesReminder: Derived tables were added in Designer 6.5
Slide 47
Intelligent SQL generationResolve loopsResolve chasm trapsResolve fan traps
Effective use of derived tablesDenormalize dataSummarize dataInclude prompts
Working with hierarchical dataNumber each node according to its position in the tree pathUse that information to answer typical hierarchical questions
Tricks of the Masters
Demonstrations today will use Designer 6.5 although many techniques are valid in 5.x
Slide 48
Questions?Contact information
David G. RathbunIntegra Solutionswww.IntegraSolutions.net
Q&A