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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:08:16+00:00 2026-05-24T10:08:16+00:00

As a follow up on my previous question here: Link These are my tables:

  • 0

As a follow up on my previous question here: Link

These are my tables:

-----------------------------------
ID | ChapterNo | HitCount |  MID
-----------------------------------
1  |    2      |   1000   |   1
2  |    2      |   2000   |   1
3  |    1      |   3000   |   1
4  |    3      |   1000   |   1
5  |    1      |   3500   |   1
-----------------------------------

to archive the result, i tried to use the ff query:

SELECT t1.id, t1.hitcount, t1.chapterno
    FROM chapter as t1
    WHERE t1.hitcount = (select max(hitcount) from chapter where chapterno = t1.chapterno and `mid` = t1.`mid`)
    AND t1.`mid` = '2524'
    ORDER BY t1.chapterno DESC

ID | ChapterNo | HitCount |  
---------------------------
4  |    3      |   1000   |
2  |    2      |   2000   |
5  |    1      |   3500   |
---------------------------

This query seem to works really fine at first, but in scale, after I import 80,000 records for testing and implementing. I find out that this ran for 30second. A explain shows:

sel_type  table      type   posible_key  key       keyLen   ref        rows        Extra
PRIMARY   t1         ref    mid_idx      mid_idx    8       const      *3289*    Using where; Using filesort
PRIMARY   chapter    ref    mid_idx      mid_idx    8       m.t1.mid   *17*      Using where; Using temporary; Using filesort

The result set is 640 rows. Is there any real good way to optimize this for larger table? Since this table and especialy this query will grow more in the future.

Will using procedure in mysql helps any for this query?

Thank you very much

  • 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-24T10:08:17+00:00Added an answer on May 24, 2026 at 10:08 am

    Try this:

    SELECT a.id, X.chapterno, X.mid, X.hitcount
    FROM 
    (select chapterno,  max(hitcount) as hitcount 
    from chapter    
    WHERE mid = 2524
    group by chapterno)X
    INNER JOIN chapter a ON (a.chapterno = X.chapterno AND a.mid=X.mid)
    ORDER BY X.chapterno DESC
    

    This query will benefit from index on(chapterno,hitcount). Also, from your data(many records with same value of MID) and EXPLAIN output, it seems that you don’t need an index on mid (I believe mid_idx is an index on mid) because it’s not selective enough…

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

Sidebar

Related Questions

In a follow-up to a previous question, let's say I have 3 tables, A,
All, this is a follow up from a previous question here: C# formatting external
This is a follow-up to my previous question (found here ). My properties files
This is a follow up question from my previous one found here I need
This is basically a follow up question to my previous question found here I'm
Here's a follow up to a previous question. My code below animates a square
to follow my previous question: XDoclet, a dead tool? what tool can I use
This question is a follow up with a previous question Previous Question The previous
This is a follow up question to a previous question I asked about calculating
This is a follow up from my previous question I have this code basically

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.