gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:17:07 29 4
gpt4 key购买 nike

我正在尝试让此查询使用我创建的索引,但它仍在进行全表扫描。我什至尝试创建一个多字段索引,但运气不佳。我尝试过 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com