You create an SQL index in DB2 for i (on IBM i) using the CREATE INDEX
statement. Here's a breakdown of the syntax and options:
Basic Syntax :
CREATE INDEX index-name
ON table-name (column1, column2, ...);
index-name
: The name you want to give to your index. It's good practice to use a naming convention that makes it clear what the index is for.table-name
: The name of the table on which you're creating the index.column1, column2, ...
: The columns that you want to include in the index. You can include one or more columns. The order of the columns is important, as it affects how the index is used.Example :
CREATE INDEX CustNameIdx
ON CUSTOMERS (LastName, FirstName);
This creates an index named CustNameIdx
on the CUSTOMERS
table, using the LastName
and FirstName
columns. Queries that filter by LastName
and then FirstName
will be able to use this index efficiently.
Key Options and Considerations:
CREATE INDEX OrderDateIdx
ON ORDERS (OrderDate DESC); -- Index in descending order of OrderDate?
* UNIQUE : You can create a unique index, which enforces that the combination of values in the indexed columns is unique across all rows in the table. This is often used for primary keys or other unique constraints.
CREATE UNIQUE INDEX CustNoIdx
ON CUSTOMERS (CustomerID);?
Partitioned Tables: If your table is partitioned, you might need to include partitioning columns in the index or specify how the index is partitioned.
Expression-based Indexes: You can create indexes on expressions involving columns, not just on the columns themselves. This can be useful for optimizing queries that filter or sort by calculated values.
CREATE INDEX UpperNameIdx
ON CUSTOMERS (UPPER(LastName));
Index Type: While DB2 for i typically uses B-tree indexes, there are other index types (like Encoded Vector Indexes) that might be appropriate for specific workloads or data characteristics. The default is usually a B-tree index.
Journaling: Indexes can be journaled, which is important for recovery in case of system failures. The journaling attributes of an index are often inherited from the table.
File vs. Index: It's important to distinguish between a file (which stores the actual data) and an index (which is a separate structure that helps speed up data access). An index is like the index in a book; it doesn't contain the data itself, but it tells you where to find it.
Best Practices:
WHERE
clauses, JOIN
conditions, ORDER BY
clauses, or GROUP BY
clauses.