优化Mysql左连接800,000+行

时间:2013-12-03 15:43:39

标签: mysql sql

我有以下查询我在具有800,000条记录的数据库上运行,但加载时间超过20秒。我在所有列上都有索引,任何人都可以提出更好的查询格式,以便在1.秒下加载这些结果吗?

SELECT SQL_CALC_FOUND_ROWS st.location_limit 
                         , u.user_id
                         , IF(listing_type = 'Company', company, last_name) name 
                      FROM users_data u
                      JOIN subscription_types st
                        ON st.subscription_id = u.subscription_id 
                     WHERE u.active = 2 
                       AND st.searchable = 1  
                     GROUP 
                        BY u.user_id 
                     ORDER 
                        BY st.search_priority ASC 
                     LIMIT 10;

这是我的解释

id  select_type  table               partitions  type  possible_keys                                    key                  key_len  ref                                                       rows    Extra
1   SIMPLE       subscription_types  NULL        ALL   PRIMARY,searchable,id_searchable,search_results  NULL                 NULL     NULL                                                      4       Using where; Using temporary; Using filesort
1   SIMPLE       users_data          NULL        ref   subscription_id,active,subscription_active       subscription_active  8        findades_anti315.subscription_types.subscription_id,const 209261  

整个查询就在这里 这是整个查询。谢谢。

SELECT SQL_CALC_FOUND_ROWS subscription_types.location_limit as location_limit,users_data.user_id,users_data.last_name,users_data.filename,users_data.user_id,users_data.phone_number,users_data.city,users_data.state_code,users_data.zip_code,users_data.country_code,users_data.quote ,users_data.subscription_id,users_data.company,users_data.position,users_data.profession_id,users_data.experience,users_data.account_type,users_data.verified,users_data.nationwide,IF(listing_type ='Company',company,last_name)as name FROM {{ 1}} LEFT JOIN users_data ON users_data.user_id = users_reviews.user_id AND users_reviews.review_status ='2'LEFT JOIN users_locations ON users_locations.user_id = users_data.user_id LEFT JOIN subscription_types ON users_data.subscription_id = subscription_types.subscription_id LEFT JOIN users_reviews ON rel_services.user_id = users_data.user_id WHERE users_data.active ='2'AND subscription_types.searchable ='1'AND((rel_services.service_id ='1383'或rel_services.service_id ='1384'OR rel_serv ices.service_id ='1385')OR subscription_types.service_limit ='all')ORDER BY subscription_types.search_priority ASC

0 个答案:

没有答案