# Indexing database query results

Indexing the result set from a ColdFusion query involves an extra step not required when indexing documents. You need to code the query and output parameters, then point the CFINDEX tag at the result set from a CFQUERY, CFLDAP, or CFPOP query.

| To index a ColdFusion query:

1. Create the collection on the ColdFusion Administrator Verity page.
2. Execute a query and output the data.
3. Populate the collection using the CFINDEX tag.

To populate a collection from a CFQUERY you specify a KEY, which corresponds to the primary key of the data source, and the BODY, the column in which you want to conduct searches. The following extract shows only the CFQUERY and CFINDEX parts of the process.

    <!--- Select the entire table --->
    <CFQUERY NAME="Messages"
        DATASOURCE="MyMail">
        SELECT *
            FROM Messages
    </CFQUERY>

    <!--- Output the result set --->
    <CFOUTPUT QUERY="Messages">
        #Message_ID#, #Subject#, #Title#, #MessageText#

    </CFOUTPUT>

    <!--- Index the result set --->
    <CFINDEX COLLECTION="DBINDEX"
        ACTION="UPDATE"
        TYPE="CUSTOM"
        BODY="MessageText"
        KEY="Message_ID"
        TITLE="Subject"
        QUERY="Messages">

This CFINDEX statement specifies the MessageText column as the core of the collection and names the table's primary key, the Message_ID column, as the KEY value. Note that the TITLE attribute names the Subject column. The TITLE attribute can be used to designate an output parameter.

To index more than one column in a collection, enter a comma-separated list of column names for values of the BODY attribute, such as:

    BODY=FirstName,LastName,Company

### Advantages of indexing a data source

The main advantage of performing searches against a Verity collection over using CFQUERY alone is that the database is indexed in a form that provides faster access. Use this technique instead of CFQUERY in the following cases:

- You want to index textual data. Verity collections containing textual data can be searched much more efficiently with CFINDEX than searching a database with CFQUERY.
- You want to give your users access to data without interacting directly with the data source itself.
- You want to improve the speed of queries.
- You want your end users to run queries but not update a database table.
- You do not want to expose your data source.