mysql - 强制使用多个索引 - mysql

标签 mysql indexing

我正在尝试让此查询使用我创建的索引,但它仍在进行全表扫描。我什至尝试创建一个多字段索引,但运气不佳。我尝试过 FORCE INDEX,但也没有成功。

Places 表有 150 万条记录。查询的目的是让它沿着街道以正确的顺序对地点进行排序。

SELECT PlacesId 
FROM Places FORCE INDEX (Streetname, StreetType, PreDirection,  
PostDirection, HouseNumber, UnitNumber) 
WHERE Type='Physical' 
ORDER BY StreetName, StreetType, PreDirection, PostDirection, HouseNumber, UnitNumber;

解释的输出

{
  "query_block": {
    "select_id": 1,
    "ordering_operation": {
      "using_filesort": true,
      "table": {
        "table_name": "Places",
        "access_type": "ALL",
        "rows": 1519419,
        "filtered": 100,
        "attached_condition": "(`importready`.`Places`.`Type` = 'Physical')"
      }
    }
  }
}

我考虑过创建一个哈希列,但我认为这不会让我实现这一目标。

我可以将街道特定字段(PreDirection、StreetName、StreetType、PostDirection)分组在一起吗?

物理总行数为 1,376,984

Table   Non_unique  Key_name    Seq_in_index    Column_name Collation   Cardinality Sub_part    Packed  Null    Index_type  Comment Index_comment
Places  0   PRIMARY 1   PlacesId    A   1519419 NULL    NULL        BTREE       
Places  1   PartAddress 1   Address1    A   1519419 25  NULL    YES BTREE       
Places  1   City    1   City    A   4763    15  NULL    YES BTREE       
Places  1   Zip 1   ZipCode A   1116    5   NULL    YES BTREE       
Places  1   HouseNumber 1   HouseNumber A   138129  NULL    NULL    YES BTREE       
Places  1   UnitNumber  1   UnitNumber  A   27625   NULL    NULL    YES BTREE       
Places  1   PostDirection   1   PostDirection   A   20  NULL    NULL    YES BTREE       
Places  1   PreDirection    1   PreDirection    A   20  NULL    NULL    YES BTREE       
Places  1   StreetType  1   StreetType  A   96  NULL    NULL    YES BTREE       
Places  1   StreetName  1   StreetName  A   56274   NULL    NULL    YES BTREE       
Places  1   Precinct    1   Precinct    A   1327    NULL    NULL    YES BTREE       
Places  1   County  1   County  A   72  NULL    NULL    YES BTREE       
Places  1   SortOrder   1   StreetName  A   46043   20  NULL    YES BTREE       
Places  1   SortOrder   2   StreetType  A   75970   5   NULL    YES BTREE       
Places  1   SortOrder   3   PreDirection    A   94963   6   NULL    YES BTREE       
Places  1   SortOrder   4   PostDirection   A   108529  6   NULL    YES BTREE       
Places  1   SortOrder   5   HouseNumber A   1519419 NULL    NULL    YES BTREE       
Places  1   SortOrder   6   UnitNumber  A   1519419 5   NULL    YES BTREE   

标记

最佳答案

来自 Mysql 文档: 在某些情况下,MySQL 可以使用索引来满足 ORDER BY 子句,而无需进行任何额外的排序。

即使 ORDER BY 与索引不完全匹配,只要索引的所有未使用部分和所有额外的 ORDER BY 列都是 WHERE 子句中的常量,也可以使用索引

索引中缺少的是字段类型(位于索引的开头)。

关于mysql - 强制使用多个索引 - mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567305/

相关文章:

c# - LINQ to XML - 如何获取索引

时间戳日期的 mySql 索引

php - 如何将一个表中的值数组匹配到另一个表并使用 php 显示匹配的值

php - 如何根据列内容前 4 个字母创建索引?

php - 从命令行运行 php 脚本并连接到 mysql 数据库时出现问题

php - 更改缩略图动态显示计数器

Excel 在索引列表中查找最大的部分值

mysql - mysql更新查询是否会在列上重建索引并一次又一次更新相同的值?

security - 保护 MySQL 数据库中敏感信息的步骤

提交表单时将php数据输入到MySQL中