Features:
The calling efficiency is extremely high. It only takes 500 milliseconds to call the test randomly with 790,000 data.
The call can be made to a specified column or to the entire table.
Easy to install, just one function
PHP code:
function ecms_rand($classid,$num){
global $empire,$dbtbpre;
$file_cache = ECMS_PATH."d/news.json"; //缓存文件
//缓存整个news表的数据
if(!file_exists($file_cache)){
//查询分类
$hm_class=$empire->query("select classid from {$dbtbpre}enewsclass");
while($hm_r=$empire->fetch($hm_class))
{
//查询分类下面的所有id
$hm_news=$empire->query("SELECT id FROM `{$dbtbpre}ecms_news` WHERE `classid` = {$hm_r[classid]}");
while($hm_nr=$empire->fetch($hm_news))
{
$news_data[$hm_r['classid']][] = $hm_nr[id];
}
}
file_put_contents($file_cache,json_encode($news_data));
}
//获取缓存数据文件
$file_arrs = json_decode(file_get_contents($file_cache),true);
if($classid=="0"){
$num = ceil($num/count($file_arrs));
foreach($file_arrs as $k=>$v)
{
if(count($v) <= $num){ continue;}
//根据数据表获取id,每个栏目随机分配文章id
$key_array = array_rand($file_arrs[$k],$num);
foreach($key_array as $v)
{
$rand_id.= $file_arrs[$k][$v].",";
}
}
$rand_id = rtrim($rand_id,",");
}else{
if(count($file_arrs[$classid]) <= $num){
$key_array = array_rand($file_arrs[$classid],count($file_arrs[$classid]));
}else{
$key_array = array_rand($file_arrs[$classid],$num);
}
//根据数据表获取id,每个栏目随机分配文章id
$key_array = array_rand($file_arrs[$classid],$num);
foreach($key_array as $v)
{
$rand_id.= $file_arrs[$classid][$v].",";
}
$rand_id = rtrim($rand_id,",");
}
return $rand_id;
}
Installation method:
Put the code in the e/class/userfun.php file
Calling method:
<?php
$id = ecms_rand(1,50);
?>
调用的文章id:
[e:loop={0,50,3,0,"id in ($id)"}]
<?=$bqr['id']?>
[/e:loop]
Summary of efficiency:
1. Reading files directly is more efficient than database query, and the connection and disconnection time is not included in the article.
2. The larger the content read at one time, the more obvious the advantage of reading the file directly (the file reading time increases slightly, which is related to the continuity of file storage and cluster size). This result is exactly the opposite of expected, indicating that MYSQL may have additional operations for reading larger files (the two times increased by nearly 30%). If it is just a simple assignment conversion, the difference should be small.
3. It can be inferred that the database efficiency will only be worse when writing files and INSERT without testing.
4. If a very small configuration file does not need to use database features, it is more suitable to store it in a separate file. There is no need to create a separate data table or record. It is more convenient to store large files such as pictures and music in files. It is more reasonable to only put index information such as paths or thumbnails in the database.
5. If you only read files on PHP, file_get_contents is more efficient than fopen and fclose. Excluding the time required to determine the existence of this function, it will take about 3 seconds less.