PC 318 - Midterms - Logan

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/323

324 Terms

1
New cards

What is an execution plan?

The roadmap (step-by-step route) the database chooses to find the data a SQL query asks for.

2
New cards

Which database component chooses the execution plan?

The query optimizer.

3
New cards

In the GPS analogy, what does the SQL query represent?

The destination (what you want).

4
New cards

In the GPS analogy, what does the execution plan represent?

The route (how to get there).

5
New cards

In the GPS analogy, what does the optimizer represent?

The GPS that chooses the route.

6
New cards

What are the five stages a SQL query goes through from request to result?

Parsing, Optimization, Execution plan, Execution, Result.

7
New cards

What happens during the parsing stage?

The database reads the request and checks it.

8
New cards

What happens during the optimization stage?

The optimizer considers possible routes and chooses one.

9
New cards

What happens during the execution stage?

The database follows the chosen route (the execution plan).

10
New cards

When is an estimated execution plan produced?

Before the query runs.

11
New cards

What does an estimated execution plan show?

A prediction of what may happen, based on information such as indexes and statistics.

12
New cards

When is an actual execution plan shown?

After the query runs.

13
New cards

Which plan shows what really happened, including runtime and actual row information?

The actual execution plan.

14
New cards

What is a clue that something is wrong when comparing the two plans?

A big gap between the expected (estimated) rows and the actual rows.

15
New cards

What is the SSMS shortcut to include the actual execution plan?

Ctrl + M

16
New cards

What is the SSMS shortcut to display the estimated execution plan?

Ctrl + L

17
New cards

What is the SSMS menu path for including the actual execution plan?

Query > Include Actual Execution Plan

18
New cards

What does SSMS stand for?

SQL Server Management Studio.

19
New cards

Which keyword placed before a query shows its plan in MySQL and PostgreSQL?

EXPLAIN

20
New cards

What four things does EXPLAIN help you see?

The plan, whether indexes are used, the rows, and the operations.

21
New cards

Do the exact plan-viewing commands depend on the DBMS?

Yes, the exact command or button depends on the database system.

22
New cards

In an execution plan, what is an operator?

A step the database performs.

23
New cards

In an execution plan, what does "rows" refer to?

How many records are handled.

24
New cards

In an execution plan, what does "cost" mean?

A relative estimate of the work.

25
New cards

What does a Table scan operator do?

Looks through rows one by one.

26
New cards

What does a Filter operator do?

Keeps only the matching rows (e.g., grade >= 90).

27
New cards

What does a Hash match operator do?

Combines related rows (a join), e.g., connecting Students and Grades by student_id.

28
New cards

What does a Sort operator do?

Puts rows in order.

29
New cards

What does a Result operator do?

Gives the requested data.

30
New cards

What is a query bottleneck?

The step that makes the whole query slower.

31
New cards

What is the classroom analogy for a bottleneck?

30 students leaving through one narrow door; the door is the bottleneck.

32
New cards

What is a database example of a bottleneck?

Reading 1,000,000 rows just to return 10 rows.

33
New cards

What are four common causes of a bottleneck?

Full scan, inefficient JOIN, missing or poor index, too much sorting.

34
New cards

What problem does a full scan cause?

Too many records are checked.

35
New cards

What problem does an inefficient JOIN cause?

Too much data is combined.

36
New cards

What problem does a missing or poor index cause?

Rows are harder to find.

37
New cards

What problem does too much sorting cause?

A large list must be ordered.

38
New cards

Is a table scan always bad?

No. It can be fine for a small table or when most rows are needed.

39
New cards

Which columns are good candidates for indexes?

Columns often used in WHERE or JOIN.

40
New cards

Write the SQL to create an index named idx_grades_student on student_id of the grades table.

CREATE INDEX idx_grades_student ON grades(student_id);

41
New cards

What is the downside of having too many indexes?

They use storage and can make INSERT/UPDATE work harder.

42
New cards

What does SELECT * do?

Returns every column of the table.

43
New cards

Why should you avoid SELECT * when you need only a few columns?

It wastes memory, I/O, and network work; ask only for the columns you need.

44
New cards

Why is WHERE YEAR(grade_date) = 2025 less index-friendly?

The function is applied to every date, which can make efficient index use harder.

45
New cards

What is a more index-friendly way to filter grades for 2025?

A range condition: WHERE grade_date >= '2025-01-01' AND grade_date < '2026-01-01'

46
New cards

What should you always do to verify a rewritten WHERE condition helps?

Check the actual execution plan.

47
New cards

What join problems should you watch for?

Large tables, weak join conditions, joining many rows before filtering, and missing or unsuitable indexes.

48
New cards

What is the N+1 query problem?

One query gets a list (e.g., 100 students) and then N more queries fetch related rows for each one (101 database calls).

49
New cards

What is the fix for the N+1 problem?

A set-based approach: one JOIN query that gets the related data together.

50
New cards

Why can a leading wildcard such as LIKE '%ana' hurt performance?

It may make efficient index use harder.

51
New cards

What is the difference between UNION and UNION ALL?

UNION removes duplicates (extra work); UNION ALL does not, so use it when duplicates are not a concern.

52
New cards

What is the better habit for excessive rows?

Filter when appropriate so fewer records are processed.

53
New cards

