explain SELECT * FROM house JOIN street s on house.STREET_ID = s.ID WHERE s.type<>'out_of' AND street.DISTRICT_ID=4 ORDER BY house.num;
explain SELECT * FROM house JOIN street s on house.STREET_ID = s.ID WHERE s.type<>'out_of' AND house.DISTRICT_ID=4 ORDER BY house.num;
Type and district_id are compounds indexed in Street table district_id is indexed in House table
- explain with street.DISTRICT_ID=4, street table is joined
<data>
<row>
<id>1</id>
<select_type>SIMPLE</select_type>
<table>street</table>
<type>ref</type>
<possible_keys>PRIMARY,FK_STREET_DISTRICT,IDX_STREET_DISTRICT_ID_STREET_TYPE</possible_keys>
<key>IDX_STREET_DISTRICT_ID_STREET_TYPE</key>
<key_len>8</key_len>
<ref>const</ref>
<rows>16</rows>
<Extra>Using index condition; Using temporary; Using filesort</Extra>
</row>
<row>
<id>1</id>
<select_type>SIMPLE</select_type>
<table>house</table>
<type>ref</type>
<possible_keys>FK_HO_ST,idx_or_st_entity</possible_keys>
<key>FK_HO_ST</key>
<key_len>8</key_len>
<ref>street.ID</ref>
<rows>70695</rows>
<Extra></Extra>
</row>
</data>
- explain with house.DISTRICT_ID=4
<data>
<row>
<id>1</id>
<select_type>SIMPLE</select_type>
<table>house</table>
<type>ALL</type>
<possible_keys>FK_ST_HO,FK_ST_DISTRICT,idx_or_st_entity,IDX_HO_DISTRICT_NUM</possible_keys>
<key>null</key>
<key_len>null</key_len>
<ref>null</ref>
<rows>989734</rows>
<Extra>Using where; Using filesort</Extra>
</row>
<row>
<id>1</id>
<select_type>SIMPLE</select_type>
<table>street</table>
<type>eq_ref</type>
<possible_keys>PRIMARY</possible_keys>
<key>PRIMARY</key>
<key_len>8</key_len>
<ref>house.STREET_ID</ref>
<rows>1</rows>
<Extra>Using where</Extra>
</row>
</data>
DISTRICT_ID is the same for house and street but the received estimated rows have a big difference. Does it mean that query with joined DISTRICT_ID is more efficient, where estimated rows = 70695?