


Detailed explanation of the types, advantages and disadvantages of Mysql index_MySQL
Index is a special file (the index on the InnoDB data table is a component of the table space), they contain reference pointers to all records in the data table.
Note:
[1] Indexes are not omnipotent! Indexes can speed up data retrieval operations but make data modification operations slower. Every time a data record is modified, the index must be refreshed. In order to compensate for this shortcoming in a certain program, many SQL commands have a DELAY_KEY_WRITE item. The function of this option is to temporarily prevent MySQL from refreshing the index immediately after each new record is inserted and each existing one is modified by this command. The refresh of the index will wait until all records are inserted/modified. In situations where many new records need to be inserted into a data table, the role of the DELAY_KEY_WRITE option will be very obvious.
[2] In addition, indexes take up considerable space on the hard disk. Therefore, only the most frequently queried and most frequently sorted data columns should be indexed. Note that if a data column contains a lot of duplicate content, indexing it will not have much practical effect.
Theoretically, it is possible to build an index for each field in the data table, but MySQL limits the total number of indexes in the same data table to 16.
1. Index of InnoDB data table
Compared with MyISAM data tables, indexes are much more important for InnoDB data. On InnoDB data tables, indexes are much more important to InnoDB data tables. On InnoDB data tables, indexes not only play a role in searching data records, but are also the basis of the data row-level locking mechanism. "Data row-level locking" means locking individual records being processed during the execution of transaction operations to prevent other users from accessing them. This locking will affect (but is not limited to) the SELECT...LOCK IN SHARE MODE, SELECT...FOR UPDATE commands, and the INSERT, UPDATE, and DELETE commands.
For efficiency reasons, row-level locking of InnoDB data tables actually occurs on their indexes, not on the data table itself. Obviously, the data row-level locking mechanism can only be effective when the relevant data table has a suitable index for locking.
2. Restrictions
If there is an inequality sign (WHERE coloum != ...) in the query condition of the WEHERE clause, MySQL will not be able to use the index.
Similarly, if a function (WHERE DAY(column) = ...) is used in the query condition of the WHERE clause, MySQL will not be able to use the index.
In a JOIN operation (when data needs to be extracted from multiple data tables), MySQL can only use indexes when the data types of the primary key and foreign key are the same.
If the comparison operators LIKE and REGEXP are used in the query conditions of the WHERE clause, MySQL can use the index only if the first character of the search template is not a wildcard character. For example, if the query condition is LIKE 'abc%', MySQL will use the index; if the query condition is LIKE '%abc', MySQL will not use the index.
In an ORDER BY operation, MySQL uses the index only if the sort condition is not a query condition expression. (However, in queries involving multiple data tables, even if indexes are available, those indexes have no effect in speeding up ORDER BY)
If a data column contains many duplicate values, even if it is indexed, it will not have good results. For example, if a data column contains only values such as "0/1" or "Y/N", there is no need to create an index for it.
Normal index, unique index and primary index
1. Ordinary index
The only task of a normal index (an index defined by the keyword KEY or INDEX) is to speed up access to data. Therefore, indexes should only be created for those data columns that appear most frequently in query conditions (WHERE column = ...) or sorting conditions (ORDER BY column). Whenever possible, you should choose a data column with the neatest and most compact data (such as an integer type data column) to create an index.
2. Unique index
Normal index allows the indexed data column to contain duplicate values. For example, because people may have the same name, the same name may appear twice or more in the same "Employee Profile" data table.
If you can determine that a certain data column will only contain values that are different from each other, you should use the keyword UNIQUE to define it as a unique index when creating an index for this data column. The advantages of this are: First, it simplifies MySQL's management of this index, and the index becomes more efficient; second, MySQL will automatically check the value of this field in the new record when a new record is inserted into the data table. Whether this field has already appeared in a record; if so, MySQL will refuse to insert the new record. In other words, a unique index can ensure the uniqueness of data records. In fact, on many occasions, the purpose of creating unique indexes is not to increase access speed, but just to avoid data duplication.
3. Main Index
It has been emphasized many times before: an index must be created for the primary key field. This index is the so-called "primary index". The only difference between a primary index and a unique index is that the keyword used when defining the former is PRIMARY instead of UNIQUE.
4. Foreign key index
If a foreign key constraint is defined for a foreign key field, MySQL will define an internal index to help it manage and use the foreign key constraints in the most efficient way.
5. Composite index
Index can cover multiple data columns, such as INDEX(columnA, columnB) index. The characteristic of this index is that MySQL can selectively use such an index. If the query operation only needs to use an index on the columnA data column, you can use the composite index INDEX (columnA, columnB). However, this usage only applies to the combination of data columns that are ranked first in the composite index. For example, INDEX(A, B, C) can be used as an index of A or (A, B), but cannot be used as an index of B, C or (B, C).
6. Index length
When defining indexes for CHAR and VARCHAR type data columns, you can limit the length of the index to a given number of characters (this number must be less than the maximum number of characters allowed in this field). The advantage of this is that it can generate an index file that is smaller in size and faster in retrieval speed. In most applications, the string data in the database are mostly based on various names. Setting the index length to 10 to 15 characters is enough to narrow the search scope to a few data records. .
When creating indexes for BLOB and TEXT type data columns, the length of the index must be limited; the maximum index length allowed by MySQL is 255 characters.
Full text index
A normal index on a text field can only speed up the retrieval of the string that appears at the front of the field content (that is, the characters at the beginning of the field content). If the field stores a larger piece of text composed of several or even multiple words, ordinary indexes will be of little use. This kind of retrieval often appears in the form of LIKE %word%, which is very complicated for MySQL. If the amount of data that needs to be processed is large, the response time will be very long.
This is where full-text indexes can come in handy. When generating this type of index, MySQL will create a list of all words that appear in the text, and query operations will retrieve relevant data records based on this list. The full-text index can be created together with the data table, or it can be used later when necessary
Add the following command:
ALTER TABLE tablename ADD FULLTEXT(column1, column2)
With a full-text index, you can use the SELECT query command to retrieve data records that contain one or more given words. The following is the basic syntax of this type of query command:
SELECT * FROM tablename
WHERE MATCH(column1, column2) AGAINST(‘word1′, ‘word2′, ‘word3′)
The above command will query all the data records containing word1, word2 and word3 in the column1 and column2 fields.
Note: InnoDB data tables do not support full-text indexing.
Optimization of queries and indexes
Only when there is enough test data in the database, its performance test results will have actual reference value. If there are only a few hundred data records in the test database, they are often all loaded into memory after the first query command is executed, which will make subsequent query commands execute very quickly - regardless of whether indexes are used or not. The database performance test results are meaningful only when the number of records in the database exceeds 1,000 and the total amount of data exceeds the total memory on the MySQL server.
When people are not sure which data columns should be used to create indexes, people can often get some help from the EXPLAIN SELECT command. This is actually just prefixing an ordinary SELECT command with the EXPLAIN keyword. With this keyword, MySQL will not execute the SELECT command, but analyze it. MySQL will list information such as the execution process of the query and the indexes used (if any) in the form of a table.
In the output of the EXPLAIN command, column 1 is the name of the data table read from the database, and they are arranged in the order they are read. The type column specifies the association relationship (JOIN) between this data table and other data tables. Among various types of relationships, the most efficient one is system, followed by const, eq_ref, ref, range, index and All (All means: corresponding to each record in the upper-level data table, this data All records in the table must be read - this can often be avoided using an index).
possible_keys data column gives the various indexes that MySQL can use when searching for data records. The key data column is the index actually selected by MySQL. The length of this index in bytes is given in the key_len data column. For example, for an index on an INTEGER data column, the byte length would be 4. If a composite index is used, you can also see which parts of it are used by MySQL in the key_len data column. As a general rule, the smaller the value in the key_len data column, the better (meaning faster).
The ref data column gives the name of the data column in another data table in the relationship. The row data column is the number of data rows that MySQL expects to read from this data table when executing this query. The product of all the numbers in the row data column gives us an idea of how many combinations this query needs to process.
Finally, the extra data column provides more information about the JOIN operation. For example, if MySQL must create a temporary data table when executing this query, you will see the words using temporary in the extra column.
The above is the entire content of this article. I hope it will be helpful to everyone’s study and I hope you will support me a lot.

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software