MySQL Performance Tuning statement



MySQL Overview
1. Database design paradigm 3
2 sub-table database and warehouses --- Member system () split horizon (how to query pagination) MyChar, vertical
3. how positioning slow query
------------ ---------
optimized index database, the index principle
SQL statement tuning
database separate read and write --MyChar
---------------------
packet having
stored procedures, triggers, functions,
stored procedures: write a piece of sql statement, Java methods similar, just call pass parameters drawbacks: sql statement is written dead, not flexible to change.
mysql (free, open source) oracle (surcharge)
MySQL pagination limit. oracle: rownum pseudo column


MySQL optimization
1. rationally designing database (3F)
2. Add index (ordinary index, primary key index, unique index, full-text index) underlying: B-Tree and B + Tree algorithm and the same binary tree, to reduce the full table scan
3 minutes table library technology division (modulo sub-table, a horizontal split, vertical split)
4. separate read and write
5. stored procedure
6. configure MySQL maximum number of connections the my.ini
7.mysql upgrade server
8. The clean up any fragmentation
9.sql statement tuning core


Three Forms database
database design
what is the data design (reducing the amount of redundancy, 3F)
what database paradigm 3
1F atoms in each column represents a constraint can not be divided
   the above mentioned id name Sex address
  1 0 zhangsan Beijing

  Whether guarantee atom (see Business)

2F guarantee a unique, primary key
   id orderNum (unique) name Sex address
  1 0 123 zhangsan Beijing
  Orders table, whether to use id as the order number? It is not allowed.
  Most long-distance calls within the project is the use of rpc id for communication, to the external look is orderNum, to ensure system security.
 
  Distributed systems to solve concurrency generated order number
  how guarantee to grab votes, the order number generation will not repeat? How to ensure orders idempotency (idempotent is not repeated)?
  Advance order number generation number, there is redis, the need to go directly to resis to fetch; Distributed Lock

3F do not have redundant data classId className repeat, this table can only keep classId
   the above mentioned id name Sex address classId className
  1 0 zhangsan Beijing, a group of
  2 lisi 0 Beijing 2 second class
  2 wangwu 0 Beijing 1 class

  From New tables:
 classId className
 1 class
 2 second class
 
 Note: may not be entirely to follow the first 3F.


MySQL sub-library sub-table
when sub-libraries:
    the electricity supplier project which will be a project split, split into multiple small projects, small projects each have their own separate database, independently of each other. - split vertically.
  Membership database, order database, payment database

When sub-table:
    the level of segmentation, sub-table rules, according to business needs. The Year log storage table, the first three sub-table according to the mobile phone number 136 135 135
  split (modulo arithmetic) level

  user table is divided into three tables 
  ID name address
  . 1 Zhang Beijing
  2 Doe Beijing
  3 Wangwu Beijing
  4 Zhao six Beijing
  5 Xiaoming Beijing
  6 Beijing red

  How to six data stored in three different tables, how very uniform? Modulo arithmetic
  table: user0 user1 user2  
  first data: 1% 3 = 1, the table put user1.  
  Second data: 2% 3 = 2, the discharge table user2.   
  Third data: 3% 3 = 0, let user0 table. And so on

  Modulo arithmetic to achieve sub-table: three tables id can not automatically grow, there is a need for specialized storage table userId, to assign the past.


Level segmentation algorithm touch cases take
advantage: very uniform distribution
how to check where to watch? Find id in which 6 Table 6% 3 = 0 to find a table in user0

Demo:

create table uuid(
 id int unsinged primary key auto_increment,
)engine=myisam charset utf8;

@Service
public class UserService{
 
 @Autowired
 private JdbcTemplate jdbcTemplate;

  / **
  * generates user information
  * /
 public String Regit (String name, String pwd) {
  . //. 1 generates the userId
  String insertUuidSql = "INSERT INTO UUID values (null)";
  // return directly used herein generally primary key id, not the following query
    jdbcTemplate.update (insertUuidSql);
  // SELECT LAST_INSERT_ID () represents primary key ID of the nearest mean
    Long jdbcTemplate.queryForObject the userId = ( "SELECT LAST_INSERT_ID ()", requiredType, Long.class);
  . 2 // store DETAILED which tables in
  String tableName = "User" + the userId%. 3;
  //. 3 inserted into the particular table
  String insertUserSql = "insert into" + tableName + "values (" + userId + "," + userName + "," + pwd + ")" ;
  System.out.println (insertUserSql);
  jdbcTemplate.update (insertUserSql);
  return "success";
 }

 /**
  * 根据id查询
  */
 public String get(Long userId){
  //1.存放具体哪张表中
  String tableName = "user"+userId%3;
  String selectUserSql="select name from "+tableName+"where id="+userId;
  String name = jdbcTemplate.queryForObject(selectUserSql,String.class);
  return name;
 }
}

@RestController
public class UserController{

 @Autowired
 private UserService userService;

 @RequestMapping("/regit")
 public String regit(String name,String pwd){
   return userService.regit(name,pwd);
 }

 @RequestMapping("/getUser")
 public String get(Long userId){
   return userService.get(userId);
 }
}

@SpringBootApplication
public class App{
 public static void main(String[] args) {
     SpringApplication.run(App.class,args);
  }
}

What are the disadvantages minutes after the table?
1. how paging query
2. Query is very limited, such as male gender check to check three tables
3 modulo arithmetic if the table is changed, the table to be re-divided, disrupting the use of RDS Ali cloud database

First the main table to store all the data table is divided according to business needs.


How to locate slow queries

What is slow query?
MySQL slow query default is 10 seconds, 10 seconds if there is no response back is slow query, usually in a production environment is set to 1 second

There will be a slow query log stored
show status command

