我有一个查询,每次用户打开他的个人资料页面时都会加载。它真的很慢。加载页面需要 20 多秒。这是一个简单的查询,但有很多行,所以不要害怕看它。:)
我会在改进我的查询方面获得任何帮助。
SELECT
`h`.`login` AS `login`,
SUM(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`pips`,NULL)) AS `total_pips`,
COUNT(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`position_num`,NULL)) AS `total_trades`,
(COUNT(IF(((`h`.`pl` > 0) AND ((`h`.`cmd` = 0) OR (`h`.`cmd` = 1))),`h`.`pl`,NULL)) / COUNT(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`position_num`,NULL))) AS `winning_trades_percent`,
SUM(if(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`gain`,NULL)) AS `total_gain`,
(SELECT AVG(`wg`.`weekly_gain_all`) FROM `gt_view_weekly_gain` `wg` where (`wg`.`login` = `h`.`login`) group by `wg`.`login`) AS `weekly_gain`,
AVG(IF(((`h`.`pips` > 0) AND ((`h`.`cmd` = 0) OR (`h`.`cmd` = 1))),`h`.`pips`,NULL)) AS `average_profit_pips`,
AVG(IF(((`h`.`pips` <= 0) AND ((`h`.`cmd` = 0) OR (`h`.`cmd` = 1))),`h`.`pips`,NULL)) AS `average_lose_pips`,
AVG(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`pips`,NULL)) AS `average_pips`,
AVG(time_to_sec(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),timediff(`h`.`close_time`,`h`.`open_time`),NULL))) AS `average_trade_time`,
STD(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`pips`,NULL)) AS `volatility`,
MAX(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`pips`,NULL)) AS `best_trade`,
MIN(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`pips`,NULL)) AS `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) AS `running_weeks`,
(SELECT `fn`.`followers_num_all` FROM `gt_view_followers_num` `fn` where ((`fn`.`guru_id` = `h`.`guru_or_guru_user_id`) AND (`h`.`is_guru_history` = 1))) AS `followers_num`,
SUM(IF(((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)),`h`.`pl`,NULL)) AS `total_profit_loss`,
SUM(IF(((`h`.`cmd` = 6) AND (`h`.`pl` > 0)),`h`.`pl`,NULL)) AS `deposits`,
SUM(IF(((`h`.`cmd` = 6) AND (`h`.`pl` < 0)),`h`.`pl`,NULL)) AS `withdrawls`,
COUNT(IF((((`h`.`cmd` = 0) OR (`h`.`cmd` = 1)) AND (`h`.`pl` > 0)),`h`.`position_num`,NULL)) AS `total_winning_trades_count`,
ABS((AVG(IF(((`h`.`pips` > 0) AND ((`h`.`cmd` = 0) OR (`h`.`cmd` = 1))),`h`.`pips`,NULL)) / AVG(IF(((`h`.`pips` <= 0) AND ((`h`.`cmd` = 0) OR (`h`.`cmd` = 1))),`h`.`pips`,NULL)))) AS `average_profit_loss_ratio`
FROM
`gt_history` `h`
WHERE
`h`.`is_closed` = 1
GROUP BY
`h`.`login`