数据库索引:
建立数据库索引:
create index name_index on [test].[dbo].[Users](name);
create index name_index on [test].[dbo].[Users](name Asc);
唯一索引:
create unique index indexName on mytable(username(length))
修改表结构:
ALTER table mytable ADD UNIQUE [indexName] (username(length))
ALTER table tbl_name ADD FULLTEXT index_name(column_list) #该语句指定了索引为FULLTEXT,用于全文索引。
删除索引:
DROP INDEX [indexName] ON mytable;
显示索引信息:
SHOW INDEX FROM table_name\G;
查看正在执行的sql语句:
show processlist #进程
select * from information_schema.`PROCESSLIST` where info is not null ORDER BY time desc;
开启日志:
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';
设置关闭:
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'OFF';
SELECT * from mysql.general_log ORDER BY event_time DESC
清空表:
truncate table mysql.general_log;
#添加慢查询日志
log_output=file
slow_query_log=on
slow_query_log_file = tmp/mysql-slow.log
log_queries_not_using_indexes=on
long_query_time =5
sql语句执行时间:
show variables; #profiling是否on 如果off,执行set profiling=1;
show profiles -- 就可以查询到sql语句的执行时间
查看慢查询是否开启:
show variables like '%quer%';
关注参数:
1: show_query_log 慢查询日志是否开启,默认为off
开启慢查询日志 set global slow_query_log='ON'
在MySQL中有一个变量专门记录当前慢查询语句的个数
show global status like '%slow%';
慢查询是否开启以及所规定时间:
show variables like 'slow_query_log';
show variableslike 'long_query_time';
set global slow_query_log='ON';
将慢查询追踪的时间设置为1s:
set global long_query_time = 1;
慢查询日志保存位置:
set global slow_query_log_file='/var/lib/mysql/test_1116.log';
慢查询数据:
SELECT * FROM `slow_log` where start_time > '2019/05/19 00:00:00';
正在查询的时间倒序:
select * from information_schema.processlist where info is not null order by time desc
慢查询几个重要指标:
start_time 开始时间,要通过这个参数,配合系统出问题的时间,定位哪些查询是罪魁祸首
query_time 查询时间
rows_sent 和 rows_examined发送的结果数以及查询扫过的行数,这两个值特别重要,特别是 rows_examined。基本上就能告诉我们,哪个查询是需要注意的“大”查询。