Mysql 反向索引和查询

Mysql reverse index and query

我创建了一个 table 具有以下属性

employee54(
    Emp_Id int, 
    First_name varchar(20),
    Middle_name varchar(20), 
    Last_name varchar(20), 
    Dept_Id int, 
    Phone_number int, 
    Address varchar(30)
)  

我尝试使用这种语法制作反向索引

create index emp_id_list on employee54(Emp_Id) reverse;

它显示

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 'reverse'

只有 MySQL 8.0+ 支持创建和使用 "reverse" 索引。
其他 MySQLs 版本仅解析 ASC 或 DESC 部分但确实使用它们。

A key_part specification can end with ASC or DESC to specify whether index values are stored in ascending or descending order. The default is ascending if no order specifier is given. ASC and DESC are not permitted for HASH indexes. As of MySQL 8.0.12, ASC and DESC are not permitted for SPATIAL indexes.

来源https://dev.mysql.com/doc/refman/8.0/en/create-index.html

CREATE INDEX emp_id_list ON employee54(Emp_id DESC) 

DESC 生成降序索引,即 "reversed" 索引。

  • 错误消息 1064 直接指向问题(或者它可能直接问题之后)。
  • 查看手册会告诉您 ASCDESC 是可能的,而不是 reverse
  • 如果您希望以 emp_id 的相反顺序获取行,那么只需执行 ORDER BY emp_id DESCENDING。这将为您提供所需的结果(对于 MySQL/MariaDB 的所有版本),即使 8.0 之前的版本没有以相反的顺序实现索引创建。