Indexing is used to optimise the performance of a database by minimising the number of disk accesses required when a query is processed.
The index is a type of data structure. It is used to locate and access the data in a database table quickly, speeding up operations with read operations like SELECT queries, WHERE clause etc.
Contains copy of primary key or candidate key of the table or something else.
Pointer holding the address of disk block where the value of the corresponding key is stored.
• Indexing is optional, but increases access speed. It is not the primary mean to access the tuple, it is the secondary mean.
• Index file is always sorted.
Note: The term primary index is sometimes used to mean an index on a primary key. However, such usage is nonstandard and should be avoided.
A file may have several indices, on different search keys. If the data file containing the records is sequentially ordered, a Primary Index is an index whose search key also defines the sequential order of the file.
Dense Indexing is used when Search Performance is the highest priority. Since every key is present, the database can find exact locations without any further sequential scanning in the data blocks.
Start Key: 1
Start Key: 11
Start Key: 21
5. Primary Indexing can be based on Data file is sorted w.r.t Primary Key attribute or non-key attributes.
// Without Indexing (Linear Search)
function findUser(users, id) {
for (let user of users) {
if (user.id === id) return user; // O(N) complexity
}
}
// With Indexing (Hash Map)
const userIndex = new Map(users.map(u => [u.id, u]));
function findUserFast(id) {
return userIndex.get(id); // O(1) complexity
}INSERT, DELETE, and UPDATE query.