浏览代码

fix #967 优化随机视频排序rnd的性能问题

magicblack 2 年之前
父节点
当前提交
c7f0803d6f
共有 1 个文件被更改,包括 12 次插入9 次删除
  1. 12 9
      application/common/model/Vod.php

+ 12 - 9
application/common/model/Vod.php

@@ -269,7 +269,6 @@ class Vod extends Base {
                 $pageurl = mac_url($pageurl,$param);
                 $pageurl = mac_url($pageurl,$param);
             }
             }
         }
         }
-
         $where['vod_status'] = ['eq',1];
         $where['vod_status'] = ['eq',1];
         if(!empty($ids)) {
         if(!empty($ids)) {
             if($ids!='all'){
             if($ids!='all'){
@@ -498,14 +497,18 @@ class Vod extends Base {
                 }
                 }
             }
             }
         }
         }
+        // 随机视频排序rnd的性能问题
+        // https://github.com/magicblack/maccms10/issues/967
+        $use_rand = false;
         if($by=='rnd'){
         if($by=='rnd'){
-            $data_count = $this->countData($where);
-            $page_total = floor($data_count / $lp['num']) + 1;
-            if($data_count < $lp['num']){
-                $lp['num'] = $data_count;
-            }
-            $randi = @mt_rand(1, $page_total);
-            $page = $randi;
+            $row = Db::query("SELECT MAX(vod_id) AS id_max, MIN(vod_id) AS id_min FROM mac_vod");
+            if (!empty($row[0]['id_min']) && !empty($row[0]['id_max']) && $row[0]['id_max'] > $row[0]['id_min']) {
+                $id_list = range($row[0]['id_min'], $row[0]['id_max']);
+                $specified_list = array_rand($id_list, 1000);
+                $where['_string'] .= " AND vod_id IN (" . join(',', $specified_list) . ")";
+                $where['_string'] = trim($where['_string'], " AND ");
+            }
+            $use_rand = true;
             $by = 'hits_week';
             $by = 'hits_week';
             $order = 'desc';
             $order = 'desc';
         }
         }
@@ -518,7 +521,7 @@ class Vod extends Base {
         }
         }
         $order= 'vod_'.$by .' ' . $order;
         $order= 'vod_'.$by .' ' . $order;
         $where_cache = $where;
         $where_cache = $where;
-        if(!empty($randi)){
+        if($use_rand){
             unset($where_cache['vod_id']);
             unset($where_cache['vod_id']);
             $where_cache['order'] = 'rnd';
             $where_cache['order'] = 'rnd';
         }
         }