How to optimize MySQL

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/lm9521/article/details/89861453

Optimization can proceed from the following aspects:

  1. Design rationalization table (in line with 3NF)
  2. Add the appropriate index (index) [four kinds: ordinary index, primary key index, unique index UNIQUE, full-text index]
  3. SQL statement optimization
  4. Technical part table (horizontal split, vertical split)
  5. Read [Write: update / delete / add] Separation
  6. Stored Procedures [modular programming, can improve the speed]
  7. For mysql configuration optimization [configure the maximum number of concurrent my.ini, adjust the cache size]
  8. mysql server hardware upgrade
  9. Timed to clear unwanted data, regular defragmentation (MyISAM)

A database of three paradigms:

The first paradigm: 1NF atomic constraints on properties, required attributes (columns) having atomic, no longer decomposition; (as long as it satisfies the relational database 1NF)

The second paradigm: 2NF is the only constraint records, records in the table is unique, we meet 2NF, we usually designed to achieve a primary key, the primary key can not contain business logic.

The third paradigm: 3NF field redundancy is bound to, which requires no redundant field. No redundant database design can be done.

However, there is no redundancy of the database may not be the best database, sometimes in order to improve operational efficiency, it must reduce the standard paradigm, appropriate to retain redundant data. Specifically: physical data model designed to comply with the third paradigm in conceptual data model design, reduce the standard paradigm of work into consideration. Reduce paradigm is to increase the field to allow redundancy.

Second, the points table and warehouses

Vertical Split:

       Vertical split is to take the table in modules into different database tables (of course, the principle still does not destroy the third paradigm), which split in the evolution of large sites are very common. When a site is still very young, only a small amount of people to develop and maintain the modules and tables all together, and constantly enrich and expand the site when the time will become more subsystems to support, then you there are modules and functions according to the table carved out of demand. In fact, with respect to the vertical segmentation of service further transformation, put it simply take the original strong coupling is split into a plurality of weakly coupled system services to business needs to see through calls between services, split table Came out to be exposed to in the form of services, rather than a direct call table of different modules, Taobao in architecture evolving process, the most important part is the service transformation, the user, transaction, shop, baby these core concepts drawn into stand-alone service, but also very beneficial for local optimization and governance, safeguard the stability of the core module

A vertical split distributed scenario.

Split level:

      Turning the vertical slicing above table are just modules into different databases, but does not solve the problem of a large amount of a single table of data, and is to bring a level segmentation table according to certain rules to divide the data into different tables or database. Billing system such as for example, by dividing time table is more appropriate, because the data processing systems are of a certain time period. And like SaaS application, the user by pressing the appropriate dimensions to the data, because the isolation between the user and the user, where the plurality of user data is typically processed does not exist, according to a simple segmentation level user_id range.

Popular understood: horizontal split lines, split into different data rows in the table, the vertical column split, split the data table into different tables

[Modulo horizontal resolution algorithm may be used]

Three, SQL optimization

Positioning slow queries

使用show status使用show status查看MySQL服务器状态信息
常用命令
--mysql数据库启动了多少时间
show status like 'uptime';
show  stauts like 'com_select'  show stauts like 'com_insert' ...类推 update  delete(显示数据库的查询,更新,添加,删除的次数)
show [session|global] status like .... 如果你不写  [session|global] 默认是session 会话,指取出当前窗口的执行,如果你想看所有(从mysql 启动到现在,则应该 global)
//显示到mysql数据库的连接数
show status like  'connections '; 
//显示慢查询次数
show status like 'slow_queries';

What is slow query?

  MySQL default SQL result does not respond within 10 seconds, for the slow query

  You can modify the default MySQL slow query time

--查询慢查询时间
show variables like 'long_query_time';
--修改慢查询时间
set long_query_time=1; ---但是重启mysql之后,long_query_time依然是my.ini中的值

How to locate the slow query log?

在默认情况下,我们的mysql不会记录慢查询,需要在启动mysql时候,指定记录慢查询才可以

bin\mysqld.exe --safe-mode  --slow-query-log [mysql5.6 可以在my.ini指定](安全模式启动,数据库将操作写入日志,以备恢复)[my.ini在 E:\Program Files\MySQL\ProgramData\MySQL Server 5.6 目录下]

bin\mysqld.exe –log-slow-queries=d:/abc.log [低版本mysql5.0可以在my.ini指定]

先关闭mysql服务,cmd安全模式启动, 如果启用了慢查询日志,默认把这个文件放在
my.ini 文件中记录的位置
# Path to the database root
datadir="E:/Program Files/MySQL/ProgramData/MySQL Server 5.6/data\"

打开文件会看到慢查询的sql语句

 

Guess you like

Origin blog.csdn.net/lm9521/article/details/89861453