long sql優化例項 todo

2021-09-16 20:48:21 字數 4679 閱讀 2971

create table `hbt_messageflow` (

`id` int(11) not null auto_increment,

`shopid` int(11) default null,

`receive` varchar(50) not null comment '接收方',

`configid` int(11) default null comment '配置資訊模版id',

`reftype` int(11) not null comment '型別',

`refid` varchar(50) not null comment '型別值',

`sendstatus` int(11) not null comment '傳送狀態',

`meno` varchar(200) not null comment 'meno',

`content` varchar(1000) not null comment '引數內容',

`addtime` datetime not null,

`updatetime` timestamp not null default current_timestamp on update current_timestamp comment '更新時間',

primary key (`id`),

key `ix_addtime` (`addtime`),

key `ix_updatetime` (`updatetime`),

key `ix_receive` (`receive`),

key `ix_reftype` (`reftype`),

key `ix_refid` (`refid`)

) engine=innodb auto_increment=1384415 default charset=utf8 comment='傳送訊息記錄表'

table information

created: 2016-09-12

engine: innodb

rows: 815558

size: 282 mib

encoding: utf8_general_ci

auto-increment: 1384415

原始long-sql

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where configid = 13 and receive = '18358169297' and sendstatus = 200 order by addtime desc limit 1
645ms

idselect_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

index

ix_receive

ix_addtime566

using where

增加limit

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where configid = 13 and receive = '18358169297' and sendstatus = 200 order by addtime desc limit 10
2.287s

idselect_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

index

ix_receive

ix_addtime

5663

using where

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where configid = 13 and receive = '18358169297' and sendstatus = 200 order by addtime desc limit 184
26ms

idselect_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

refix_receive

ix_receive

152const

12192

using index condition; using where; using filesort

去除desc

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where configid = 13 and receive = '18358169297' and sendstatus = 200 order by addtime limit 1
id

select_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

index

ix_receive

ix_addtime566

using where

更改order列

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where configid = 13 and receive = '18358169297' and sendstatus = 200 order by id desc limit 1
id

select_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

refix_receive

ix_receive

152const

12192

using where

刪除sendstatus和configid條件

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where receive = '18358169297'  order by addtime desc limit 1
1ms

idselect_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

index

ix_receive

ix_addtime566

using where

更改receive條件為15901736035(特徵:首次命中記錄的id靠前)

explain select id ,shopid,receive, configid, reftype, refid, sendstatus, meno, content,addtime from hbt_messageflow where configid = 13 and receive = '15901736035' and sendstatus = 200 order by addtime desc limit 1
1ms

idselect_type

table

type

possible_keys

keykey_len

refrows

extra

1******

hbt_messageflow

refix_receive

ix_receive

152const

184using index condition; using where; using filesort

receive + addtime聯合查詢

MySQL優化例項

在apache,php,mysql的體系架構中,mysql對於效能的影響最大,也是關鍵的核心部分。對於discuz 論壇程式也是如此,mysql的設定是否合理優化,直接影響到論壇的速度和承載量!同時,mysql也是優化難度最大的乙個部分,不但需要理解一些mysql專業知識,同時還需要長時間的觀察統計...

MySQL優化例項

mysql優化例項 在apache,php,mysql的體系架構中,mysql對於效能的影響最大,也是關鍵的核心部分。對於discuz 論壇程式也是如此,mysql的設定是否合理優化,直接 影響到論壇的速度和承載量!同時,mysql也是優化難度最大的乙個部分,不但需要理解一些mysql專業知識,同時...

mysql優化例項

1.查詢所有欄位的sql耗時 0.003497 秒 2.查詢所需欄位sql耗時 limit 3 0.001194 秒 select group sn,small desc,group name,end time,group image,already orders already orders vi...