mysql性能的检查和调优方法(2)

来源:asp之家 时间:2009-05-17 09:21:00 

但是,如果我用别的userid查询,结果又会有所不同:

mysql> desc select * from imgs where userid="admin" order by clicks desc limit 10;

+----+-------------+-------+------+---------------+--------+---------+-------+------+-----------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+--------+---------+-------+------+-----------------------------+

| 1 | SIMPLE | imgs | ref | userid | userid | 51 | const | 2944 | Using where; Using filesort |

+----+-------------+-------+------+---------------+--------+---------+-------+------+-----------------------------+

1 row in set (0.00 sec)

这个结果和userid="7mini"的结果基本相同,但是mysql用userid索引一次搜索后结果集的大小达到2944条,这2944条记录都会加入内存进行filesort,效率比起7mini那次来说就差很多了。这时可以有两种办法可以解决,第一种办法是再加一个索引和判断条件,因为我只需要根据点击量取最大的10条数据,所以有很多数据我根本不需要加进来排序,比如点击量小于10的,这些数据可能占了很大部分。

我对clicks加一个索引,然后加入一个where条件再查询:

create index clicks on imgs(clicks);

mysql> desc select * from imgs where userid="admin" order by clicks desc limit 10;

+----+-------------+-------+------+---------------+--------+---------+-------+------+-----------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+--------+---------+-------+------+-----------------------------+

| 1 | SIMPLE | imgs | ref | userid,clicks | userid | 51 | const | 2944 | Using where; Using filesort |

+----+-------------+-------+------+---------------+--------+---------+-------+------+-----------------------------+

1 row in set (0.00 sec)

这时可以看到possible_keys变成了userid,clicks,possible_keys是可以匹配的所有索引,mysql会从possible_keys中自己判断并取用其中一个索引来执行语句,值得注意的是,mysql取用的这个索引未必是最优化的。这次查询mysql还是使用userid这个索引来查询的,并没有按照我的意愿,所以结果还是没有什么变化。改一下sql加上use index强制mysql使用clicks索引:

mysql> desc select * from imgs use index (clicks) where userid='admin' and clicks>10 order by clicks desc limit 10

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+

| 1 | SIMPLE | imgs | range | clicks | clicks | 4 | NULL | 5455 | Using where |

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+

1 row in set (0.00 sec)

这时mysql用到了clicks索引进行查询,但是结果集比userid还要大!看来还要再进行限制:

mysql> desc select * from imgs use index (clicks) where userid='admin' and clicks>1000 order by clicks desc limit 10

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+

| 1 | SIMPLE | imgs | range | clicks | clicks | 4 | NULL | 312 | Using where |

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+

1 row in set (0.00 sec)

标签:mysql,性能,检查,调优
0
投稿

猜你喜欢

  • 重新发现HTML表格

    2009-12-02 09:47:00
  • Pytorch 中retain_graph的用法详解

    2021-01-20 21:23:45
  • 从品牌网站看交互设计

    2009-08-18 12:39:00
  • 基于php无限分类的深入理解

    2023-11-15 04:07:39
  • 详解pyqt5 动画在QThread线程中无法运行问题

    2021-07-22 21:00:43
  • 发现IE6下URL path不会自动补全

    2009-03-31 12:52:00
  • Matplotlib 3D 绘制小红花原理

    2022-11-23 11:53:08
  • 创建、调用JavaScript对象的方法集锦

    2024-02-25 08:01:03
  • python中argparse模块用法实例详解

    2022-01-09 23:34:40
  • Python SMTP发送邮件遇到的一些问题及解决办法

    2023-04-17 12:06:52
  • 在vue-cli脚手架中配置一个vue-router前端路由

    2024-05-28 15:59:13
  • Go语言实现定时器的方法

    2024-02-09 04:55:21
  • Python实现删除重复视频文件的方法详解

    2022-06-16 03:24:29
  • Python入门之三角函数sin()函数实例详解

    2021-11-09 18:31:21
  • 理想高通滤波实现Python opencv示例

    2022-09-29 03:58:07
  • python检测lvs real server状态

    2021-12-12 21:21:36
  • python爬虫爬取淘宝商品信息

    2023-07-04 10:33:24
  • 详解Python 爬取13个旅游城市,告诉你五一大家最爱去哪玩?

    2022-05-22 08:39:32
  • python右对齐的实例方法

    2022-01-15 20:59:52
  • Python 多个图同时在不同窗口显示的实现方法

    2022-07-02 01:20:35
  • asp之家 网络编程 m.aspxhome.com