CS FundamentalsHardIBM · Deloitte pattern
A query optimizer estimates the selectivity of `WHERE age = 30` using a histogram on the age column rather than assuming a uniform distribution across all possible ages. Why does a histogram typically give a more accurate cardinality estimate?
- A. Histograms only work for numeric columns, never strings
- B. Histograms are always 100% accurate, unlike any other statistic
- C. A histogram captures the actual, possibly skewed, frequency distribution of values (e.g. many more 30-year-olds than 90-year-olds in a customer table) rather than assuming every value occurs equally often, which uniform-distribution assumptions get wrong for skewed data
- D. Histograms eliminate the need for an index on the column
Correct answer: C. A histogram captures the actual, possibly skewed, frequency distribution of values (e.g. many more 30-year-olds than 90-year-olds in a customer table) rather than assuming every value occurs equally often, which uniform-distribution assumptions get wrong for skewed data
Real-world data is often skewed (far more rows for common values than rare ones). A histogram buckets the column's actual value distribution, letting the optimizer estimate `age = 30`'s selectivity from the real frequency of 30s rather than (total rows / number of distinct values), which badly misestimates cost whenever the distribution is uneven.
More DBMS and SQL questions
- A table has 1,000,000 rows, and a column's selectivity (distinct values / total rows) is estimated as 1/100,000. Roughly how many rows…
- A B+ tree index on a column is said to have a 'fan-out' of 100 (each internal node has up to 100 children). For a table with 10,000,000…
- Which of these is the best reason a database might choose to use a bitmap index rather than a B+ tree index on a column?
- Two transactions, T1 and T2, both want to update the same row. T1 acquires a shared (read) lock, then later tries to upgrade to an…
- A query uses SELECT ... FOR UPDATE to explicitly lock the rows it reads within a transaction. What is the main purpose of this, compared…
- A long-running read-only transaction under snapshot isolation sees a consistent view of the database as of its start time, even while…