Question

I'm am trying to get this query to use the indexes I created but it is still doing a full table scan. I even tried to create a multiple field index but not luck. I have tried the FORCE INDEX but no luck as well.

The Places table has 1.5million records. The intent of the query is to get it to sort the place in the right order along a street.

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

Output of Explain

{
  "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')"
      }
    }
  }
}

I thought about creating a hash to column but I don't think that will get me there.

Could I group the street specific fields (PreDirection, StreetName, StreetType, PostDirection) together?

Total RowCount for Physical is 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   

Mark

Was it helpful?

Solution

From Mysql documentation: In some cases, MySQL can use an index to satisfy an ORDER BY clause without doing any extra sorting.

The index can also be used even if the ORDER BY does not match the index exactly, as long as all of the unused portions of the index and all the extra ORDER BY columns are constants in the WHERE clause

What is missing from your index is field Type (in the beginning of the index).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top