使用冗余字段代替sql函数计算

This commit is contained in:
wangliwen 2022-06-28 13:40:59 +08:00
parent d16d7add39
commit d10367c4c6
4 changed files with 35 additions and 7 deletions

View File

@ -11,6 +11,7 @@ alter table `tb_data_resource` drop index `name`;
alter table `tb_data_resource` drop index `deptId`;
alter table `sys_dept` drop index `type`;
alter table `sys_dept` drop index `district`;
alter table `tb_resource_collection` drop index `userId`;
-- 创建索引
alter table `tb_resource_collection` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '收藏的资源id';
alter table`tb_resource_car` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '加入申购车的id';
@ -21,6 +22,7 @@ alter table `t_ability_application` ADD INDEX `userId`(`user_id`) USING BTREE co
alter table `tb_data_resource` ADD FULLTEXT INDEX `type`(`type`);
alter table `tb_data_attr` ADD FULLTEXT INDEX `attr_value`(`attr_value`);
alter table `tb_data_resource` ADD FULLTEXT INDEX `name`(`name`);
ALTER TABLE `tb_data_resource` ADD INDEX `deptId`(`dept_id`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `type`(`type`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `district`(`district`) USING BTREE;
alter table `tb_data_resource` ADD INDEX `deptId`(`dept_id`) USING BTREE;
alter table `sys_dept` ADD INDEX `type`(`type`) USING BTREE;
alter table `sys_dept` ADD INDEX `district`(`district`) USING BTREE;
alter table `tb_resource_collection` ADD INDEX `userId`(`user_id`) USING BTREE;

View File

@ -0,0 +1,13 @@
create view `tb_data_resource_assignmark` AS select
tdr.id,
(IFNULL( visits / 100, 0 ) + IFNULL( trs.score, 0 ) + IFNULL( applyCount, 0 ) + IFNULL( collectCount, 0 )) as total
from
tb_data_resource tdr
left join ( select resource_id, avg( score ) as "score" from tb_resource_score where 1 = 1 and del_flag = 0 group by resource_id ) trs on tdr.id = trs.resource_id
left join ( select resource_id, count( id ) as "applyCount" from t_ability_application where 1 = 1 and del_flag = 0 group by resource_id ) taa on tdr.id = taa.resource_id
left join ( select resource_id, count( id ) as "collectCount" from tb_resource_collection where 1 = 1 and del_flag = 0 group by resource_id ) trc on tdr.id = trc.resource_id
where
1 = 1
and tdr.del_flag = 0
order by
total desc ;

View File

@ -0,0 +1,11 @@
alter table `tb_data_resource` ADD COLUMN `total` int NULL comment '整体评分';
update tb_data_resource,
tb_data_resource_assignmark
SET tb_data_resource.total = round(tb_data_resource_assignmark.total)
WHERE
tb_data_resource.id = tb_data_resource_assignmark.id;
alter table `tb_resource_collection` drop index `total`;
alter table `tb_data_resource`ADD INDEX `total`(`total` DESC) USING BTREE;

View File

@ -223,10 +223,11 @@
tdr.undercarriage_reason,
tdr.undercarriage_user_name,
tdr.info_list,
tdr.total AS total,
IFNULL(trs.score, 0 ) AS "score",
IFNULL(taa.applyCount, 0 ) AS "applyCount",
IFNULL(trc.collectCount, 0) AS "collectCount",
(IFNULL(visits / 100, 0) + IFNULL(trs.score, 0) + IFNULL(applyCount,0)+ IFNULL(collectCount,0)) AS total
IFNULL(trc.collectCount, 0) AS "collectCount"
<!-- (IFNULL(visits / 100, 0) + IFNULL(trs.score, 0) + IFNULL(applyCount,0)+ IFNULL(collectCount,0)) AS total-->
FROM
tb_data_resource tdr
LEFT JOIN ( SELECT resource_id, SUM(score) AS "score" FROM tb_resource_score WHERE 1 = 1 AND del_flag = 0 GROUP
@ -380,13 +381,14 @@
tdr.undercarriage_reason,
tdr.undercarriage_user_name,
tdr.info_list,
tdr.total AS total,
IFNULL(trs.score, 0 ) AS "score",
IFNULL(taa.applyCount, 0 ) AS "applyCount",
IFNULL(trc.collectCount, 0) AS "collectCount",
sd.name AS "deptName",
IFNULL(trc2.isCollect, 'false') AS "isCollect",
IFNULL(taa2.approve_status, '未申请') AS "applyState",
(IFNULL(visits / 100, 0) + IFNULL(trs.score, 0) + IFNULL(applyCount,0) + IFNULL(collectCount,0)) AS total
IFNULL(taa2.approve_status, '未申请') AS "applyState"
<!-- (IFNULL(visits / 100, 0) + IFNULL(trs.score, 0) + IFNULL(applyCount,0) + IFNULL(collectCount,0)) AS total-->
FROM
tb_data_resource tdr
LEFT JOIN ( SELECT resource_id, AVG(score) AS "score" FROM tb_resource_score WHERE 1 = 1 AND del_flag = 0 GROUP