MySQL database optimization (4) - MySQL index optimization
1. Index basics
Index type:
1. Ordinary index: Created into any data type
2. Unique index: restricted index The value must be unique
3. Full-text index: It can only be created on char, varchar, and text type fields. It is mainly used to improve text query speed. MyISAM engine support.
4. Single-column index: Create an index for a single field in the table
5. Multi-column index: Create an index for multiple fields
6. Spatial index: Created using spatial parameters to provide the system with the efficiency of obtaining control data
Basic operations of index:
CREATE TABLE t_user1(id INT , userName VARCHAR(20), PASSWORD VARCHAR(20), INDEX (userName) ); CREATE TABLE t_user2(id INT , userName VARCHAR(20), PASSWORD VARCHAR(20), UNIQUE INDEX index_userName(userName) ); CREATE TABLE t_user3(id INT , userName VARCHAR(20), PASSWORD VARCHAR(20), INDEX index_userName_password(userName,PASSWORD)//多列索引 ); CREATE INDEX index_userName ON t_user4(userName);--对已经创建的表指定索引 CREATE UNIQUE INDEX index_userName ON t_user4(userName); CREATE INDEX index_userName_password ON t_user4(userName,PASSWORD); ALTER TABLE t_user5 ADD INDEX index_userName(userName);--修改索引 ALTER TABLE t_user5 ADD UNIQUE INDEX index_userName(userName); ALTER TABLE t_user5 ADD INDEX index_userName_password(userName,PASSWORD); DROP INDEX index_userName ON t_user5; DROP INDEX index_userName_password ON t_user5;
Adding an index can speed up query efficiency and avoid full table data query. Instead, use Search the index and find the target data. select t.name from user t where t.id=5, if an index is created on the actor_id column, mysql uses the index to find the row with id=5, that is to say, it first searches by value on the index, and then returns all the rows containing the row of data for the value.
The index can contain one or more columns. If the index contains multiple columns, the order of the columns is also very important, because MySQL can only efficiently use the leftmost prefix column of the index. Moreover, the creation and maintenance of indexes also require system resources, which involves how to create efficient indexes to improve query efficiency.
2. Index type of mysql
Generally speaking, the data index itself is also very large and it is impossible to store it all in the memory. Therefore, the index is often based on the index file. The form is stored on disk. In this case, disk I/O consumption will occur during the index search process, compared with memory access, so the structural organization of the index should minimize the number of disk I/O accesses during the search process. This requires a high-quality data structure to organize and store database indexes.
General index types make use of data structure algorithms. For example, B-Tree is both an index type in mysql and a dynamic search tree: Binary Search Tree, Balanced Binary Search Tree , Red-Black Tree, binary tree structure of B-tree/B+-tree/B*-tree (B~Tree).
Since these MySQL index types are based on the implementation of data structure algorithms, general development begins to be applied on the basis. The applications of indexes created based on different data structures are also different.
3. Common misunderstandings in index use
1. Independent column index
Wrong use : select t.name from user t where t.age+1=5. For this condition age+1=5, mysql cannot automatically parse. Even if an index is created for the age column, mysql is Index query will not be used during query, and full table scan will still be performed.
Incorrect usage: select t.name from user t where TO_DAYS(`CURRENT_DATE`())-TO_DAYS(date_col)<=10; Same as above
Correct principle:Always put the index column on the separate side of the comparison operator.
2. Multi-column index
Common mistakes: Create an independent index without columns, or create multiple columns in the wrong order Index, or simply index all the columns in the where condition.
Correct use: Select the appropriate index order for different index types
For example, select t.name from user t where t.staffId= 2 and custom_id=7;
In response to the above query, should we create a (staffId, custom_id) or reverse the order of these two columns.
Taking B-Tree as an example, the order of the index columns means that the index will be sorted by the leftmost column first, from left to right. It is generally wise to put the fields with the highest frequency of filter conditions at the front of the index. The index designed in this way can filter out the required rows as quickly as possible.
4. Summary
Check out a java cloud platform project I did before, using the ORM framework. At that time, the project experienced slow cascading queries, so I combined JDBC+ORM combined form programming. Now I’m looking at the table designs of several databases, and for index optimization. It still needs to be applied again. Generally, the indexes created only use a single column index and are created using the primary key. There is no big problem with this, but if the query time-consuming problem occurs, table structure optimization, index optimization, and query optimization need to go hand in hand, but Relying on ORM framework re-selection or re-optimization cannot solve the problem.
The above is the content of MySQL database optimization (4) - MySQL index optimization. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
