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

The Archive Base Latest Questions

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

I’m looking for a best practice advice how to speed up queries and at

  • 0

I’m looking for a best practice advice how to speed up queries and at the same time to minimize the overhead needed to invoke date/mktime functions. To trivialize the problem I’m dealing with the following table layout:

CREATE TABLE my_table(
  id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,   
  important_data INTEGER,
  date INTEGER);

The user can choose to show 1) all entries between two dates:

SELECT * FROM my_table 
  WHERE date >= ? AND date <= ? 
  ORDER BY date DESC;

Output:

10-21-2009 12:12:12, 10002
10-21-2009 14:12:12, 15002
10-22-2009 14:05:01, 20030
10-23-2009 15:23:35, 300
....  

I don’t think there is much to improve in this case.

2) Summarize/group the output by day, week, month, year:

SELECT COUNT(*) AS count, SUM(important_data) AS important_data
  FROM my_table 
  WHERE date >= ? AND date <= ? 
  ORDER BY date DESC;

Example output by month:

10-2009, 100002
11-2009, 200030
12-2009, 3000
01-2010, 0 /* <- very important to show empty dates, with no entries in the table! */
....  

To accomplish option 2) I’m currently running a very costly for-loop with mktime/date like the following:

for(...){ /* example for group by day */
  $span_from = (int)mktime(0, 0, 0, date("m", $time_min), date("d", $time_min)+$i, date("Y", $time_min));
  $span_to = (int)mktime(0, 0, 0, date("m", $time_min), date("d", $time_min)+$i+1, date("Y", $time_min)); 
  $query = "..";  
  $output = date("m-d-y", ..);
}

What are my ideas so far? Add additional/ redundant columns (INTEGER) for day (20091212), month (200912), week (200942) and year (2009). This way I can get rid of all the unnecessary queries in the for loop. However I’m still facing the problem to very fastly calculate all dates that doesn’t have any equivalent in database. One way to simply move the problem could be to let MySQL do the job and simply use one big query (calculate all the dates/use MySQL date functions) with a left join (the data). Would it be wise to let MySQL take the extra load? Anyway I’m reluctant to use all these mktime/date in the for loop. Since I have complete control over the table layout and code even suggestions with major changes are welcome!

Update

Thanks to Greg I came up with the following SQL query. However it still bugs me to use 50 lines of sql statements – build up with php – that maybe could be done faster and more elegantly otherwise:

SELECT * FROM (  
  SELECT DATE_ADD('2009-01-30', INTERVAL 0 DAY) AS day UNION ALL
  SELECT DATE_ADD('2009-01-30', INTERVAL 1 DAY) AS day UNION ALL
  SELECT DATE_ADD('2009-01-30', INTERVAL 2 DAY) AS day UNION ALL
  SELECT DATE_ADD('2009-01-30', INTERVAL 3 DAY) AS day UNION ALL
  ......
  SELECT DATE_ADD('2009-01-30', INTERVAL 50 DAY) AS day ) AS dates
LEFT JOIN (
    SELECT DATE_FORMAT(date, '%Y-%m-%d') AS date, SUM(data) AS data
    FROM test 
    GROUP BY date  
  ) AS results
ON DATE_FORMAT(dates.day, '%Y-%m-%d') = results.date;
  • 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-11T22:38:28+00:00Added an answer on May 11, 2026 at 10:38 pm

    You definitely shouldn’t be doing a query inside a loop.
    You can group like this:

    SELECT COUNT(*) AS count, SUM(important_data) AS important_data, DATE_FORMAT('%Y-%m', date) AS month
      FROM my_table 
      WHERE date BETWEEN ? AND ? -- This should be the min and max of the whole range
      GROUP BY  DATE_FORMAT('%Y-%m', date)
      ORDER BY date DESC;
    

    Then pull these into an array keyed by date and loop over your data range as you are doing (that loop should be pretty light on CPU).

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

Sidebar

Related Questions

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
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.