optimization - MySQL different explain plan for the same query -
i've got following query performs locally (0.4 execution time acceptable):
select categories.*, parameters_values.key, l.* categories join parameters_values on parameters_values.parameter_id = 2 , parameters_values.country_id = 223 join categories_countries on categories.category_id = categories_countries.category_id , categories_countries.country_id = 223 join listings l on l.listing_id = ( select listings.listing_id listings join listings_categories on listings.listing_id = listings_categories.listing_id join categories c on listings_categories.category_id = c.category_id (c.category_id = categories.category_id or c.subcategory = categories.category_id) , case parameters_values.`key` when 1 listings.transaction_type = 1 or listings.transaction_type = 3 when 2 listings.transaction_type = 2 or listings.transaction_type = 3 when 3 true end , listings.country_id = 223 , listings.property_type = 1 , listings.active = 1 , listings.published_flag = 1 , st_intersects( point(listings.lng, listings.lat), (select ifnull(`l`.`polygon`, get_square(`l`.`lat`, `l`.`lng`, `l`.`radius`, `l`.`country_id`)) `locations` `l` `location_id` = 1 limit 1) ) limit 1 ) order categories.subcategory, categories.`order`
it checks existence of listings in category, there location search in there, nothing overly complicated. execution plan locally looks follows:
+----+--------------------+----------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+-------------------------------------+------+------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | | +----+--------------------+----------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+-------------------------------------+------+------------------------------------------------+ | 1 | primary | parameters_values | ref | parameter_id,country_id | parameter_id | 8 | const,const | 3 | using temporary; using filesort | | 1 | primary | categories_countries | ref | primary,categories_countries_country_id_foreign | categories_countries_country_id_foreign | 4 | const | 122 | using index | | 1 | primary | categories | eq_ref | primary | primary | 4 | cp.categories_countries.category_id | 1 | null | | 1 | primary | l | eq_ref | primary,listings_listing_id_transaction_type | primary | 4 | func | 1 | using | | 2 | dependent subquery | c | | primary,categories_subcategory_foreign | null | null | null | 127 | range checked each record (index map: 0x5) | | 2 | dependent subquery | listings_categories | ref | primary,listings_categories_category_id_foreign | listings_categories_category_id_foreign | 4 | cp.c.category_id | 273 | using index | | 2 | dependent subquery | listings | eq_ref | primary,listings_country_id_foreign,listings_country_created_at,listings_country_units_created_at,listings_listing_id_transaction_type | primary | 4 | cp.listings_categories.listing_id | 1 | using | | 3 | subquery | l | const | primary | primary | 4 | const | 1 | null | +----+--------------------+----------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+-------------------------------------+------+------------------------------------------------+
however query appears executed differently on server last 4 rows following:
| 2 | dependent subquery | listings | ref | primary,listings_country_id_foreign,listings_country_created_at,listings_country_units_created_at,listings_listing_id_transaction_type | listings_country_id_foreign | 4 | const | 18238 | using | | 2 | dependent subquery | listings_categories | ref | primary,listings_categories_category_id_foreign | primary | 4 | cp.listings.listing_id | 1 | using index | | 2 | dependent subquery | c | eq_ref | primary,categories_subcategory_foreign | primary | 4 | cp.listings_categories.category_id | 1 | using | | 3 | subquery | l | const | primary | primary | 4 | const | 1 | null | +----+--------------------+----------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+-------------------------------------+-------+---------------------------------+
funny enough search former plan no use of key performs well, latter takes 23 seconds execute. how tell mysql follow same execution plan?
Comments
Post a Comment