Administration: Omnidex Indexing

ActiveCounts

The SQL Behind ActiveCounts

The example on the previous page demonstrated ActiveCounts in a web page; however, ActiveCounts is not a set of web controls. ActiveCounts is really just a powerful set of SQL statements that are highly optimized using the Omnidex indexes. This means that ActiveCounts can be implemented in any application that calls SQL.

ActiveCounts is accomplished using SQL statements that request a set of values and their counts based on the criteria so far. The following basic SQL statements supply the counts needed for the example on the previous page.

First, the user requested to see the states and their counts:

  select        STATE, count(*)
    from        LIST
    group by    STATE;

The user selected the states in the Western United States, and then requested to see the Genders and their counts:

  select        GENDER, count(*)
    from        LIST
    where       STATE in ('AZ','CA','CO','NM','OR','UT','WA')
    group by    GENDER;

The user selected the Women in the Western United States, and then requested to see the Educations and their counts:

  select        EDUCATION, count(*)
    from        LIST
    where       STATE in ('AZ','CA','CO','NM','OR','UT','WA') and 
                GENDER = 'F'
    group by    EDUCATION;

The user selected those people with graduate degrees, and may have continued narrowing his search.

As you can see, these SQL statements simple request counts that are filtered by the criteria entered so far, grouped by the next value they want to see. The simplicity of this approach allows it to be more easily integrated into an application.

More Complex SQL

The actual SQL statements for the example on the previous page was slightly more complex, only in that it references dimension tables to provide descriptions rather than codes. The actual SQL statements are as follows:

First, the presentation of the States:

  select        STATES.DESCRIPTION,
                LIST.STATE,
                count(*)
    from        LIST
    join        STATES on LIST.STATE = STATES.STATE
    group by    STATES.DESCRIPTION,
                LIST.STATE;

Notice that the DESCRIPTION column was retrieved for presentation, and the STATE column was retrieved for inclusion in future criteria.

Second, the presentation of the Genders:

  select        GENDERS.DESCRIPTION,
                LIST.GENDER,
                count(*)
    from        LIST
    join        GENDERS on LIST.GENDER = GENDERS.GENDER
    where       LIST.STATE in ('AZ', 'CA', 'CO', 'NM', 'OR', 'UT', 'WA')
    group by    GENDERS.DESCRIPTION,
                LIST.GENDER;

Third, the presentation of the Educations:

  select        $ifnull(EDUCATION.DESCRIPTION, case LIST.EDUCATION
                  when ' ' then 'Did not specify'
                  else LIST.EDUCATION
                end),
                LIST.EDUCATION,
                count(*)
    from        LIST
    left join   EDUCATION on LIST.EDUCATION = EDUCATION.EDUCATION
    where       LIST.GENDER = 'F' and
                LIST.STATE in ('AZ', 'CA', 'CO', 'NM', 'OR', 'UT', 'WA')
    group by    EDUCATION.DESCRIPTION,
                LIST.EDUCATION
    order by    LIST.EDUCATION;

Notice that this SQL statement is more complex, involving $IFNULL functions and Left Outer Joins. These were beneficial because not every row in the LIST table had a value in the EDUCATION column. This SQL would be the standard approach for obtaining the descriptions for those entries that did have a value. This example shows that the SQL can be more complex and still be used in ActiveCounts.

All of these SQL statements are straightforward, traditional SQL. The secret to ActiveCounts is the optimization of the SQL statements. Specialized optimization that uses the Omnidex indexing allows these queries to be fast, even on very large databases. Continue on to read about this optimization.

Additional Resources

See also:

 
Back to top
admin/indexing/activecounts/sql.txt ยท Last modified: 2016/06/28 22:38 (external edit)