Sobes.tech
Senior

You see that there is no index, the table is large. How will you add an index?

sobes.tech AI

Answer from AI

If the table is large and has no index, adding an index can take a long time and block the table, leading to access issues.

Approaches:

  1. Adding an index without locking

    • In some DBMSs (e.g., PostgreSQL), you can use the CONCURRENTLY option to create an index without locking the table:
      CREATE INDEX CONCURRENTLY idx_name ON table(column);
      
    • This allows continued work with the table during index creation.
  2. Incremental addition

    • If the DBMS does not support creating an index without locking, you can:
      • Create a new table with the index.
      • Transfer data in parts.
      • Rename the tables.
  3. Scheduling during low load times

    • Run the operation during periods of minimal user activity.
  4. Monitoring and testing

    • Before adding an index, check load and execution time in a test environment.

In Go code, you can organize migrations using libraries (e.g., golang-migrate) to manage schema changes carefully and with rollback options.

You see that there is no index, the table is large… - sobes.tech