Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ possible_keys: NULL
Extra: Using where
```

In this example, the value of *key* is NULL, which means that Azure Database for MySQL flexible server can't locate any indexes optimized for the query. As a result, it performs a full table scan. Let's optimize this query by adding an index on the **ID** column, and then run the EXPLAIN statement again.
In this example, the value of *key* is NULL, which means that Azure Database for MySQL Flexible Server can't locate any indexes optimized for the query. As a result, it performs a full table scan. Let's optimize this query by adding an index on the **ID** column, and then run the EXPLAIN statement again.

```sql
mysql> ALTER TABLE tb1 ADD KEY (id);
Expand All @@ -51,7 +51,7 @@ possible_keys: id
Extra: NULL
```

Now, the output shows that Azure Database for MySQL flexible server uses an index to limit the number of rows to 1, which dramatically shortens the search time.
Now, the output shows that Azure Database for MySQL Flexible Server uses an index to limit the number of rows to 1, which dramatically shortens the search time.

## Covering index

Expand All @@ -74,9 +74,9 @@ possible_keys: NULL
Extra: Using where; Using temporary; Using filesort
```

The output shows that Azure Database for MySQL flexible server doesn't use any indexes, because proper indexes are unavailable. The output also shows *Using temporary; Using filesort*, which indicates that Azure Database for MySQL flexible server creates a temporary table to satisfy the **GROUP BY** clause.
The output shows that Azure Database for MySQL Flexible Server doesn't use any indexes, because proper indexes are unavailable. The output also shows *Using temporary; Using filesort*, which indicates that Azure Database for MySQL Flexible Server creates a temporary table to satisfy the **GROUP BY** clause.

Creating an index only on column **c2** makes no difference, and Azure Database for MySQL flexible server still needs to create a temporary table:
Creating an index only on column **c2** makes no difference, and Azure Database for MySQL Flexible Server still needs to create a temporary table:

```sql
mysql> ALTER TABLE tb1 ADD KEY (c2);
Expand Down Expand Up @@ -116,7 +116,7 @@ possible_keys: covered
Extra: Using where; Using index
```

As the output of the EXPLAIN above shows, Azure Database for MySQL flexible server now uses the covered index and avoids having to creating a temporary table.
As the output of the EXPLAIN above shows, Azure Database for MySQL Flexible Server now uses the covered index and avoids having to creating a temporary table.

## Combined index

Expand All @@ -139,7 +139,7 @@ possible_keys: NULL
Extra: Using where; Using filesort
```

Azure Database for MySQL flexible server performs a *file sort* operation that is fairly slow, especially when it has to sort many rows. To optimize this query, create a combined index on both of the columns that are being sorted.
Azure Database for MySQL Flexible Server performs a *file sort* operation that is fairly slow, especially when it has to sort many rows. To optimize this query, create a combined index on both of the columns that are being sorted.

```sql
mysql> ALTER TABLE tb1 ADD KEY my_sort2 (c1, c2);
Expand All @@ -159,11 +159,11 @@ possible_keys: NULL
Extra: Using where; Using index
```

The output of the EXPLAIN statement now shows that Azure Database for MySQL flexible server uses a combined index to avoid additional sorting as the index is already sorted.
The output of the EXPLAIN statement now shows that Azure Database for MySQL Flexible Server uses a combined index to avoid additional sorting as the index is already sorted.

## Conclusion

You can increase performance significantly by using EXPLAIN together with different types of indexes. Having an index on a table doesn't necessarily mean that Azure Database for MySQL flexible server can use it for your queries. Always validate your assumptions by using EXPLAIN and optimize your queries using indexes.
You can increase performance significantly by using EXPLAIN together with different types of indexes. Having an index on a table doesn't necessarily mean that Azure Database for MySQL Flexible Server can use it for your queries. Always validate your assumptions by using EXPLAIN and optimize your queries using indexes.

## Next step

Expand Down