


A brief introduction to the index design principles of MySQL and the differences between common indexes
The following editor will bring you a brief discussion of mysqlindexdesign principlesand the differences between common indexes. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look.
Index definition: It is a separate database structure stored on disk, which contains reference pointers to all records in the data table.
Design principles for database indexes:
In order to make the use of indexes more efficient, when creating an index, you must consider which fields to create the index on and what type of index to create. index.
So what are the index design principles?
1. Select a unique index
The value of the unique index is unique, and a record can be determined more quickly through the index.
For example, the middle school ID in the student table is a unique field. Establishing a unique index for this field can quickly determine a student's information.
If you use a name, there may be the same name, which will slow down the query speed.
2. Create indexes for fields that often require sorting, grouping and union operations
For fields that often require operations such as ORDER BY, GROUP BY, DISTINCT and UNION, the sorting operation will A lot of time wasted.
If you create an index for it, you can effectively avoid the sort operation.
3. Create indexes for fields that are often used as query conditions
If a field is often used as a query condition, the query speed of this field will affect the query speed of the entire table. Therefore,
Creating an index for such a field can improve the query speed of the entire table.
4. Limit the number of indexes
The more indexes, the better. Each index requires disk space. The more indexes, the more disk space is required.
When modifying the table, it is troublesome to reconstruct and update the index. The more indexes, the more time-consuming it becomes to update the table.
5. Try to use an index with a small amount of data
If the index value is very long, the query speed will be affected. For example, a full-text search for a CHAR (100) type field will definitely take more time than a CHAR (10) type field.
6. Try to use prefixes to index
If the value of the index field is very long, it is best to use the prefix of the value to index. For example, full-text search for TEXT and BLOG type fields will be a waste of time. If only the first few characters of the field are retrieved, the retrieval speed can be improved.7. Delete indexes that are no longer used or are rarely usedAfter the data in the table is heavily updated, or the way the data is used is changed, some of the original indexes may no longer be needed. Database administrators should regularly find these indexes and delete them to reduce the impact of the indexes on update operations.
---------- ------------------------------------------------
1. primary key field, the system automatically creates the index of the primary key; 2. unique key field, the system automatically creates the corresponding index;
3. foreign key constraint Fields defined as foreign keys;
4. Fields used to connect tables in queries;
#3. The creation of indexes must consider the way the data is operated.
1. The content rarely changes and is often queried, so it doesn’t matter if you create a few more indexes for it;
2. Tables that change frequently and routinely For example, you need to carefully create the necessary indexes; 4. The difference between primary key and unique key1. As Primary The domain/domain group of Key cannot be null. And Unique Key can.
2. There can only be one Primary Key in a table, and multiple Unique Keys can exist at the same time.The bigger difference is in the logical design. Primary Key is generally used as a record identifier in logical design. This is also the original intention of setting
Primary Key, while Unique Key is only to ensure the uniqueness of the domain/domain group.
5. Composite index and single index
Composite index refers to a multi-field joint index. These fields are often combined when querying Query the conditions again
The unique index is mainly indexed by the primary key ID, and the storage structure sequence is consistent with the physical structure
For example: create index idx on tbl(a,b)
Sort by a first, and similarly sort a by b, so when you check a or ab,
can use this index. But when you only check b, the index is not very helpful to you. .Maybe you can jump to search.
--------------------------------------------- -------
Instances of adding and deleting indexes:
1. The primary key of the table, Foreign keys must have indexes;
2. Tables with data volume exceeding 300w should have indexes;
3. Tables that are often connected to other tables must be connected before Indexes should be established on the fields;
4. Fields that often appear in the Where clause, especially fields in large tables, should be indexed;
5. Indexes It should be built on highly selective fields;
6. Indexes should be built on small fields. For large text fields or even super long fields, do not build indexes;
7. Composite index The establishment requires careful analysis; try to consider using a single-field index instead:
A. Correctly select the main column field in the composite index, which is generally a field with better selectivity;
B. Do several fields of a composite index often appear in the Where clause in an AND manner at the same time? Are there few or no single field queries? If so, you can create a composite index; otherwise, consider a single-field index;
C. If the fields included in the composite index often appear alone in the Where clause, break it into multiple single-field indexes;
D. If the compound index contains more than 3 fields, carefully consider the necessity and consider reducing the number of compound fields;
E. If there are both single-field indexes and these several Composite indexes on fields can generally be deleted;
8. Do not create too many indexes for tables that frequently perform data operations;
9. Delete useless indexes to avoid executing Plan to have a negative impact;
The above are some common judgments when establishing an index. In a word, the establishment of indexes must be cautious, and the necessity of each index should be carefully analyzed and there must be a basis for establishment. Because too many indexes and insufficient or incorrect indexes are not beneficial to performance: each index created on the table will increase storage overhead, and the index will also increase processing overhead for insert, delete, and update operations. In addition, too many compound indexes are generally of no value when there are single-field indexes; on the contrary, they will also reduce the performance when data is added and deleted, especially for frequently updated tables, the negative impact is even greater big
The above is the detailed content of A brief introduction to the index design principles of MySQL and the differences between common indexes. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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

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.

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

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.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

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

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
