Add initial optimizations
If your query will include aggregate functions on one or more fields, adding a bitslice index to one or more of those fields may improve performance. A bitslice index is an array which first converts each numeric data value in a field to a binary bit string. A bitmap is then created for each digit in the binary value to record rows that have a 1 for that binary digit in their bit string representation.
This type of index works well for numeric fields where each row likely contains a unique number.
In this task, you will add a bitslice index to the Price
field and test it to get the average price for all SELL
transactions.
- Create the bitslice index
PriceIdx
on the Price
field by executing the following statement in the SQL Shell:
Begin code:
End code.
You will see the statement echoed to the SQL shell, followed by preparation and execution metrics.
Begin code:
End code.Even if an index is available, the InterSystems SQL query optimizer will not necessarily use it if it does not improve the SQL query performance.
Next, let's see if the new bitslice index makes a difference in how the query is executed.
- Show the query plan by running the following statement in the SQL Shell:
Begin code:
End code.
As you will see, the query plan remains the same as before. The relative cost is still 9,244,148. The InterSystems SQL query optimizer will not use the new index.

You can edit queries to make sure the SQL query optimizer uses the new index.
- Remove the
WHERE
clause from the query and show the query plan again.
Begin code:
End code.
In this case, the bitslice index is read as the first step of the query plan. The master map is no longer read in this plan.

From this query, you can see that the InterSystems SQL query optimizer will use the bitslice index in some cases, such as this simpler query, and that doing so greatly decreases the relative cost, shown in the query plan. Next, we need to find a way to get the query optimizer to use the index even when the WHERE
clause is present.
Often, the query optimizer will use a bitslice index when there is also a bitmap index on the field in the WHERE
clause. Without a WHERE
clause, a query can simply aggregate all the data in the bitslice index. When there is a WHERE
clause, the query must not include the bits of the index for rows that do not satisfy the WHERE
condition, which can only be done efficiently in tandem with a bitmap index.