This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
admin:indexing:basicstrategies:aggregations [2011/01/13 22:44] els created |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | {{page>:top_add&nofooter&noeditbtn}} | ||
| - | ====== Administration: Indexing Strategies ====== | ||
| - | |||
| - | ===== Basic Strategies ===== | ||
| - | |||
| - | [[admin:indexing:basicstrategies:home|Overview]] | | ||
| - | [[admin:indexing:basicstrategies:criteria|Criteria]] | | ||
| - | [[admin:indexing:basicstrategies:joins|Table Joins]] | | ||
| - | **[[admin:indexing:basicstrategies:aggregations|Aggregations]]** | | ||
| - | [[admin:indexing:basicstrategies:orderby|Order By's]] | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ==== Optimizing Aggregations ==== | ||
| - | |||
| - | Aggregations are usually optimized by creating an index containing all of the columns in the GROUP BY clause and all of the columns referenced in COUNT, SUM, MIN, MAX and AVERAGE functions. It is preferable to have the columns in the GROUP BY clause precede the columns being aggregated. | ||
| - | |||
| - | The COUNT(*) aggregation is treated uniquely. The use of the asterisk does not equate to an individual column and does not expand the side of the index. In fact, Omnidex maintains counts at each step of the search, so simple COUNT(*) aggregations no not need specialized indexing. | ||
| - | |||
| - | Some applications will use GROUP BY clauses that consist of columns from a child table and columns from parent or grandparent tables. In these cases, the Omnidex index in the child should include all columns from the child table as well as the foreign keys that point to the parent table. | ||
| - | |||
| - | It is fine for one index to service multiple aggregations be containing a superset of all of the columns. For example, an index on columns A, B, C and D would allow sums of D grouped by A, and also averages of D and C grouped by B and A. The performance of these aggregations degrades as the index width increases, so indexes should not contain an excessive number of columns. | ||
| - | |||
| - | == Example 1. Ungrouped COUNT(*) aggregations == | ||
| - | |||
| - | In this example, the COUNT(*) does not require any additional indexes. | ||
| - | |||
| - | select count(*) | ||
| - | from INDIVIDUALS | ||
| - | where STATE = 'CO' and NAME = 'John' | ||
| - | |||
| - | The STATE and NAME indexes which were created to satisfy the criteria will also satisfy the requested count. | ||
| - | |||
| - | == Example 2. Grouped COUNT(*) aggregations == | ||
| - | |||
| - | In this example, the COUNT(*) does not require any additional indexes, but the GROUP BY clause requires an index. | ||
| - | |||
| - | select GENDER, count(*) | ||
| - | from INDIVIDUALS | ||
| - | where STATE = 'CO' and NAME = 'John' | ||
| - | group by GENDER | ||
| - | |||
| - | The STATE and NAME columns must be indexed to satisfy the criteria and the GENDER column must be indexed to satisfy the GROUP BY clause. | ||
| - | |||
| - | == Example 3. Aggregations between a child and multiple parents == | ||
| - | |||
| - | In this example, the GROUP BY clause contains columns from multiple parents. | ||
| - | |||
| - | select STATES.DESCRIPTION, INDIVIDUALS.ZIP, GENDERS.DESCRIPTION, max(INDIVIDUALS.AGE) | ||
| - | from INDIVIDUALS join STATES.STATE = INDIVIDUALS.STATE | ||
| - | join GENDERS on INDIVIDUALS.GENDER = GENDERS.GENDER | ||
| - | group by STATES.DESCRIPTION, INDIVIDUALS.ZIP, GENDERS.DESCRIPTION | ||
| - | |||
| - | This statement is indexed by creating a multi-column Omnidex index on the STATE, ZIP, GENDER and AGE columns in INDIVIDUALS. The STATE and GENDER columns are foreign keys that correlate to the STATES.DESCRIPTION and GENDERS.DESCRIPTION columns. | ||
| - | |||
| - | == Example 4. Multiple aggregations satisfied by one index == | ||
| - | |||
| - | In this example, the both SQL statements can be satisfied by one Omnidex index. | ||
| - | |||
| - | select GENDER, INCOME, count(*), count(DISTINCT HOUSEHOLD) | ||
| - | from INDIVIDUALS | ||
| - | group by GENDER, INCOME | ||
| - | |||
| - | select GENDER, EDUCATION, MAX(AGE), MIN(AGE), | ||
| - | from INDIVIDUALS | ||
| - | group by GENDER, EDUCATION | ||
| - | |||
| - | A multi-column Omnidex index containing GENDER, INCOME, EDUCATION, AGE and HOUSEHOLD would satisfy both of these statements because all GROUP BY columns and all aggregated columns can be found in this index. | ||
| - | |||
| - | ===== ===== | ||
| - | |||
| - | **[[prev|Prev]]** | **[[next|Next]]** | ||
| - | |||
| - | ====== Additional Resources ====== | ||
| - | |||
| - | See also: | ||
| - | |||
| - | {{page>:admin:indexing:see_also&nofooter&noeditbtn}} | ||
| - | |||
| - | {{page>:bottom_add&nofooter&noeditbtn}} | ||