热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

类似groupby的分组计数功能

之前同事发过一个语句,实现的功能比较简单,类似groupby的分组计数功能,因为where条件有like,又无法用groupby来实现。SELECTa.N0,b.N1,c.N2,d.N3,e.N4,f.N5,g.N6,h.N7,i.N8,j.N9from(selectcount(*)N0fromtb
之前同事发过一个语句,实现的功能比较简单,类似group by的分组计数功能,因为where条件有like,又无法用group by来实现。
SELECT a.N0,b.N1,c.N2,d.N3,e.N4,f.N5,g.N6,h.N7,i.N8,j.N9 from 
 (select count(*) N0 from tbl_loginfo_20141110 where keyrecord like '0%' or keyrecord like 'GJ_0%') a,
 (select count(*) N1 from tbl_loginfo_20141110 where keyrecord like '1%' or keyrecord like 'GJ_1%') b,
 (select count(*) N2 from tbl_loginfo_20141110 where keyrecord like '2%' or keyrecord like 'GJ_2%') c,
 (select count(*) N3 from tbl_loginfo_20141110 where keyrecord like '3%' or keyrecord like 'GJ_3%') d,
 (select count(*) N4 from tbl_loginfo_20141110 where keyrecord like '4%' or keyrecord like 'GJ_4%') e,
 (select count(*) N5 from tbl_loginfo_20141110 where keyrecord like '5%' or keyrecord like 'GJ_5%') f,
 (select count(*) N6 from tbl_loginfo_20141110 where keyrecord like '6%' or keyrecord like 'GJ_6%') g,
 (select count(*) N7 from tbl_loginfo_20141110 where keyrecord like '7%' or keyrecord like 'GJ_7%') h,
 (select count(*) N8 from tbl_loginfo_20141110 where keyrecord like '8%' or keyrecord like 'GJ_8%') i,
 (select count(*) N9 from tbl_loginfo_20141110 where keyrecord like '9%' or keyrecord like 'GJ_9%') j;
 
为了了解语句的性能,我做了如下类似的测试: 
select * from v$version;   --Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
drop table a;
create table a as select * from dba_objects where rownum<=50000;
begin
  for x in 1..6 loop
    insert into a select * from a;
  end loop;
  commit;
end;

select count(*) from a;   --3200000
select bytes/1024/1024 from user_segments where segment_name=&#39;A&#39;;  --357M

alter system flush shared_pool;
alter system flush buffer_cache;

SELECT a.N0,b.N1,c.N2,d.N3,e.N4,f.N5,g.N6,h.N7,i.N8,j.N9 from 
 (select count(*) N0 from a where object_name like &#39;A%&#39; or object_name like &#39;V%&#39;) a,
 (select count(*) N1 from a where object_name like &#39;B%&#39; or object_name like &#39;V%&#39;) b,
 (select count(*) N2 from a where object_name like &#39;C%&#39; or object_name like &#39;V%&#39;) c,
 (select count(*) N3 from a where object_name like &#39;D%&#39; or object_name like &#39;V%&#39;) d,
 (select count(*) N4 from a where object_name like &#39;E%&#39; or object_name like &#39;V%&#39;) e,
 (select count(*) N5 from a where object_name like &#39;F%&#39; or object_name like &#39;V%&#39;) f,
 (select count(*) N6 from a where object_name like &#39;G%&#39; or object_name like &#39;V%&#39;) g,
 (select count(*) N7 from a where object_name like &#39;H%&#39; or object_name like &#39;V%&#39;) h,
 (select count(*) N8 from a where object_name like &#39;I%&#39; or object_name like &#39;V%&#39;) i,
 (select count(*) N9 from a where object_name like &#39;J%&#39; or object_name like &#39;V%&#39;) j;
--58s
alter system flush shared_pool;
alter system flush buffer_cache;

--改写后
select 
sum(case when object_name like &#39;A%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N0,
sum(case when object_name like &#39;B%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N1, 
sum(case when object_name like &#39;C%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N2, 
sum(case when object_name like &#39;D%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N3, 
sum(case when object_name like &#39;E%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N4, 
sum(case when object_name like &#39;F%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N5, 
sum(case when object_name like &#39;G%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N6, 
sum(case when object_name like &#39;H%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N7, 
sum(case when object_name like &#39;I%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N8, 
sum(case when object_name like &#39;J%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N9
from a; 

--19s





--对比执行计划:
--前者执行计划:
SQL> explain plan for
  2  SELECT a.N0,b.N1,c.N2,d.N3,e.N4,f.N5,g.N6,h.N7,i.N8,j.N9 from
  3   (select count(*) N0 from a where object_name like &#39;A%&#39; or object_name like &#39;V%&#39;) a,
  4   (select count(*) N1 from a where object_name like &#39;B%&#39; or object_name like &#39;V%&#39;) b,
  5   (select count(*) N2 from a where object_name like &#39;C%&#39; or object_name like &#39;V%&#39;) c,
  6   (select count(*) N3 from a where object_name like &#39;D%&#39; or object_name like &#39;V%&#39;) d,
  7   (select count(*) N4 from a where object_name like &#39;E%&#39; or object_name like &#39;V%&#39;) e,
  8   (select count(*) N5 from a where object_name like &#39;F%&#39; or object_name like &#39;V%&#39;) f,
  9   (select count(*) N6 from a where object_name like &#39;G%&#39; or object_name like &#39;V%&#39;) g,
 10   (select count(*) N7 from a where object_name like &#39;H%&#39; or object_name like &#39;V%&#39;) h,
 11   (select count(*) N8 from a where object_name like &#39;I%&#39; or object_name like &#39;V%&#39;) i,
 12   (select count(*) N9 from a where object_name like &#39;J%&#39; or object_name like &#39;V%&#39;) j;

Explained.

Elapsed: 00:00:00.15
SQL> @getplan
&#39;general,outline,starts&#39;

Enter value for plan type:

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------
Plan hash value: 2527411742

-------------------------------------------------------------------------------------
| Id  | Operation                    | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |      |     1 |   130 |   123K  (1)| 00:24:46 |
|   1 |  NESTED LOOPS                |      |     1 |   130 |   123K  (1)| 00:24:46 |
|   2 |   NESTED LOOPS               |      |     1 |   117 |   111K  (1)| 00:22:17 |
|   3 |    NESTED LOOPS              |      |     1 |   104 | 99032   (1)| 00:19:49 |
|   4 |     NESTED LOOPS             |      |     1 |    91 | 86653   (1)| 00:17:20 |
|   5 |      NESTED LOOPS            |      |     1 |    78 | 74274   (1)| 00:14:52 |
|   6 |       NESTED LOOPS           |      |     1 |    65 | 61895   (1)| 00:12:23 |
|   7 |        NESTED LOOPS          |      |     1 |    52 | 49516   (1)| 00:09:55 |
|   8 |         NESTED LOOPS         |      |     1 |    39 | 37137   (1)| 00:07:26 |
|   9 |          NESTED LOOPS        |      |     1 |    26 | 24758   (1)| 00:04:58 |
|  10 |           VIEW               |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  11 |            SORT AGGREGATE    |      |     1 |    66 |            |          |
|* 12 |             TABLE ACCESS FULL| A    | 91587 |  5903K| 12379   (1)| 00:02:29 |
|  13 |           VIEW               |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  14 |            SORT AGGREGATE    |      |     1 |    66 |            |          |
|* 15 |             TABLE ACCESS FULL| A    |   137K|  8831K| 12379   (1)| 00:02:29 |
|  16 |          VIEW                |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  17 |           SORT AGGREGATE     |      |     1 |    66 |            |          |
|* 18 |            TABLE ACCESS FULL | A    | 85818 |  5531K| 12379   (1)| 00:02:29 |
|  19 |         VIEW                 |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  20 |          SORT AGGREGATE      |      |     1 |    66 |            |          |
|* 21 |           TABLE ACCESS FULL  | A    |   111K|  7158K| 12379   (1)| 00:02:29 |
|  22 |        VIEW                  |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  23 |         SORT AGGREGATE       |      |     1 |    66 |            |          |
|* 24 |          TABLE ACCESS FULL   | A    | 86539 |  5577K| 12379   (1)| 00:02:29 |
|  25 |       VIEW                   |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  26 |        SORT AGGREGATE        |      |     1 |    66 |            |          |
|* 27 |         TABLE ACCESS FULL    | A    | 91587 |  5903K| 12379   (1)| 00:02:29 |
|  28 |      VIEW                    |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  29 |       SORT AGGREGATE         |      |     1 |    66 |            |          |
|* 30 |        TABLE ACCESS FULL     | A    |   228K|    14M| 12379   (1)| 00:02:29 |
|  31 |     VIEW                     |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  32 |      SORT AGGREGATE          |      |     1 |    66 |            |          |
|* 33 |       TABLE ACCESS FULL      | A    | 87981 |  5670K| 12379   (1)| 00:02:29 |
|  34 |    VIEW                      |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  35 |     SORT AGGREGATE           |      |     1 |    66 |            |          |
|* 36 |      TABLE ACCESS FULL       | A    | 84376 |  5438K| 12379   (1)| 00:02:29 |
|  37 |   VIEW                       |      |     1 |    13 | 12379   (1)| 00:02:29 |
|  38 |    SORT AGGREGATE            |      |     1 |    66 |            |          |
|* 39 |     TABLE ACCESS FULL        | A    |   112K|  7251K| 12379   (1)| 00:02:29 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

  12 - filter("OBJECT_NAME" LIKE &#39;J%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  15 - filter("OBJECT_NAME" LIKE &#39;I%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  18 - filter("OBJECT_NAME" LIKE &#39;H%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  21 - filter("OBJECT_NAME" LIKE &#39;G%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  24 - filter("OBJECT_NAME" LIKE &#39;F%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  27 - filter("OBJECT_NAME" LIKE &#39;E%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  30 - filter("OBJECT_NAME" LIKE &#39;D%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  33 - filter("OBJECT_NAME" LIKE &#39;C%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  36 - filter("OBJECT_NAME" LIKE &#39;B%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  39 - filter("OBJECT_NAME" LIKE &#39;A%&#39; OR "OBJECT_NAME" LIKE &#39;V%&#39;)
  
  
  
  
  
--后者执行计划:  
SQL> explain plan for
  2  select
  3  sum(case when object_name like &#39;A%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N0,
  4  sum(case when object_name like &#39;B%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N1,
  5  sum(case when object_name like &#39;C%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N2,
  6  sum(case when object_name like &#39;D%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N3,
  7  sum(case when object_name like &#39;E%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N4,
  8  sum(case when object_name like &#39;F%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N5,
  9  sum(case when object_name like &#39;G%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N6,
 10  sum(case when object_name like &#39;H%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N7,
 11  sum(case when object_name like &#39;I%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N8,
 12  sum(case when object_name like &#39;J%&#39; or object_name like &#39;V%&#39; then 1 else 0 end) N9
 13  from a;

Explained.

Elapsed: 00:00:00.01
SQL> @getplan
&#39;general,outline,starts&#39;

Enter value for plan type:

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------
Plan hash value: 3918351354

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    66 | 12349   (1)| 00:02:29 |
|   1 |  SORT AGGREGATE    |      |     1 |    66 |            |          |
|   2 |   TABLE ACCESS FULL| A    |  3097K|   194M| 12349   (1)| 00:02:29 |
---------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)  


可以看出,前者10次全表扫描,后者1次全表扫描。从而时间上也大大降低了。由58s降低到19s。
优化这个sql主要还是思路的转换,难点在于怎样把10次全表扫描转化成1次全表扫描。
在OLAP中,可以加并行使sql速度更快。





推荐阅读
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • SQL中UPDATE SET FROM语句的使用方法及应用场景
    本文详细介绍了SQL中UPDATE SET FROM语句的使用方法,通过具体示例展示了如何利用该语句高效地更新多表关联数据。适合数据库管理员和开发人员参考。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 使用C#开发SQL Server存储过程的指南
    本文介绍如何利用C#在SQL Server中创建存储过程,涵盖背景、步骤和应用场景,旨在帮助开发者更好地理解和应用这一技术。 ... [详细]
  • 本文探讨了适用于Spring Boot应用程序的Web版SQL管理工具,这些工具不仅支持H2数据库,还能够处理MySQL和Oracle等主流数据库的表结构修改。 ... [详细]
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • 本文详细介绍了如何在 Linux 平台上安装和配置 PostgreSQL 数据库。通过访问官方资源并遵循特定的操作步骤,用户可以在不同发行版(如 Ubuntu 和 Red Hat)上顺利完成 PostgreSQL 的安装。 ... [详细]
  • 如何在PostgreSQL中查看数据表
    本文将指导您使用pgAdmin工具连接到PostgreSQL数据库,并展示如何浏览和查找其中的数据表。通过简单的步骤,您可以轻松访问所需的表结构和数据。 ... [详细]
  • 利用存储过程构建年度日历表的详细指南
    本文将介绍如何使用SQL存储过程创建一个完整的年度日历表。通过实例演示,帮助读者掌握存储过程的应用技巧,并提供详细的代码解析和执行步骤。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 在使用SQL Server进行动态SQL查询时,如果遇到LIKE语句无法正确返回预期结果的情况,通常是因为参数传递方式不当。本文将详细探讨这一问题,并提供解决方案及相关的技术背景。 ... [详细]
  • 本文介绍如何通过创建替代插入触发器,使对视图的插入操作能够正确更新相关的基本表。涉及的表包括:飞机(Aircraft)、员工(Employee)和认证(Certification)。 ... [详细]
  • MySQL缓存机制深度解析
    本文详细探讨了MySQL的缓存机制,包括主从复制、读写分离以及缓存同步策略等内容。通过理解这些概念和技术,读者可以更好地优化数据库性能。 ... [详细]
author-avatar
zhaojiapin_313
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有