如何优化少于运算符的查询?

时间:2020-08-26 10:49:04

标签: mysql

我正在使用1百万条记录来运行此查询,但是即使200秒后也无法给出结果,因此我杀死了该查询。下面是查询

SELECT truncate((SELECT coalesce(sum(credit-debit),0) FROM trans_laga where uid = a.uid
    AND wallet_type = a.wallet_type AND status = 1 and freeze=0 and id<=a.id), 4)
    FROM `trans_laga` AS `a`
 WHERE a.status =1 AND a.wallet_type != 4 ORDER BY `a`.`id` desc
LIMIT 0, 50000;

下面是解释输出

enter image description here

为什么这个查询运行太慢,甚至没有给出结果?

1 个答案:

答案 0 :(得分:0)

m3

这应该写为:

<!DOCTYPE html>
<html>
<head>
  <title>Registration system PHP and MySQL</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="header">
    <h2>Register</h2>
  </div>
  <?php
  $server="localhost";
  $userid ="root";
  $Password = "";
  $myDB = "project";

  $con = mysqli_connect($server,$userid,$Password,$myDB);

  // Check connection
  if (!$con) {
      die("Connection failed: ".mysqli_connect_error());
  } else {
      echo "Connected successfully";
  }
  ?>
  <form method="POST" action="test.php">
    Name:<br>
    <input type="text" name="name"><br><br>
    Department:<br>
    <label for="department">Choose a department:</label>
    <select>
      <option>Select</option>
  <?php 

  $sqlq = "SELECT * FROM users";
  $result = mysqli_query($con, $sqlq);
  while ($row = mysqli_fetch_array($result)){
        echo '<option>'.$row['department'].'</option>';
  }
  ?>
  </select>
  <br><br>
  Email:<br>
  <input type="text" name="email"><br><br>
  <button type="submit" name="register_btn">Register</button>
</div>
<p>Already a member? Sign in</p>
  </form>
  <?php

  if(isset($_POST['register_btn'])){  
    $name = $_POST['name'];
    $department= $_POST['Department'];
    $email = $_POST['email'];
    
    $query="INSERT INTO `users`
                            (`id`,`name`,`department`, `email`, `password`) 
                    VALUES ('','$name','$department','$email','abcd')";
    
    if(mysqli_query($con, $query)){
        echo "Records inserted successfully.";
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
    }
}
?>
</body>
</html>

以上将不会使您的查询更快。为此,您必须查看说明计划中具有key = null

的两行