mysql depended_query 优化案例一则

月度利息统计sql优化

原因:写的sql语句复杂,理解起来有难度,另一方面,查询性能比较低

原来的语句如下:

SELECT

tp.year,

tp.month,

tp.bid_id,

b.`title`,

DATE(b.`time`) `time`,

tp.receive_date,

u.`name`,

u.`reality_name`,

b.`amount`,

b.`apr`,

b.`period_unit`,

b.`period`,

tp.receive_interest,

tp.bid_invest_count,

IFNULL((SELECT SUM(s.`amount`) FROM t_invests s WHERE s.`bid_id`=tp.bid_id AND s.user_id IN (SELECT r.user_id FROM `t_user_repair` r)), 0)  AS bid_amount1,

IFNULL((SELECT SUM(s.`receive_corpus`) FROM t_bill_invests s WHERE s.`bid_id`=tp.bid_id AND DATE(IFNULL(s.`real_receive_time`, s.`receive_time`))=tp.`receive_date` AND s.user_id IN (SELECT r.user_id FROM `t_user_repair` r)), 0) AS bid_corpus1,

IFNULL((SELECT SUM(s.`receive_interest`) FROM t_bill_invests s WHERE s.`bid_id`=tp.bid_id AND DATE(IFNULL(s.`real_receive_time`, s.`receive_time`))=tp.`receive_date` AND s.user_id IN (SELECT r.user_id FROM `t_user_repair` r)), 0) AS bid_interest1,

IFNULL((SELECT COUNT(1) FROM t_bill_invests s WHERE s.`bid_id`=tp.bid_id AND DATE(IFNULL(s.`real_receive_time`, s.`receive_time`))=tp.`receive_date` AND s.user_id IN (SELECT r.user_id FROM `t_user_repair` r)), 0) AS bid_invest_count1, 

IFNULL((SELECT SUM(s.`receive_corpus`) FROM t_bill_invests s WHERE s.`bid_id`=tp.bid_id AND DATE(IFNULL(s.`real_receive_time`, s.`receive_time`))=tp.`receive_date` AND s.user_id NOT IN (SELECT r.user_id FROM `t_user_repair` r)), 0) AS bid_corpus2

FROM

(

SELECT

t.`bid_id`,

DATE(IFNULL(t.`real_receive_time`, t.`receive_time`)) AS `receive_date`,

YEAR(IFNULL(t.`real_receive_time`, t.`receive_time`)) AS `year`,

MONTH(IFNULL(t.`real_receive_time`, t.`receive_time`)) AS `month`,

 

IFNULL(SUM(t.`receive_interest`),0) receive_interest,

COUNT(1) AS bid_invest_count

FROM t_bill_invests t

WHERE 1=1 AND DATE(IFNULL(t.`real_receive_time`, t.`receive_time`)) >= ‘2015-09-01‘ AND DATE(IFNULL(t.`real_receive_time`, t.`receive_time`)) <= ‘2015-10-31

GROUP BY `year`, `month`, t.`bid_id`, DATE(IFNULL(t.`real_receive_time`, t.`receive_time`))

)tp

LEFT JOIN t_bids b ON tp.bid_id=b.`id`

LEFT JOIN t_users u ON b.`user_id`=u.`id`;

执行结果所需时间信息如下:

/* Affected rows: 0  已找到记录: 129  警告: 0  持续时间 1 query: 28.704 sec. (+ 10.031 sec. network) */

最终优化语句:

SELECT

YEAR(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) AS `year`,

MONTH(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) AS `month`,

tp.bid_id,

b.`title`,

DATE(b.`time`) `time`,

DATE(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) AS `receive_date`,

u.`name`, 

u.`reality_name`, 

b.`amount`,

b.`apr`, 

b.`period_unit`, 

b.`period`, 

sum(if(tp.real_receive_time,tp.`receive_interest`,0)) receive_interest, 

COUNT(1) AS bid_invest_count , 

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,ti.amount,0 )) bid_amount1,

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,tp.receive_corpus,0 )) bid_corpus1,

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,tp.receive_interest,0 )) bid_interest1, 

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r),1,0 )) bid_invest_count1, 

