Beckham _ mysql optimization learning, and Beckham _ mysql Optimization

Source: Internet
Author: User

Beckham _ mysql optimization learning, and Beckham _ mysql Optimization
Mysql OptimizationBrief:
1. Database Design Optimization
2. SQL statement Optimization
3. Table Segmentation
4. read/write splitting technology

 

I. Database Design Optimization

1. Table design must comply with the three paradigms. Of course, appropriate inverse paradigms are also required.

2. What is the three paradigm?

1. paradigm: atomicity, which cannot be separated

2. paradigm: On the basis of satisfying the 1 paradigm, we will consider whether or not the 2 paradigm is satisfied. As long as the table record satisfies the uniqueness, It is also said that the same table cannot have identical records. Generally, you can design a primary key in the table.

Three paradigms: Based on the fulfillment of the two paradigms, we consider whether or not the three paradigms are satisfied. As long as the table does not have redundancy.

 

Ii. SQL statement Optimization

1. General SQL optimization steps

A. Run the show status command to check the execution efficiency of various SQL statements.

B. Locate SQL statements with low execution efficiency

C. Analyze the execution of inefficient SQL statements through explain/desc

D. Identify the problem and take corresponding optimization measures

2. showstatus command

This command can display the current status of the mysql database. It is mainly concerned with commands starting with 'com '.

Showstatus like 'com % ''' show session status like 'com % '// display the current console status

Showglobal status like 'com % '// displays the status of the database from startup to present

3. showvariables command

This command can view the current mysql variable settings, mainly concerned with the slow query time

 

4. How to find slow query SQL statements in mysql

(Note: mysql database supports recording slow query statements into logs for analysis by programmers. By default, mysql does not enable slow query logs)

Step: a. Start mysql slow Query

A1. when starting the mysql service, specify-slow-query-log

A2. After logging on to mysql using the client, set the variable

B. View slow query time

The default value is 10 seconds.

C. Modify the slow query time

Set to 1 second

(This can only take effect in the current environment. If you want to take effect every time, modify the mysql configuration file)

D. View slow query logs

E. Optimize the SQL statements for slow queries. The cheapest way is to add an index.

F. After the index is added

5. Index impact

A. increase disk space

B. inconvenience caused by addition, deletion, and modification

6. Columns suitable for Indexing

A. index should be created frequently as a query condition Field

B. A field with poor uniqueness (that is, the value of this field does not change much) is not suitable for independent index creation, even if it is frequently used as a query condition.

C. It is not suitable for creating indexes for frequently updated fields.

D. fields that do not appear in the where clause should not be indexed.

7. Use of Indexes

Test Table:

Create Table: create table 't2 '(

'Id' int (11) not null default '0 ',

'Name' char (5) default null,

'Age' tinyint (4) default null,

Primary key ('id ')

) ENGINE = MyISAM DEFAULTCHARSET = utf8;

Insert into t2 (name, age) values ('A', 2), ('A', 3), ('B', 4), ('C ', 3 );

The most important condition for an index to be used for a query is that the index must be used in the query condition.

Indexes may be used in the following situations:

A. For the created multi-column index, the index is generally used as long as the leftmost column is used for the query condition.


B. For queries using like, if '% aaa' is used, the index 'aaa %' is not used.

The following tables do not use indexes:

A. If there is or in the condition, it will not be used even if there is a condition with an index.


B. if multiple-column indexes are not the first part, they are not used.
C. The like Query starts with %.
D. If the column type is a string, use quotation marks to reference the data in the condition. Otherwise, no index is used.
E. If mysql estimates that full table scan is faster than indexing, no index is used.

8. Verify index usage

Showstatus like 'handler _ read % ';


Note:
Handler_read_key: the higher the value, the better. The higher the value indicates the number of times the index is queried.

Handler_read_rnd_next: a higher value indicates inefficient query.

 

9. Common SQL Optimization

A. insert data in large batches:

For MyIsam:

1. alter table table_name disable keys;

2. loading data;

3. alter table table_name enable keys;

For Innodb:

1. Sort the data to be imported by primary key

2. set unique_checks = 0. Disable uniqueness verification.

3. set autocommit = 0. Disable Automatic submission.

(Note: The difference between myisam and innodb is:

A. myisam does not support external connections, and innodb does.

B. myisam does not support transactions, and innodb does)


B. Optimize group

By default, mysql sorts the column names after group. If the query contains group by but you want to avoid consumption of sorting results, you can use order by null to disable sorting.

 

Iii. Table Segmentation

When the data in a table is large, other optimization methods have been taken into account. When it does not play a major role, we need to consider Table sharding. Split a large table into multiple small tables.

Table sharding method:

A. Vertical table sharding

At this time, there are many columns in the Table. At this time, you can use the primary key to divide the columns in the table into multiple tables, and then associate the columns based on the primary key. (after splitting, the columns in each table are different)

Before table sharding: personal information table

Id

Name

Age

Email

Intro

1

A

11

11@qq.com

Xxxx

2

B

22

22@qq.com

Yyyy

 

 

 

 

 

 

After Table sharding: personal information table

Id

Name

Age

Email

1

A

11

11@qq.com

2

B

22

22@qq.com

Personal Profile

Id

Intro

1

Xxxx

2

Yyyy

 

B. Horizontal table sharding

You can perform table sharding by modulo. Therefore, you need to determine the number of small tables to be divided into, that is, the value of the modulo. In addition, the columns in each table are consistent after the split.

Before table sharding: personal information table

Id

Name

Age

Email

Intro

1

A

11

11@qq.com

Xxxx

2

B

22

22@qq.com

Yyyy

 

 

 

 

 

Determine that the modulo value is 2. Therefore, we can divide this table into two small tables.

1. Determine the id value. id/2 =?

After Table sharding: personal information table 0

Id

Name

Age

Email

Intro

1

A

11

11@qq.com

Xxxx

Personal information table 1

Id

Name

Age

Email

Intro

2

B

22

22@qq.com

Yyyy

 

 

Iv. read/write splitting

Generally, a mysql server carries all database operations. However, when the traffic is high, the mysql server is prone to bottlenecks. To reduce the pressure on the mysql server (mysql itself supports master-slave replication)

You can perform read/write splitting.

1. Before read/write splitting

 

2. read/write splitting

 


The quieter you become, the more you are able to hear!

 

 

 

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.