What is the better habit for unnecessary JOINs?

Connect only the tables you actually need.

54
New cards

What is the better habit for repeated calls to the database?

Avoid unnecessary round trips.

55
New cards

What are the steps of the query optimization loop?

Write query, View plan, Analyze, Find bottleneck, Optimize, Test again.

56
New cards

What is the rhythm of query optimization?

Optimize, Measure, Repeat.

57
New cards

If a query is slow, should you add an index immediately?

Not always. First check the execution plan and find the bottleneck.

58
New cards

What fixes might a slow query need besides an index?

A clearer WHERE condition, fewer columns, a better JOIN, or less data to process.

59
New cards

What is the key rule when tuning a slow query?

Find the problem, make a change, test again. Don't guess; check the plan.

60
New cards

What is the best first step when teachers often search WHERE student_id = ? on a large grades table?

Investigate an index on student_id and verify with the execution plan.

61
New cards

What is database indexing?

A data structure that helps a database find and retrieve data faster by reducing the amount of data it needs to examine.

62
New cards

What is the book analogy for an index?

Like a book index, it lets you jump straight to the page without reading the whole book.

63
New cards

How does a query run without an index?

It checks Row 1, Row 2, Row 3 and so on until it finds a match (full table scan).

64
New cards

How does a query run with an index?

Query goes to the index, which leads to the target record.

65
New cards

What is a single-column index?

An index that uses one column (e.g., student_id).

66
New cards

What is a composite index?

An index that uses two or more columns (e.g., (course, year_level)).

67
New cards

What is a unique index?

An index that prevents duplicate values in the indexed key.

68
New cards

What is a covering index?

An index that contains all the columns needed by a particular query.

69
New cards

Write the SQL for a composite index named idx_student on course and year_level of Students.

CREATE INDEX idx_student ON Students(course, year_level);

70
New cards

Does column order matter in a composite index?

Yes. Column order affects how useful the index is.

71
New cards

What is the benefit of a covering index?

The engine may answer the query directly from the index without visiting the base table, reducing extra table lookups.

72
New cards

Write the SQL for a covering index named idx_cover for SELECT student_id, name FROM Students WHERE student_id = 1005.

CREATE INDEX idx_cover ON Students(student_id, name);

73
New cards

Do exact index types and behavior depend on the DBMS?

Yes, they depend on the DBMS and the query optimizer.

74
New cards

When should you NOT create an index?

Very small tables, rarely queried columns, columns with little filtering benefit, tables with frequent INSERT/UPDATE/DELETE, or when an existing index already supports the query.

75
New cards

Do more indexes automatically mean better performance?

No.

76
New cards

Which clauses should you look at when choosing what to index?

WHERE, JOIN, ORDER BY, and GROUP BY.

77
New cards

What four factors should you consider when choosing indexes?

Selectivity, query frequency, data modification frequency, and existing indexes.

78
New cards

What is selectivity?

How effectively a condition or column narrows down matching rows.

79
New cards

What is cardinality in indexing (Part 2)?

The number of distinct values in a column.

80
New cards

Does student_id have high or low selectivity?

High (many distinct values, few rows match each value).

81
New cards

Does year_level have high or low selectivity?

Low (few distinct values, many rows match each value).

82
New cards

What is the goal when creating indexes?

Provide meaningful performance benefits without unnecessary maintenance costs.

83
New cards

What is physical database optimization?

Optimization focused on how data and database structures are physically stored and accessed.

84
New cards

What is the main goal of physical database optimization?

Reduce unnecessary disk I/O and improve data access efficiency.

85
New cards

What are the six areas of physical database optimization?

Index organization, data storage, partitioning, memory/cache, fragmentation, and storage configuration.

86
New cards

What is a clustered index?

An index that organizes table data around the index key; closely tied to table data; typically one per table.

87
New cards

What is a non-clustered index?

A separate index structure containing keys and references to table rows; multiple can usually exist.

88
New cards

Clustered vs non-clustered in one phrase?

Clustered: data is organized around the index. Non-clustered: the index points to the data.

89
New cards

What are the three parts of a B-tree index?

Root, internal nodes, and leaf nodes.

90
New cards

What is the role of the root node in a B-tree?

It is the starting point of the search.

91
New cards

What is the role of internal nodes in a B-tree?

They guide the search.

92
New cards

What do leaf nodes in a B-tree hold?

The indexed keys or references to rows.

93
New cards

Why are B-trees popular?

They are balanced, search efficiently, support range queries, and reduce page access.

94
New cards

What is a heap table?

A table whose rows are stored without ordering by a particular index; separate indexes point to the rows.

95
New cards

What is an index-organized table?

A table whose data is stored within an index structure; the primary key determines the organization.

96
New cards

What causes fragmentation in a database?

INSERT, UPDATE, and DELETE operations changing pages.

97
New cards

What are the possible effects of fragmentation?

Less efficient storage, more pages to read, increased storage use, and possible performance degradation.

98
New cards

What maintenance actions address fragmentation?

Rebuild indexes, reorganize indexes, update statistics, and remove unnecessary indexes.

99
New cards

What is partitioning?

Dividing a large table or index into smaller sections called partitions.

100
New cards

What are the benefits of partitioning?

Easier management of large datasets, less scanned data through partition pruning, and simpler maintenance operations.