sum(if(tp.user_id not in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,tp.receive_corpus,0 )) bid_corpus2

 

FROM t_bill_invests tp

LEFT JOIN t_bids b ON tp.bid_id=b.`id`

LEFT JOIN t_users u ON b.`user_id`=u.`id`

left join (select id,amount from t_invests) ti on ti.id=tp.invest_id

 

WHERE 1=1 AND DATE(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) >= ‘2015-09-01‘  AND DATE(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) <= ‘2015-10-31

GROUP BY tp.`bid_id`, DATE(tp.`receive_time`);

 

执行时间:

/* Affected rows: 0  已找到记录: 129  警告: 0  持续时间 1 query: 0.671 sec. */

结果一样,但最终结果只需0.67s

优化思路,原来语句explain中:

在select_type中有大量的dependent_subquery,此种类型的查询极耗性能,在sql的编写中应该极力避免。

  1. 结合业务需求,查看group by写法,发现没有必要写那么多的group by,只需要抓住核心的tp.`bid_id`, DATE(tp.`receive_time`)便可确定一组帐单信息。

2.  尽量改写查询列,让其为subquery,如下:

SELECT

YEAR(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) AS `year`,

MONTH(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) AS `month`,

tp.bid_id,

b.`title`,

DATE(b.`time`) `time`, 

DATE(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) AS `receive_date`, 

u.`name`,

u.`reality_name`, 

b.`amount`,

b.`apr`, 

b.`period_unit`, 

b.`period`, 

sum(if(tp.real_receive_time,tp.`receive_interest`,0)) receive_interest, 

COUNT(1) AS bid_invest_count ,

IFNULL((SELECT SUM(s.`amount`) FROM t_invests s    WHERE s.`bid_id`=tp.bid_id and s.user_id IN (SELECT r.user_id FROM `t_user_repair` r)), 0) as bid_amount1, 

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,tp.receive_corpus,0 )) bid_corpus1, 

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,tp.receive_interest,0 )) bid_interest1,

sum(if(tp.user_id in (SELECT r.user_id FROM `t_user_repair` r),1,0 )) bid_invest_count1, 

sum(if(tp.user_id not in (SELECT r.user_id FROM `t_user_repair` r) and tp.real_receive_time is not null,tp.receive_corpus,0 )) bid_corpus2 

FROM t_bill_invests tp

LEFT JOIN t_bids b ON tp.bid_id=b.`id`

LEFT JOIN t_users u ON b.`user_id`=u.`id`

 

WHERE 1=1 AND DATE(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) >= ‘2015-04-01‘  AND DATE(IFNULL(tp.`real_receive_time`, tp.`receive_time`)) <= ‘2015-10-31‘

GROUP BY tp.`bid_id`, DATE(tp.`receive_time`);

在其explain中还有残留的依赖外查询:

主要是由框中的语句引起,于是再改写,让其改成临时表连接,就是最后的sql语句:

只剩临时表与了查询,性能有了大幅度的提升,优化结束。

总结:统计类查询经常不小心就会写成depended_subquery的形式,可以结合sum count case if条件判断来改写,让其可以达到subquery的形式,另一方面也可以改成临时表的方式并通过连接来提高性能。

时间: 2024-10-25 21:44:57

mysql depended_query 优化案例一则的相关文章

mysql参数优化案例

Mysql SQL 优化案例1

今天公司同事反馈一个SQL语句删除数据删除了一个小时,还没有删除完,强制中断.表中只有几百条数据. sql 反馈如下: DELETE t FROM o.`AI_AD_U_L` t WHERE EXISTS (SELECT 1 FROM o.`AI_AD_U_L_TEMP` AS a WHERE a.`ca_id`=t.`ca_id`); 第一步: 查看表上的索引: show index from  AI_AD_U_L; show index from I_AD_U_L_TEMP 发现关联字段上a

mysql优化案例

