码迷,mamicode.com
首页 > 其他好文 > 详细

慢查询

时间:2015-09-12 23:29:09      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:

慢查询

慢查询

迅速的定位 速度慢 的语句

  1. 了解 mysql 数据库的一些运行状态
    show status;
    show status like ‘uptime‘;
    show status like ‘com_select‘;
    show status like ‘com_update‘;
    
    show session status like ‘com_update‘;  # 当前回话, 默认
    show global status like ‘com_update‘;   # 从 mysql 启动开始
    
    show status like ‘connections‘;
    
    show global status like ‘slow_queries‘;
    
    show variables like ‘long_query_time‘;  # 慢查询
    
    set long_query_time=1;                  # 为了方便, 将慢查询的时间修改为 1
    
  2. 构建慢查询
    create database tmp_db;
    use tmp_db;
    
    drop table if exists dept;
    create table dept (
    deptno mediumint unsigned not null default 0,
    dname varchar(20) not null default ‘‘,
    loc varchar(13) not null default ‘‘
    ) engine = myisam charset utf8;
    
    drop table if exists emp;
    create table emp(
            empno mediumint unsigned not null default 0 comment ‘编号‘,
            ename varchar(20) not null default ‘‘ comment ‘名字‘,
            job varchar(9) not null default ‘‘ comment ‘工作‘,
            mgr mediumint unsigned not null default 0 comment ‘上级编号‘,
            hiredate date not null comment ‘入职时间‘,
            sal decimal(7,2) not null comment ‘薪水‘,
            comm decimal(7,2) not null comment ‘红利‘,
            deptno mediumint unsigned not null default 0 comment ‘部门编号‘
            ) engine=myisam charset utf8;
    
    drop table salgrade;
    create table salgrade(
            grade mediumint unsigned not null default 0,
            losal decimal(7,2) not null,
            hisal decimal(7,2) not null
            ) engine=myisam charset utf8;
    
    insert into salgrade(grade, losal, hisal) values
    (1, 700, 2000),
    (2, 1201, 1400),
    (3, 1401, 2000),
    (4, 2001, 3000),
    (5, 3001, 9000);
    
    delimiter $$
    drop function if exists rand_string;
    create function rand_string(n int) returns varchar(100)
        begin
        declare chars_str varchar(100) default "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        declare return_str varchar(255) default ‘‘;
        declare i int default 0;
        while i < n do
    set return_str = concat(return_str, substring(chars_str, floor(1 + rand() * 52), 1));
    set i = i + 1;
        end while;
        return return_str;
        end
        $$
        delimiter ;
    
    drop function if exists rand_num;
    
    delimiter $$
    create function rand_num() returns int(5)
    begin
    return floor(10 + rand() * 500);
    end
    $$
    delimiter ;
    
    drop Procedure if exists insert_emp;
        delimiter $$
    Create Procedure insert_emp(max_num int(5))
        begin
        declare i int default 0;
        declare here int;
    set autocommit = 0;
    set here = (select max(empno) from emp);
        if isnull(here) then
    set here = 0;
        end if;
    
        while i < max_num do
    set i = i + 1;
    insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (i+here, rand_string(8), ‘saleman‘, 1, current_date(), 2000, 400, rand_num());
        end while;
        commit;
    set autocommit = 1;
        end
        $$
        delimiter ;
    
    call insert_emp(10000);
    call insert_emp(40000);
    
  3. 把慢查询的 sql 语句记录到日志上, 默认不记录
    vi /etc/my.cnf
    slow-query-log=1
    slow-query-log-file=slow_query.log
    # 慢查询时间, 默认为 10s
    long-query-time=2
    log-queries-not-using-indexes=0
    log_bin_trust_function_creators=1   -- 不写上这句不能创建函数
    

慢查询

标签:

原文地址:http://www.cnblogs.com/sunznx/p/4803728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!