用find_in_set优化mysql查询

时间:2016-04-12 08:14:08

标签: mysql

此查询运行时间太长... 412.2163秒

我们拥有具有不同属性的表,并且配置文件持有者可以在tbl_starcast_profile列中以逗号分隔的ID具有一个或多个此类ID(即属性),并且在配置文件表和用户表以及属性表上执行搜索FIND_IN_SET

请检查查询...让我知道此查询是否可以进一步优化。

tbl_starcast_profile结构 - 样本

    column                                           type
profile_id                                            int(11)
user_id                                               int(11)
                     ..
language_ids                                          text
accents_ids                                           text
singing_style_ids                                     text

现在,这些language_ids,accent_ids列的值类似于" 12,34,56" (这些是相应表格的ID)

tbl_starcast_users结构 - 样本

    column                                           type
user_id                                               int(11)
performer_types                                       varchar(150)
                     ..
favourite_user_ids                                    text

tbl_starcast_performer_types表 - 示例

performer_type_id   performer_type_name     performer_type_display_name     performer_type_description  date_added             date_modified    
1                          actor                   Actor                           actor                  2015-12-28 18:05:05    2016-03-21 21:41:05
2                          model                   Model                           model                  2015-12-28 18:05:05   2016-02-12 17:57:30
3                          vo_Artiste            Voice Over Artist           vo_Artiste             2015-12-28 18:05:15     2016-02-12 17:59:02
4                          comedian                Comedian                      comedian                 2015-12-28 18:05:15   2016-02-12 17:57:09
5                          presenter               Presenter                     presenter              2015-12-28 18:05:27     2016-02-12 17:58:54
6                          dancer_movement       Dancer                        dancer_movement        2015-12-28 18:06:13   2016-02-12 17:57:17

提前致谢

SELECT p. * , group_concat( DISTINCT l.skill_name ) AS languages, concat( u.first_name, ' ', u.last_name ) AS full_name, 
u.favourite_user_ids

FROM (SELECT * , group_concat( DISTINCT CONCAT( language_ids, ',', music_ids, ',', dance_ids, ',', performance_ids, ',', presenting_ids, ',', sports_ids, ',', vehicle_licences_ids, ',', other_skills_ids )
SEPARATOR ',' ) AS superset_skills FROM tbl_starcast_profile GROUP BY profile_id) p, 

tbl_starcast_users u, 
tbl_starcast_performer_types per, 
tbl_starcast_skills l, 
tbl_starcast_unique_characteristics uniq, 
tbl_starcast_awards awrd

WHERE u.profile_id = p.profile_id
AND p.user_id = u.user_id
AND (u.performer_types REGEXP '(^|,)(4)(,|$)'AND (find_in_set( per.performer_type_id, u.performer_types ) >0) )
AND (find_in_set( l.skill_id, concat( p.language_ids, p.music_ids, p.dance_ids, p.performance_ids, p.presenting_ids, p.sports_ids, p.vehicle_licences_ids, p.other_skills_ids ) ) >0 ) 

GROUP BY p.profile_id;

0 个答案:

没有答案