mysql 查询一个分区表,当查询条件存在数据时执行效率OK,当不存在数据时执行不完,一直在sending data,当去掉desc就没问题。换个版本貌似也没问题。 mysql> select version(); +------------+ | version() | +------------+ | 5.7.11-log | +------------+ 1 row in set (0.00 sec) mysql> use zabbix Database changed mysql> SELECT * FROM history h WHERE h.itemid='106107' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; +--------+------------+-----------+-----------+ | itemid | clock | value | ns | +--------+------------+-----------+-----------+ | 106107 | 1533828123 | 1792.0000 | 151803000 | | 106107 | 1533828003 | 1792.0000 | 44536142 | +--------+------------+-----------+-----------+ 2 rows in set (0.00 sec) mysql> explain SELECT * FROM history h WHERE h.itemid='106107' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | h | p201808,p201809,p201810,p201811,p201812,p201901,p201902,p201903,p201904,p201905,p201906,p201907,p201908,p201909,p201910,p201911,p201912 | range | history_1,idx_history_clock | history_1 | 12 | NULL | 172 | 100.00 | Using index condition | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.05 sec) ysql> SELECT * FROM history h WHERE h.itemid='1061055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; ^C^C -- query aborted ERROR 1317 (70100): Query execution was interrupted mysql> explain SELECT * FROM history h WHERE h.itemid='1061055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | h | p201808,p201809,p201810,p201811,p201812,p201901,p201902,p201903,p201904,p201905,p201906,p201907,p201908,p201909,p201910,p201911,p201912 | range | history_1,idx_history_clock | history_1 | 12 | NULL | 1 | 100.00 | Using index condition | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec) mysql> SELECT * FROM history h WHERE h.itemid='1061055' AND h.clock>1533723653 ORDER BY h.clock LIMIT 2 OFFSET 0; Empty set (0.00 sec) mysql> explain SELECT * FROM history h WHERE h.itemid='1061055' AND h.clock>1533723653 ORDER BY h.clock LIMIT 2 OFFSET 0; +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | h | p201808,p201809,p201810,p201811,p201812,p201901,p201902,p201903,p201904,p201905,p201906,p201907,p201908,p201909,p201910,p201911,p201912 | range | history_1,idx_history_clock | history_1 | 12 | NULL | 1 | 100.00 | Using index condition | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec) 创建了降序索引,效率提升 mysql> create index idx_history_2 on history (itemid desc); Query OK, 0 rows affected (36 min 50.11 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> SELECT * FROM history h WHERE h.itemid='1060001055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; Empty set (0.00 sec) mysql> explain SELECT * FROM history h WHERE h.itemid='1060001055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+------+-------------------------------------------+-----------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+------+-------------------------------------------+-----------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | h | p201808,p201809,p201810,p201811,p201812,p201901,p201902,p201903,p201904,p201905,p201906,p201907,p201908,p201909,p201910,p201911,p201912 | ref | history_1,idx_history_clock,idx_history_2 | history_1 | 8 | const | 1 | 31.59 | Using where | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+------+-------------------------------------------+-----------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) 升级了版本,效率提升 mysql> mysql> select version(); +---------------+ | version() | +---------------+ | 5.7.22-22-log | +---------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM history h WHERE h.itemid='1060001055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; ERROR 1046 (3D000): No database selected mysql> show databses; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databses' at line 1 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | zabbix | +--------------------+ 5 rows in set (0.00 sec) mysql> use zabbix Database changed mysql> SELECT * FROM history h WHERE h.itemid='1060001055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; Empty set (0.01 sec) mysql> explain SELECT * FROM history h WHERE h.itemid='1060001055' AND h.clock>1533723653 ORDER BY h.clock DESC LIMIT 2 OFFSET 0; +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | h | p201808,p201809,p201810,p201811,p201812,p201901,p201902,p201903,p201904,p201905,p201906,p201907,p201908,p201909,p201910,p201911,p201912 | range | history_1,idx_history_clock | history_1 | 12 | NULL | 1 | 100.00 | Using index condition | +----+-------------+-------+-----------------------------------------------------------------------------------------------------------------------------------------+-------+-----------------------------+-----------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)
mysql 5.7.11查询分区表的一个问题
来源:这里教程网
时间:2026-03-01 11:11:45
作者:
编辑推荐:
- MySQL插件调用03-01
- mysql 5.7.11查询分区表的一个问题03-01
- 3ds MAX实例教程:制作汽车轮胎并给轮胎建模03-01
- 神器:新手快速搭建MySQL服务03-01
- Word2013批量删除批注的方法步骤图03-01
- 浅谈MySQL中优化sql语句查询常用的30种方法03-01
- 怎么开启Word2013格式跟踪功能03-01
- 3台linux服务器互信03-01
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- MySQL插件调用
MySQL插件调用
26-03-01 - 3ds MAX实例教程:制作汽车轮胎并给轮胎建模
3ds MAX实例教程:制作汽车轮胎并给轮胎建模
26-03-01 - Grafana+Prometheus 监控 MySql服务
Grafana+Prometheus 监控 MySql服务
26-03-01 - 3D MAX教程:树屋室内效果图制作流程解析
3D MAX教程:树屋室内效果图制作流程解析
26-03-01 - MySQL 8.0能彻底解决困扰运维的复制延迟问题!
MySQL 8.0能彻底解决困扰运维的复制延迟问题!
26-03-01 - 怎么查看Word2013文档修改次数
怎么查看Word2013文档修改次数
26-03-01 - 【Mysql】MySQL 5.7中如何定位DDL被阻塞的问题
【Mysql】MySQL 5.7中如何定位DDL被阻塞的问题
26-03-01 - MySQL:slave 延迟一列 外键检查和自增加锁
MySQL:slave 延迟一列 外键检查和自增加锁
26-03-01 - 3ds MAX材质教程:虎皮毛发抱枕的制作流程
3ds MAX材质教程:虎皮毛发抱枕的制作流程
26-03-01 - 怎样更改word2013中所有英文字体
怎样更改word2013中所有英文字体
26-03-01
