Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:有没有办法减少这么多视图的创建_Mysql_Performance_Oracle_Query Optimization - Fatal编程技术网

MySQL:有没有办法减少这么多视图的创建

MySQL:有没有办法减少这么多视图的创建,mysql,performance,oracle,query-optimization,Mysql,Performance,Oracle,Query Optimization,我有三张这样的桌子: select rad.state_id AS state_id , sum (case when oslc.candidate_id is not null then 1 else 0 end) AS shortlisted , sum (case when osec.candidate_id is not null then 1 else 0 end) AS selected from registered_applicant_de

我有三张这样的桌子:

select rad.state_id AS state_id
         ,  sum (case when oslc.candidate_id is not null then 1 else 0 end) AS shortlisted
         ,  sum (case when osec.candidate_id is not null then 1 else 0 end) AS selected
from registered_applicant_details rad
     left outer join oc_shortlisted_candidates oslc 
          on (rad.applicant_id = oslc.candidate_id) 
     left outer join oc_selected_candidates osec  
          on (rad.applicant_id = osec.candidate_id) 
group by rad.state_id;
表1结构:
姓名:注册申请人详细信息
字段:申请人id INT主键、状态id INT

表2结构:
姓名:oc_入围_候选人
字段:候选者_id;>>>哪一个外键是指注册申请人详细信息中的申请人id

表3结构:
姓名:oc_选定_候选人
字段:候选者_id;>>>哪一个外键是指注册申请人详细信息中的申请人id

我想要这样的结果集:state\u wise\u counts

state_id | shortlisted_count | selected_counts

我得到结果的方法是

步骤1:我创建了两个这样的视图

CREATE VIEW  state_wise_shortlisted_count AS 
    (select rad.state_id AS state_id,
             count(0) AS shortlisted 
      from (oc_shortlisted_candidates oec 
             join registered_applicant_details rad) 
      where (oec.candidate_id = rad.applicant_id) 
      group by rad.state_id);

CREATE VIEW state_wise_selected_count AS 
      (select rad.state_id AS state_id,
               count(0) AS selected 
      from (oc_selected_candidates oec 
            join registered_applicant_details rad)
      where (oec.candidate_id = rad.applicant_id) 
      group by rad.state_id);
第2步:现在再次使用state_id连接这两个视图

SELECT s.state_id, sho.shortlisted, sel.selected
FROM statewise_shortlisted_count sho
JOIN statewise_selected_count sel ON sel.state_id = sho.state_id;
我的方法的缺点 由于我们有两个外部表格,即(入围的候选表格和选定的候选表格),我正在创建两个视图,但如果我们有10个表格,则意味着我需要创建10个视图。
因此,对于“状态统计”,我们需要创建10个视图, 如果还有一个属性,即“city”,如果我们想再次使用“city\u wise\u counts”,我需要再创建10个视图。
我认为这不是正确的方法

请给我提供正确的解决方案。

注意:我不想使用子查询,因为这些表有大约10000条记录&我需要减少来自应用程序的db调用数量

不确定子查询的性能是什么意思。对于投影中的每个计数,当前代码从RAD表读取一次。子查询怎么会更糟

试着这样做:

select rad.state_id AS state_id
         ,  sum (case when oslc.candidate_id is not null then 1 else 0 end) AS shortlisted
         ,  sum (case when osec.candidate_id is not null then 1 else 0 end) AS selected
from registered_applicant_details rad
     left outer join oc_shortlisted_candidates oslc 
          on (rad.applicant_id = oslc.candidate_id) 
     left outer join oc_selected_candidates osec  
          on (rad.applicant_id = osec.candidate_id) 
group by rad.state_id;

警告:未经测试的代码

不确定子查询的性能是什么意思。对于投影中的每个计数,当前代码从RAD表读取一次。子查询怎么会更糟

试着这样做:

select rad.state_id AS state_id
         ,  sum (case when oslc.candidate_id is not null then 1 else 0 end) AS shortlisted
         ,  sum (case when osec.candidate_id is not null then 1 else 0 end) AS selected
from registered_applicant_details rad
     left outer join oc_shortlisted_candidates oslc 
          on (rad.applicant_id = oslc.candidate_id) 
     left outer join oc_selected_candidates osec  
          on (rad.applicant_id = osec.candidate_id) 
group by rad.state_id;

警告:未经测试的代码

非常感谢,先生。它绝对有效。查询中的一个小变化:选择rad.state_id作为state_id,sum(如果oslc.candidate_id不为null,则1 else 0 end)作为短名单,sum(如果osec.candidate_id不为null,则1 else 0 end)作为从注册的_申请人详细信息中选择的rad left outer join oc_短名单_候选者oslc on(rad.applicator\u id=oslc.candidate\u id)左外连接oc\u selected\u candidates osec on(rad.applicator\u id=osec.candidate\u id)组由rad.state\u id;我非常感谢您,先生。非常感谢,先生。它绝对有效。查询中的一个小更改:选择rad.state\u id作为state\u id,sum(当oslc.candidate_id不为空时,则为1 else 0 end)作为入围者,从注册申请人详细信息中选择的sum(当osec.candidate_id不为空时,则为1 else 0 end)rad left EXTER join oc_入围候选人oslc on(rad.candidater_id=oslc.candidater_id)左外部加入由rad.state\u id在(rad.applicator\u id=osec.candidate\u id)组中选择的候选对象osec;我非常感谢您,先生。