Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Thanks for the suggestions. Here's what I ended up doing:</p> <pre><code>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 &lt;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; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload