Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Query Execution Plans and Bottlenecks
00:00
5 left

Query Execution Plans and Bottlenecks

HardSQL · PostgreSQL

Problem

American Express's disputes team pulls a cardholder's disputed high-value transactions for case review. The query below is used to build that view, but on the production card_transactions table, which holds billions of rows, it's showing up as one of the slowest queries in the nightly performance report.

CREATE TABLE card_transactions (
    transaction_id      BIGINT PRIMARY KEY,
    cardholder_id        BIGINT,
    card_number_masked   TEXT,       -- e.g. '**** **** **** 4471'
    merchant_id           INT,
    amount                 NUMERIC(10,2),
    txn_time                TIMESTAMP,
    status                   TEXT
);
CREATE TABLE disputes (
    dispute_id      BIGINT PRIMARY KEY,
    transaction_id   BIGINT REFERENCES card_transactions(transaction_id),
    filed_at          TIMESTAMP,
    reason             TEXT
);
CREATE INDEX idx_txn_cardholder ON card_transactions(cardholder_id);

SELECT t.transaction_id, t.amount, t.txn_time, t.merchant_id
FROM card_transactions t
WHERE t.cardholder_id = 88213471
  AND EXTRACT(YEAR FROM t.txn_time) = 2024
  AND t.amount > 500
  AND t.card_number_masked LIKE '%4471'
  AND (SELECT COUNT(*) FROM disputes d WHERE d.transaction_id = t.transaction_id) > 0
ORDER BY t.amount DESC
OFFSET 5000 LIMIT 50;

Explain the execution plan of this query and identify potential bottlenecks.

Output

  1. Explain the likely PostgreSQL execution plan at a high level, including how each filter affects row elimination.
  2. Identify the main performance bottlenecks in the query.
  3. Mention any indexing or query-shape issues that make the plan expensive on a billion-row table.
  4. Keep the answer focused on this exact query and its runtime behavior.

Schema

card_transactions
ColumnTypeDescription
transaction_idPKBIGINTUnique transaction identifier
cardholder_idBIGINTCardholder owning the transaction
card_number_maskedTEXTMasked card number used for display and filtering
merchant_idINTMerchant identifier
amountNUMERIC(10,2)Transaction amount
txn_timeTIMESTAMPTransaction timestamp
statusTEXTTransaction status
disputes
ColumnTypeDescription
dispute_idPKBIGINTUnique dispute identifier
transaction_idBIGINTReferenced transaction identifier
filed_atTIMESTAMPWhen the dispute was filed
reasonTEXTDispute reason
Tablescard_transactionsdisputes
Interviewer

Your question is Query Execution Plans and Bottlenecks. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.