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

  • SEARCH
  • Home
  • 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 1107977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:00:47+00:00 2026-05-17T02:00:47+00:00

2 tables are still very small compared on how they will be in the

  • 0

2 tables are still very small compared on how they will be in the future.
Already on doing tests on some regular queries , performance problems arise.

Could someone enlighten me on a better table structure or query ?

====

Table 1 : tv_show ( currently 117 rows)

Table 2 : tv_episodes ( currently 43,000 rows ).

tv_show contains an id , which we call t_id on tv_episodes.

tv_episodes contains this t_id and a field called episodes_num.

each episode has an episode number and a season number , episodes_num is the total episode number regardless of the season.

for example : all seasons have 10 episodes: Season 2 episode 1 = episodes_num: 11

Now for the query :
I want for each tv show the latest episodes_num.

SELECT tv.*,ep.* FROM tv_show tv
INNER JOIN tv_episodes ep ON ( tv.id = ep.t_id ) 
LEFT OUTER JOIN tv_episodes ep2 
ON ( tv.id = ep2.t_id AND ep.episode_num < ep2.episode_num )
WHERE ep2.t_id is NULL

This works , but gives the server a very hard time ( query above 10 sec!)

Please help.

Edit : MySQL version: *5.1.47

Update : Thanks for al the solutions , this is the one which is the fastest.

    SELECT tv_episodes.* ,tv_show.*

    FROM tv_episodes
    INNER JOIN 
    (
        SELECT t_id, MAX(episode_num) AS episode_num

        FROM tv_episodes
        GROUP BY t_id
    ) max_eps
    ON tv_episodes.t_id = max_eps.t_id AND tv_episodes.episode_num = max_eps.episode_num
  INNER JOIN
 tv_show ON tv_show.id=tv_episodes.t_id



CREATE TABLE `tv_show` (
 `id` int(255) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `specification` varchar(50) NOT NULL,
 `year` int(4) NOT NULL,
 `status` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `name` (`name`),
 KEY `specification` (`specification`),
 KEY `year` (`year`),
 KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=118 DEFAULT CHARSET=latin1





CREATE TABLE `tv_episodes` (
 `id` int(255) NOT NULL AUTO_INCREMENT COMMENT 'e_id',
 `t_id` int(200) NOT NULL,
 `season` int(2) NOT NULL,
 `episode` int(4) NOT NULL,
 `episode_num` int(10) NOT NULL,
 `airing` date NOT NULL,
 `online` enum('1','2') NOT NULL COMMENT '1=yes 2=no',
 `added` int(15) NOT NULL,
 `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`id`),
 KEY `t_id` (`t_id`),
 KEY `season` (`season`),
 KEY `episode` (`episode`),
 KEY `season_num` (`episode_num`),
 KEY `airing` (`airing`),
 KEY `added` (`added`)
) ENGINE=MyISAM AUTO_INCREMENT=43420 DEFAULT CHARSET=latin1
  • 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-17T02:00:48+00:00Added an answer on May 17, 2026 at 2:00 am

    Update: Based on your latest comments and my own tweaking, I’ve edited my answer.

    This should return the tv_episode records for each tv_id having the maximum episodes_num

    SELECT tv_episodes.*
    FROM tv_episodes
    INNER JOIN 
    (
        SELECT t_id, MAX(episodes_num) AS episodes_num
        FROM tv_episodes
        GROUP BY t_id
    ) max_eps
    ON tv_episodes.t_id = max_eps.t_id AND tv_episodes.episodes_num = max_eps.episodes_num;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.