5左连接导致查询超时,mysql 5.5

时间:2015-04-10 22:24:38

标签: mysql sql left-join query-optimization

重点是获取所有艺术家记录,以及其他5个表中的任何匹配记录。

可能有50,000个艺术家唱片。

我打算从这个语句中创建一个视图,然后使用它来保持sugarcrm db更新。

非常感谢任何帮助。

这是SQL语句,

 SELECT 
 /*foreign keys*/
 band_profiles.id AS 'BP_ARTIST_ID', 
 bandsintown_artists.id AS 'BID_ARTIST_ID',
 contacts.id AS 'CONTACTS_MGMT_ID',
 facebook_stats.id AS 'FB_STATS_ID',
 outbound_links.id AS'OUTB_LINKS_ID',

 /*high level*/
 band_profiles.name AS 'ACCOUNT_NAME', 
 band_profiles.description AS 'ACCOUNT_DESCRIPTION', 
 band_profiles.bandsintown_status AS 'BIT_STATUS',
 band_profiles.created_at AS 'DR_CREATED_DATETIME', 
 band_profiles.updated_at AS 'DR_UPDATED_DATETIME',

 /*account mgmt fields*/
 contacts.description AS 'ACCOUNT_MGMT_CONTACT',
 contacts.contact_type AS 'ACCOUNT_MGMT_TYPE',
 contacts.custom_contact_type AS 'ACCOUNT_MGMT_TYPE_C',

 /*account web & social*/
 band_profiles.website_url AS 'WEBSITE_URL', 
 band_profiles.twitter_url AS 'ACCOUNT_TWITTER_URL',
 band_profiles.facebook_url AS 'ACCOUNT_FACEBOOK_URL', 
 band_profiles.facebook_page_id AS 'ACCOUNT_FACEBOOK_ID',
 band_profiles.facebook_like_count AS 'ACCOUNT_FACEBOOK_LIKES',
 facebook_stats.like_count AS 'FB_TOTAL_LIKES',
 facebook_stats.share_count AS 'FB_TOTAL_SHARES',
 facebook_stats.comment_count AS 'FB_TOTAL_COMMENTS',
 facebook_stats.click_count AS 'FB_TOTAL_CLICKS',
 outbound_links.target_url AS 'OUTBOUND_LINK',
 outbound_links.link_type AS 'OUTB_LINK_TYPE',

 bandsintown_artists.facebook_tour_dates_url AS 'ACCOUNT_FB_TOUR_DATES'


 FROM band_profiles
 LEFT JOIN bandsintown_artists
 ON band_profiles.id = bandsintown_artists.band_profile_id
 LEFT JOIN contacts
 ON band_profiles.id = contacts.id
 LEFT JOIN facebook_stats
 ON band_profiles.id = facebook_stats.band_profile_id
 LEFT JOIN outbound_links
 on band_profiles.id = outbound_links.band_profile_id
 GROUP BY band_profiles.id
 LIMIT 10   

这是我得到的错误代码

 Error Code: 2013. Lost connection to MySQL server during query 600.000 sec

1 个答案:

答案 0 :(得分:-1)

如果您是sysadm,则可以增加mysql超时值。见http://www.rackspace.com/knowledge_center/article/how-to-change-the-mysql-timeout-on-a-server

根据数据库中已删除条目的数量,通过修剪JOIN查询中已删除的条目,您可能会在此查询中看到显着的内存/时间节省

还通过band_profiles.deleted = 0

限制您的最终输出
LEFT JOIN bandsintown_artists
 ON (band_profiles.id = bandsintown_artists.band_profile_id AND bandsintown_artists.deleted = 0)
 LEFT JOIN contacts
 ON (band_profiles.id = contacts.id AND contacts.deleted = 0)
 LEFT JOIN facebook_stats
 ON (band_profiles.id = facebook_stats.band_profile_id AND facebook_stats.deleted = 0)
 LEFT JOIN outbound_links
 on (band_profiles.id = outbound_links.band_profile_id AND outbound_links.deleted = 0)
WHERE band_profiles.deleted = 0