营销型网站的分类不包含网站建设费用价格
web/
2025/10/8 17:23:48/
文章来源:
营销型网站的分类不包含,网站建设费用价格,汽修网站建设免费,海洋馆网站建设简介#xff1a;order by limit x ,x 在实际工作中有各种业务需求要有order by的排序#xff0c;有时候处理不好则会造成系统宕机#xff01;原理#xff1a;a.通过索引来获取排序b.通过内部算法获取排序#xff1a; 案例具体SQL#xff1a; SELECT c.order_price orderP… 简介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 1392044and 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,
) ENGINEInnoDB AUTO_INCREMENT53925518 DEFAULT CHARSETutf8CREATE 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)
) ENGINEInnoDB AUTO_INCREMENT13974852 DEFAULT CHARSETutf8 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时强制索引效果 mysqlexplain 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 1392044and 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时候的效果 强制索引
mysqlexplain 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 1392044and 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索引执行时间
mysqlSELECT 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 1392044and 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.使用强制索引执行时间
mysqlSELECT 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 1392044and 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. 总结 具体场景具体分析本例子中 强制索引是索引全扫描limit值越大性能就会越差而默认走tscp 索引是根据 where条件 token,store_id值ref 等值过滤的。效果比较强制IDX_CR_MO_TO
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/89168.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!