排名 优化 mysql_SQL调优之排名优化

mysql> explain extended select t.rowno from

(SELECT @rowno:=@rowno+1 as rowno,ur.customer_id as userId from t_hss_user_info_rank ur,

(select @rowno:=0) rno order by ur.give_pers_count DESC,

ur.last_give_time DESC

) t where t.userid= '1000010000000010';

+----+-------------+------------+--------+---------------+-----------------+---------+-------+--------+----------+----------------+

| id | select_type | table    | type   | possible_keys | key       | key_len | ref   | rows   | filtered | Extra |

+----+-------------+------------+--------+---------------+-----------------+---------+-------+--------+----------+----------------+

| 1 | PRIMARY |    | ref    |   | | 152   | const   | 10   | 100.00 | Using where |

| 2 | DERIVED |    | system   | NULL       | NULL     | NULL   | NULL   | 1    | 100.00 | NULL |

| 2 | DERIVED |   ur      | index   | NULL      | give_pers_count | 11    | NULL   | 311847  | 100.00 | Using index |

| 3 | DERIVED |   NULL    | NULL   | NULL        | NULL       | NULL   | NULL   | NULL   | NULL | No tables used |

+----+-------------+------------+--------+---------------+-----------------+---------+-------+--------+----------+----------------+

4 rows in set, 1 warning (0.00 sec)

mysql> show create table t_hss_user_info_rank\G

*************************** 1. row ***************************

Table: t_hss_user_info_rank

Create Table: CREATE TABLE `t_hss_user_info_rank` (

`customer_id` varchar(50) NOT NULL COMMENT '会员Id',

`user_name` varchar(50) DEFAULT NULL COMMENT '会员名称',

`phone_no` varchar(11) DEFAULT NULL,

`give_count` int(10) DEFAULT NULL COMMENT '赠送份额',

`give_pers_count` int(10) DEFAULT NULL COMMENT '赠送人数',

`last_give_time` datetime DEFAULT NULL COMMENT '最后赠送时间',

`create_time` datetime DEFAULT NULL COMMENT '创建时间',

`modify_time` datetime DEFAULT NULL COMMENT '修改时间',

`scene_no` int(10) DEFAULT NULL COMMENT '赠送份额',

PRIMARY KEY (`customer_id`),

KEY `index_user_rank_customer_id` (`customer_id`,`give_count`,`last_give_time`),

KEY `give_pers_count` (`give_pers_count`,`last_give_time`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 row in set (0.00 sec)

mysql> select t.rowno from (SELECT @rowno:=@rowno+1 as rowno,ur.customer_id as userId from t_hss_user_info_rank ur,(select @rowno:=0) rno order by ur.give_pers_count DESC, ur.last_give_time DESC ) t where t.userid= '1000010000000010';

+-------+

| rowno |

+-------+

| 139 |

+-------+

1 row in set (2.42 sec)

1、优化方式(一),利用整形检索快的原理,将customer_id在临时表里转化为整形;

select t.rowno from (SELECT @rowno:=@rowno+1 as rowno,cast(ur.customer_id as unsigned) as userId from t_hss_user_info_rank ur,(select @rowno:=0) rno order by ur.give_pers_count asc, ur.last_give_time asc ) t where t.userid= '1000010000000010';

+-------+

| rowno |

+-------+

| 139 |

+-------+

1 row in set (0.71 sec)

这表现出一个问题,表结构设计不合理,能用unsigned int表示,为什么用varchar?

2、优化方式(二),使用中间表,存储排序结果,每天更新一次;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值