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 789553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:32:32+00:00 2026-05-14T21:32:32+00:00

I have a web page where users upload&watch videos. Last week I asked what

  • 0

I have a web page where users upload&watch videos. Last week I asked what is the best way to track video views so that I could display the most viewed videos this week (videos from all dates).

Now I need some help optimizing a query with which I get the videos from the database. The relevant tables are this:

video (~239371 rows)
VID(int), UID(int), title(varchar), status(enum), type(varchar), is_duplicate(enum), is_adult(enum), channel_id(tinyint)

signup (~115440 rows)
UID(int), username(varchar)

videos_views (~359202 rows after 6 days of collecting data, so this table will grow rapidly)
videos_id(int), views_date(date), num_of_views(int)

The table video holds the videos, signup hodls users and videos_views holds data about video views (each video can have one row per day in that table).

I have this query that does the trick, but takes ~10s to execute, and I imagine this will only get worse over time as the videos_views table grows in size.

SELECT
 v.VID, 
 v.title, 
 v.vkey, 
 v.duration, 
 v.addtime, 
 v.UID, 
 v.viewnumber, 
 v.com_num, 
 v.rate, 
 v.THB, 
 s.username,
 SUM(vvt.num_of_views) AS tmp_num
FROM
 video v
  LEFT JOIN videos_views vvt ON v.VID = vvt.videos_id
  LEFT JOIN signup s on v.UID = s.UID
WHERE
 v.status = 'Converted'
 AND v.type = 'public'
 AND v.is_duplicate = '0'
 AND v.is_adult = '0'
 AND v.channel_id <> 10
 AND vvt.views_date >= '2001-05-11'
GROUP BY
 vvt.videos_id
ORDER BY
 tmp_num DESC
LIMIT
 8

All the relevant fields are indexed.
And here is a screenshot of the EXPLAIN result:
alt text

So, how can I optimize this?

UPDATE
This is my query based on the answer by Quassnoi. It returns the correct videos, but it messes up the JOIN on the signup table. For some records the username field is NULL, for others it contains the wrong username.

SELECT
    v.VID,
    v.title,
    v.vkey,
    v.duration,
    v.addtime,
    v.UID,
    v.viewnumber,
    v.com_num,
    v.rate,
    v.THB,
    s.username
FROM
    (SELECT
        videos_id,
        SUM(num_of_views) AS tmp_num
    FROM
        videos_views
    WHERE
        views_date >= '2010-05-13'
    GROUP BY
        videos_id
    ) q
        JOIN video v ON v.VID = q.videos_id
        LEFT JOIN signup s ON s.UID = v.VID
WHERE
    v.type = 'public'
    AND v.channel_id <> 10
    AND v.is_adult = '0'
    AND is_duplicate = '0'
ORDER BY
    tmp_num DESC
LIMIT
    8

Here is the resultset:
alt text

  • 1 1 Answer
  • 1 View
  • 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-14T21:32:33+00:00Added an answer on May 14, 2026 at 9:32 pm

    Create the following index:

    video_views (views_date, videos_id)
    

    , and get rid of the LEFT JOIN between videos and views (it does not work with your current query, anyway):

    SELECT  *
    FROM    (
            SELECT  videos_id, SUM(num_of_views) AS tmp_num
            FROM    video_views
            GROUP BY
                    videos_id
            ) q
    JOIN    videos v
    ON      v.vid = q.videos_id
    LEFT JOIN
            signup s
    ON      s.UID = v.UID
    ORDER BY
            tmp_num DESC
    LIMIT 8
    

    If you want to return zero for videos that had never been viewed, change the order of fields in the index:

    video_views (videos_id, views_date)
    

    and rewrite the query:

    SELECT  *,
            (
            SELECT  COALESCE(SUM(num_of_views), 0)
            FROM    video_views vw
            WHERE   vw.videos_id = v.vid
                    AND views_date >= '2001-05-11'
            ) AS tmp_num
    FROM    videos v
    LEFT JOIN
            signup s
    ON      s.UID = v.UID
    ORDER BY
            tmp_num DESC
    LIMIT 8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Youtube like web-page where users upload&watch videos. I would like to
I have a web page on which users can upload text files (but a
I have an ASP.NET 4.0 web application where users upload videos to the server.
I have a web page where users can see data from MongoDB on a
I have a members site where users are given up to 7 web page
I have a requirement to allow users to open word document from web page
I have a pretty straight forward uploadified page that allows users to upload files,
I have written a web application that involves having users upload pictures to the
I have a web-app that allows users to upload and download image files by
We have created a web application, using ASP.NET, that allows users to upload documents

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.