duanhuang3074 2014-09-07 10:36
浏览 66
已采纳

优化几个MySQL(子)查询

I have some big problems to find the best way for getting one big Table with my users plays:

The functionality works this way: People can play two different games and earn playmoney with this. Additional, they have referred user, from which they also gain amounts to their balance. My current query takes forever and looks like this:

$sql = $mysqli->query("SELECT 
    n.id, n.account, n.email, n.lastpayout, n.referupnumber, 
    (SELECT sum(r.win) FROM rolls r WHERE r.konto = n.id AND r.zeit > n.lastpayout) as sumroll,
    (SELECT sum(m.gewinn) FROM multi m WHERE m.account = n.id AND m.zeit > n.lastpayout) as summulti,
    (SELECT count(nx.referupnumber) FROM nxt_account nx WHERE nx.referupnumber = n.id) as amountref
FROM
    nxt_account n");

$amountuser = $sql->num_rows;
$c = 1;  
while($row = $sql->fetch_array()) {

    $id_thistime = $row['id'];
    $sql_ref = $mysqli->query("SELECT 
    nn.id,
    (SELECT 
            sum(rr.win)
        FROM
            rolls rr
        WHERE
            rr.zeit >= nn.lastpayout AND rr.konto = n.id) as refamount
FROM
    nxt_account nn
WHERE
    nn.referupnumber = '".$id_thistime."'");
    $total_ref = 0;

while($row_ref = $sql_ref->fetch_array()) {

    $total_ref = $total_ref + $row_ref['refamount'];

}

$total_amount = $row['sumroll'] + $row['summulti'] + $total_ref;   }

Thank you very much if you can understand this and give me any hint how to optimize these querys.

  • 写回答

2条回答 默认 最新

  • duanbiyi7319 2014-09-07 11:13
    关注

    I suggest you change your main query to this and make all calculations from one database request:

        SELECT 
            n.id, n.account, n.email, n.lastpayout, n.referupnumber, 
            (SELECT sum(r.win) 
             FROM rolls r 
             WHERE r.konto = n.id AND r.zeit > n.lastpayout) as sumroll,
            (SELECT sum(m.gewinn) 
             FROM multi m 
             WHERE m.account = n.id AND m.zeit > n.lastpayout) as summulti,
            (SELECT count(nx.referupnumber) 
             FROM nxt_account nx 
             WHERE nx.referupnumber = n.id) as amountref,
            (SELECT sum(rr.win)
             FROM  rolls rr INNER JOIN nxt_account nn ON
                rr.zeit >= nn.lastpayout AND rr.konto = n.id
             WHERE nn.referupnumber =n.id) as refamount
        FROM
            nxt_account n
    

    The way it is done you have as many requests as there are records in the nxt_account table. This is probably the main reason why calculation is slow.

    If you just need one number of totals for all accounts, you should do that calculation in SQL also.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效