Part of the MySQL database Java Web Overview

Java Web series summary posted: Java Web knowledge summary summary


MySQL Overview

About MySQL

1, MySQL is one of the most popular relational database management system, WEB applications, MySQL is the best RDBMS (Relational DataBase Management System, a relational database management system) software.
2, the advantages of relational databases is that data will be stored in separate tables rather than putting all the data in a large warehouse, thus increasing the speed and improved flexibility.
3, MySQL is the most commonly used standardized SQL language to access the database. Its small size, fast, and open source, greatly reducing the cost.

MySQL system features include :( from Baidu Encyclopedia)
1. Written in C and C ++, and uses a variety of compilers tested to ensure source code portability.
2. Support for AIX, FreeBSD, HP-UX, Linux, Mac OS, NovellNetware, OpenBSD, OS / 2 Wrap, Solaris, Windows and other operating systems.
3. It provides an API for a variety of programming languages. These programming languages including C, C ++, Python, Java , Perl, PHP, Eiffel, Ruby, .NET , and Tcl.
4. Multi-threading, make full use of CPU resources.
5. SQL query optimization algorithms, effectively speed up the search.
6. Both can be used as a separate application on the client application server network environment, it can be embedded as a library and other software.
7. Provide multi-language support, common coding such as the Chinese GB 2312, BIG5, Japanese Shift_JIS can all be used as a data table names and column names data.
8. Provide TCP / IP, ODBC and JDBC database connectivity and other ways.
9. To provide for management, inspection and management tools to optimize database operations.
10. Support for large databases. You can handle tens of millions of records have a large database.
11. Support for multiple storage engines.
12. MySQL is open source, so you do not need to pay extra fee.
13. MySQL data using standard SQL language form.
14. MySQL has very good support for PHP, PHP is more popular Web development language.
15. MySQL can be customized using the GPL agreement, you can modify the source code to develop their own system MySQL.
16. Online DDL / change function, data architecture to support dynamic application flexibility and staff development (5.6 new)
17. Copy the global transaction identifier that can support self-healing Clusters (5.6 new)
18. Copy without crashes slave, increases availability (5.6 new)
19. the copy from the multi-threaded machine, can improve the performance (new 5.6)
20.3 times faster performance (5.7 [3] new)
21. new optimizer (5.7 Add )
22. native JSON support (5.7 new)
23. a multi-source copy (new 5.7)
spatial expansion of 24. GIS (new 5.7)

MySQL architecture

MySQL logical architecture:
MySQL Logical Architecture

MySQL workflow:
 MySQL workflow

More:
MySQL logical architecture and workflow
MySQL will know that knowledge must be summarized twelve

MySQL query optimization

MySQL query process

 MySQL query process

MySQL query execution throughout the process, in general is divided into six steps:

  • The client sends a query to the MySQL server
  • First, the server checks the query cache, if the cache hit, then immediately return the results stored in the cache. Otherwise, proceed to the next stage
  • SQL server parses, pretreatment, and then the corresponding execution plan generated by the optimizer
  • MySQL to execute the query execution according to plan, calling storage engine API
  • The results are returned to the client, while caching query results

MySQL query performance optimization

MySQL query performance optimization from three aspects to consider, database table structure optimization, index optimization and query optimization.

Database table structure optimization

1, select the optimal data type

  • Choose smaller appropriate data type
  • Indexed column, as far as possible does not allow NULL
  • TimeStamp stored in preference to Datetime
  • Instead of using tinyint bit, boolean, etc.
  • blob and text usage scenarios

2, MySQL scheme trap

  • Design table can not have too many columns, the column will affect the performance of thousands of
  • Associated operational design table not too much, otherwise will be relatively slow (less than 12)

Index Tuning:

1, the index type

  • B-tree (B + tree), Hash, spatial indexing, full-text index

2, indexing strategy

  • 独立列
  • 前缀索引-如果列很长,通常可以索引开始的部分字符,这样可以有效节约索引空间,从而- 提高索引效率
  • 多列索引-适应于多个查询条件时

3、建立索引的代价

  • 增加了数据库的存储空间,
  • 在插入和修改数据时要花费较多的时间(因为索引也要随之变动)

查询优化

1、查询方式优化(切分、分解,分而治之)

2、查询器优化,尽量避免不使用索引的情况出现

  • 放弃索引的场景:!=/<>、in、is NULL等符号的出现,索引列出现运算场景
  • 用Between代替in,Distinct代替Group by,Union all 代替 Union,如果可以的话
  • Like查询时,打头字母搜索,如XXX%,这种匹配方式可使用索引

3、使用Explain查看执行计划

4、打开慢查询,查看耗时多的慢查询语句

更多:
MySQL逻辑结构及性能优化原理
MySQL查询性能优化
MySQL查询语句优化



