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

Posted

技术标签:

【中文标题】5 左连接导致查询超时,mysql 5.5【英文标题】:5 left joins cause query to time out, mysql 5.5 【发布时间】:2015-04-10 22:24:38 【问题描述】:

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

可能有 50,000 条艺术家记录。

我计划根据该语句创建一个视图,然后使用它来保持 Sugarcrm 数据库处于最新状态。

非常感谢任何帮助。

这是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

【问题讨论】:

为什么不用任何聚合就使用GROUP BY LIMIT 10 没有ORDER BY 有点毫无意义。 请在您的查询上运行EXPLAIN 并向我们展示结果。 请为查询中的每个表提供SHOW CREATE TABLE tablename,以便我们检查列的数据类型和您拥有的任何索引。 【参考方案1】:

如果你是系统管理员,你可以增加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

【讨论】:

以上是关于5 左连接导致查询超时,mysql 5.5的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 5 左连接未知列

MySQL Left Join(左连接) 耗时严重的问题

MySQL连接查询之内连接左连接右连接自连接

MySQL左连接不返回连接表的空值

左连接会导致性能大幅下降。如何修复它

为啥 mySQL 查询,左连接“明显”比我的内连接快