• Mysql的进阶小记


    作者:邓聪聪

    授权账号:
    grant all on Syslog.* to loguser@'10.0.0.%' identified by '123456';

    mysql的相关添加项,增加安全性
    /etc/my.cnf #服务端的操作
    1;innodb_file_per_table #每张表的数据由单独文件保存 --show variables like '%per_table%' #查询开关状态
    2;character-set-server=utf8mb4 #设置服务实例的字符集统一 --status
    3;slow_query_log #慢查询日志开关
    4;long_query_time=3 #设置慢查询的定义时间
    5;log_queries_not_using_indexes #启用未使用索引的的慢语句记录

    /etc/my.cnf.d/mysql-clients.cnf #mysql客户端的操作
    1;default-character-set=utf8mb4 #设置字符集统一
    2;safe-updates #设置删除、更新都为安全模式,防止误操作的严重后果 --mysql -U #安全模式

    通用日志记录:--一般用于分析或者优化时去使用
    1;show variables like 'general_log'; --文件的形式存在 #查询开关 --set GLOBAL general_log=on; #设置开启
    2;show variables like 'log_output'; #默认是文件形式存在,修改存储进表 --set global log_output='table'; #设置存入mysql数据库的表中

    慢查询开关;
    1;show variables like 'slow_query_log';
    2;select @@long_query_time; #查询默认的慢查询时间,默认是10s
    3;set profiling=ON; #慢查询分析工具 --show profiles; #查编号 --show profile for query 2; #分析某个语句的慢动作原因

    二进制日志的存储
    1;show variables like 'binlog_format'; #分为3中模式,1基于语句--不推荐,2基于行记录,3有系统判定自动选择方式


    查看是否启用索引
    1;select @@log_queries_not_using_indexes;
    2;show indexes from t1; #查看表内的索引信息

    创建索引,优化语句的查询速度
    1;create index idx_age on t1(age);

    建表结构:
    1;create table t1 (id int, name varchar(10), address varchar(20));
    2;insert t1 values (3,'张三','天津');


    语句去重
    select distinct 字段 from 表;

    查询某表的重复字段

    select date,COUNT(*) as count FROM TABLE where date>='2020-11-1' and date <'2020-11-21' and port_id=104 GROUP BY date HAVING COUNT >1

    select * from core_trafficlog where port_id=183 and date >='2020/4/1' and date <'2020-4-2' and date in (select date from core_trafficlog where port_id=183 and date >='2020/4/1' and date <'2020-4-2' group by date having Count(*)>1)

    select date,count(*) as count from core_trafficlog where date >='2020/11/21' and date <'2020-11-22' and port_id=179 group by date having COUNT(date) >1

    查询语句 #DQL
    例:
    MariaDB [students]> select * from t1;
    +----+---------+--------+------+-----------+
    | id | name | gender | age | address |
    +----+---------+--------+------+-----------+
    | 3 | 张s三 | 男 | 18 | 北京 |
    | 4 | 李四 | 男 | 20 | 天津 |
    | 5 | 陆琴 | 女 | 22 | 河北 |
    | 6 | 刘六 | 男 | 21 | 黑龙江 |
    +----+---------+--------+------+-----------+
    1;select gender as '性别',avg(age) as '平均年龄' from t1 group by gender; #查询以男、女为分别的平均年龄并分组,其中as为别名
    2;select classid,gender,count(*) from t1 group by classid,gender; #查询以班级为分组的男生和女生的总数
    3;select * from t1 order by age asc/desc; #排序 --默认从小打到,desc #从大到小
    4;select classid,gender,avg(age) from t1 group by classid,gender having avg(age) > 21; #以班级分组,显示平均年龄,且平均年龄大于avg(age)的
    5;select * from t1 where age between 21 and 25; #介于两者之间
    同 select * from t1 where age >21 and age< 25; #介于两者之间
    不同 select * from t1 where age in (21,25); #只显示匹配的信息

    联表查询:
    例:
    MariaDB [students]> select * from t2;
    +----+--------+-----+---------+
    | id | name | age | address |
    +----+--------+-----+---------+
    | 1 | 手打 | 29 | 河北 |
    | 2 | 特 | 33 | 法国 |
    | 3 | 华通 | 33 | 天津 |
    | 4 | 打算 | 32 | 武汉 |
    +----+--------+-----+---------+
    一,纵向合并查询,条件是记录一样,如(字段数相同)或字段一样 union #*具有去重的效果
    1;select id,name,age,address from t1 union select * from t2;两张表纵向合并显示,字段需要对应上,否则无法合并
    二,横向合并,两张表的字段横向展开,内容对内容进行横向排列cross join,交叉连接
    1;select * from t1 cross join t2;
    挑出条件的显示
    //1;select * from t1,t2 where techerid=t2.id; #把两表的交集作为条件查询 --老式查询//
    2;select * from t1 inner join t2 on techerid=t2.id; #把两表的交集作为条件查询

     查询指定数据库的大小及数据条目

    select  table_schema as '数据库', sum(table_rows) as '记录数', sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)', sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)' from information_schema.tables where table_schema='traffic2';

     查看mysql库各表容量大小

    select table_schema as '数据库',table_name as '表名',table_rows as '记录数',truncate(data_length/1024/1024, 2) as '数据容量(MB)',truncate(index_length/1024/1024, 2) as '索引容量(MB)'from information_schema.tableswhere table_schema='traffic2'order by data_length desc, index_length desc;

    删除指定时间(字段)的数据

     delete from core_trafficlog where date < '2020-9-1';

    备份数据库中表的前100条

    mysqldump -uroot -pxxx database --where="true limit 100"> backup100.sql

    数据库备份携带条件

    mysqldump -uroot -padmin traffic core_trafficlog -w "port_id=241 and date >='2020/12/1' and date <'2020/12/2'" >test.sql

    只导出数据库表结构,不导出数据

    mysqldump  -uroot -p123456 --no-data -d traffic >test.sql

  • 相关阅读:
    无线传感网3-1.目标物的覆盖技术
    无线传感网2-传感器布局方法
    JAVA 第二周课程总结
    2019春总结作业
    第十二周作业
    第十一周作业
    第十周
    第九周作业
    第八周作业
    第七周作业
  • 原文地址:https://www.cnblogs.com/dengcongcong/p/10803809.html
Copyright © 2020-2023  润新知