码迷,mamicode.com
首页 > 数据库 > 详细

统计推荐人总数、下单金额的sql优化

时间:2015-12-20 13:03:40      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

表结构:

 
CREATE TABLE `p_account_log` (
  `log_id` mediumint(8unsigned NOT NULL AUTO_INCREMENT,
  `uid` mediumint(8unsigned NOT NULL COMMENT 号‘,
  `use_money` decimal(10,2NOT NULL DEFAULT ‘0.00‘ COMMENT 金‘,
  `change_time` int(10unsigned NOT NULL COMMENT 间‘,
  `change_desc` varchar(255NOT NULL DEFAULT ‘‘ COMMENT 注‘,
  `change_type` tinyint(3unsigned NOT NULL COMMENT 型0,1费, 2员 3励 99他‘,
  PRIMARY KEY (`log_id`),
  KEY `user_id` (`uid`)
ENGINE=InnoDB AUTO_INCREMENT=82416 DEFAULT CHARSET=utf8 COMMENT=表‘;
CREATE TABLE `p_order` (
  `order_id` int(10unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(11DEFAULT ‘0‘ COMMENT 号‘,
  `username` varchar(30DEFAULT ‘‘ COMMENT 名‘,
  `courier_id` int(11DEFAULT ‘0‘ COMMENT 递ID‘,
  `courier_name` varchar(180DEFAULT ‘‘ COMMENT 称‘,
  `sum_money` decimal(10,2DEFAULT ‘0.00‘ COMMENT 额‘,
  `order_status` tinyint(1DEFAULT ‘0‘ COMMENT 态‘,
  `pay_status` tinyint(1DEFAULT ‘0‘ COMMENT 态‘,
  `add_time` int(10DEFAULT ‘0‘ COMMENT 间‘,
  `pay_time` int(10DEFAULT ‘0‘ COMMENT 间‘,
  `sender_name` varchar(30DEFAULT ‘‘ COMMENT 名‘,
  `sender_province` varchar(30DEFAULT ‘‘ COMMENT 份‘,
  `sender_city` varchar(30DEFAULT NULL COMMENT 市‘,
  `sender_district` varchar(30DEFAULT ‘‘ COMMENT 区‘,
  `sender_address` varchar(180DEFAULT ‘‘ COMMENT 址‘,
  `sender_zip` varchar(10DEFAULT ‘‘ COMMENT 码‘,
  `sender_mobile` varchar(20DEFAULT ‘‘ COMMENT 机‘,
  `sender_telphone` varchar(20DEFAULT ‘‘ COMMENT 机‘,
  PRIMARY KEY (`order_id`)
ENGINE=InnoDB AUTO_INCREMENT=63284 DEFAULT CHARSET=utf8 COMMENT=表‘;
CREATE TABLE `p_user` (
  `uid` int(10unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(30DEFAULT ‘‘ COMMENT 名‘,
  `password` varchar(32DEFAULT ‘‘ COMMENT 码‘,
  `mobile` varchar(20DEFAULT ‘‘ COMMENT 号‘,
  `qq` varchar(20DEFAULT ‘‘ COMMENT ‘QQ‘,
  `email` varchar(160DEFAULT ‘‘ COMMENT 件‘,
  `amount` decimal(10,2DEFAULT ‘0.00‘ COMMENT 额‘,
  `agent_id` int(11DEFAULT ‘0‘ COMMENT 级ID‘,
  `start_time` int(10DEFAULT ‘0‘ COMMENT 期‘,
  `wenti` tinyint(2DEFAULT ‘0‘ COMMENT 题‘,
  `daan` varchar(100DEFAULT ‘‘ COMMENT 案‘,
  `referrer_uid` int(11DEFAULT ‘0‘ COMMENT 人‘,
  `status` tinyint(1DEFAULT ‘1‘ COMMENT ‘0用 1用‘,
  `add_time` int(10DEFAULT ‘0‘ COMMENT 期‘,
  PRIMARY KEY (`uid`)
ENGINE=InnoDB AUTO_INCREMENT=4502 DEFAULT CHARSET=utf8 COMMENT=表‘;


数据量不是很大,会员4500,82000,订单63000
结果:统计推荐人推荐会员总数、提成金额、推荐会员下单总金额。这里会员总数和提成金额是根据会员等级再变动,所以无法用固定比例。
第一次写的sql语句如下:

 
SELECT t2.referrer_uid,t1.username,t2.count,
(SELECT sum(use_moneyfrom p_account_log where change_type=and p_account_log.uid=t1.uidas use_money,
(SELECT sum(sum_moneyfrom p_order where uid in (select uid from p_user where referrer_uid=t1.uid)) as sum_money
from p_user t1 
INNER JOIN (select referrer_uid,count(*as count from p_user where referrer_uid>GROUP BY referrer_uidt2
on t1.uid=t2.referrer_uid
ORDER BY use_money desc
 

执行时间9.6S,毫无疑问那个双层子查询拖慢了速度,改变一下:

 
SELECT t2.referrer_uid,t1.username,t2.count,
(SELECT sum(use_moneyfrom p_account_log where change_type=and p_account_log.uid=t1.uidas use_money,
SUM(t3.sum_moneyas sum_money
from p_user t1 
INNER JOIN (select referrer_uid,count(*as count from p_user where referrer_uid>GROUP BY referrer_uidt2
on t1.uid=t2.referrer_uid
INNER JOIN 
(SELECT m1.uid,m2.referrer_uid,m2.username,sum(sum_moneyas sum_money from p_order m1
INNER JOIN p_user m2 on m1.uid=m2.uid and m2.referrer_uid>0
GROUP BY m1.uid,m2.referrer_uidt3 
on t3.referrer_uid=t2.referrer_uid
group by t2.referrer_uid
ORDER BY use_money desc
 

执行时间0.2S,啊哦,本来还想着把提成金额也分解出来,看情况不用了。
结论:子查询尽量别滥用!

统计推荐人总数、下单金额的sql优化

标签:

原文地址:http://www.cnblogs.com/homexigua/p/5060638.html

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