MyISAM与InnoDB引擎

概述

MyISAM更适合读取大于写入的业务,同时不支持事务。
InnoDB支持事务,行级锁支持,外键支持,效率上比MyISAM稍慢。

详细:
MySQL存储引擎中的MyISAM和InnoDB区别详解

文件存储

MyISAM物理文件结构

.frm文件:与表相关的元数据信息都存放在frm文件,包括表结构的定义信息等(与引擎无关的文件)。
.myd文件:myisam存储引擎专用,用于存储myisam表的数据
.myi文件:myisam存储引擎专用,用于存储myisam表的索引相关信息

InnoDB的物理文件结构

.frm与表相关的元数据信息都存放在frm文件,包括表结构的定义信息等(与引擎无关的文件)。

.ibd文件和.ibdata文件:这两种文件都是存放innodb数据的文件,之所以用两种文件来存放innodb的数据,是因为innodb的数据存储方式能够通过配置来决定是使用共享表空间存放存储数据,还是用独享表空间存放存储数据。

  • 独享表空间存储方式使用.ibd文件,并且每个表一个ibd文件
  • 共享表空间存储方式使用.ibdata文件,所有表共同使用一个ibdata文件
  • 决定使用哪种方式的参数在mysql的配置文件中 innodb_file_per_table=ON/OFF

InnoDB共享表空间与独立表空间

简介

Innodb存储引擎可将所有数据存放于ibdata*的共享表空间,也可将每张表存放于独立的.ibd文件的独立表空间(部分数据)。
共享表空间以及独立表空间都是针对数据的存储方式而言的。

  • 共享表空间: 某一个数据库的所有的表数据,索引文件全部放在一个文件中,默认这个共享表空间的文件路径在data目录下。 默认的文件名为:ibdata1 初始化为10M。
    可以在配置文件my.cnf中使用参数innodb_data_file_path设置一个或者多个文件组成表空间。
    共享表空间中会包含Undo信息,在事务未提交时数据即已经写入了表空间文件,当事务rollback时Undo信息不会被删除,但是此空间会被标记,后续会以覆盖的方式被重新使用。

  • 独立表空间: 每一个表都将会生成以独立的文件方式来进行存储,每一个表都有一个.frm表描述(结构)文件,还有一个.ibd文件。 其中这个文件包括了单独一个表的数据、索引、插入缓冲的内容,其余数据仍存放在共享表空间中,默认情况下独立表空间的存储位置也是在表的位置之中。
    可以在配置文件my.cnf中通过配置参数innodb_file_per_table = ON来开启独立表空间。

共享表空间VS独立表空间
共享表空间

优点:
可以将表空间分成多个文件存放到各个磁盘上(表空间文件大小不受表大小的限制,如一个表可以分布在不同的文件上)。数据和文件放在一起方便管理。

缺点:
所有的数据和索引存放到一个文件中,虽然可以把一个大文件分成多个小文件,但是多个表及索引在表空间中混合存储,这样对于一个表做了大量删除操作后表空间中将会有大量的空隙,特别是对于统计分析,日值系统这类应用最不适合用共享表空间。

独立表空间:

优点:
1. 每个表都有自已独立的表空间。
2. 每个表的数据和索引都会存在自已的表空间中。
3. 可以实现单表在不同的数据库中移动。
4. 空间可以回收(除drop table操作处,表空不能自已回收)

  • Drop table操作自动回收表空间,如果对于统计分析或是日值表,删除大量数据后可以通过:alter table TableName engine=innodb; 回缩不用的空间。
  • 对于使innodb-plugin的Innodb使用turncate table也会使空间收缩。
  • 对于使用独立表空间的表,不管怎么删除,表空间的碎片不会太严重的影响性能,而且还有机会处理。

缺点:
单表增加过大,如超过100个G。
相比较之下,使用独占表空间的效率以及性能会更高一点。

参考:
mysql 共享表空间、独立表空间

更多:
MySQL存储引擎MyISAM与InnoDB区别总结整理
MySQL技术内幕–innodb存储引擎图解
MySQL技术内幕 InnoDB存储引擎-阅读笔记
MySQL体系结构和存储引擎概述


MySQL三大特性

  • Insert buffer - write performance guarantee
  • Two write - high reliability
  • Adaptive Hash - to enhance read and write performance

Reference:
[MySQL] three characteristics of InnoDB - insert buffer
MySQL InnoDB features: two write (Double the Write)
InnoDB Redo the Flush and dirty page refresh mechanism in-depth analysis
mysql redo logs and brush dirty
[MySQL] InnoDB characteristics of the three - two write times
[MySQL] InnoDB characteristics of the three - adaptive hash index


MySQL knowledge

MySQL FAQ

Guess you like

Origin blog.csdn.net/zangdaiyang1991/article/details/91376730