Home Database Mysql Tutorial mysql advanced (2) index simple tutorial

mysql advanced (2) index simple tutorial

Feb 09, 2017 pm 03:11 PM
mysql index Advanced

Mysql Index Simple Tutorial

Basic Concepts

Indexing refers to storing the content of field A that you set as an index in an independent interval S, which contains only the content of this field. When searching for the contents of this field A, it will be searched directly from this independent interval instead of searching in the data table. After finding these qualified fields, read the physical address of the real data record pointed to by field A, and then output the corresponding data content. If you are looking for a field that is not indexed, it will be searched from the data table. Because the data table has many irrelevant fields, the database program will not omit or search them. It takes a certain amount of resources to determine those irrelevant fields and jump in the record multiple times. Of course, the more indexes you set, the better. Because the indexes are placed in this independent interval S, the larger the independent interval S, the greater the search resources. If you only have one field indexed, then your search on this field is very fast.

The purpose of creating an index is to speed up the search or sorting of records in the table. Setting an index for a table comes with a price: First, it increases the storage space of the database, and second, it takes more time to insert and modify data (because the index also changes accordingly).

The advantage of the index is that it can sort the specified columns and improve the speed of retrieval.

A simple example:

The data in a certain column is

id name

12 Xiao Li

10 Xiaolong

5 Xiaoqing

99 Xiaohong

After creating an index on the id column, an index table will be generated

id index

5 3

10 2

12 1

99 4

When querying where id =10, the index table is used. Because below 10 is 5. Therefore, the table scan operation is no longer performed. Return the second piece of data, corresponding to the second row of the main table. This improves the query speed. If no index is added, the entire main table will be scanned.

You need to search Baidu for related information such as the type of index and which columns need to be indexed. What I tell you here are some basic concepts.

The functions, advantages and disadvantages of database indexes

Why create an index? This is because creating indexes can greatly improve system performance.

First, by creating a unique index, the uniqueness of each row of data in the database table can be guaranteed.

Second, it can greatly speed up data retrieval, which is also the main reason for creating an index.

Third, it can speed up the connection between tables, which is particularly meaningful in achieving referential integrity of data.

Fourth, when using grouping and sorting clauses for data retrieval, the time for grouping and sorting in queries can also be significantly reduced.

Fifth, by using indexes, you can use optimization hiders during the query process to improve system performance.

Some people may ask: There are so many advantages to adding an index, why not create an index for every column in the table? Although this idea is reasonable, it is also one-sided. Although indexes have many advantages, it is very unwise to add an index to every column in the table. This is because there are many downsides to adding an index.

First, creating and maintaining indexes takes time, and this time increases as the amount of data increases.

Second, the index needs to occupy physical space. In addition to the data space occupied by the data table, each index also occupies a certain amount of physical space. If you want to build a clustered index, the space required will be larger. .

Third, when adding, deleting, and modifying data in the table, the index must be dynamically maintained, which reduces the data maintenance speed.

Indexes are built on certain columns in the database table. Therefore, when creating an index, you should carefully consider which columns can be indexed and which columns cannot be indexed. Generally speaking, indexes should be created on these columns, for example:

On columns that are often searched, you can speed up the search;

On the column that is the primary key, force the column The uniqueness and arrangement structure of the data in the organization table;

are often used in the columns of the connection. These columns are mainly foreign keys, which can speed up the connection;

In the columns that are often used in the connection, Create an index on the column that needs to be searched based on the range, because the index has been sorted and its specified range is continuous;

Create indexes on columns that often need to be sorted, because the index has been sorted, so that queries can use the sorting of the index to speed up sorting query time;

Create indexes on columns that are often used in the WHERE clause , speed up the judgment of conditions.

Similarly, indexes should not be created for some columns. Generally speaking, these columns that should not be indexed have the following characteristics:

First, indexes should not be created for columns that are rarely used or referenced in queries. This is because, since these columns are rarely used, indexing or not indexing does not improve query speed. On the contrary, due to the addition of indexes, the maintenance speed of the system is reduced and the space requirements are increased.

Second, indexes should not be added to columns with few data values. This is because, since these columns have very few values, such as the gender column of the personnel table, in the query results, the data rows in the result set account for a large proportion of the data rows in the table, that is, the data that needs to be searched in the table. The proportion of rows is huge. Increasing the index does not significantly speed up retrieval.

Third, indexes should not be added to columns defined as text, image and bit data types. This is because the data volume of these columns is either quite large or has very few values.

