Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the PK of a DB2 table a clustered index by default?

Tags:

sql

db2

This can be a silly question but I want to be sure 100%.

Is the PK of a DB2 table a clustered index by default?

like image 842
Luka Avatar asked Dec 16 '11 18:12

Luka


People also ask

Is primary key always clustered index?

For example, when you create a table with a UNIQUE constraint, Database Engine automatically creates a nonclustered index. If you configure a PRIMARY KEY, Database Engine automatically creates a clustered index, unless a clustered index already exists.

Is primary index a clustered index?

Primary Keys of the table by default are clustered index.

Should primary key be clustered or nonclustered?

If your primary key is of the UNIQUEIDENTIFIER , make sure to specify that it's NONCLUSTERED . If you make it clustered, every insert will have to do a bunch of shuffling of records to insert the new row in the correct position. This will tank performance.

Which of the following is the default clustered index on a table?

A clustered index is an index which defines the physical order in which table records are stored in a database. Since there can be only one way in which records are physically stored in a database table, there can be only one clustered index per table. By default a clustered index is created on a primary key column.


2 Answers

From: DB2 docs - Clustering indexes

Although a table can have several indexes, only one index can be a clustering index. If you do not define a clustering index for a table, DB2 recognizes the first index that is created on the table as the implicit clustering index when it orders data rows.

So no, by default the Primary Key is NOT the clustered index of the table.

The first created index, unique or not, is the "implicit" clustering index and DB2 tries to insert the records as nearly as possible in the order of the values of this index.

If you later create another index and identify it as clustering, then DB2 identifies it as the clustering index but does not rearrange the data that is already in the table. This can be done with the REORG utility.

like image 158
ypercubeᵀᴹ Avatar answered Oct 02 '22 15:10

ypercubeᵀᴹ


From the Publib (this assumes DB2 for z/OS, version 9)

When a table has a clustering index, an INSERT statement causes DB2 to insert the records as nearly as possible in the order of their index values. The first index that you define on the table serves implicitly as the clustering index unless you explicitly specify CLUSTER when you create or alter another index. For example, if you first define a unique index on the EMPNO column of the EMP table, DB2 inserts rows into the EMP table in the order of the employee identification number unless you explicitly define another index to be the clustering index.

You can see which index is the clustering index for a table (in this example, TEST.TABLE1) using the following query, if you're on z/OS:

SELECT NAME
FROM SYSIBM.SYSINDEXES
WHERE TBCREATOR  = 'TEST'
  AND TBNAME     = 'TABLE1'
  AND CLUSTERING = 'Y'

And this one for Linux/Unix/Windows (LUW):

SELECT *
FROM SYSCAT.INDEXES
WHERE TABSCHEMA = 'TEST'
  AND TABNAME   = 'TABLE1'
  AND INDEXTYPE = 'CLUS'
like image 28
bhamby Avatar answered Oct 02 '22 15:10

bhamby