optimization - Mysql - Help for optimizing query -


i have query:

select bi.id,         bi.location,         bi.expense_group,         bi.level,         bi.is_active,         bi.type,         full_name,         ( bl.bud_amount )                                     budgetamount,         ( coalesce(( ( bl.bud_amount * 3 ) - (                      + bal.bal_amount1 + bal.bal_amount2                      + bal.bal_amount3 ) ), 0) ) difference,         ( coalesce(round(( + bal.bal_amount1 + bal.bal_amount2                            + bal.bal_amount3 ) / 3), 0) )     average,         bal.bal_amount1                                       bal1,         bal.bal_amount2                                       bal2,         bal.bal_amount3                                       bal3    (select *            budget_items bi           bi.location not null) bi         left join (select budget_item_id,                           sum(case                                 when budget_id = 21491 amount                               end) bud_amount                      budget_lines                    group  budget_item_id) bl                on bl.budget_item_id = bi.id         join (select budget_item_id,                      ifnull(sum(case                                   when balance_id = 12841 amount                                 end), 0) bal_amount1,                      ifnull(sum(case                                   when balance_id = 18647 amount                                 end), 0) bal_amount2,                      ifnull(sum(case                                   when balance_id = 18674 amount                                 end), 0) bal_amount3                 balance_lines               group  budget_item_id) bal           on bal.budget_item_id = bi.id  order  bi.location  

it takes lot of time. in budget_lines , balance_lines tables have more 5,000,000 rows in each.

i attach explain of query, you'll ne able see problem. ids in every table indexed. there column if indexed spped query? or maybe need change it.

*** left join necessary because need items nudget_items, if don't exist in balance/budget_line table.

schema is: every budget has budget_lines. every balance has balance_lines. query aimed have 1 table summarize differences between budget , several balances.

enter image description here

you can see bigger image here: http://i.stack.imgur.com/dlf8v.png

edit: after @sebas answers: enter image description here

for @sabes hunger, put here describe: budget_items enter image description here

budget_lines enter image description here

balance_lines enter image description here

maybe this; without sample data, , indexes it's difficult see

select *    budget_items bi   bi.location not null) bi  inner join  --added inner clarity  -changed order inner's before outers.   (select budget_item_id, sum(case when balance_id = 12841 coalesce(amount,0) end), 0) bal_amount1,                            sum(case when balance_id = 18647 coalesce(amount,0) end), 0) bal_amount2,                            sum(case when balance_id = 18674 coalesce(amount,0) end), 0) bal_amount3       balance_lines     balance_id in (12841, 18647, 18674) --this way balance_ids aren't in list don't evaluated    group  budget_item_id) bal   on bal.budget_item_id = bi.id  left join   (select budget_item_id, sum(case when budget_id = 21491 coalesce(amount,0) end) bud_amount      budget_lines    budget_id = 21491 --again since care budget_id 21491, can limit results    group  budget_item_id) bl   on bl.budget_item_id = bi.id  

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -