SQL调优之排名优化
2016-08-30 15:28
148 查看
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';
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、优化方式(二),使用中间表,存储排序结果,每天更新一次;
(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 | <derived2> | ref | <auto_key0> | <auto_key0> | 152 | const | 10 | 100.00 | Using where | | 2 | DERIVED | <derived3> | 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、优化方式(二),使用中间表,存储排序结果,每天更新一次;
相关文章推荐
- Oracle SQL的调优及表、索引的设计和优化
- SQL优化:基本概念(索引调优、统计信息、查询调整、资源调控)
- 性能调优之SQL优化
- 数据库性能调优——sql语句优化(转载及整理)
- 数据库性能调优——sql语句优化(转载及整理)
- Oracle SQL的调优及表、索引的设计和优化
- MYSQL语句调优:SQL优化总结
- 数据库调优教程(十二) 优化sql语句
- 数据库调优教程(十二) 优化sql语句
- Greenplum优化--SQL调优篇
- 以淘宝商品搜索漫谈查询条件的排序对效率的影响(SQL查询性能优化,附调优(性能诊断)DMV)
- 图书推荐:《Oracle 高性能SQL引擎剖析:SQL 优化与调优机制详解》
- Oracle 高性能SQL引擎剖析:SQL优化与调优机制详解
- 数据库调优教程(十二) 优化sql语句
- sql优化之(DMV)& 找出性能差的语句 & sql 2005性能调优
- SQL SERVER性能调优之三(T-SQL优化)
- 图书推荐:《Oracle 高性能SQL引擎剖析:SQL 优化与调优机制详解》
- Oracle 高性能SQL引擎剖析:SQL优化与调优机制详解
- oracle笔记整理13——性能调优之SQL优化
- SQL Tuning / SQL 性能 优化 调优