You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/2025-04-09-ibm-db2-indexes-en.md
+125-1
Original file line number
Diff line number
Diff line change
@@ -102,4 +102,128 @@ ORDER BY
102
102
TABSCHEMA, TABNAME, INDNAME;
103
103
```
104
104
105
-
Let me know if you need help interpreting the results or refining the query!
105
+
Let me know if you need help interpreting the results or refining the query!
106
+
107
+
---
108
+
109
+
In IBM Db2, you can create an index on a table using the `CREATE INDEX` statement. Indexes improve query performance by allowing faster data retrieval, especially for columns frequently used in `WHERE`, `JOIN`, or `ORDER BY` clauses. Below is the syntax and explanation for creating an index, along with examples.
110
+
111
+
---
112
+
113
+
### Basic Syntax
114
+
```sql
115
+
CREATE [UNIQUE] INDEX index_name
116
+
ON table_name (column_name [ASC | DESC], ...)
117
+
[options];
118
+
```
119
+
120
+
#### Key Components
121
+
-**`[UNIQUE]`**: Optional. Ensures no duplicate values are allowed in the indexed column(s). Omit this if duplicates are allowed.
122
+
-**`index_name`**: The name you give to the index (must be unique within the schema).
123
+
-**`table_name`**: The name of the table where the index will be created.
124
+
-**`column_name`**: The column(s) to index. You can specify multiple columns for a composite index.
125
+
-**`[ASC | DESC]`**: Optional. Specifies the sort order (ascending or descending). Default is `ASC`.
126
+
-**`[options]`**: Additional options like `CLUSTER`, `INCLUDE`, or storage specifications (optional, depending on needs).
127
+
128
+
---
129
+
130
+
### Examples
131
+
132
+
#### 1. Create a Simple Index
133
+
To create an index on the `employee_id` column of the `employees` table:
134
+
```sql
135
+
CREATEINDEXidx_employee_id
136
+
ON employees (employee_id);
137
+
```
138
+
- This creates a non-unique index, allowing duplicate `employee_id` values.
139
+
140
+
#### 2. Create a Unique Index
141
+
To create a unique index on the `email` column (no duplicate emails allowed):
142
+
```sql
143
+
CREATEUNIQUE INDEXidx_email
144
+
ON employees (email);
145
+
```
146
+
- If duplicate values exist in `email`, this will fail with an error.
147
+
148
+
#### 3. Create a Composite Index
149
+
To create an index on multiple columns, such as `last_name` and `first_name`:
150
+
```sql
151
+
CREATEINDEXidx_name
152
+
ON employees (last_name, first_name);
153
+
```
154
+
- This is useful for queries filtering or sorting by both columns.
155
+
156
+
#### 4. Create an Index with Descending Order
157
+
To create an index on `hire_date` in descending order:
158
+
```sql
159
+
CREATEINDEXidx_hire_date
160
+
ON employees (hire_date DESC);
161
+
```
162
+
- Helpful for queries like `ORDER BY hire_date DESC`.
163
+
164
+
#### 5. Create a Clustered Index (Optional)
165
+
A clustered index determines the physical order of data in the table (only one per table):
166
+
```sql
167
+
CREATEINDEXidx_employee_id_clust
168
+
ON employees (employee_id)
169
+
CLUSTER;
170
+
```
171
+
- Note: Use this sparingly, as it reorganizes the table physically.
172
+
173
+
---
174
+
175
+
### Additional Options
176
+
-**`INCLUDE (column_name)`**: Adds non-key columns to the index for faster retrieval (Db2 LUW feature):
177
+
```sql
178
+
CREATEINDEXidx_salary
179
+
ON employees (department_id)
180
+
INCLUDE (salary);
181
+
```
182
+
-**`SPECIFICATION ONLY`**: Creates the index definition without building it (for deferred population):
183
+
```sql
184
+
CREATEINDEXidx_temp
185
+
ON employees (employee_id)
186
+
SPECIFICATION ONLY;
187
+
```
188
+
189
+
---
190
+
191
+
### Key Notes
192
+
1.**Permissions**: You need the `INDEX` privilege on the table or appropriate authority (e.g., `DBADM`).
193
+
2.**Existing Data**: If creating a `UNIQUE` index, the table must not already contain duplicate values in the indexed column(s), or the command will fail.
194
+
3.**Naming**: Index names must be unique within the schema.
195
+
4.**Performance**: Indexes speed up reads but can slow down writes (`INSERT`, `UPDATE`, `DELETE`) due to maintenance overhead.
196
+
5.**Verification**: After creating, check the index with:
197
+
```sql
198
+
SELECT INDNAME, COLNAMES
199
+
FROMSYSCAT.INDEXES
200
+
WHERE TABNAME ='YOUR_TABLE_NAME'
201
+
AND TABSCHEMA ='YOUR_SCHEMA_NAME';
202
+
```
203
+
204
+
---
205
+
206
+
### Example Workflow
207
+
Suppose you have an `employees` table and want to index `department_id` for faster queries:
208
+
```sql
209
+
CREATEINDEXidx_department_id
210
+
ON employees (department_id);
211
+
```
212
+
Then verify:
213
+
```sql
214
+
SELECT INDNAME, COLNAMES
215
+
FROMSYSCAT.INDEXES
216
+
WHERE TABNAME ='EMPLOYEES'
217
+
AND TABSCHEMA ='MY_SCHEMA';
218
+
```
219
+
220
+
#### Output
221
+
```
222
+
INDNAME COLNAMES
223
+
---------------- -----------
224
+
IDX_DEPARTMENT_ID +DEPARTMENT_ID
225
+
```
226
+
227
+
---
228
+
229
+
Let me know if you need help with a specific index or additional options!
0 commit comments