使用FIND_IN_SET和多表JOINS进行MYSQL查询优化

时间:2015-05-11 14:17:21

标签: mysql indexing find-in-set

以下查询大约需要6-7秒。我希望结果不到1秒。我该怎么办。在find_in_set中使用索引的catsid colum会有帮助吗?

**EXPLAIN SELECT** sp_events.id,sp_events.id as speventId,
event.id as eventId,event.cityid as city_id,city.name as cityname,
event.title as event_name,event.title as eventName,
UNIX_TIMESTAMP(sp_events.dates) as sphinx_date,
sp_events.dates as sphinx_start_date,
UNIX_TIMESTAMP(sp_events.enddates) as sphinx_enddate,
sp_events.enddates as
sphinx_end_date,
UNIX_TIMESTAMP(event.dates) as date,event.dates as start_date,
UNIX_TIMESTAMP(event.enddates) as enddate,
event.enddates as end_date,
UNIX_TIMESTAMP(event.times) as times,
event.times as start_time,
TIME_TO_SEC(event.times) as sorttime,
TIME_TO_SEC(event.times) as startTime,
UNIX_TIMESTAMP(event.endtimes) as endtimes,
event.endtimes as end_time,event.booking_url,
event.image_low,event.preferred_image,
event.multiple_booking,event.recurrence_type,
event.recurrence_days,event.recurrence_times,
event.recurrence_endtimes,event.partner_url,event.partner_url_cc,
event.catsid as catsid,event.catsid as event_cat_id,
event.artist_id as aid,event.artist_id as artist_id,event.price,
event.partner,event.partner as eventpartner,
content.title as venue_name,content.title as venueName,
content.id as venueId,contact.latitude as lat,
contact.longitude as lon,RADIANS(contact.latitude) as lat1,
RADIANS(contact.longitude) as lon1,contact.city as venuecity,
city.countryid as country_id,event.image_high,
sd.stateCode 
**FROM** jos_sphinx_events as sp_events **LEFT JOIN** 
jos_eventlist_events as event ON sp_events.eventid = event.id **LEFT join** 
jos_content as content on content.id=event.locid **LEFT JOIN** 
jos_contact_details as contact ON content.id=contact.id **LEFT JOIN** 
jos_city_details as city on city.id=event.cityid **LEFT JOIN** 
jos_state_details as sd ON sd.id= city.stateid 
**WHERE** event.published='1'
      AND  event.enddates>=DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY)
      and  content.state='1'
      AND  city.published='1'
      AND  sp_events.eventid IN (
        SELECT  eventid
            FROM  jos_sphinx_events
            WHERE  (FIND_IN_SET(911,catsid)
                      OR  FIND_IN_SET(630,catsid)
                      OR  FIND_IN_SET(631,catsid)
                      OR  FIND_IN_SET(632,catsid)
                      OR  FIND_IN_SET(633,catsid)
                      OR  FIND_IN_SET(634,catsid)
                      OR  FIND_IN_SET(635,catsid)
                      OR  FIND_IN_SET(636,catsid)
                      OR  FIND_IN_SET(637,catsid)
                      OR  FIND_IN_SET(638,catsid)
                      OR  FIND_IN_SET(639,catsid)
                      OR  FIND_IN_SET(640,catsid)
                      OR  FIND_IN_SET(641,catsid)
                      OR  FIND_IN_SET(642,catsid)
                      OR  FIND_IN_SET(661,catsid)
                      OR  FIND_IN_SET(662,catsid)
                      OR  FIND_IN_SET(663,catsid)
                      OR  FIND_IN_SET(664,catsid)
                      OR  FIND_IN_SET(711,catsid)
                   )
              and  published='1'
              AND  enddates>=DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY)
              AND  enddates<=DATE_ADD(CURRENT_DATE(),INTERVAL 2 YEAR)
                          )
    limit  5

上述查询的解释结果如下: The Explain result of the above query is as follow :

1 个答案:

答案 0 :(得分:0)

IN ( SELECT ... )更改为JOIN。这可能会大大提高性能。

通常情况下,最好在表格中设置“设置”,而不是在列中的commalist中设置。

此综合指数可能有所帮助:

INDEX(published, enddates)

请在子查询的字段中添加表别名资格 - 我不知道为什么它是“依赖”。分析可能会产生其他建议。

请为每张表提供SHOW CREATE TABLE;还有一些可能会被曝光的建议。