Sobes.tech
Senior

How would you implement a table in a database with 100 million records with the ability to search by an int32 column?

sobes.tech AI

Answer from AI

Design a table with a column of type INT (for PostgreSQL/MySQL) or NUMBER(10) (for Oracle) to store identifiers or other integer values up to 2^31-1.

To efficiently search this column, an index should be created.

CREATE TABLE large_table (
    id BIGINT PRIMARY KEY, -- Preferably BIGINT for 100M+ records, even if the value is int32
    int_column INT,
    -- other columns...
);

CREATE INDEX idx_int_column ON large_table (int_column);

Using BIGINT for the primary key even when having an INT column is due to the potential growth of records or the need to use a wider ID range.

In case of range searches or more complex predicates, specialized index types (e.g., B-tree) can be considered, which are suitable in most cases.

For performance optimization with such data volume, additional measures may be needed:

  • Partitioning: Split the table into smaller parts based on some criterion, such as the range of int_column values or date if present.
  • Query optimization: Ensure queries utilize the created index. Use EXPLAIN / EXPLAIN PLAN to analyze query execution plans.
  • Database tuning: Buffer cache, memory, and I/O system parameters should be configured for large data volumes.
  • Fault tolerance and backup: These aspects become critically important with such data volume.

Example of a query using the index:

SELECT *
FROM large_table
WHERE int_column = 12345; -- This query will use the idx_int_column index

Partitioning example (PostgreSQL):

CREATE TABLE large_table (
    id BIGINT NOT NULL,
    int_column INT NOT NULL,
    -- other columns...
) PARTITION BY RANGE (int_column);

CREATE TABLE large_table_part1 PARTITION OF large_table
FOR VALUES FROM (MINVALUE) TO (10000000);

CREATE TABLE large_table_part2 PARTITION OF large_table
FOR VALUES FROM (10000000) TO (20000000);

-- Create index on each partition
CREATE INDEX idx_int_column_part1 ON large_table_part1 (int_column);
CREATE INDEX idx_int_column_part2 ON large_table_part2 (int_column);