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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:41:37+00:00 2026-05-24T09:41:37+00:00

First off, I’ve looked at several other questions about optimizing sql queries, but I’m

  • 0

First off, I’ve looked at several other questions about optimizing sql queries, but I’m still unclear for my situation what is causing my problem. I read a few articles on the topic as well and have tried implementing a couple possible solutions, as I’ll describe below, but nothing has yet worked or even made an appreciable dent in the problem.

The application is a nutrition tracking system – users enter the foods they eat and based on an imported USDA database the application breaks down the foods to the individual nutrients and gives the user a breakdown of the nutrient quantities on a (for now) daily basis.

here’s
A PDF of the abbreviated database schema
and here it is as a (perhaps poor quality) JPG. I made this in open office – if there are suggestions for better ways to visualize a database, I’m open to suggestions on that front as well! The blue tables are directly from the USDA, and the green and black tables are ones I’ve made. I’ve omitted a lot of data in order to not clutter things up unnecessarily.

Here’s the query I’m trying to run that takes a very long time:

SELECT listing.date_time,listing.nutrdesc,data.total_nutr_mass,listing.units 
FROM 
    (SELECT nutrdesc, nutr_no, date_time, units 
    FROM meals, nutr_def 
    WHERE meals.users_userid = '2' 
        AND date_time BETWEEN '2009-8-12' AND '2009-9-12' 
        AND (nutr_no <100000 
            OR nutr_no IN 
            (SELECT nutr_def_nutr_no 
            FROM nutr_rights 
            WHERE nutr_rights.users_userid = '2'))
    ) as listing
LEFT JOIN 
(SELECT nutrdesc, date_time, nut_data.nutr_no, sum(ingred_gram_mass*entry_qty_num*nutr_val/100) AS total_nutr_mass 
FROM nut_data, recipe_ingredients, food_entries, meals, nutr_def 
WHERE nut_data.nutr_no = nutr_def.nutr_no 
    AND ndb_no = ingred_ndb_no 
    AND foods_food_id = entry_ident 
    AND meals_meal_id = meal_id 
    AND users_userid = '2' 
    AND date_time BETWEEN '2009-8-12' AND '2009-9-12' 
GROUP BY date_time,nut_data.nutr_no ) as data 
ON data.date_time = listing.date_time 
AND listing.nutr_no = data.nutr_no 
ORDER BY listing.date_time,listing.nutrdesc,listing.units 

So I know that’s rather complex – The first select gets a listing of all the nutrients that the user consumed within the given date range, and the second fills in all the quantities.

When I implement them separately, the first query is really fast, but the second is slow and gets very slow when the date ranges get large. The join makes the whole thing ridiculously slow. I know that the ‘main’ problem is the join between these two derived tables, and I can get rid of that and do the join by hand basically in php much faster, but I’m not convinced that’s the whole story.

For example: for 1 month of data, the query takes about 8 seconds, which is slow, but not completely terrible. Separately, each query takes ~.01 and ~2 seconds respectively. 2 seconds still seems high to me.

If I try to retrieve a year’s worth of data, it takes several (>10) minutes to run the whole query, which is problematic – the client-server connection sometimes times out, and in any case we don’t want I don’t want to sit there with a spinning ‘please wait’ icon. Mainly, I feel like there’s a problem because it takes more than 12x as long to retrieve 12x more information, when it should take less than 12x as long, if I were doing things right.

Here’s the ‘explain’ for each of the slow queries: (the whole thing, and just the second half).

Whole thing:

+----+--------------------+--------------------+----------------+-------------------------------+------------------+---------+-----------------------------------------------------------------------+------+----------------------------------------------+
| id | select_type        | table              | type           | possible_keys                 | key              | key_len | ref                                                                   | rows | Extra                                        |
+----+--------------------+--------------------+----------------+-------------------------------+------------------+---------+-----------------------------------------------------------------------+------+----------------------------------------------+
|  1 | PRIMARY            | <derived2>         | ALL            | NULL                          | NULL             | NULL    | NULL                                                                  | 5053 | Using temporary; Using filesort              | 
|  1 | PRIMARY            | <derived4>         | ALL            | NULL                          | NULL             | NULL    | NULL                                                                  | 4341 |                                              | 
|  4 | DERIVED            | meals              | range          | PRIMARY,day_ind               | day_ind          | 9       | NULL                                                                  |   30 | Using where; Using temporary; Using filesort | 
|  4 | DERIVED            | food_entries       | ref            | meals_meal_id                 | meals_meal_id    | 5       | nutrition.meals.meal_id                                               |   15 | Using where                                  | 
|  4 | DERIVED            | recipe_ingredients | ref            | foods_food_id,ingred_ndb_no   | foods_food_id    | 4       | nutrition.food_entries.entry_ident                                    |    2 |                                              | 
|  4 | DERIVED            | nutr_def           | ALL            | PRIMARY                       | NULL             | NULL    | NULL                                                                  |  174 |                                              | 
|  4 | DERIVED            | nut_data           | ref            | PRIMARY                       | PRIMARY          | 36      | nutrition.nutr_def.nutr_no,nutrition.recipe_ingredients.ingred_ndb_no |    1 |                                              | 
|  2 | DERIVED            | meals              | range          | day_ind                       | day_ind          | 9       | NULL                                                                  |   30 | Using where                                  | 
|  2 | DERIVED            | nutr_def           | ALL            | PRIMARY                       | NULL             | NULL    | NULL                                                                  |  174 | Using where                                  | 
|  3 | DEPENDENT SUBQUERY | nutr_rights        | index_subquery | users_userid,nutr_def_nutr_no | nutr_def_nutr_no | 19      | func                                                                  |    1 | Using index; Using where                     | 
+----+--------------------+--------------------+----------------+-------------------------------+------------------+---------+-----------------------------------------------------------------------+------+----------------------------------------------+
10 rows in set (2.82 sec)

