MySQL商城索性优化练手

主键

  KEY `cate_id` (`cate_id`),
  KEY `price` (`price`)

数据

mysql> select cate_id,price from goods2;
+---------+-------+
| cate_id | price |
+---------+-------+
|      30 |     1 |
|      36 |     0 |
|      36 |     0 |
|      40 |     0 |
|      48 |     0 |
+---------+-------+
mysql> explain select cate_id,price from goods2 where cate_id=36 and price >1 \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: goods2
         type: ref
possible_keys: cate_id,price
          key: cate_id
      key_len: 4
          ref: const
         rows: 2
        Extra: Using where
1 row in set (0.00 sec)

扫描行数是2 因为cate_id=36 表内总共是2

改进

将 cate_id和price改成联合索引

  KEY `cate_id_price` (`cate_id`,`price`)
alter table goods2 drop index cate_id;
alter table goods2 drop index price;
alter table goods2 add index cate_id_price(cate_id,price);
mysql> explain select cate_id,price from goods2 where cate_id=36 and price >1 \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: goods2
         type: ref
possible_keys: cate_id_price
          key: cate_id_price
      key_len: 4
          ref: const
         rows: 1
        Extra: Using where; Using index
1 row in set (0.00 sec)

发小扫描行数由 2变成了1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值