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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:10:42+00:00 2026-06-18T05:10:42+00:00

I’m looking to speed up this query somehow. Possibly to the point where its

  • 0

I’m looking to speed up this query somehow. Possibly to the point where its acceptable to run it on a low traffic site. It takes 15+ seconds which is way too long.

Currently there are 13k rows in the table, which is approx 1 months data, however i’m expecting that monthly data to double once the site is in production. The aim is to select the top 10 gains for the last week.

there are two tables, users table and the tracker table, in the tracker table there are multiple rows for each user, at least 8 per day, per user.

The aim of the query is to take the latest row for each user, subtract from that a value from a row inserted 1 week ago to get the amount of xp they have gained and select the top 10 highest gainers.

Table schema (which im sure could be improved too)

users table

id int(11) primary
rsn varchar(12) unique
joined timestamp
disabled bool

tracker table

id int(11) primary
user_id int(11) index /* Associated with the id in the users table */
timestamp timestamp
overall_rank int(11)
overall_level int(4)
overall_exp int(10)

And the query.

SELECT  `users`.`rsn` ,  `tracker`.`timestamp` , @uid :=  `tracker`.`user_id` , (
    SELECT  `overall_exp` 
    FROM  `tracker` 
    WHERE  `user_id` = @uid 
    ORDER BY  `timestamp` DESC 
    LIMIT 1
) - (
    SELECT  `overall_exp` 
    FROM  `tracker` 
    WHERE  `timestamp` >= SUBDATE( NOW( ) , INTERVAL 1 WEEK ) 
    AND  `user_id` = @uid 
    ORDER BY  `timestamp` ASC 
    LIMIT 1 ) AS  `gained_exp` 
FROM  `tracker` 
JOIN  `users` ON  `tracker`.`user_id` =  `users`.`id` 
ORDER BY  `gained_exp` DESC 
LIMIT 10

Explain output

+----+----------------------+---------+-------+---------------+---------+---------+------------------+-------+----------------------------------------------+
| id | select_type          | table   | type  | possible_keys | key     | key_len | ref              | rows  | Extra                                        |
+----+----------------------+---------+-------+---------------+---------+---------+------------------+-------+----------------------------------------------+
|  1 | PRIMARY              | users   | index | PRIMARY       | rsn     | 14      | NULL             |    71 | Using index; Using temporary; Using filesort |
|  1 | PRIMARY              | tracker | ref   | user_id       | user_id | 4       | surreal.users.id |   103 |                                              |
|  3 | UNCACHEABLE SUBQUERY | tracker | ALL   | NULL          | NULL    | NULL    | NULL             | 11752 | Using where; Using filesort                  |
|  2 | UNCACHEABLE SUBQUERY | tracker | ALL   | NULL          | NULL    | NULL    | NULL             | 11752 | Using where; Using filesort                  |
+----+----------------------+---------+-------+---------------+---------+---------+------------------+-------+----------------------------------------------+
  • 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-06-18T05:10:43+00:00Added an answer on June 18, 2026 at 5:10 am

    Try avoiding the correlated subqueries by first finding the timestamps for today and 1 week ago for all users, then joining twice again to tracker to find the corresponding values of overall_exp before doing the calculation:

    SELECT rsn, ts.thisweek, ts.user_id,
           last.overall_exp - previous.overall_exp AS gained_exp
    FROM (SELECT user_id, MIN(timestamp) AS lastweek, MAX(timestamp) AS thisweek
          FROM tracker
          WHERE timestamp >= SUBDATE(NOW(), INTERVAL 1 WEEK)
          GROUP BY user_id) AS ts
    INNER JOIN tracker previous
    ON previous.user_id = ts.user_id AND previous.timestamp = ts.lastweek
    INNER JOIN tracker last
    ON last.user_id = ts.user_id AND last.timestamp = ts.thisweek
    JOIN users ON ts.user_id = users.id
    ORDER BY gained_exp DESC
    LIMIT 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.