Fourth, when the modification performance is far greater than the retrieval performance, the index should not be created. This is because modification performance and retrieval performance are contradictory to each other. When adding indexes, retrieval performance will be improved, but modification performance will be reduced. When reducing indexes, modification performance will increase and retrieval performance will decrease. Therefore, when modification performance is much greater than retrieval performance, indexes should not be created.

Methods to create indexes and characteristics of indexes

Methods to create indexes

There are many ways to create indexes, including direct index creation methods and indirect index creation methods method. Create an index directly, such as using the CREATE INDEX statement or use the Create Index Wizard, or create an index indirectly, such as defining a primary key constraint or a unique key constraint in a table, and the index is also created at the same time.

Although both methods can create indexes, the specific contents of creating indexes are different.

Use the CREATE INDEX statement or use the Create Index Wizard to create an index. This is the most basic way to create an index, and this method is the most flexible. You can customize the index to meet your needs.

When using this method to create an index, you can use many options, such as specifying the fullness of the data page, sorting, sorting statistics, etc., so that you can optimize the index. Using this method, you can specify the type, uniqueness and compositeness of the index, that is, you can create a clustered index or a non-clustered index, you can create an index on one column or on two Or create an index on more than two columns.

You can also create an index indirectly by defining primary key constraints or unique key constraints. A primary key constraint is a logic that maintains data integrity by restricting records in a table to have the same primary key record. When creating a primary key constraint, the system automatically creates a unique clustered index.

Although, logically, the primary key constraint is an important structure, but in terms of physical structure, the structure corresponding to the primary key constraint is a unique clustered index. In other words, in physical implementation, there is no primary key constraint, but only a unique clustered index.

Similarly, when creating a unique key constraint, an index is also created. This index is a unique non-clustered index. Therefore, when using constraints to create an index, the type and characteristics of the index have basically been determined, and there is less room for user customization.

When defining a primary key or unique key constraint on a table, if the table already has a standard index created using the CREATE INDEX statement, the index created by the primary key constraint or unique key constraint will overwrite the previously created index. Standard index. In other words, indexes created by primary key constraints or unique key constraints have a higher priority than indexes created using the CREATE INDEX statement.

Characteristics of index

Index has two characteristics, namely unique index and composite index.

Unique index ensures that all data in the index column is unique and does not contain redundant data. If the table already has a primary key constraint or a unique key constraint, SQL Server automatically creates a unique index when the table is created or modified.

However, if uniqueness must be guaranteed, a primary key constraint or a unique key constraint should be created instead of a unique index. When creating a unique index, you should carefully consider these rules: When creating a primary key constraint or a unique key constraint on a table, SQL Server automatically creates a unique index;

If the table already contains data, Then when creating an index, SQL Server checks the redundancy of the data already in the table; whenever an insert statement is used to insert data or a modify statement is used to modify data, SQL Server checks the data for redundancy: If there are redundant values, then SQL Server cancels the execution of the statement and returns an error message;

Ensure that each row of data in the table has a unique value, which ensures that each entity can be uniquely confirmed; only when the entity can be guaranteed Create a unique index on the complete column. For example, you cannot create a unique index on the name column in the personnel table because people can have the same name.

A composite index is an index created on two or more columns. When searching, when two or more columns serve as a key value, it is best to create a composite index on these columns. When creating a composite index, you should consider these rules: Up to 16 columns can be combined into a single composite index, and the total length of the columns that make up the composite index cannot exceed 900 bytes, which means that the length of the composite column cannot be too long;

In a composite index, all columns must come from the same table, and composite columns cannot be created across tables; in a composite index, the order of the columns is very important, so the order of the columns must be carefully arranged. In principle, the most unique column should be defined first. For example, the index on (COL1, COL2) is different from the index on (COL2, COL1) because the order of the columns of the two indexes is different;

In order for the query optimizer to use a composite index, the WHERE clause in the query statement must refer to the first column in the composite index; when there are multiple key columns in the table, the composite index is very useful; using a composite index can improve Query performance, reduce the number of indexes created on a table.

Type of index

Non-unique index means that the values ​​in this index are allowed to be repeated. Relative to the unique index, the values ​​in this index are not allowed to be repeated.

A simple example is like our ID card. If stored in the database. If you create an index on the name, it is a non-unique index because a person with the same name exists. If you create an index on the ID number, it will be a unique index. Because the numbers are repeated, it will be troublesome.

The above is the content of mysql advanced (2) index simple tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

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.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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 and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles