MySQL advanced learning index advantages and disadvantages and rules of use

MySQL advanced learning index advantages and disadvantages and rules of use

1. Advantages and Disadvantages of Indexes

Advantages: fast retrieval, faster grouping and sorting

Disadvantages: Occupies storage space and reduces data table modification operations

2. Classification of Indexes

Primary key index That is, the primary index is created based on the primary key pk_clolum (length), and duplicates and null values ​​are not allowed.
Unique Index The values ​​of the columns used to create the index must be unique, and null values ​​are allowed.
Normal index An index built with normal columns in a table, without any restrictions
Full-text index Index built on columns of large text objects
Full-text index Indexes built with columns of large text objects;
Composite Index An index built using multiple columns. The values ​​in these columns do not allow null values.

3. Rules for using indexes

1. Situations suitable for indexing

  • The primary key automatically creates a unique index;
  • Columns that often appear as query conditions in WHERE or ORDER BY statements should be indexed;
  • The column to be sorted needs to be indexed;
  • The fields and foreign key relationships associated with other tables in the query are indexed;
  • Prefer combined indexes under high concurrency conditions;
  • Columns used for aggregate functions can be indexed. For example, column_1 needs to be indexed when max(column_1) or count(column_1) is used.

2. Situations where indexing is not suitable

  • Do not create indexes for columns that are frequently added, deleted, or modified;
  • There are a lot of repeated columns that are not indexed;
  • Do not create an index if the table has too few records.

3. Index failure

  • There cannot be a NULL value in a composite index. If there is, then this column is invalid for the composite index.
  • In the LIKE operation, '%aaa%' will not use the index, that is, the index will be invalid, but 'aaa%' can use the index;
  • Using expressions or functions on indexed columns will invalidate the index;
  • Use not equal to in the query condition, including the < symbol, > symbol and ! = will cause the index to fail;
  • Using IS NULL or IS NOT NULL in the query condition will cause the index to become invalid;
  • If the string is not enclosed in single quotes, the index will be invalid;
  • Using OR to connect multiple conditions in a query will cause the index to fail unless each condition linked by OR is indexed;
  • If the sorting field uses an index, the selected field must also be an index field, otherwise the index will be invalid;
  • Try not to include multi-column sorting; if you must, it is best to build a composite index for the queue.

4. SQL about index

1. Add index when creating table

-- Add an index when creating a table -- INDEX keyword -- myindex Index name you choose yourself -- (username(16)) to which column to add it CREATE TABLE mytable(
 ID INT NOT NULL,
 username VARCHAR(16) NOT NULL,
 INDEX myindex (username(16))
);

2. Add index after creating table

-- Add index -- myindex index name (self-defined)
-- mytable table name CREATE INDEX myindex ON mytable(username(16));
or ALTER TABLE mytable ADD INDEX myindex(username);

3 View index

-- mytable table name show index FROM mytable;

4. Delete index

-- myindex index name (self-defined)
-- mytable table name DROP INDEX myindex ON mytable;
or ALTER TABLE mytable DROP INDEX myindex;

Summarize

This is the end of this article about the advantages and disadvantages of indexing and the use of rules for advanced MySQL learning. For more relevant MySQL advanced indexing content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • mysql add index mysql how to create index
  • MySQL index type summary and usage tips and precautions
  • How to view, create and delete indexes in MySQL
  • MySQL Create Index method, syntax structure and examples
  • MySQL performance optimization index optimization
  • Analysis of several situations where Mysql indexes fail
  • Analysis of the connection and difference between MySQL primary key and index
  • In-depth understanding based on MySQL full-text index
  • Solve the problem that IN subquery in MySQL will cause the index to be unusable
  • MySQL index analysis and optimization

<<:  Example of how to modify styles via CSS variables

>>:  A brief discussion on the problem of Docker run container being in created state

Recommend

Using Nginx to implement grayscale release

Grayscale release refers to a release method that...

JavaScript to achieve digital clock effects

This article example shares the specific code for...

How to introduce scss into react project

First download the dependencies yarn add sass-loa...

Solution to the problem of MySQL deleting and inserting data very slowly

When a company developer executes an insert state...

How does Vue solve the cross-domain problem of axios request front end

Table of contents Preface 1. Why do cross-domain ...

Vue button permission control introduction

Table of contents 1. Steps 1. Define buttom permi...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

Using Docker to create static website applications (multiple ways)

There are many servers that can host static websi...

Detailed explanation of Vue's methods and properties

Vue methods and properties 1. Methods Usage 1 met...

Alpine Docker image font problem solving operations

1. Run fonts, open the font folder, and find the ...

MySQL not null constraint case explanation

Table of contents Set a not null constraint when ...

How to modify the MySQL character set

1. Check the character set of MySQL show variable...

Detailed explanation of Vue mixin usage and option merging

Table of contents 1. Use in components 2. Option ...

Example explanation of MySQL foreign key constraints

MySQL's foreign key constraint is used to est...