mysql数据库优化与mysql在web性能优化
本文章介绍了关于mysql数据库优化与mysql在web性能优化 ,有需了解的同学可看看怎么样。
语句:
Ddl(数据定义语言) alter create drop
Dml(数据操作语言) inset update
Dtl(数据事务语言) conmmit rollback savepoint
Select
Dcl(数据控制语句) grant赋权限 revoke回收
Mysql数据库优化:
1、 数据库表 要设计合理(符合3NF,有时候也需要适当的逆范式)
2、 Sql语句的优化(索引,常用小技巧)
3、 数据库的配置
4、 适当的硬件配置和操作系统
5、 读写分离
问:什么是数据库3范式?
1NF: 就是具有原子性,不可分割(只要使用的是关系型数据库,就会自动符合)
2NF: 在满足1NF的基础上,我们考虑是否满足2NF,只要表的记录满足唯一性,也就是说,你的同一张表中不可能出现完全相同的记录,一般说我们在表中设计一个主键即可。
3NF: 在满足2NF: 的基础上,我们考虑是否满足3NF,既我们的字段信息可以通过关联的关系,派生即可(通常我们通过外键来处理)使用外键数据库的存储引擎必须是innoDB
问2:数据库参数配置
对于innodb存储引擎最重要的就是内存,所以下面的两个参数调的很大
Innodb_additional_mem_pool_size = 64M
Innodb_buffer_pool_size = 1G 缓冲池大小
对于myisam,需要调整key_buffer_size
用show status 语句可以看到当前状态,以决定调整那些参数
一、显示你使用过多少次insert , update , delete 等
Sql: show status like “Com”;
//在命令窗口中不关闭的时候查询会准确,如果关闭就会从新开始统计
Show status like “Com_update”;
//就算关闭窗口也会将全部的你执行过的次数统计出来
Show globalstatus like “Com_insert”;
Example: session
假如已经使用了6次update
1、 用session统计 会是6次
如果关闭后命令窗口后在执行Show session statuslike “Com_update”; 就为0了
2、 但是如果用Show global status like “Com_insert”;就是6次
二、显示试图连接Mysql服务器的次数
Show status like “Connections”;
数据库启动多长时间了
Show status like “uptime”;
显示慢查询多少次(默认是10秒)
Show status like “Slow_queries”;
四、如何在一个项目中,找到慢查询的select,mysql数据库支持把慢查询的语句记录到日志中,供程序员来分析
步骤:
1、 启动mysql(特殊的启动方式)
a) 在mysql的安装目录下的bin目录下启动mysqld.exe –slow-query
b) Netstat –an 查看3306端口是否启动
c) 查询慢查询的次数 show status like “Slow_queries”;
d) 设置慢查询的时间 set long_query_time=1;
:
比如说增加主键索引
Alter table user add primary key(id);
删除主键索引
Alter table user drop primary key
删除索引
Alter table user drop index 索引名
显示索引
Show index(es) from 表名
Show keys from 表名
Desc 表名
增加索引致使查询会变快好多,其原理就像一本书如果没有目录的话那么如果你想找一个知识点会很难找到,只能一点一点的翻着找,如果有目录的话会很快的定位到这个知识点在那个章节中大概什么位置这样查询起来自然就会快了啊,但是有利必有弊,索引会对查询带来好处,但是对add update delete 来说自然就很麻烦了,比如说你添加一个知识点,你不许还有在目录中添加他是属于那章那节中的那个知识点,同样在修改和删除的时候也会随之改变,来保持信息的准确性。
一个自动分析是否需要使用索引的命令:explain
Example: explain select * from emp where id = 9;
索引的分类:
主键索引(primary key)
唯一键索引(unique)
Index(普通索引)
全文索引(fulltext)
复合索引(多列和在一起)
在那些列上添加索引比较合适:
1、比较频繁的作为查询条件的字段应该加上索引
2、 唯一性比较差的字段不适合单独创建索引,及时频繁作为查询条件
3、 更新非常频繁的字段不适合创建索引
4、 不会出现在where子句中的字段不该创建索引
查询一个表中的所有索引: show indexes from table(表名)
索引的使用:
查询要使用索引最重要的条件是查询条件中需要使用索引
以下几种情况可能会使用到索引
1、 对于创建的多列索引,只要查询条件使用了最左边的列,索引一般就会被使用
2、 对于使用like的查询,查询如果是‘%aaa’不会使用到索引‘aaa%’会使用到索引
以下的表中将不使用索引
1、 如果条件中有or,即使其中有条件带索引也不会使用
2、 对于多列索引,不是使用的第一部分,则不会使用索引
3、 Like查询是以%开头
4、 如果列类型是字符串,那么一定要在条件中将数据使用引号引起来,否则不使用索引。
5、 如果mysql估计使用全表扫描要比使用索引快,则不使用索引。
查看索引的使用情况
Show status like ‘handler_read%’;
只有handler_read_key 越大越好
Handler_read_rnd_next 越小越好
型:
MyISAM 不支持事务和外键,一张表由三个文件组成,.frm .myi .myd
innoDB 支持事务和外键
对于MyISAM来说查询快,不过删除字段时空间是不会释放的,必须使用手动释放 optimize table table_name
数据库分表:
1、 水平分表
2、 垂直分表:
Stu表:
id
Name
Pass
Photo
Mark表
Id
Sid
Question
answer
垂直分表针对于关联类型的表,比如说,有一个学生的个人信息(有头像)表,一个考试信息(考试题和答案)表,这时我想查一个学生的考试分数和个人信息,那么mysql会将学生的个人信息和考试信息表关联,速度回降低很多,所以要将考试信息中的答案和题目分开在创建一个表,如果在提高还可以将头像分开为一个单独的表(如百度单独的图片服务器)
读写分离:
Web程序慢,首先的一个检测对象就是数据库,写过几年程序的都知道。现在想追踪MySQL中那些拖累服务器性能的SQL语句,怎么办?需要开启一个慢查询输出的一个机关:log_slow_queries。可以在MySQL配置文件中(my.ini/my.cnf)中设置,也可以通过MySQL客户端临时设置。第二种方法的好处是,可以不用重启MySQL服务,而使设置生效。那就来试试这个:
首先通过客户端连接到Mysql服务器,然后输入下面的语句:
SET GLOBAL log_slow_queries = ON;
SET GLOBAL long_query_time = 3;
这样MySQL就会把耗时>=3秒的SQL语句给记录下来,并输出到一个慢查询日志文件中。问题来了,这个慢查询日志文件在什么地方呢?如下,在MYSQL客户端中执行下面的语句:
show variables like 'slow_query_log_file';
就会看到MySQL慢查询日志文件位置。我的是:/usr/local/mysql/data/host-slow.log

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
