This is an old revision of the document!


Administration: Indexing Strategies

Basic Strategies

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 do 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. This occurs when one index contains a superset of all of the columns for several queries. 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 will begin to degrade as the index width increases, so indexes should not contain an excessive number of columns. A good rule of thumb is to keep the width of these indexes below 64 bytes, though they can be as large as 240 bytes.

Example 1. Ungrouped COUNT(*) aggregations

In this example, the COUNT(*) does not require any additional indexes.

select     count(*)
  from     INDIVIDUALS
  where    GENDER = 'M' and NAME = 'John'

The GENDER 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    BIRTHDATE = 'Jan 10, 1986' and NAME = 'John'
  group by GENDER

The BIRTHDATE and NAME columns must be indexed to satisfy the criteria and the GENDER column must be indexed to satisfy the GROUP BY clause. As discussed earlier, GENDER is a low-cardinality column and can be an Omnidex Bitmap index.

Example 3. Aggregations between a child and multiple parents

In this example, the GROUP BY clause contains columns from multiple parents.

select     S.DESCRIPTION, I.ZIP, G.DESCRIPTION, count(DISTINCT I.HOUSEHOLD)
  from     INDIVIDUALS I join STATES S on S.STATE = I.STATE 
                         join GENDERS G on I.GENDER = G.GENDER 
  group by S.DESCRIPTION, I.ZIP, G.DESCRIPTION

This statement is indexed by creating a multi-column Omnidex index on the STATE, ZIP, GENDER and HOUSEHOLD columns in INDIVIDUALS. The STATE and GENDER columns are included because they are foreign keys that correlate to the STATES.DESCRIPTION and GENDERS.DESCRIPTION columns. The HOUSEHOLD column is included because it is referenced in an aggregation.

Example 4. Multiple aggregations satisfied by one index

In this example, the both SQL statements can be satisfied by one Omnidex index.

select     GENDER, BIRTHDATE, count(DISTINCT HOUSEHOLD)
  from     INDIVIDUALS
  group by GENDER, BIRTHDATE
select     HOUSEHOLD, GENDER, count(*)
  from     INDIVIDUALS
  group by HOUSEHOLD, GENDER

A multi-column Omnidex index containing GENDER, BIRTHDATE and HOUSEHOLD would satisfy both of these statements because all GROUP BY columns and all aggregated columns can be found in this index.

Sample Environment File

This sample environment file shows the Omnidex indexes that will optimize these queries.

> create environment
>>  in                   "simple.xml"
>>  with                 delete;
Environment created in simple.xml
>
>
> create database       "SIMPLE"
>>   type                FLATFILE
>>   index_directory     "idx"
>>  in                   "simple.xml";
Database SIMPLE created in simple.xml
>
>
> create table          "HOUSEHOLDS"
>>  physical             "dat\households.dat"
>>  (
>>   "HOUSEHOLD"         CHARACTER(12),
>>   "ADDRESS"           CHARACTER(50),
>>   "CITY"              CHARACTER(28)     quicktext,
>>   "STATE"             CHARACTER(2)      omnidex,
>>   "ZIP"               CHARACTER(5),
>>   "COUNTRY"           CHARACTER(2),
>>   constraint HOUSEHOLDS_HOUSEHOLD_PK primary ("HOUSEHOLD")
>>  )
>>  in                   "simple.xml";
Table HOUSEHOLDS created in simple.xml
>
>
> create table          "INDIVIDUALS"
>>  physical             "dat\individuals.dat"
>>  (
>>   "INDIVIDUAL"        CHARACTER(12),
>>   "HOUSEHOLD"         CHARACTER(12),
>>   "NAME"              CHARACTER(50)     quicktext,
>>   "GENDER"            CHARACTER(1)      omnidex bitmap,
>>   "BIRTHDATE"         ANSI DATE         omnidex,
>>   "PHONE"             CHARACTER(14),
>>   "EMAIL"             CHARACTER(60),
>>   constraint INDIVIDUALS_INDIVIDUAL_PK primary ("INDIVIDUAL"),
>>   constraint INDIVIDUALS_HOUSEHOLD_FK foreign ("HOUSEHOLD") references "HOUSEHOLDS"
>>  )
>>  in                   "simple.xml";
Table INDIVIDUALS created in simple.xml

Additional Resources

See also:

 
Back to top
admin/indexing/basicstrategies/aggregations.1294970502.txt.gz ยท Last modified: 2012/10/26 14:52 (external edit)