MySQL优化案例 Mysql5.1大表分区效率测试 Mysql5.1大表分区效率测试MySQL | add at 2009-03-27 12:29:31 by PConline | view:60, comment:0 mysql5.1开始支持数据表分区了,原来的分表可以不用了,分表的不足在于多表查询不方便.呵呵,下面来简单测试下表分区的查询效率. 1.用来测试的数据为discuz论坛的数据库,表为cdb_posts表,数据量为1500多万条mysql> select count(*) fro

记一次mysql多表查询(left jion)优化案例

一次mysql多表查询(left jion)优化案例 在新上线的供需模块中,发现某一个查询按钮点击后,出不来结果,找到该按钮对应sql手动执行,发现需要20-30秒才能出结果,所以服务端程序判断超时,故先不显示结果 以下是对这条查询的优化记录 1 数据库配置 数据库配置:4C8G 主表数据:3W+ 2 sql语句 提取sql语句,简化如下 SELECT taba.id, taba.title, taba.type, taba.end_time, tabb.username, tabc.orgna

MySQL分页优化中的“INNER JOIN方式优化分页算法”到底在什么情况下会生效?

本文出处:http://www.cnblogs.com/wy123/p/7003157.html 最近无意间看到一个MySQL分页优化的测试案例,并没有非常具体地说明测试场景的情况下,给出了一种经典的方案,因为现实中很多情况都不是固定不变的,能总结出来通用性的做法或者说是规律,是要考虑非常多的场景的,同时,面对能够达到优化的方式要追究其原因,同样的做法,换了个场景,达不到优化效果的,还要追究其原因.个人对此场景在不用情况表示怀疑,然后自己测试了一把,果然发现一些问题,同时也证实了一些预期的想法.

大型网站MySQL深度优化揭秘 2

学无止境,老男孩教育成就你人生的起点! 相信自己相信老男孩!!! 老男孩-51cto-公开课-大型网站MySQL深度优化揭秘 部分整理有没跟上的抱歉 ? 目 录 大型网站MySQL深度优化揭秘????2 第1章 优化的思路和线路????2 1.1 网站优化的思路????2 1.2 MySQL优化,nginx这样的东西怎么优化?????2 第2章 硬件层面优化????3 2.1 数据库物理机????3 2.1.1 CPU????3 2.1.2 Memory????3 2.1.3 disk(磁盘IO

MySQL通用优化技巧 | Ucloud运维在线微信群分享

MySQL通用优化技巧 | Ucloud运维在线微信群分享 2015-09-17 MySQL中文网 本文根据DevOps华南运维圈@UCloud微信群「大话运维」的嘉宾分享整理而成.「大话运维」将邀请业界运维前线技术专家作为分享嘉宾,分享技术趋势和技术实战,为运维朋友提供各种踩坑.躲坑.绕坑新技能. 嘉宾介绍 叶金荣Oracle MySQL ACE,国内最早的MySQL推广者.2006年创办国内首个MySQL专业技术网站 MySQL 中文网.资深MySQL专家,10余年MySQL经验,擅长Mys

中国移动MySQL数据库优化最佳实践

原创 2016-08-12 章颖 DBAplus社群 本文根据DBAplus社群第69期线上分享整理而成,文末还有书送哦~ 讲师介绍章颖 数据研发工程师 现任中国移动杭州研发中心数据研发工程师,擅长MySQL故障诊断,性能调优,MySQL高可用技术,曾任中国电信综合平台开发运营中心DBA 开源数据库MySQL比较容易碰到性能瓶颈,为此经常需要对MySQL数据库进行优化,而MySQL数据库优化需要运维DBA与相关开发共同参与,其中MySQL参数及服务器配置优化主要由运维DBA完成,开发则需要从数据

MySQL架构优化实战系列1:数据类型与索引调优全解析

一.数据类型优化 数据类型 整数   数字类型:整数和实数 tinyint(8).smallint(16).mediuint(24).int(32).bigint(64) 数字表示对应最大存储位数,如 tinyint (-127 --- 128),tinyint unsigned 表示不允许负数,则范围为 (0 -- 255). 常规数据库中 int(11) 只是表示控制显示字符的个数是11个,int(1) 和 int(20) 存储和计算是一样的,即 int(1) 照样可以存储1111(4位数)