加载jQuery DataTable时,网页会崩溃 - 行太多



我正在在管理系统网站上工作,该网站由从服务器上运行的Postgres数据库中提取的数据表组成。我一直在通过编写PHP查询并回响数据库中的数据行来检索数据。这适用于我网站上的所有页面,除了试图从具有超过50,000个记录的表中检索数据的一个页面 - 当我尝试加载该页面时,它会段落或完全崩溃我的浏览器。使用PGADMIN从该表查看数据时,大约需要10-20秒。

有人知道为什么会发生这种情况,或者我如何可以加快数据表的负载时间?谢谢!

编辑

这是我使用PHP查询数据库的方式:

$query = "SELECT respondentid, pro, homephone, otherphone, fname, lname, note from respondent";
$result = pg_query($query);
echo "<table id='respondents'>";
echo "<thead> <tr> <th>Headings</th> </tr></thead>";
while($row = pg_fetch_array( $result )) {
  echo '<tr>';
  echo '<td>' . $row['columnnames'] . '</td>';
  echo "</tr>";
  echo "</table>";          

这是数据库列:

respondentid serial not null,
pro character varying(3),
homephone character varying(15),
otherphone character varying(15),
fname character varying(15),
lname character varying(20),
note text

您可以通过此快速解决方法从表中检索所有行:

  • while循环包装代码,
  • ORDER BY子句添加到查询,
  • 使用LIMIT子句仅选择几行。

echo "<table id='respondents'>";
echo "<thead> <tr> <th>Headings</th> </tr></thead>";
$rows_total_result = pg_query('SELECT COUNT(*) FROM respondent');
$rows_total_row = pg_fetch_row($rows_total_result);
$rows_total = $rows_total_row[0];
$offset = 0;
$row_count = 1000;
while (($offset + $row_count) <= $rows_total) {
    $query = "SELECT respondentid, pro, homephone, otherphone, fname, lname, note "
           . "from respondent ORDER BY respondentid "
           . "LIMIT {$row_count} OFFSET {$offset}";
    $result = pg_query($query);
    while ($row = pg_fetch_array($result)) {
        echo '<tr>';
        echo '<td>' . $row['columnnames'] . '</td>';
        echo "</tr>";
    }
    $offset += $row_count;
}
echo "</table>";

不过,这不会解决浏览器崩溃的问题,因为数据大小和可能长时间的执行时间。

常见的解决方案是实施分页以浏览大桌子。

  • 从基本到异国情调的五种方法

最新更新