php - Improving query run time (Takes more than 20seconds to load a page) -
i've got query loaded everytime user open profile page. , slow. takes more 20 seconds load page. kinda simple query, alot of lines don't scared looking @ it. :)
i appericiate on improving query.
select `h`.`login` `login`, sum(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`pips`,null)) `total_pips`, count(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`position_num`,null)) `total_trades`, (count(if(((`h`.`pl` > 0) , ((`h`.`cmd` = 0) or (`h`.`cmd` = 1))),`h`.`pl`,null)) / count(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`position_num`,null))) `winning_trades_percent`, sum(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`gain`,null)) `total_gain`, (select avg(`wg`.`weekly_gain_all`) `gt_view_weekly_gain` `wg` (`wg`.`login` = `h`.`login`) group `wg`.`login`) `weekly_gain`, avg(if(((`h`.`pips` > 0) , ((`h`.`cmd` = 0) or (`h`.`cmd` = 1))),`h`.`pips`,null)) `average_profit_pips`, avg(if(((`h`.`pips` <= 0) , ((`h`.`cmd` = 0) or (`h`.`cmd` = 1))),`h`.`pips`,null)) `average_lose_pips`, avg(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`pips`,null)) `average_pips`, avg(time_to_sec(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),timediff(`h`.`close_time`,`h`.`open_time`),null))) `average_trade_time`, std(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`pips`,null)) `volatility`, max(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`pips`,null)) `best_trade`, min(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`pips`,null)) `worst_trade`, (ceiling(((ceiling(to_days(now())) - ceiling(to_days(min(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`open_time`,null))))) / 7)) - 1) `running_weeks`, (select `fn`.`followers_num_all` `gt_view_followers_num` `fn` ((`fn`.`guru_id` = `h`.`guru_or_guru_user_id`) , (`h`.`is_guru_history` = 1))) `followers_num`, sum(if(((`h`.`cmd` = 0) or (`h`.`cmd` = 1)),`h`.`pl`,null)) `total_profit_loss`, sum(if(((`h`.`cmd` = 6) , (`h`.`pl` > 0)),`h`.`pl`,null)) `deposits`, sum(if(((`h`.`cmd` = 6) , (`h`.`pl` < 0)),`h`.`pl`,null)) `withdrawls`, count(if((((`h`.`cmd` = 0) or (`h`.`cmd` = 1)) , (`h`.`pl` > 0)),`h`.`position_num`,null)) `total_winning_trades_count`, abs((avg(if(((`h`.`pips` > 0) , ((`h`.`cmd` = 0) or (`h`.`cmd` = 1))),`h`.`pips`,null)) / avg(if(((`h`.`pips` <= 0) , ((`h`.`cmd` = 0) or (`h`.`cmd` = 1))),`h`.`pips`,null)))) `average_profit_loss_ratio` `gt_history` `h` `h`.`is_closed` = 1 group `h`.`login`
it seems query computes values users. if wish display 1 user, might faster add and (login='xxx')
in clause.
otherwise, mihai suggested, caching might best approach.
Comments
Post a Comment