L.10 Flashcards

Optimizing For Query Performance

1
Q

What are the main topics covered in Query Performance Optimization?

A

Views (Chapter 7), Indices (Chapter 17 - practical use), Query Optimization (Chapter 19)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why do different queries have different execution times?

A

Query performance depends on:
* The number of rows in involved tables
* The type of operations used (projection, selection, joins)
* The order of execution (Query Plan)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some ways to improve slow query performance?

A

Improve slow query performance by:
* Use caches
* Use views to store frequent query results
* Add indices for faster selection
* Rewrite queries for faster execution order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a materialized view, and how is it different from a regular view?

A

A regular view is a saved query that gets executed every time it’s called.
A materialized view is stored on disk and improves performance but requires updates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different strategies for updating materialized views?

A

Strategies for updating materialized views include:
* Immediate Update: Updates as soon as base tables change (slower inserts)
* Lazy Update: Updates when queried (slower query execution)
* Periodic/On-Demand Update: Updates on a schedule (risk of outdated data)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a full table scan, and why is it slow?

A

A full table scan occurs when a query searches every row in a table (O(n) complexity), making it slow for large datasets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do indices improve query performance?

A

Indices provide fast lookups by reducing search complexity from O(n) to O(1) or O(log n), depending on the index type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you create an index in SQL?

A

CREATE INDEX index_name ON table_name(column_name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a unique index, and how do you create one?

A

A unique index ensures no duplicate values in a column. CREATE UNIQUE INDEX index_name ON table_name(column_name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the trade-offs of using an index?

A

Trade-offs of using an index include:
* ✅ Faster queries (avoids full table scans)
* ❌ More disk space usage
* ❌ Slower insert/update/delete operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the two types of query rewriting (optimization)?

A

The two types of query rewriting are:
* Heuristic Optimization: Uses general transformation rules to improve efficiency.
* Cost-Based Optimization: Estimates query cost and chooses the best execution plan.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an example of query rewriting for optimization?

A

Instead of:
SELECT * FROM city JOIN country
ON city.country = country.code
WHERE country.area > 100000;

Rewrite to:
SELECT * FROM (SELECT * FROM country WHERE area > 100000) c
JOIN city ON city.country = c.code;

This reduces the number of rows before joining, making it faster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does EXPLAIN do in SQL?

A

EXPLAIN shows the query execution plan without running the actual query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between EXPLAIN and EXPLAIN ANALYZE?

A

EXPLAIN: Shows the query plan without execution.
EXPLAIN ANALYZE: Runs the query and displays actual performance statistics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does cost-based optimization work in databases?

A

-Cost-based optimization generates multiple equivalent query plans

-Assigns a cost value to each plan

-Executes the plan with the lowest cost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the main cost components of a query?

A

Main cost components of a query include:
* Access cost (disk reads/writes)
* Computation cost (CPU operations)
* Memory usage
* Communication cost (if querying remote data)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the problem with excessive joins in a query?

A

Joins across many tables can be slow, especially in normalized databases.
Queries requiring many joins often have high computational costs.

18
Q

What are some solutions to improve performance in queries with excessive joins?

A

Solutions include:
* Caching results
* Keeping redundant data (denormalization)
* Using (materialized) views

19
Q

What are the limitations of views?

A

Limitations of views include:
* Cannot be updated in most cases
* Cannot use JOIN, DISTINCT, or aggregation
* Does not always improve performance (unless materialized)

20
Q

What are the two main view implementation techniques?

A

The two main view implementation techniques are:
* Query Modification: Saves a query but runs it every time (no real performance gain).
* Materialized View: Saves results to disk for faster queries.

21
Q

Why are materialized views not considered redundant data?

A

Because the DBMS manages them, ensuring updates when necessary.

22
Q

What is the computational complexity of a full table scan?

A

O(n), where n is the number of rows in the table.

23
Q

What type of index reduces query complexity to O(1)?

A

A hash-based index.

24
Q

What is the most common type of database index, and what is its complexity?

A

A B-tree index, which supports range queries with O(log n) complexity.

25
How do you delete an index in SQL?
DROP INDEX index_name;
26
Why does creating an index slow down insert/update/delete operations?
The index must be updated every time data changes, adding extra overhead.
27
What types of queries don’t usually benefit from an index?
Queries that don’t usually benefit from an index include: * Queries using LIKE '%value%' (pattern matching) * Queries with low selectivity (e.g., filtering on a Boolean field)
28
What is a query plan?
A tree-like structure that represents the execution order of query operations.
29
What are the three steps of cost-based optimization?
The three steps of cost-based optimization are: * Generate multiple query plans * Assign cost values * Execute the plan with the lowest cost
30
What is heuristic query optimization?
Applying predefined rules (RA equivalence rules) to improve query performance without analyzing the actual data.
31
What is cost-based query optimization?
Analyzing database statistics to estimate execution costs and choose the fastest query plan.
32
What kind of statistical information helps with cost-based optimization?
Statistical information includes: * Number of rows in a table * Index availability * Attribute selectivity (e.g., Sex is less selective than LastName) * Value distribution (histograms)
33
Why do databases break queries into query blocks for optimization?
The search space for possible query plans is too large to optimize the entire query at once.
34
What are some common heuristic optimization rules?
Common heuristic optimization rules include: * Remove redundant projections * Combine selections * Apply selections before joins
35
Why is pushing selections before joins a good optimization strategy?
It reduces the number of rows that need to be joined, making the query faster.
36
What SQL command allows you to view the query execution plan?
EXPLAIN SELECT * FROM table_name;
37
What does EXPLAIN ANALYZE do?
It executes the query and compares the predicted cost vs. actual execution time.
38
Why might a database fail to optimize a query correctly?
Possible reasons include: * The optimizer doesn’t recognize an equivalent, faster query * Missing statistics (outdated index info, missing histograms) * The query is too complex for the optimizer to evaluate all options
39
How can materialized views avoid redundant joins?
They store precomputed query results, eliminating the need for expensive repeated joins.
40
What is the main takeaway from query optimization?
Main takeaways include: * Views (especially materialized) help improve performance * Indices speed up searches but slow down writes * Query optimization finds the best execution plan using RA rules or cost-based analysis