Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6329333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:38:31+00:00 2026-05-24T17:38:31+00:00

I have the following SQL query that runs fairly well, however it seems to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:38:33+00:00Added an answer on May 24, 2026 at 5:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query which runs perfectly well on both Oracle and SQL
I have an SQL query that takes the following form: UPDATE foo SET flag=true
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
I have following SQL query that selects some results from my table: select avg(c3),
I have the following SQL query that comes out at 700 declare @a decimal(10,0)
I have the following sql query that fetches images from an establishment table. The
I have the following SQL query that performs horribly due to the select count(1)
I have the following SQL Server 2008 query that works for all of my
We have the following simple Stored Procedure that runs as an overnight SQL server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.