Help Center/ TaurusDB/ FAQs/ Database Usage/ How Do I Write Data to or Create Indexes for an Ultra-large Table?
Updated on 2024-09-06 GMT+08:00

How Do I Write Data to or Create Indexes for an Ultra-large Table?

Writing Data to an Ultra-large Table

For a table with tens of millions or hundreds of millions of data records, you are advised to use the following methods to improve data write efficiency:
  • Delete unnecessary indexes.

    When data is updated, the index data is also updated. For a table with large amounts of data, avoid creating too many indexes as this can slow down the update process. Delete unnecessary indexes based on service evaluation.

  • Use batch insertion to insert multiple data records.

    This is because batch insertion only requires a single remote request to the database.

    Example:

    insert into tb1 values(1,'value1');
    insert into tb2 values(2,'value2');
    insert into tb3 values(3,'value3');

    After optimization:

    insert into tb values(1,'value1'),(2,'value2'),(3,'value3');
  • When inserting multiple data records, manually control transactions.

    By manually controlling transactions, multiple execution units can be merged into a single transaction, avoiding the overhead of multiple transactions while ensuring data integrity and consistency.

    Example:

    insert into table1 values(1,'value1'),(2,'value2'),(3,'value3');
    insert into table2 values(4,'value1'),(5,'value2'),(6,'value3');
    insert into table3 values(7,'value1'),(8,'value2'),(9,'value3');

    After optimization:

    start transaction;
    insert into table1 values(1,'value1'),(2,'value2'),(3,'value3');
    insert into table2 values(4,'value1'),(5,'value2'),(6,'value3');
    insert into table3 values(7,'value1'),(8,'value2'),(9,'value3');
    commit;

    Having too many merged statements can lead to large transactions, which will lock the table for a long time. Evaluate service needs and control the number of statements in a transaction accordingly.

  • When inserting data, insert primary keys in sequential order. You can use AUTO_INCREMENT.

    Inserting primary keys in a random order can cause page splitting, which can negatively impact performance.

    Example:

    Inserting primary keys in a random order: 6 2 9 7 2

    Inserting primary keys in sequential order: 1 2 4 6 8

  • Avoid using UUIDs or other natural keys, such as ID card numbers, as primary keys.

    UUIDs generated each time are unordered, and inserting them as primary keys can cause page splitting, which can negatively impact performance.

  • Avoid modifying primary keys during service operations.

    Modifying primary keys requires modifying the index structure, which can be costly.

  • Reduce the length of primary keys as much as possible if the business permits.
  • Do not use foreign keys to maintain foreign key relationships. Use programs instead.
  • Separate read and write operations. Place read operations on read replicas to avoid slow insertion caused by I/Os.

Creating Indexes for an Ultra-large Table

For a table with tens of millions or hundreds of millions of data records, you are advised to use the following methods to improve index creation efficiency:

  • Keep the index field as small as possible.
  • Select a column with high distinction as the index column.
  • If each field in the table cannot guarantee uniqueness, cannot guarantee NOT NULL, or is not suitable for indexing, create a custom ID auto-increment column as the primary key, which will automatically ensure ordered insertion.
  • To create an index, insert data first and then run the alter table add index command.
  • Use GaussDB(for MySQL) Parallel DDL to create an index. When database hardware resources are idle, you can use parallel DDL to accelerate DDL execution, preventing subsequent DML operations from being blocked and shortening the DDL operation window.