【问题标题】:Speed up database query with function用函数加速数据库查询
【发布时间】:2014-01-19 01:03:09
【问题描述】:

你好我的数据库查询很慢。在 html 表中列出 4000 行需要 5 分钟...请帮助。

         **$query = "SELECT * FROM tblRazduzeniUgovori WHERE Status='razduzen'";**
         if ($result = mysqli_query($con, $query)) {
             while($row = mysqli_fetch_array($result))
             {
               echo "<tr>";
               echo "<td><input type='checkbox' name='brojUgovora[]' value='" . $row['Broj'] . "'/></td>";

谢谢。

【问题讨论】:

  • 考虑为相关表提供 SHOW CREATE 语句并为查询提供 EXPLAIN
  • 是查询速度慢,还是生成和显示 HTML 中的 4000 行速度慢?
  • 在 HTML 中生成和显示 4000 行很慢
  • 一个好的开始是将'SELECT *'编辑为更具体的查询

标签: php mysql performance function


【解决方案1】:

首先:将状态添加为数据库表的索引,这将加快脚本的选择部分。

第二种:将字符串拼接起来,而不是每次都发出一个echo语句,echo * 4000会很慢。

$query = "SELECT * FROM tblRazduzeniUgovori WHERE Status='razduzen'";
     $output = '';
     if ($result = mysqli_query($con, $query)) {
         while($row = mysqli_fetch_array($result))
         {
           $output .= "<tr>";
           $output .= "<td><input type='checkbox' name='brojUgovora[]' value='" . $row['Broj'] . "'/></td>";
         }
      }
      echo $output;

【讨论】:

    【解决方案2】:

    这可能不是你要找的,但我建议不要显示这么多行,你可以限制:)

    <?php
    $displayed_items = 100; // items displayed per page
    $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
    $end = $start + $displayed_items;
    $query = "SELECT * FROM tblRazduzeniUgovori 
              WHERE Status='razduzen' LIMIT {$start}, {$end}";
    ?>
    <table>
    <?php
    if ($result = mysqli_query($con, $query)) {
          while($row = mysqli_fetch_array($result)): ?>
               <tr>
               <?php foreach ($a as $row):?>
                   <td>
                      <?php echo $a ?>
                   </td>
               <?php endforeach; ?>
               </tr>
    <?php endwhile; ?>
    </table>
    <form action='#' method='get'>
         <input type='hidden' name='start' value='<?php echo $start + $displayed_items ?>' />
         <input type='submit' value='next page' />
    </form>
    

    这样您只需 单击下一步即可看到结果,不要忘记将 ORDER BY 添加到查询中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      相关资源
      最近更新 更多