Database Design and Performance
This chapter explains how a DBMS executes queries, how indexes improve access, what affects performance, how B-tree indexes work, and how transactions preserve correctness.
4.1 Query Processing
Introduction / Definition
Query processing converts a high-level query into an execution plan and produces the requested result. Typical stages are parsing, validation, rewriting, optimization, plan selection, and execution. The optimizer estimates costs and chooses efficient access paths and join algorithms.
Syntax / Structure
- SQL → Parse → Validate → Optimize → Execution Plan → Execute → Result
Detailed Explanation
For a primary-key lookup, an index may be used instead of scanning the whole table. For joins, the DBMS may choose nested-loop, hash join, or sort-merge strategies.
Example
SELECT * FROM Student WHERE Student_ID = 101;
Output / Result
The DBMS chooses an execution strategy and returns the matching row.
Important Points / Notes
- Optimization is often cost-based.
- Statistics affect plan selection.
- EXPLAIN can help inspect plans.
- Equivalent queries can have different costs.
Mini Practice Question
Why can two logically equivalent queries have different execution times?
4.2 Indexing
Introduction / Definition
An index is an auxiliary data structure that helps locate table records efficiently. It stores searchable key values and references to data locations. Indexes can accelerate selective searches and joins, but they consume storage and add maintenance work during writes.
Syntax / Structure
- CREATE INDEX index_name ON table_name(column_name);
Detailed Explanation
Without an index, a DBMS may scan many or all rows. With a suitable index, it can navigate directly to likely matching records. Index usefulness depends on selectivity, workload, table size, and optimizer decisions.
Example
CREATE INDEX idx_student_dept ON Student(Dept_ID);
Output / Result
Queries involving Dept_ID may obtain a faster access path, depending on the data and optimizer.
Important Points / Notes
- Indexes can speed reads.
- Indexes can slow writes.
- Too many indexes waste resources.
- Composite index column order matters.
Mini Practice Question
Why might an index on a column with only two possible values have limited benefit?
4.3 Database Performance
Introduction / Definition
Database performance describes how efficiently a database handles workload. Important measures include response time, throughput, resource utilization, concurrency, and scalability. Performance depends on queries, indexes, schema, memory, storage, network latency, locks, and configuration.
Syntax / Structure
Throughput = Completed operations / Elapsed time
Performance ≈ Work completed / Time
Detailed Explanation
If 10,000 requests finish in 20 seconds, throughput is 10,000/20 = 500 requests per second. Performance tuning should start with measurement and bottleneck identification.
Example
10,000 requests / 20 s = 500 requests/s
Output / Result
Measured throughput = 500 requests per second.
Important Points / Notes
- Measure before tuning.
- Inspect slow queries and plans.
- Consider CPU, memory, storage, network, and locks.
- Optimize based on actual workload.
Mini Practice Question
Calculate throughput for 2,400 requests completed in 12 seconds.
4.4 B-tree Index Structure
Introduction / Definition
A B-tree is a balanced multiway search tree used as a foundation for many database indexes. It keeps keys ordered and maintains low height by storing many keys per node. B+ trees, a common database variant, keep data entries at leaf level and link leaves for efficient range scans.
Syntax / Structure
Search: compare key → follow child pointer → repeat
Approximate height/search behavior: O(log_m N)
Detailed Explanation
The high branching factor means a large number of keys can be represented with few levels. This is valuable when tree nodes correspond to storage pages. Insertions and deletions preserve balance through splitting and merging.
Example
Conceptual B+ tree:
[30 | 60]
/ | \
[10,20] [40,50] [70,80]
Output / Result
A search for 50 follows the root to the middle leaf and finds 50 efficiently.
Important Points / Notes
- Balanced height.
- High branching factor reduces levels.
- B+ tree leaves support ordered/range access.
- Node size is related to storage-page characteristics.
Mini Practice Question
Why is a high branching factor useful for database indexes?
4.5 Transactions
Introduction / Definition
A transaction is a logical unit of database work that should satisfy the ACID properties. Atomicity means all-or-nothing execution. Consistency preserves rules, isolation controls interaction among concurrent transactions, and durability preserves committed results after failures.
Syntax / Structure
BEGIN; ... COMMIT;
ROLLBACK;
Detailed Explanation
In a bank transfer, subtracting money from one account and adding it to another must be treated as one unit. If an operation fails, the transaction can be rolled back so the database does not retain a partial transfer.
Example
BEGIN;
UPDATE Account SET Balance = Balance - 1000 WHERE Account_ID = 1;
UPDATE Account SET Balance = Balance + 1000 WHERE Account_ID = 2;
COMMIT;
Output / Result
Both updates are committed together if successful; otherwise the transaction can be rolled back.
Important Points / Notes
- ACID = Atomicity, Consistency, Isolation, Durability.
- COMMIT makes changes permanent according to DBMS guarantees.
- ROLLBACK cancels uncommitted changes.
Mini Practice Question
Which ACID property prevents a bank transfer from being only partially applied?
4.6 Transaction Processing
Introduction / Definition
Transaction processing manages concurrent transactions while preserving correctness and recovery. DBMSs may use locks, timestamps, multi-version concurrency control, logging, checkpoints, and recovery algorithms. The objective is to allow useful concurrency without producing unacceptable anomalies.
Syntax / Structure
- Begin → Read/Write → Concurrency Control → Commit/Rollback
Detailed Explanation
If two transactions read the same old balance and then overwrite each other's changes, a lost-update anomaly can occur. Concurrency control coordinates these operations according to the database's isolation guarantees.
Example
T1: Read Balance = 5000
T2: Read Balance = 5000
T1: Write 4500
T2: Write 4800
Output / Result
Without suitable concurrency control, one update may overwrite the other.
Important Points / Notes
- Concurrency control prevents or manages anomalies.
- Logging supports recovery.
- Isolation levels determine visibility among concurrent transactions.
Mini Practice Question
Name one problem that can occur when two transactions update the same row.