zoukankan      html  css  js  c++  java
  • SQL语句技巧_索引的优化_慢查询日志开启_root密码的破解

    1.正则表达式的使用 regexp
    例:
    select name,email from t where email regexp '@163[.,]com$'
    使用like方式查询
    selct name,email from t where email like '%@163.com' or email like '%@163,com'

    2.巧用rand()提取随机行
    select * from t3 order by rand() limit 3;

    3.利用group by 的with rollup
    使用group by 的with rollup子句可以检索出更多的分组聚合信息
    注:with rollup 不可以和 order by 同时使用
    select cname,pname,count(pname) from demo group by cname,pname with rollup;

    4.用bit group functions 做统计
    在使用group by 语句时可以同时使用bit_and,bit_or 函数来完成统计工作.这两个函数的作用主要是做数值之间的逻辑运算.
    select id,bit_or(kind) from order_rab group by id
    对order_rab表中的id分组时对kind做位与和或计算
    select id,bit_and(kind) from order_rab group by id

    -------------------------------------------------------
    优化SQL语句的一般步骤

    1.通过show status命令了解各种sql的执行频率
    show [session|global] status;
    其中: session(默认)表示当前连接
    global 表示自数据库启动至今
    show status;
    show global status;
    show status like 'Com_%';
    show global status like 'Com_%';

    参数说明:
    com_select 执行select操作的次数,一次查询只累计加1
    com_update 执行update操作的次数
    com_insert 执行insert操作的次数,对批量插入只算一次
    com_delete 执行delete操作的次数

    针对Innodb存储引擎的
    innodb_row_read 执行select操作的次数
    innodb_row_updated 执行update操作的次数
    innodb_row_inserted 执行insert操作的次数
    innodb_row_deleted 执行delete操作的次数

    其它:
    connections 连接mysql的数量
    uptime 服务器已经工作的秒数
    slow_queries 慢查询的次数
    mysql>show variables like '%long%'; 看到long_query_time的值就是默认慢查询记录的时间(秒)

    2.定位执行效率较低的SQL语句 explain 或 desc
    explain select * from table; 或
    desc select * from table;
    字段说明:
    select_type:SIMPLE表示select类型,
    type:range 表示表的连接类型,性能有好到差
    system(表仅一行)
    const()只一行匹配)
    eq_ref 对于前面的每一行使用主键和唯一
    ...

    3.使用like的查询,后面如果是常量并且只有%号不在第一个字符,索引才可能会被使用
    explain select * from table where name like '%3'; 索引不会被使用
    explain select * from table where name like '3%'; 使用

    4.or 的前台条件字段都要加索引字段才会生效,否则整体不生效

    5.查看索引使用情况

    show status like 'Handler_read%';
    Handler_read_key 这个值代表了一个行被索引值读的次数,
    Handler_read_rnd_next 值高,则意叶着查询运行的低效.并且应该建立索引补救


    6.慢查询日志
    log_slow_queries=slow.log # 慢查询日志的路径
    long_query_time=5 # 记录的时间
    重启mysql服务

    7.socket问题
    有时候登陆mysql提示不能用socket登陆,此时可以换成tcp方式去登陆,但是必须要在php去用之前把这事情解决好
    mysql -uroot -proot --protocol tcp -hlocalhost


    8.root密码丢失破解
    1.停止服务 pkill mysqld
    #跳过授权表mysql.user和mysql.db这些表
    2.mysqld_safe --skip-grant-tables --user=mysql &

    3.mysql -uroot 回车

    4.update user set password=password('root') where user='root' and host='localhost';
    5.pkill mysqld
    6.重启mysql服务
    mysqld_safe --user=mysql &

  • 相关阅读:
    线性代数12.图和网络
    【转载】STM32之中断与事件---中断与事件的区别
    头文件重复包含(转)
    C语言位操作
    NOP使用注意事项
    头文件intrins.h的用法
    RAM、SRAM、SDRAM、ROM、EPROM、EEPROM、Flash存储器概念
    const在C语言中的用法
    volatile的作用
    absacc.h keil软件里怎么找不到 ,如何找?
  • 原文地址:https://www.cnblogs.com/ahwu/p/3669068.html
Copyright © 2011-2022 走看看