質問

MyTable (
    PKSubset INTEGER UNSIGNED,
    PKSubset2 SMALLINT UNSIGNED,
    NonPkAttribute TINYINT UNSIGNED NOT NULL,
    CONSTRAINT pk_mytbl PRIMARY KEY (PKSubset, PKSubset2),
)

Given the above table, are the individual attributes PKSubset and PKSubset2, automatically indexed because they are part of the primary key? Or is it only the whole primary key (PKSubset, PKSubset2) that is indexed?

The answer may differ for different databases, and I am particularly interested in the case for the H2 database. However, answers for other databases are welcome and appreciated!

役に立ちましたか?

解決

From the Database Performance Tuning section:

Index Usage

This database uses indexes to improve the performance of SELECT, UPDATE, DELETE. If a column is used in the WHERE clause of a query, and if an index exists on this column, then the index can be used. Multi-column indexes are used if all or the first columns of the index are used.

This is fairly typical for most RDBMS. You get only one index for your compound primary key, but this index can be used for lookups on your PKSubset column or on both PKSubset and PKSubset2 columns; not for lookups on PKSubset2 alone, though.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top