索引优化——单表优化

优化目的是:

将type 类型优化为ref,


CREATE TABLE IF NOT EXISTS `runoob`.`aritcle` (
   `id` INT(11) NOT NULL PRIMARY KEY auto_increment,
  `author_id` INT(11) NOT NULL,
  `category_id` INT(11) NOT NULL,
  `views` INT(11) NOT NULL,
  `comments` INT(11) NULL DEFAULT NULL,
  `title` varchar(11) NULL,
  `content` varchar(11) NULL
  )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8

use runoob

insert into aritcle(`author_id`,`category_id`,  `views`,`comments`, `title`, `content`)
values (1,1,1,1,'1','1');

insert into aritcle(`author_id`,`category_id`,  `views`,`comments`, `title`, `content`)
values (2,2,2,2,'2','2');

insert into aritcle(`author_id`,`category_id`,  `views`,`comments`, `title`, `content`)
values (1,1,3,3,'3','3');


select *  from aritcle



explain
 select 
id,author_id
from aritcle
where category_id=1
and comments>1
order by views
desc limit 1;


create index idx_atticle_ccv  on aritcle(author_id,comments,views)

show index from aritcle

explain
 select 
id,author_id
from aritcle
where category_id=1
and comments=3
order by views
desc limit 1;

drop index idx_atticle_ccv on aritcle



create index idx_atticle_ccv  on aritcle(category_id,views)
show index from aritcle

explain
 select 
id,author_id
from aritcle
where category_id=1
and comments>1
order by views
desc limit 1;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迅捷的软件产品制作专家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值