mysql 5.7.11查詢分割槽表的一個問題
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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29863023/viewspace-2199600/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle查詢分割槽表的最後一個分割槽值Oracle
- 如何查詢分割槽表的分割槽及子分割槽
- 分割槽表分割槽索引查詢效率探究索引
- 一個MySQL多表查詢的問題MySql
- oracle實用sql(14)--查詢分割槽表的分割槽列和子分割槽列OracleSQL
- [MySQL] - 聯表查詢,查詢一個不在另一個表的記錄MySql
- SCI分割槽查詢
- 巧用分割槽查詢案例一則
- 【MYSQL】 分割槽表MySql
- MySQL 分割槽表MySql
- MySQL分割槽表MySql
- 資料庫分割槽表分割槽未分配導致的一些問題資料庫
- MySQL的List分割槽表MySql
- Oracle查詢Interval partition分割槽表內資料Oracle
- oracle 並行cpu查詢分割槽表測試Oracle並行
- MySQL 分割槽表探索MySql
- mysql 分表 分割槽MySql
- mysql 分割槽表用法MySql
- MySQL表分割槽管理MySql
- 一次分割槽查詢異常的分析
- MySQL分割槽表的分割槽原理和優缺點MySql
- MySQL的分割槽(一)MySql
- MySQL 拷貝一個InnoDB分割槽表到另一個例項MySql
- 將一個非分割槽錶轉換為分割槽表
- mysql的分割槽和分表MySql
- mysql的分割槽與分表MySql
- MySQL 分割槽表 partition線上修改分割槽欄位MySql
- 一個使用JDBC按Date查詢查詢的問題JDBC
- mysql 進行表分割槽MySql
- Mysql表分割槽實現MySql
- mysql分割槽表筆記MySql筆記
- MySQL入門--分割槽表MySql
- mysql —— 分表分割槽(1)MySql
- Mysql表分割槽實操MySql
- 關於修改分割槽表的問題總結
- 【分割槽】如何將一個普通錶轉換為分割槽表
- 將mysql非分割槽錶轉換為分割槽表MySql
- PostgreSQL 原始碼解讀(98)- 分割槽表#4(資料查詢路由#1-“擴充套件”分割槽表)SQL原始碼路由套件