Sobes.tech
Senior

How does creating an index affect table accessibility? Is there a way to create an index without locking?

sobes.tech AI

Answer from AI

Creating an index in a database typically requires locking the table or part of it to ensure data integrity during index construction. This can lead to temporary reduced availability of the table for write operations and sometimes read operations.

However, modern DBMSs support online index creation without locking. For example, in PostgreSQL and MySQL (InnoDB), indexes can be created with options that do not lock the table for a long time.

In Node.js, this depends on the database and driver used, but generally:

  • To minimize locks, indexes are created during off-peak times.
  • They use commands or DBMS parameters for online index creation.

Example for MySQL:

CREATE INDEX idx_name ON table_name(column_name) ALGORITHM=INPLACE, LOCK=NONE;

This allows creating an index without fully locking the table.

Thus, to avoid reducing availability, you should use DBMS features for online index creation and plan operations during low load periods.

How does creating an index affect table… - sobes.tech