SQL Query Optimization & Performance Flashcards

(31 cards)

1
Q

What is query optimization?

A

Query optimization is the process a database uses to choose the fastest way to run a query. The database analyzes possible strategies and picks the one that reads the least data and does the least work.

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

What is a query execution plan?

A

An execution plan is the step-by-step strategy the database decides to follow to retrieve the data requested in a query.

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

Why do execution plans matter?

A

They reveal exactly how the database runs your query so you can see whether it scans tables

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

What is a full table scan?

A

A full table scan happens when the database reads every row in a table because it cannot find a faster way to locate the requested data.

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

Why are full table scans slow?

A

Because the database must examine every row even if you only need a few results.

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

What is an index in SQL?

A

An index is a sorted data structure that allows the database to quickly locate rows without scanning the entire table.

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

How does an index speed up queries?

A

Instead of searching row by row the database jumps directly to where the desired values exist.

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

What is a clustered index?

A

A clustered index determines the physical order of rows in a table

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

What is a non-clustered index?

A

A non-clustered index is a separate structure that stores indexed values along with pointers to the actual rows in the table.

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

What is index selectivity?

A

Selectivity describes how unique values are in an indexed column. Highly unique values make indexes more effective.

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

Why are indexes sometimes ignored by the database?

A

If a query returns a large percentage of the table the database may decide scanning the table is faster than using the index.

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

What is a composite index?

A

A composite index indexes multiple columns together so queries filtering by those columns run faster.

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

What is index order importance?

A

In composite indexes the order of columns matters because the database uses the leftmost columns first.

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

What is index fragmentation?

A

Fragmentation happens when indexed data becomes scattered over time

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

What is partitioning in databases?

A

Partitioning splits a large table into smaller physical pieces so queries can scan only the relevant partitions.

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

Why does partitioning improve performance?

A

Because the database reads far less data when it can skip irrelevant partitions.

17
Q

What is partition pruning?

A

Partition pruning occurs when the database automatically ignores partitions that do not match the query filter.

18
Q

What is a hash join?

A

A hash join builds a hash table from one dataset and quickly matches rows from another dataset using that structure.

19
Q

What is a nested loop join?

A

A nested loop join compares rows from one table to rows in another table repeatedly

20
Q

What is a merge join?

A

A merge join works on sorted datasets and efficiently merges them by comparing rows in order.

21
Q

Why are joins expensive operations?

A

Because the database must match rows between tables which can involve scanning and comparing many rows.

22
Q

What is predicate pushdown?

A

Predicate pushdown means filtering data as early as possible so later stages process fewer rows.

23
Q

Why should filters be applied early in queries?

A

Because reducing the number of rows early dramatically decreases the amount of work the database must do later.

24
Q

What is a covering index?

A

A covering index contains all the columns needed for a query so the database does not need to read the main table at all.

25
What is query caching?
Query caching stores the results of previous queries so repeated queries can return results instantly.
26
What causes slow SQL queries?
Common causes include missing indexes
27
Why should SELECT * be avoided in large systems?
Because it retrieves all columns even if they are not needed
28
What is database cardinality?
Cardinality refers to how many unique values exist in a column which helps the optimizer decide how to run queries.
29
What is statistics in query optimization?
Statistics are data summaries about tables that help the optimizer estimate how many rows a query will return.
30
Why must database statistics be updated?
If statistics become outdated the database may choose inefficient query plans.
31
What is a query bottleneck?
A query bottleneck is the slowest part of a query execution plan that limits overall performance.