Chris
Chris

Reputation: 764

Optimizing join / union sql?

I have the following SQL query that runs fairly well, however it seems to stall when returning categories with 60,000-70,000 rows. I would like any tips on how I can optimize this, either server side or in the code itself to make this run as fast as possible. Although it will usually be cached, this statement will be used on a network with 100,000's of hits a day and I need it to be smooth or to at least finish the query execution.

SELECT * FROM
( 
    (
        SELECT DISTINCT
            vc.category_id, t.tube_title, v.*, COUNT(vc.video_id) AS total_clicks
        FROM video_click vc
        JOIN tube t ON vc.tube_id = t.tube_id
        JOIN video v ON v.video_id = vc.video_id
        WHERE
                vc.time >= 1313153417
            AND
                vc.category_id = 123
        GROUP BY vc.video_id
    )
    UNION
    (
        SELECT DISTINCT
            vd.category_id, t.tube_title, v.*, COUNT(NULL) as total_clicks
        FROM
            video_data vd
        JOIN tube t ON vd.tube_id = t.tube_id
        JOIN video v ON v.video_id = vd.video_id
        WHERE
            vd.category_id = 123
        GROUP BY vd.video_id
    )
) AS final_video
GROUP BY final_video.video_id
ORDER BY total_clicks DESC

I appreciate any tips or help to allow the above to run on larger DB selects. Thanks!

Upvotes: 0

Views: 142

Answers (1)

a'r
a'r

Reputation: 37029

It looks like that you are only unioning with the video_data table to ensure you get counts for videos with no clicks. This can be more easily achieved by using a left join.

SELECT
    vd.category_id, t.tube_title, v.*, COUNT(vc.video_id) AS total_clicks
FROM video_data vd
    LEFT JOIN video_click vc ON vc.video_id = vd.video_id AND vc.time > 1313153417
    JOIN tube t ON vd.tube_id = t.tube_id
    JOIN video v ON vd.video_id = v.video_id
WHERE
    vd.category_id = 123
GROUP BY v.video_id
ORDER BY COUNT(vc.video_id) desc

This should run faster than your previous query, but please make sure you check that it returns the results that you want.

Upvotes: 1

Related Questions