Sobes.tech
Senior

How to speed up queries to a transaction table with a billion records over the last month for a specific account?

sobes.tech AI

Answer from AI

To speed up queries to a transaction table with a billion records for a specific account over the last month, several approaches can be applied:

  1. Indexing — create a composite index on the account and transaction date fields. This will allow quick filtering of the needed records.

  2. Partitioning the table — split the table by date (e.g., by months). Then, queries for the last month will only access the relevant partition segment.

  3. Materialized views — if queries are repeated often, you can create an aggregated or filtered view, updated periodically.

  4. Using caching — for example, cache the results of queries for frequently requested accounts.

  5. Query optimization — ensure that the query uses indexes, avoid SELECT *, and select only the necessary fields.

Example of creating an index in SQL:

CREATE INDEX idx_account_date ON transactions (account_id, transaction_date);

And an example of partitioning by date (using PostgreSQL as an example):

CREATE TABLE transactions (
    id BIGSERIAL PRIMARY KEY,
    account_id INT NOT NULL,
    transaction_date DATE NOT NULL,
    amount NUMERIC
) PARTITION BY RANGE (transaction_date);

CREATE TABLE transactions_2024_06 PARTITION OF transactions
    FOR VALUES FROM ('2024-06-01') TO ('2024-07-01');

This approach significantly reduces the amount of data to scan and speeds up query execution.