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

The Archive Base Latest Questions

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

I’ve got a very large table (~100Million Records) in MySQL that contains information about

  • 0

I’ve got a very large table (~100Million Records) in MySQL that contains information about files. One of the pieces of information is the modified date of each file.

I need to write a query that will count the number of files that fit into specified date ranges. To do that I made a small table that specifies these ranges (all in days) and looks like this:

DateRanges
range_id   range_name   range_start   range_end
1          0-90         0             90
2          91-180       91            180
3          181-365      181           365
4          366-1095     366           1095
5          1096+        1096          999999999

And wrote a query that looks like this:

SELECT r.range_name, sum(IF((DATEDIFF(CURDATE(),t.file_last_access) > r.range_start and DATEDIFF(CURDATE(),t.file_last_access) < r.range_end),1,0)) as FileCount
FROM `DateRanges` r, `HugeFileTable` t
GROUP BY r.range_name

However, quite predictably, this query takes forever to run. I think that is because I am asking MySQL to go through the HugeFileTable 5 times, each time performing the DATEDIFF() calculation on each file.

What I want to do instead is to go through the HugeFileTable record by record only once, and for each file increment the count in the appropriate range_name running total. I can’t figure out how to do that….

Can anyone help out with this?

Thanks.

EDIT: MySQL Version: 5.0.45, Tables are MyISAM

EDIT2: Here’s the descibe that was asked for in the comments

id  select_type  table  type  possible_keys  key  key_len  ref  rows      Extra  
1   SIMPLE       r      ALL   NULL           NULL NULL     NULL 5         Using temporary; Using filesort 
1   SIMPLE       t      ALL   NULL           NULL NULL     NULL 96506321   
  • 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-11T17:41:38+00:00Added an answer on May 11, 2026 at 5:41 pm

    First, create an index on HugeFileTable.file_last_access.

    Then try the following query:

    SELECT r.range_name, COUNT(t.file_last_access) as FileCount
    FROM `DateRanges` r
     JOIN `HugeFileTable` t 
     ON (t.file_last_access BETWEEN 
       CURDATE() + INTERVAL r.range_start DAY AND 
       CURDATE() + INTERVAL r.range_end DAY)
    GROUP BY r.range_name;
    

    Here’s the EXPLAIN plan that I got when I tried this query on MySQL 5.0.75 (edited down for brevity):

    +-------+-------+------------------+----------------------------------------------+
    | table | type  | key              | Extra                                        |
    +-------+-------+------------------+----------------------------------------------+
    | t     | index | file_last_access | Using index; Using temporary; Using filesort | 
    | r     | ALL   | NULL             | Using where                                  | 
    +-------+-------+------------------+----------------------------------------------+
    

    It’s still not going to perform very well. By using GROUP BY, the query incurs a temporary table, which may be expensive. Not much you can do about that.

    But at least this query eliminates the Cartesian product that you had in your original query.


    update: Here’s another query that uses a correlated subquery but I have eliminated the GROUP BY.

    SELECT r.range_name,
      (SELECT COUNT(*) 
       FROM `HugeFileTable` t 
       WHERE t.file_last_access BETWEEN 
         CURDATE() - INTERVAL r.range_end DAY AND 
         CURDATE() - INTERVAL r.range_start DAY
      ) as FileCount
    FROM `DateRanges` r;
    

    The EXPLAIN plan shows no temporary table or filesort (at least with the trivial amount of rows I have in my test tables):

    +----+--------------------+-------+-------+------------------+--------------------------+
    | id | select_type        | table | type  | key              | Extra                    |
    +----+--------------------+-------+-------+------------------+--------------------------+
    |  1 | PRIMARY            | r     | ALL   | NULL             |                          | 
    |  2 | DEPENDENT SUBQUERY | t     | index | file_last_access | Using where; Using index | 
    +----+--------------------+-------+-------+------------------+--------------------------+
    

    Try this query on your data set and see if it performs better.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I've tracked down a weird MySQL problem to the two different ways I was

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.