请问这样的SQL语句怎么样优化呢?查询时间好久啊

问题描述

select f.pc_name, f.phc_name, (select count(1) from p_resident c where c.phc_id = f.phc_id and c.pr_income is not null and c.pr_income < 4000) fourqian, (select count(1) from p_resident c where c.phc_id = f.phc_id and c.pr_income is not null and c.pr_income >= 4000 and c.pr_income < 5000) fourfiveqian, (select count(1) from p_resident c where c.phc_id = f.phc_id and c.pr_income is not null and c.pr_income >= 5000 and c.pr_income < 6000) fivesixqian, (select count(1) from p_resident c where c.phc_id = f.phc_id and c.pr_income is not null and c.pr_income >= 6000 and c.pr_income < 7000) sixsevenqian, (select count(1) from p_resident c where c.phc_id = f.phc_id and c.pr_income is not null and c.pr_income >= 7000) sevenqian from (select t.pc_name, a.phc_name, a.phc_id from p_community t, p_home_community a where t.pc_id = a.pc_id and t.pc_id in (select c.po_id from p_organization c start with c.po_id = 5027 connect by prior c.po_id = c.po_parent_id)) f按地区统计各个阶段收入的个数,查询要好长时间

解决方案

select t.pc_name, a.phc_name, c.fourqian, c.fourfiveqian, c.fivesixqian, c.sixsevenqian, c.sevenqian from p_community t, p_home_community a, (select c.phc_id, sum(case when c.pr_income < 4000 then 1 else 0 end) fourqian, sum(case when c.pr_income >= 4000 and c.pr_income < 5000 then 1 else 0 end) fourfiveqian, sum(case when c.pr_income >= 5000 and c.pr_income < 6000 then 1 else 0 end) fivesixqian, sum(case when c.pr_income >= 6000 and c.pr_income < 7000 then 1 else 0 end) sixsevenqian, sum(case when c.pr_income >= 7000 then 1 else 0 end) sevenqian from p_resident c where c.pr_income is not nullgroup by c.phc_id ) c where t.pc_id = a.pc_id and a.pc_id = c.pc_id and t.pc_id in (select c.po_id from p_organization c start with c.po_id = 5027connect by prior c.po_id = c.po_parent_id)

时间: 2024-07-30 14:17:28

请问这样的SQL语句怎么样优化呢?查询时间好久啊的相关文章

改进数据库SQL语句进行优化的理由

数据|数据库|优化|语句 应用程序的优化通常可分为两个方面:源代码的优化和SQL语句的优化.源代码的优化在时间成本和风险上代价很高:另一方面,源代码的优化对数据库系统性能的提升收效有限. 优化的理由 1)SQL语句是对数据库(数据)进行操作的惟一途径: 2)SQL语句消耗了70%~90%的数据库资源: 3)SQL语句独立于程序设计逻辑,相对于对程序源代码的优化,对SQL语句的优化在时间成本和风险上的代价都很低: 4)SQL语句可以有不同的写法: 5)SQL语句易学,难精通. 优化技术的发展 第一

修改数据库字段-请问怎么用sql语句去修改sqlite中的数据字段的类型呢?

问题描述 请问怎么用sql语句去修改sqlite中的数据字段的类型呢? 我现在想写一个方法,用来修改sqlite数据库中某个表中的某个列的类型,因为现在客户需要发生变化了,我需要在数据完整的情况下吧数据类型给改变了,也就是说我不能替换数据库,哪就只有用sql语句去修改类型了,跪求高手... 解决方案 sqlite数据库中数据类型存在sqlite_master表中 select sql from sqlite_master where tb_name='tbname' 把这条sql语句改了,表结构

sql server-求助:sqlserver一条sql语句的优化 是否需要索引 建立什么样的索引合适

问题描述 求助:sqlserver一条sql语句的优化 是否需要索引 建立什么样的索引合适 1C select COUNT(*) total from (select distinct(device_token) from MDM_POLICY_UPDATE where len(device_token)=64 and SW='crmi_poly') a 解决方案 我觉得直接可以用count(device_token) 然后直接groupby device个人感觉最拖后腿的应该是len()这个函

sql-求下面这段SQL语句的优化

问题描述 求下面这段SQL语句的优化 update #temp set res = ( select top 1 [Desc] from Response nolock where req_no = ( select top 1 req_no from Request nolock where id = #temp.id order by update_date desc ) order by update_date desc ), [pro_status] = ( select top 1 [

Oracle数据库中SQL语句的优化技巧_oracle

在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT'; 2. /*+FIRST_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳响应时间,使资源消耗最小化.

SQL语句性能优化(续)_MsSql

上篇介绍了一下自己在项目中遇到的一种使用sql语句的优化方式(性能优化--SQL语句),但是说的不够完整.在对比的过程中,没有将max函数考虑在内,经人提醒之后赶紧做了一个测试,测试过程中又学到了不少的东西. 上次用的是select count(*) 和select * 的执行效率问题,因为我的需求是获取数据的一个总数来自动给出新的id,然后网友给出可以使用max的方式给出新id.其实这也是一种不错的思路(当时我们也用过该函数,只不过因为系统数据本身问题,不适合用该函数),然后我就对max函数的

请教大神一条sql语句的优化

问题描述 请教大神一条sql语句的优化 表名pm,以temp分组,count两个信息,一个是全部个数,另一个是status为1 id temp status 1 1 0 2 1 1 3 2 0 4 2 1 5 2 1 select total.temp ,used.c1,total.c2 from (select temp,count(1) c1 from pm group by temp ) total left join (select temp,count(1) c2 from pm wh

Oracle之SQL语句性能优化(34条优化方法)_oracle

好多同学对sql的优化好像是知道的甚少,最近总结了以下34条仅供参考. (1)选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理,在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.如果有3个以上的表连接查询, 那就需要选择交叉表(intersection table)作为基础表, 交叉表是指那个被其他表所引用的表. (2) WH

SQL语句性能优化(续)

上篇介绍了一下自己在项目中遇到的一种使用sql语句的优化方式(性能优化--SQL语句),但是说的不够完整.在对比的过程中,没有将max函数考虑在内,经人提醒之后赶紧做了一个测试,测试过程中又学到了不少的东西. 上次用的是select count(*) 和select * 的执行效率问题,因为我的需求是获取数据的一个总数来自动给出新的id,然后网友给出可以使用max的方式给出新id.其实这也是一种不错的思路(当时我们也用过该函数,只不过因为系统数据本身问题,不适合用该函数),然后我就对max函数的