加快SQL查询,已经有效了

时间:2015-05-22 14:10:36

标签: mysql sql performance

我有这个查询

SELECT distinct(season) as seasons,
        s.name as seriename,
        c.content_digest,
        c.sub_category_id,
        c.source_url,
        c.synopsis,
        c.short_synopsis,
        c.title,
        c.duration,
        c.subtitle_url,
        c.logo_url,
        c.provider_id,
        c.event_date,
        c.start_time,
        c.live,
        c.seriesid as cSerieId,
        c.episode,
        c.season,
        c.content_id FROM content c
        INNER JOIN provider ON provider.id = c.provider_id
        LEFT OUTER JOIN serie s ON s.seriesid = c.seriesid
        WHERE live = '0' AND c.version >= provider.version
        ORDER BY seriename DESC, title
        DESC
        LIMIT 0, 18446744073709551615;

已对其进行索引并进行了一些优化以快速提供结果(已在0.3秒内交付了44000行)。 由于某些未知的原因,我的装备磨损,它不再快0.3秒,快2.5秒。

这是以上SQL的EXPLAIN

1   SIMPLE  provider    index   PRIMARY,provider_id_idx,provider_version_idx    provider_version_idx    5       16  Using index; Using temporary; Using filesort

1   SIMPLE  c   ref c_providerid_idx,c_live_idx,c_version_idx   c_providerid_idx    5   provider.id 3179    Using where

1   SIMPLE  s   ref seriesid_idx    seriesid_idx    97  c.seriesid  1   

据我了解,它正在使用其索引,但仍然是2.5秒.. 我用google搜索缓存,激活它,尝试调整它,但仍然,2.5秒。

有什么建议吗? 如果你需要,我会发布更多信息...

最终结果

SELECT s.name as seriename, 
            c.season as seasons,
            c.content_digest,
            c.sub_category_id,
            c.source_url,
            c.synopsis,
            c.short_synopsis,
            c.title,
            c.duration,
            c.subtitle_url,
            c.logo_url,
            c.provider_id,
            c.event_date,
            c.start_time,
            c.live,
            c.seriesid as cSerieId,
            c.episode,
            c.season,
            c.content_id FROM content c
            INNER JOIN provider ON provider.id = c.provider_id
            LEFT OUTER JOIN serie s ON s.seriesid = c.seriesid
            WHERE live = '0' AND c.version >= provider.version
            GROUP BY (CASE WHEN ( seriename IS NOT NULL )
            THEN seriename ELSE title END) 
            ORDER BY seriename DESC, title DESC
            LIMIT 0, 50

1 个答案:

答案 0 :(得分:0)

根据服务器的负载和数据集的大小,请求的时间可能会有所不同。

我不确定,但它有时会带来c.version >= provider.version和联接,而不是WHERE。请参阅下面的完整查询。让我知道EXPLAIN结果。

SELECT distinct(season) as seasons,
    s.name as seriename,
    c.content_digest,
    c.sub_category_id,
    c.source_url,
    c.synopsis,
    c.short_synopsis,
    c.title,
    c.duration,
    c.subtitle_url,
    c.logo_url,
    c.provider_id,
    c.event_date,
    c.start_time,
    c.live,
    c.seriesid as cSerieId,
    c.episode,
    c.season,
    c.content_id FROM content c
    INNER JOIN provider ON provider.id = c.provider_id 
    AND c.version >= provider.version
    LEFT OUTER JOIN serie s ON s.seriesid = c.seriesid
    WHERE live = '0' 
    ORDER BY seriename DESC, title
    DESC
    LIMIT 0, 18446744073709551615;

还在(version, live, id)表中创建content列的复合(多列)索引。以下是创建多列索引的查询: ALTER TABLE content ADD INDEX idxc_vr_lv_id (version, live, id);