mysql按升序创建索引_MySQL 降序索引
MySQL 降序索引
簡介:在本教程中,您將了解MySQL降序索引以及如何利用它來提高查詢性能。
MySQL降序索引簡介
降序索引是以降序存儲鍵值的索引。在MySQL 8.0之前,您可以DESC在索引定義中指定。但是,MySQL忽略了它。與此同時,MySQL可以以相反的順序掃描索引,但成本很高。
以下語句創(chuàng)建一個帶索引的新表:
CREATE TABLE t(
a INT NOT NULL,
b INT NOT NULL,
INDEX a_asc_b_desc (a ASC, b DESC)
);
當您使用SHOW CREATE TABLE在MySQL 5.7,你會發(fā)現(xiàn),DESC如下圖所示被忽略:
mysql> SHOW CREATE TABLE t\G;
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`a` int(11) NOT NULL,
`b` int(11) NOT NULL,
KEY `a_asc_b_desc` (`a`,`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
從MySQL 8.0開始,如果DESC在索引定義中使用關(guān)鍵字,則鍵值將按降序存儲。在查詢中請求降序時,查詢優(yōu)化器可以利用降序索引。
以下顯示了MySQL 8.0中的表結(jié)構(gòu):
mysql> SHOW CREATE TABLE t\G;
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`a` int(11) NOT NULL,
`b` int(11) NOT NULL,
KEY `a_asc_b_desc` (`a`,`b` DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
MySQL降序索引示例
首先,使用不同順序的四個索引重新創(chuàng)建表t:
DROP TABLE t;
CREATE TABLE t (
a INT,
b INT,
INDEX a_asc_b_asc (a ASC , b ASC),
INDEX a_asc_b_desc (a ASC , b DESC),
INDEX a_desc_b_asc (a DESC , b ASC),
INDEX a_desc_b_desc (a DESC , b DESC)
);
其次,使用下面的存儲過程來插入行到t表:
DELIMITER $$
CREATE PROCEDURE insertSampleData(
IN rowCount INT,
IN low INT,
IN high INT
)
BEGIN
DECLARE counter INT DEFAULT 0;
REPEAT
SET counter := counter + 1;
-- insert data
INSERT INTO t(a,b)
VALUES(
ROUND((RAND() * (high-low))+high),
ROUND((RAND() * (high-low))+high)
);
UNTIL counter >= rowCount
END REPEAT;
END$$
DELIMITER ;
存儲的過程中插入的行數(shù)(rowCount)與之間的值low和high到a和b所述列t表。
讓我們10,000在t表中插入行,其中隨機值介于1和1000之間:
CALL insertSampleData(10000,1,1000);
第三,從t表中查詢具有不同排序順序的數(shù)據(jù):
按升序排列a和b列中的值:
EXPLAIN SELECT
*
FROM
t
ORDER BY a , b; -- use index a_asc_b_asc
這是輸出:
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | t | NULL | index | NULL | a_asc_b_asc | 10 | NULL | 10192 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+-------------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.03 sec)
按升序?qū)列中的值進行排序,按降序?qū)α?b 中的值進行排序:
EXPLAIN SELECT
*
FROM
t
ORDER BY a , b DESC; -- use index a_asc_b_desc
輸出是:
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | t | NULL | index | NULL | a_asc_b_desc | 10 | NULL | 10192 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
按降序?qū)列中的值進行排序,按升序?qū)α?b?中的值進行排序:
EXPLAIN SELECT
*
FROM
t
ORDER BY a DESC , b; -- use index a_desc_b_asc
以下說明輸出:
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | t | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10192 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+--------------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.42 sec)
按列a和b降序?qū)χ颠M行排序:
EXPLAIN SELECT
*
FROM
t
ORDER BY a DESC , b DESC; -- use index a_desc_b_desc
以下顯示輸出:
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | t | NULL | index | NULL | a_desc_b_desc | 10 | NULL | 10192 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
在本教程中,您學習了如何使用MySQL降序索引來提高查詢性能。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的mysql按升序创建索引_MySQL 降序索引的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php里运行js,在PHP 中运行JS
- 下一篇: linux中group命令详解,linu