MySQL query optimization Flashcards

1
Q

What is true about the query optimizer?

a) usually generates multiple query plans
b) always generate one single query plan
c) the developer can choose which query plan to execute
d) all options are wrong

A

a)

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

Which of the following fields have the highest index cardinality?

a) first_name
b) address
c) number_of_children
d) email

A

d)

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

Assuming that there is no index by column ID on table products and the following query takes 3 seconds:

SELECT * FROM products WHERE id = 2;

a) the execution time is normal
b) the query can be considered “slow”
c) we cannot improve the execution time
d) adding an index by id will reduce the execution time at most 50%

A

b)

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

What is a query execution plan?

A

It is a list of instructions that the database needs to follow in order to execute a query on the data. Is generated to know if a query is slow or not

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

The user can determine what is the plan that is executed. True or false?

A

False. The query optimizer generates multiple query plans for a single query and determines the most efficient plan to run.

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

What is the explain command?

A

To examine the query plan we use the EXPLAIN command, which shows the query plan. This command does not run the actual SQL statement (except subqueries), and does not provide any tuning recommendations, but it does provide valuable information to help us make tuning decisions!

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

What does the key column means in the following code?

EXPLAIN SELECT * FROM inventory WHERE item_id = 16102176;

id: 1
select_type: SIMPLE
table: inventory
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 787338
Extra: Using where

A

The query is not using an index. So it can be considered a poorly tuned SQL query.

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

How to identify primary issues using the explain command?

A
  • No index used (NULL specified in the key column).
  • A large number of rows processed (the rows column).
  • A large number of indexes evaluated (the possible_keys column).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If we add an index, the query execution plan will change. True or false

A

False. The generated query plan is not guaranteed and can change, depending on several factors.

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

For update and delete statements, we need to rewrite the query as a select statement to confirm index usage. True or false

A

True

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

What are indexes?

A

An index is a structure that is sorting and a pointer from each record to their corresponding records in the original table where the data is stored. Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries. Inde xing makes columns faster to query by creating pointers to where data is stored within a database.

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

Indexes are stored and searched as B-trees. True or false

A

True. It creates a tree-like structure that sorts data for quick searching.

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

How to create an index after table creation?

A

CREATE INDEX index_name ON table_name (column_list);

This can take a lot of time.

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

When we have multiple different indexes, the user can choose what is the best indexing for the query. True or false

A

False. when we have multiple different indexes (that are listed in possible_keys of the query plan, explain command), MySQL tries to identify the most effective index for the query. The optimizer chooses an index based on the estimated cost to do the least amount of work, not what a human considers the right order

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

Which datatype to use when we want to store exact monetary values?

A

decimal

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

Which datatype to use when we want to store monetary values that do not need to be exact?

A

flout or double

17
Q

Which datatype to use when we only have a few unique string values?

A

enum