Home > php教程 > php手册 > php|mysql|大数据量分页|BETWEEN AND , IN()

php|mysql|大数据量分页|BETWEEN AND , IN()

WBOY
Release: 2016-06-06 19:35:48
Original
1131 people have browsed it

之前遇到过一个日志记录表,记录数量超过200万时,普通的采用LIMIT,OFFSET的分页方法明显感觉比较慢,翻页大概需要至少10几秒,这肯定是不可取的。 那么当你的数据表数据量非常大的时候,并且具有完整性的auto_incrementID,那么就可以考虑使用BEETWEENAND或

之前遇到过一个日志记录表,记录数量超过200万时,普通的采用LIMIT , OFFSET的分页方法明显感觉比较慢,翻页大概需要至少10几秒,这肯定是不可取的。
那么当你的数据表数据量非常大的时候,并且具有完整性的auto_increment ID,那么就可以考虑使用BEETWEEN AND 或者是  IN(n1, n2, n3, n4, n5, n6, ......) PHP MySQL
<?php
header('Content-type:text/html; charset=utf-8');
$link = mysql_connect('localhost', 'root', '36936999');
mysql_select_db('test', $link);
function fetch($sql, &$link){
	$query = mysql_query($sql, $link);
	return mysql_fetch_array($query);	
}
function fetchs($sql, &$link){
	$query = mysql_query($sql, $link);
	while($row = mysql_fetch_array($query, MYSQL_ASSOC)){
		$record[] = $row;	
	}
	return $record;
}
//==========================================================================================================//
$stime = microtime(true);
//==========================================================================================================//

$table = 'sign';
$pageSize = 80;
$pageOffset = 100;

$page = (int)$_GET['page'];
$page = $page ? $page -1 : 0;

$row = fetch("SELECT COUNT(id) AS total FROM {$table}", $link);
$total = $row['total'];
$count = ceil($total/$pageSize);

echo '当前是页数:'.($page+1).'<br />';
echo '当前表格的数据总量为:'.$total.'<br />';
echo '设定的每页的记录数(步长)为:'.$pageSize.'<br />';
echo '那么则会有分页数量:'.$count.'<br />';
echo '设定的分页步长为:'.$pageOffset.'<br />';

//===========================================================================================================//

$start = $page * $pageSize;

$end = $start + $pageSize -1;

$sql = "SELECT id,uid,time,num,letter FROM sign WHERE id BETWEEN $start AND $end";
echo $sql.'<br />';

$result = fetchs($sql, $link);
echo '<div style="width:600px; height:200px; margin-top:30px; word-break:break-all; word-wrap:break-word;">';
foreach($result as $val){
	echo '<span>'.$val['id'].',</span>';
}
echo '</div>';

$page ++;

echo '<div style="width:600px; height:200px; margin-top:30px; word-break:break-all; word-wrap:break-word;">';
if($page >= 1 && $page < $count){
	for($i=1; $i<$pageOffset; $i++){
		echo '<a style="margin:3px;" href="page.php?page='.($page+$i).'">'.($page+$i).'</a>';
	}	
}
echo '</div>';

//===========================================================================================================//
$etime = microtime(true);

echo $stime.'<br>';
echo $etime.'<br>';
echo '页面花费时间为:'.round($etime - $stime, 3);
Copy after login
php|mysql|大数据量分页|BETWEEN AND , IN() php|mysql|大数据量分页|BETWEEN AND , IN()
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template