How to modify slow queries
- queries slow query
Show Status like 'Slow_queries';
- slow query query time
Show Variables like 'long_query_time';
- Slow modified query time
set long_query_time = 1; represents the slow modified query time is 1 second

How to locate the slow query log
in default, MySQL does not record slow queries, we need to start the MySQL when the specified record slow queries can.

Edit Profile /etc/my.cnf add the following
[mysqld]
slow_query_log the ON =
slow_query_log_file = /var/lib/mysql/test-10-226-slow.log
long_query_time. 1 =

After modifying the configuration restart MySQL
systemctl restart mysqld
MySQL-uroot--p


MySQL Index Overview

Why indexing? Improve query performance
Why can improve query efficiency index - the index to achieve the principle of binary search

Index Classification:
primary key index - primary key
to add a primary key index mode:
1. Create a table of time to add: the above mentioned id int unsinged Primary Key AUTO_INCREMENT,
2. if not added when creating the table: alert table table add primary key (the column name) ;

Drop Primary Index: alert table table name drop primary key;

C:\ProgramData\MySQL\MySQL Server 5.6\data\test 下:

* .frm file table structure
* .MYD data structure file
* .MYI index file

The only index
composite index
full-text index
general index


The principle underlying index

B-tree index using the underlying binary search called binary search

Full table scan is no index - the index to reduce the full table scan

First generation b-tree index index file

Index What are the disadvantages? Add, delete index files need to be updated


General index and unique index

Unique index: key UNIQUE
Create Table table name (
 ID int Primary Key AUTO_INCREMENT, // primary key index
 name varchar (32) unique; // unique index
)

Note: unique field can be null, and may have a plurality of null, but if there is specific content, can not be repeated.
With very little unique index, instead of the primary key index.


Common Index:
Create Table table name (
 ID unsigned int,
 name VARCHAR (32)
)

Create a regular index: creat index index name on table (column 1, column 2);

creat index index_aaa on aaa (name);

- there is no implementation plan view an index
explain select * from aaa where name = "zhangsan";
check out the ref type to use the index represents all are full table scan

Full-text index:

the Table aaa the Create (
 the above mentioned id int Primary Key,
 title VARCHAR (200),
 body TEXT,
 FULLTEXT (title, body) // create a full-text index
) engine = innodb;


Fuzzy query: select * from aaa where body like '% Zhang%'; wrong usage, the index does not take effect

Use the execution plan to see whether the index:
EXPLAIN the SELECT * from aaa body like the WHERE 'Zhang%%';

Use index query: select * from aaa where match (title, body) against ( 'photos')

Use the execution plan to see whether the index:
EXPLAIN the SELECT * from aaa match the WHERE (title, body) Against ( 'photos')

When using a full-text index:
Do not like
full-text index of the table will not adopt business practice. Full-text indexing a very big disadvantage, InnoDB does not support full-text indexing (database storage engine) in the

 
SQL statement optimization summary

Index advantages and disadvantages:
Advantages: improve program efficiency
Disadvantages: add, delete slow, index files need to be updated, adding memory

What fields fit the indexed?
Queries more, there are very many different values.

Indexing scene:
when indexed, where conditions require query and values are very much different. The only several values (sex: 0,1), does not require indexing.


Notes index, sql tuning parts:


- create a primary key index
alert talbe table add primary key (column names);
- to create a composite index file
alert table table add index my_ind (column 1, column 2);

Note:
1. For multi-column indexes created, if you do not use the first part, it will not create the index.
explain select * from table where column 1 = 'aaa'; - use the index
explain select * from table where column 2 = 'bbb'; - the use of a full table scan

2. The use of the index, do not use like '%%', so will a full table scan, using like, do not begin with%
explain the SELECT * from table where column 1 like '% aaa%'; - Use a full table scan
explain select * from table where column 1 like 'aaa%'; - use the index

3. or, conditions must be indexed, as long as one condition is not indexed, there will be a full table scan
explain select * from table where column 1 = 'aaa' or column 3 = 'ccc'; - use the index

4 determines whether null, use is null not null =
5. The packet group by using the index will not be used, will be a full table scan.
explain select * from table group by the column 2;
6. High efficiency packets need to prohibit the sort (the default packet ordering)
explain select * from table group by the column null by Order 2;
7. The AAA SELECT * WHERE from the userId> = 100 and select * from aaa where userId> 100 high efficiency which
do not use greater than or equal, will determine two full table scan

8. in and add notin the index will not use the index, can not use in between
9. When the query is very large, the use of cache points table, pagination.


MySQL storage engine differences

MySQL storage engine: innodb / myisam / memory

Mainstream: innodb support transaction mechanism

innodb differs myisam:
Bulk Add --myisam high efficiency
innodb-- very secure transaction mechanism

Lock mechanism:
MyISAM table lock is
innodb a row lock, it will not affect the entire table

Data Structure:
MyISAM supports full-text search, full-text search database generally do not own.

Support b-tree data structure

Index cache support.

Myisam Notes

Create myisam Engine Table Structure:
Create Table CCC (ID int, name VARCHAR (32)) Engine = myisam;
add data:
INSERT INTO CCC values (. 1, 'A');
INSERT INTO CCC values (2, 'B');
INTO CCC values INSERT (. 3, 'B');
INSERT INTO SELECT CCC ID, name from CCC;

Id to delete all the data 3
delete from ccc where id = 3;

You will find: ccc.MYD file size has not changed, Cons: no real deleted can be recovered very quickly after deleting come

I really want to delete use: optimize table ccc; told myisam organize, clear of fragmentation.
Business practice is not physically delete data. data migration.


Published 43 original articles · won praise 32 · views 40000 +

Guess you like

Origin blog.csdn.net/yz2015/article/details/79601688