# 老版本MySQL mysql> update user set password=password("你的新密码") where user="root"; # 新版本MySQL eg:mysql5.7.21 mysql> update user set authentication_string = password("123456") where user='root' and Host = 'localhost';
SELECT DATE_FORMAT(add_date , '%Y-%m') AStime , avg( timestampdiff(DAY , add_date , real_end_date) ) AS avg_finish_time FROM iwork_issue WHERE add_date BETWEEN'2017-01-01 00:00:00' AND'2019-01-01 00:00:00' AND real_end_date ISNOTNULL AND state =3 AND timestampdiff(DAY , add_date , real_end_date) <=90 GROUPBY time
SELECT floor( timestampdiff(DAY , start_time , online_time) /7+1 ) *7AS t , count(*) FROM iwork_project b WHERE start_time BETWEEN'2018-01-01 00:00:00' AND'2019-01-01 00:00:00' AND online_time ISNOTNULL GROUPBY t HAVING t >0
按天维度统计
eg: bug解决周期分布
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT timestampdiff(DAY , add_date , solve_date) AS t , count(*) FROM iwork_bug WHERE add_date BETWEEN'2018-01-01 00:00:00' AND'2019-01-01 00:00:00' AND state IN(2 , 4) GROUPBY t HAVING t >0
按小时维度统计
eg: 需求录入时间分布图(0-24h)
1 2 3 4 5 6 7 8 9 10
SELECT DATE_FORMAT(add_date , '%H') hours , COUNT(add_date) FROM iwork_issue b WHERE add_date BETWEEN'2018-01-01' AND'2019-01-01' GROUPBY hours;
按分钟维度统计
eg:3天内bugfix最快(Top1-3)的业务线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
SELECT avg(timestampdiff(MINUTE , add_date , solve_date)) AS t , bg.id , bg.bg_name FROM iwork_bug b LEFTJOIN iwork_product p ON(p.id = b.org_id) LEFTJOIN iwork_business_group bg ON(bg.id = p.bg_id) WHERE add_date BETWEEN'2018-01-01 00:00:00' AND'2019-01-01 00:00:00' AND b.state IN(2 , 4) AND timestampdiff(DAY , add_date , solve_date) <3 GROUPBY p.bg_id ORDERBY t ASC LIMIT 0 , 3
索引调优
查询是在单个表上。
GROUP BY仅命名构成索引最左前缀的列,而没有命名其他列。(如果查询具有DISTINCT子句,而不是GROUP BY,则所有不同的属性都引用构成索引最左前缀的列。) eg:
如果表t1在(c1,c2,c3)上具有索引,如果查询具有GROUP BY c1,c2,则松散索引扫描适用。
如果查询具有GROUP BY c2,c3(列不是最左边的前缀)或GROUP BY c1,c2,c4(c4不在索引中),则不适用。