search
HomeDatabaseMysql TutorialA 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.



8. Small tables should not be indexed; when they contain a large number of columns and do not need to search for non-null values, you can consider not building an index


---------- ------------------------------------------------

mysql indexRelated tips:

1. Fields often used to filter records .

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;

5. Fields often used as the basis for sorting (order by fields);

2. Indexes will occupy disk space, and creating unnecessary indexes will only cause waste.

#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 key

1. 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools