[MySQL 5.6优化] --order by limit x,x 优化

  • 简介:
    order by limit x ,x 在实际工作中有各种业务需求要有order by的排序,有时候处理不好则会造成系统宕机!
  • 原理:
    a.通过索引来获取排序

b.通过内部算法获取排序:

  • 案例

具体SQL:

SELECT c.order_price orderPrice,c.preferential_amount preferentialAmount,c.order_sumprice orderSumprice,cast(c.mode as SIGNED) rechargeType,cast(c.pay_type as SIGNED) payType,cast(c.type as SIGNED) appType,c.order_sn orderSn,c.create_time payTime,u.nickname nickName,u.headimgurl headImg,u.real_name memberName,cast(c.pay_status as SIGNED) payStatusFROM t_order cLEFT JOIN t_user u ON c.user_id= u.idWHERE c.token= '1392044'and c.pay_status in (1, 3)and c.refund_status= 0and c.store_id= 36574order by c.create_time desclimit 0,15

表结构:

CREATE TABLE `t_order ` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`order_sn` varchar(30) DEFAULT NULL COMMENT ',`preferential_amount` decimal(10,2) DEFAULT '0.00' COMMENT,`order_sumprice` decimal(10,2) DEFAULT '0.00' COMMENT ,`mode` tinyint(3) unsigned DEFAULT '1' COMMENT '',`pay_type` tinyint(1) DEFAULT '1' COMMENT '',`type` tinyint(4) DEFAULT '1' COMMENT '',`create_time` int(10) unsigned DEFAULT '0' COMMENT '',PRIMARY KEY (`id`),UNIQUE KEY `order_sn` (`order_sn`),KEY `IDX_CR_MO_TO` (`create_time`,`token`,`user_id`),KEY `idx_store_token_createtime` (`store_id`,`token`,`create_time`) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=53925518 DEFAULT CHARSET=utf8CREATE TABLE `t_user ` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`nickname` varchar(20) DEFAULT NULL COMMENT '',`headimgurl` varchar(255) DEFAULT NULL,`real_name` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `openid` (`openid`),KEY `IDX_NICKNAME` (`nickname')
) ENGINE=InnoDB AUTO_INCREMENT=13974852 DEFAULT CHARSET=utf8

1、SQL优化器默认选择索引执行计划为:

 *************************** 1. row ***************************id: 1select_type: SIMPLEtable: ctype: ref
possible_keys: idx_tscc,IDX_CR_MO_TOkey: idx_tscpkey_len: 68ref: const,constrows: 26980Extra: Using index condition; Using where; Using filesort
*************************** 2. row ***************************id: 1select_type: SIMPLEtable: utype: eq_ref
possible_keys: PRIMARYkey: PRIMARYkey_len: 4ref: youdian_life_sewsq.c.user_idrows: 1Extra: Using where共返回 2 行记录,花费 5 ms.

执行时间:共返回 15 行记录,花费 128 ms.

2、当使用IDX_CR_MO_TO (create_time,token,user_id)索引时,避免Using filesortl临时表,减少rows
执行计划为:

*************************** 1. row ***************************id: 1select_type: SIMPLEtable: ctype: index
possible_keys: key: IDX_CR_MO_TOkey_len: 73ref: rows: 15Extra: Using where
*************************** 2. row ***************************id: 1select_type: SIMPLEtable: utype: eq_ref
possible_keys: PRIMARYkey: PRIMARYkey_len: 4ref: youdian_life_sewsq.c.user_idrows: 1Extra: Using where

执行时间:共返回 15 行记录,花费 234 ms

3、当使用limit 100时强制索引效果:

mysql>explain SELECT c.order_price orderPrice,c.preferential_amount preferentialAmount,c.order_sumprice orderSumprice,cast(c.mode as SIGNED) rechargeType,cast(c.pay_type as SIGNED) payType,cast(c.type as SIGNED) appType,c.order_sn orderSn,c.create_time payTime,u.nickname nickName,u.headimgurl headImg,u.real_name memberName,cast(c.pay_status as SIGNED) payStatusFROM tp_order c force index(IDX_CR_MO_TO)LEFT JOIN tp_user u ON c.user_id= u.idWHERE c.token= '1392044'and c.pay_status in (1, 3)and c.refund_status= 0and c.store_id= 36574order by c.create_time desclimit 100\G
*************************** 1. row ***************************id: 1select_type: SIMPLEtable: ctype: index
possible_keys: key: IDX_CR_MO_TOkey_len: 73ref: rows: 100Extra: Using where
*************************** 2. row ***************************id: 1select_type: SIMPLEtable: utype: eq_ref
possible_keys: PRIMARYkey: PRIMARYkey_len: 4ref: youdian_life_sewsq.c.user_idrows: 1Extra: Using where

3、当limit 为1000,10时候的效果:

强制索引:
mysql>explain SELECT c.order_price orderPrice,c.preferential_amount preferentialAmount,c.order_sumprice orderSumprice,cast(c.mode as SIGNED) rechargeType,cast(c.pay_type as SIGNED) payType,cast(c.type as SIGNED) appType,c.order_sn orderSn,c.create_time payTime,u.nickname nickName,u.headimgurl headImg,u.real_name memberName,cast(c.pay_status as SIGNED) payStatusFROM tp_order c force index(IDX_CR_MO_TO)LEFT JOIN tp_user u ON c.user_id= u.idWHERE c.token= '1392044'and c.pay_status in (1, 3)and c.refund_status= 0and c.store_id= 36574order by c.create_time desclimit 1000,10\G
*************************** 1. row ***************************id: 1select_type: SIMPLEtable: ctype: index
possible_keys: key: IDX_CR_MO_TOkey_len: 73ref: rows: 1010Extra: Using where
*************************** 2. row ***************************id: 1select_type: SIMPLEtable: utype: eq_ref
possible_keys: PRIMARYkey: PRIMARYkey_len: 4ref: youdian_life_sewsq.c.user_idrows: 1Extra: Using where
默认执行计划:
************************** 1. row ***************************id: 1select_type: SIMPLEtable: ctype: ref
possible_keys:  idx_tscc,IDX_CR_MO_TOkey: idx_tscpkey_len: 68ref: const,constrows: 27002Extra: Using index condition; Using where; Using filesort
*************************** 2. row ***************************id: 1select_type: SIMPLEtable: utype: eq_ref
possible_keys: PRIMARYkey: PRIMARYkey_len: 4ref: youdian_life_sewsq.c.user_idrows: 1Extra: Using where

4、limit 1000,10执行时间对比

使用idx_tscc索引执行时间:
mysql>SELECT c.order_price orderPrice,c.preferential_amount preferentialAmount,c.order_sumprice orderSumprice,cast(c.mode as SIGNED) rechargeType,cast(c.pay_type as SIGNED) payType,cast(c.type as SIGNED) appType,c.order_sn orderSn,c.create_time payTime,u.nickname nickName,u.headimgurl headImg,u.real_name memberName,cast(c.pay_status as SIGNED) payStatusFROM tp_order c LEFT JOIN tp_user u ON c.user_id= u.idWHERE c.token= '1392044'and c.pay_status in (1, 3)and c.refund_status= 0and c.store_id= 36574order by c.create_time desclimit 1000,10\G
共返回 10 行记录,花费 220 ms.使用强制索引执行时间:
mysql>SELECT c.order_price orderPrice,c.preferential_amount preferentialAmount,c.order_sumprice orderSumprice,cast(c.mode as SIGNED) rechargeType,cast(c.pay_type as SIGNED) payType,cast(c.type as SIGNED) appType,c.order_sn orderSn,c.create_time payTime,u.nickname nickName,u.headimgurl headImg,u.real_name memberName,cast(c.pay_status as SIGNED) payStatusFROM tp_order c  force index(IDX_CR_MO_TO)LEFT JOIN tp_user u ON c.user_id= u.idWHERE c.token= '1392044'and c.pay_status in (1, 3)and c.refund_status= 0and c.store_id= 36574order by c.create_time desclimit 1000,10\G
共返回 10 行记录,花费 17444 ms.

总结: 具体场景具体分析:
本例子中

  1. 强制索引是索引全扫描,limit值越大性能就会越差
  2. 而默认走tscp 索引,是根据 where条件 token,store_id值ref 等值过滤的。效果比较强制IDX_CR_MO_TO

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/456198.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【R】语言第四课----读取文件

install.packages("readxl") getwd() setwd("E:/作业4") library(readxl) tianmao<-read_excel(tianmaoTV.xlsx,skip1)#把第一行跳过&#xff0c;直接从第二行开始读取 #创建新变量 tianmao[total_sales]<-tianmao$current_price*tianmao$month_sale…

Html.Partial和Html. RenderPartial用法

Html.Partial和Html. RenderPartial用法Html.partial和RenderPartial的用法与区别Html.partial和RenderPartial都是输出html片段&#xff0c;区别在于Partial是将视图内容直接生成一个字符串并返回&#xff08;相当于有个转义的过程&#xff09;&#xff0c;RenderPartial方法是…

算术编码简单研究

算术编码 是一种无损数据压缩方法&#xff0c;也是一种熵编码的方法。和其它熵编码方法不同的地方在于&#xff0c;其他的熵编码方法通常是把输入的消息分割为符号&#xff0c;然后对每个符号进行编码&#xff0c;而算术编码是直接把整个输入的消息编码为一个数&#xff0c;一个…

Thinkphp5 还有这种操作?

2019独角兽企业重金招聘Python工程师标准>>> 在 _initialize 中取出 控制器名和方法名 define(CONTROLLER_NAME,Request::instance()->controller()); define(MODULE_NAME,Request::instance()->module()); define(ACTION_NAME,Request::instance()->actio…

【R】语言第五课----画图

?plot#高级绘图函数 可以完整地绘制出一张图 ?mtcars plot(mtcars$wt) plot(mtcars[,1:2]) plot(mtcars) plot(mtcars$wt,mtcars$disp) plot(mtcars$wt,mtcars$disp,typep) plot(mtcars$wt,mtcars$disp,typel) plot(mtcars$wt,mtcars$disp,typeb) plot(mtcars$wt,mtcars$disp…

Solidworks如何将参考平面的图形投影到某曲面上

1 画好草图&#xff0c;点击曲线-分割线 2 选择要投影的草图和被投影的面&#xff08;那个球面&#xff09;&#xff0c;最后效果如下图所示 3 为了获取连续的轨迹&#xff0c;我们可以再次选择这个草图&#xff0c;然后在投影面中选择平面&#xff0c;最后得到的图形如下图所示…

向极限挑战:算术编码 (转)

向极限挑战&#xff1a;算术编码 (转) http://blog.csdn.net/hhf383530895/archive/2009/08/24/4478605.aspx 我们在上一章中已经明白&#xff0c;Huffman 编码使用整数个二进制位对符号进行编码&#xff0c;这种方法在许多情况下无法得到最优的压缩 效果。假设某个字符的出…

np.random.seed(0)作用

在用python时时常会看到如下代码&#xff1a; import numpy as np np.random.seed(0) 其中np.random.seed(0)的作用是使得随机数据可预测&#xff0c;当我们设置相同的seed&#xff0c;每次生成的随机数相同。 如果不设置seed&#xff0c;则每次会生成不同的随机数&#xf…

发送邮件被退回,提示: Helo command rejected: Invalid name 错误

我自己配置的 postfix dovecot server&#xff0c; 配置了outlook 后&#xff0c; 相同的账号。 在有的电脑上能收发成功&#xff0c; 在有的电脑上发送邮件就出现退信。提示 Helo command rejected: Invalid name 错误。经过分析&#xff0c; 原来是计算机名的问题。 计算机名…

Series和DataFrame、相关性及NaN处理

pandas核心数据结构 pandas是以numpy为基础的&#xff0c;还提供了一些额外的方法 Series series用来表示一维数据结构&#xff0c;与python内部的数组类似&#xff0c;但多了一些额外的功能。 series内部由两个相互关联的数组组成&#xff1a;主数组用来存放数组&#xff…

Hive谓词解析过程分析

where col1 100 and abs(col2) > 0在Hive中的处理过程 where过滤条件称为谓词predicate。 以上where过滤条件在经过Hive的语法解析后&#xff0c;生成如下的语法树&#xff1a; TOK_WHERE AND TOK_TABLE_OR_C…

算术编码(Arithmetic Coding)源代码

Ian H. Witten、Radford M. Neal和John G. Cleary在1987年发表了一篇启发性的论文。论文中描述了一种基于整数运算的通用算术编码器&#xff0c;而且还给出了由计算错误导致的效率低下的分析。以下源代码来自于这篇论文&#xff1a;《基于算术编码的数据压缩》&#xff08;Arit…

pandas读写各种类型数据

read_X()通常是pandas模块下的&#xff0c;to_X()是dataframe的方法 CSV 读取 使用pandas.read_csv()方法&#xff0c;返回的是一个dataframe csv默认是以"&#xff0c;"分割的 csv文件内容 1、read_csv()默认以第一行数据作为标题 2、调用dataframe的head()方法…

python 类装饰器

1 装饰器无参数 class tracer: def __init__(self,func): self.calls 0 self.func func def __call__(self,*args): self.calls 1 print(call %s to %s %(self.calls, self.func.__name__)) self.func(*args) tracer def spam(a, b, c): print(a b c) …

【数据分析】使用pandas和numpy分析美国大选献金项目

1. 数据载入与总览 1.1 数据加载 #绘图工具 import matplotlib.pyplot as plt %matplotlib inline #数据处理工具 import numpy as np import pandas as pd from pandas import Series,DataFrame#数据路径自己指定&#xff0c;本案例数据路径就在当前文件夹下面子文件夹usa_e…

《容器技术系列》一1.4 Docker运行案例分析

本节书摘来华章计算机《容器技术系列》一书中的第1章 &#xff0c;第1.4节&#xff0c;孙宏亮 著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。 1.4 Docker运行案例分析 1.3节着重介绍了Docker架构中各个模块的功能&#xff0c;学完后我们可以对Docker的架构有一…

算术编码的原理与分析

转自&#xff1a;http://kulasuki115.blogcn.com/diary,201492702.shtml 前言 人类已进入信息时代&#xff0c;信息时代的重要特征是信息的数字化&#xff0c;人们越来越依靠计算机获取和利用信息&#xff0c;这就需要对信息的表示、存储、传输和处理等关键技术进行研究。我们…

3月22日AM

看了思维章节精讲视频课&#xff0c;并且总结了部分思维章节内容转载于:https://www.cnblogs.com/bgd140206102/p/6601440.html

阿里巴巴Dubbo实现的源码分析

Dubbo概述Dubbo是阿里巴巴开源出来的一个分布式服务框架&#xff0c;致力于提供高性能和透明化的RPC远程服务调用方案&#xff0c;以及作为SOA服务治理的方案。它的核心功能包括&#xff1a; remoting:远程通讯基础&#xff0c;提供对多种NIO框架抽象封装&#xff0c;包括“同步…

POJ 2106-Boolean Expressions,双栈运用类似表达式求值!

Boolean Expressions 首先声明此题后台可能极水&#xff08;毕竟这种数据不好造&#xff01;&#xff09;。昨天写了一天却总是找不到bug&#xff0c;讨论区各种数据都过了&#xff0c;甚至怀疑输入有问题&#xff0c;但看到gets也可以过&#xff0c;难道是思路错了&#xff1f…