Second chunk (data):

+----+-------------+--------------------+-------+-----------------------------+---------------+---------+-----------------------------------------------------------------------+------+----------------------------------------------+
| id | select_type | table              | type  | possible_keys               | key           | key_len | ref                                                                   | rows | Extra                                        |
+----+-------------+--------------------+-------+-----------------------------+---------------+---------+-----------------------------------------------------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | meals              | range | PRIMARY,day_ind             | day_ind       | 9       | NULL                                                                  |   30 | Using where; Using temporary; Using filesort | 
|  1 | SIMPLE      | food_entries       | ref   | meals_meal_id               | meals_meal_id | 5       | nutrition.meals.meal_id                                               |   15 | Using where                                  | 
|  1 | SIMPLE      | recipe_ingredients | ref   | foods_food_id,ingred_ndb_no | foods_food_id | 4       | nutrition.food_entries.entry_ident                                    |    2 |                                              | 
|  1 | SIMPLE      | nutr_def           | ALL   | PRIMARY                     | NULL          | NULL    | NULL                                                                  |  174 |                                              | 
|  1 | SIMPLE      | nut_data           | ref   | PRIMARY                     | PRIMARY       | 36      | nutrition.nutr_def.nutr_no,nutrition.recipe_ingredients.ingred_ndb_no |    1 |                                              | 
+----+-------------+--------------------+-------+-----------------------------+---------------+---------+-----------------------------------------------------------------------+------+----------------------------------------------+
5 rows in set (0.00 sec)

I’ve ‘analyzed’ all the tables involved in the query, and added an index on the datetime field that is joining meals and food entries. I called it ‘day_ind’. I hoped that would accelerate things, but it didn’t seem to make a difference. I also tried removing the ‘sum’ function, as I understand that having a function in the query will frequently mean a full table scan, which is obviously much slower. Unfortunately removing the ‘sum’ didn’t seem to make a difference either (well, about 3-5% or so, but not the order magnitude that I’m looking for).

I would love any suggestions and will be happy to provide any more information you need to help diagnose and improve this problem. Thanks in advance!

  • 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-24T09:41:38+00:00Added an answer on May 24, 2026 at 9:41 am

    Okay, so I eventually figured out what I’m gonna end up doing. I couldn’t make the ‘data’ query any faster – that’s still the bottleneck. But now I’ve made it so the total query process is pretty close to linear, not exponential.
    I split the query into two parts and made each one into a temporary table. Then I added an index for each of those temp tables and did the join separately afterwards. This made the total execution time for 1 month of data drop from 8 to 2 seconds, and for 1 year of data from ~10 minutes to ~30 seconds. Good enough for now, I think. I can work with that.

    Thanks for the suggestions. Here’s what I ended up doing:

    create table listing (
        SELECT nutrdesc, nutr_no, date_time, units 
        FROM meals, nutr_def 
        WHERE meals.users_userid = '2' 
            AND date_time BETWEEN '2009-8-12' AND '2009-9-12' 
            AND (
                nutr_no <100000 OR nutr_no IN (
                    SELECT nutr_def_nutr_no 
                    FROM nutr_rights 
                    WHERE nutr_rights.users_userid = '2'
                    )
                )
        );
    
    create table data (
        SELECT nutrdesc, date_time, nut_data.nutr_no, sum(ingred_gram_mass*entry_qty_num*nutr_val/100) AS total_nutr_mass 
        FROM nut_data, recipe_ingredients, food_entries, meals, nutr_def 
        WHERE nut_data.nutr_no = nutr_def.nutr_no 
            AND ndb_no = ingred_ndb_no 
            AND foods_food_id = entry_ident 
            AND meals_meal_id = meal_id 
            AND users_userid = '2' 
            AND date_time BETWEEN '2009-8-12' AND '2009-9-12' 
        GROUP BY date_time,nut_data.nutr_no
    );
    
    create index joiner on data(nutr_no, date_time);
    create index joiner on listing(nutr_no, date_time);
    
    SELECT listing.date_time,listing.nutrdesc,data.total_nutr_mass,listing.units
    FROM listing
    LEFT JOIN data
    ON data.date_time = listing.date_time
    AND listing.nutr_no = data.nutr_no
    ORDER BY listing.date_time,listing.nutrdesc,listing.units;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm a beginner in mobile development. I've seen many similar questions but
First off, I know next to nothing about language theory, and I barely know
first off this is a class assignment so i would appreciate help but just
First off, please forgive the stupidness of this question but Im not from a
First off: Everything works , but I would like to fine tune this a
First off I've been working with Java's Concurrency package quite a bit lately but
First off, I think I know what's going on, but I thought I'd bring
First off, I'm not all that familiar with cookies but I know how they
First off, I'll admit that I'm anal about such things. (Bad, bad, me.) However,
First off, please forgive me for my lack of understanding... I'm still learning :)

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.