How to view and optimize MySql indexes

How to view and optimize MySql indexes

MySQL supports hash and btree indexes. InnoDB and MyISAM only support Btree indexes, while the Memory and Heap storage engines can support both hash and Btree indexes.

We can query the current index usage with the following statement:

show status like '%Handler_read%';

+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 0 |
| Handler_read_last | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+-----------------------+-------+

If the index is working, the value of Handler_read_key will be high. This value represents the number of times a row is read by the index value. If the value is low, the performance improvement of adding an index to the table name is not high, so the index is not often used.

If the Handler_read_rnd_next value is high, it means that the query is running inefficiently and an index should be created to remedy the situation. This value means the number of requests to read the next row in the data file. If a lot of table scans are happening, Handler_read_rnd_next will be high. This indicates that the index is incorrect or not being used.

optimization:

Optimize the insert statement:

1. Try to use insert into test values(),(),(),()...

2. If you insert multiple rows from different customers, you can get a higher speed by using the INSERT delayed statement. Delayed means that the INSERT statement is executed immediately. In fact, the data is placed in a memory queue and is not actually written to disk. This is much faster than inserting each statement separately. Low_priority is just the opposite. Insertion is performed after all other users have finished reading and writing the table.

3. Store index files and data files on different disks (using table creation statements)

4. If you are doing batch inserts, you can increase the bulk_insert_buffer_size variable value to increase the speed, but only for MyISAM tables.

5. When loading a table from a text file, use load data file, which is usually 20 times faster than using insert

Optimize the group by statement:

By default, MySQL sorts all group by fields, which is similar to order by. If the query includes a group by but the user wants to avoid the consumption of sorted results, you can specify order by null to suppress the sort.

Optimize the order by statement:

In some cases, MySQL can use an index to satisfy the ORDER BY clause, thus eliminating the need for additional sorting. The where condition and order by use the same index, and the order by order is the same as the index order, and the order by fields are in ascending or descending order.

Optimizing nested queries:

MySQL 4.1 began to support subqueries, but in some cases, subqueries can be replaced by more efficient joins, especially when the passive table to be joined has an index. The reason is that MySQL does not need to create a temporary table in memory to complete this query that logically requires two steps.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL uses covering index to avoid table return and optimize query
  • How to optimize MySQL indexes
  • How to optimize MySQL index function based on Explain keyword
  • How to use indexes to optimize MySQL ORDER BY statements
  • MySQL functional index optimization solution
  • MySQL performance optimization: how to use indexes efficiently and correctly
  • An article to master MySQL index query optimization skills
  • MySQL database optimization: index implementation principle and usage analysis
  • A brief discussion on MySQL index optimization analysis
  • Understanding MySQL - Indexing and Optimization Summary
  • How to design and optimize MySQL indexes

<<:  Tips for making web table frames

>>:  Vue interpretation of responsive principle source code analysis

Recommend

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

4 ways to optimize MySQL queries for millions of data

Table of contents 1. The reason why the limit is ...

Install mysql offline using rpm under centos 6.4

Use the rpm installation package to install mysql...

About the problem of offline installation of Docker package on CentOS 8.4

The virtual machine used is CentOS 8.4, which sim...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

How to operate json fields in MySQL

MySQL 5.7.8 introduced the json field. This type ...

How to implement property hijacking with JavaScript defineProperty

Table of contents Preface Descriptors Detailed ex...

The complete usage of setup, ref, and reactive in Vue3 combination API

1. Getting started with setUp Briefly introduce t...

vue-router hook function implements routing guard

Table of contents Overview Global hook function R...

Implementation of react automatic construction routing

Table of contents sequence 1. Centralized routing...

Using front-end HTML+CSS+JS to develop a simple TODOLIST function (notepad)

Table of contents 1. Brief Introduction 2. Run sc...

Detailed explanation of the basic use of Apache POI

Table of contents Basic Introduction Getting Star...