奇奇怪怪的ORA-01841错误,分析处理过程(全)
墨墨导读:由于执行计划中,对过滤谓词顺序的改变,导致SQL运行报错。
最近,遇到了一个关于ORA-01841的报错,起初,认为这个错误处理起来应该不困难,但实际上折腾了很久,才最终找到问题原因,并解决掉,下面将本次解决和分析的过程用样例来说明。
ORA-01841的错误提示是“(full) year must be between -4713 and +9999, and not be 0”,翻译过来,大意是完整的年份值需在-4712到+9999之间,并且不得为0。出现这个错误,通常都是数据本身存在问题导致的,但本案例中,又不仅仅是数据的问题。
下面就来回顾一下问题处理的过程。为了简化问题,方便理解,以下描述均是在事后构建的模拟环境中进行的:
执行以下SQL时,发生了ora-01841的报错:
SQL> select *
from (
select *
from test_tab1
where c1 not like 'X%'
)
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01' ;
ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
no rows selected
结合SQL和报错信息,最初的怀疑是内层查询的结果集的C1列上,有不正常的数据,导致出现了报错。因此,首先检查内层查询的结果:
SQL> select *
from test_tab1
where c1 not like 'X%' ;
ID C1
---------- --------------------------------
1 2020-10-04
2 2020-09-17
3 2020-10-14
4 2020-11-03
5 2020-12-04
我们可以看到,内层查询的结果集中,并没有不正常的数据。
到此时,想了许久,也做了各种测试,但均没有找到问题原因。决定看一下执行计划:
SQL> set autot on
SQL> select *
from (
select *
from test_tab1
where c1 not like 'X%'
)
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01' ;
ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("TEST_TAB1"."C1",'yyyy-mm-dd')>TO_DATE('
2020-11-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "C1" NOT LIKE
'X%')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
419 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL>
从执行计划中看,CBO对该SQL做了自动改写,将外层查询的条件,推到了内层查询。而且,从谓词信息部分,我们可以看到SQL中的条件“to_date(c1,‘yyyy-mm-dd’) > date’2020-11-01’”在两个过滤条件中,是位于靠前的位置。
也就是说,当数据库对表中的数据做过滤时,是先用“to_date(c1,‘yyyy-mm-dd’) > date’2020-11-01’”来检查。这样,如果有某行数据的C1列中的值不正常,就会导致这样的报错。
我们来验证一下:
SQL> select * from test_tab1;
ID C1
---------- --------------------------------
1 2020-10-04
2 2020-09-17
3 2020-10-14
4 2020-11-03
5 2020-12-04
6 XXXXXXXXX1
6 rows selected.
果然,最后一行的C1列中的值是不能正常转换为日期的。
未被CBO自动改写的原始SQL,其内层查询,会将不能正常转换为日期的数据排除掉,然后在外层再去做TO_DATE的转换。如果CBO按照这种方式来处理,就不会报错了。
知道了原因,那我们要如何处理呢? 我们可以改写SQL,使其必须先执行内层查询,然后再执行外层查询。 比如可以在内层查询中加入ROWNUM。
SQL> select *
from (
select t.*,
rownum rn
from test_tab1 t
where c1 not like 'X%'
)
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3 4 5 6 7 8
ID C1 RN
---------- -------------------------------- ----------
4 2020-11-03 4
5 2020-12-04 5
Execution Plan
----------------------------------------------------------
Plan hash value: 4134971776
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 220 | 3 (0)| 00:00:01 |
|* 1 | VIEW | | 5 | 220 | 3 (0)| 00:00:01 |
| 2 | COUNT | | | | | |
|* 3 | TABLE ACCESS FULL| TEST_TAB1 | 5 | 70 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))
3 - filter("C1" NOT LIKE 'X%')
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
711 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2 rows processed
如上所示,我们可以看到,新的执行计划如我们所愿,先进行内层查询的执行
再将TO_DATE转换施加到内层查询的结果之上。
或者,在内层查询上,对C1进行一些不影响结果值的运算。例如:
SQL> select *
from (
select id, c1||'' c1
from test_tab1
where c1 not like 'X%'
)
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3 4 5 6 7
ID C1
---------- --------------------------------
4 2020-11-03
5 2020-12-04
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" NOT LIKE 'X%' AND
TO_DATE("C1"||'','yyyy-mm-dd')>TO_DATE(' 2020-11-01 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
645 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2 rows processed
如上所示,这种处理方法,虽然外层的过滤条件被推入到了内层,但会放到后边执行,这样,当前边的条件已经将不正常的数据过滤掉后,也就不会报错了。 同理,对C1做一些UPPER,LOWER的函数运算,也有同样的效果。
但是,这又引起了我的一个新的疑问,如果初始SQL就是只有一层(如下所示),两个过滤条件在一起时,CBO是先用哪个过滤条件来过滤呢?
select *
from test_tab1
where c1 not like 'X%'
and to_date(c1,'yyyy-mm-dd') > date'2020-11-01';
执行后的结果如下:
SQL> set autot on
SQL> select *
from test_tab1
where c1 not like 'X%'
and to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3 4
ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "C1" NOT LIKE 'X%')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
434 bytes sent via SQL*Net to client
520 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
如上所示,我们发现仍然会报ora-01841的错误。
和过滤条件在WHERE子句中出现的顺序是否有关呢?试试调换条件后的结果;
SQL> select *
from test_tab1
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01'
and c1 not like 'X%'; 2 3 4
ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "C1" NOT LIKE 'X%')
Statistics
----------------------------------------------------------
1 recursive calls
4 db block gets
4 consistent gets
0 physical reads
0 redo size
434 bytes sent via SQL*Net to client
520 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
如上所示,看来和条件出现的顺序是无关的。 但是,如果是RBO(基于规则的优化器)模式,则会是先使用最后出现的条件,再使用前边的。即,从后往前施加条件。这也是为什么网上曾流传过的一个SQL编写技巧–将过滤性最好的条件写到WHERE子句中的最后。但,自Oracle 10g以后,默认就是CBO(基于成本的优化器)了,除非像上面实验那样使用RULE的提示,否则,都会是以CBO方式来运作。
这正好给了我们一个启示,在CBO下,在选择先执行哪个过滤条件时,是否会依据统计信息,计算并排序各个过滤条件的选择性,选择性越好的,则越会先被执行呢?
我们测试验证一下。主要测试思路如下: 1、默认情况下,CBO估算大部分非相等的过滤条件时,都会采用5%这样一个选择率。所以,条件“to_date(c1,‘yyyy-mm-dd’) > date’2020-11-01’”的选择率会是5%,即,经过该条件过滤后,CBO认为会返回总记录的5%的行数。
2、CBO在计算NOT LIKE这类条件时,其计算思路是先计算出LIKE的选择率(类似于相等条件,是条件列中唯一值数量的倒数),然后用1-(like的选择率)就是NOT LIKE的选择率。
3、向表中再插入94行形如‘XXXXXXXXX1’这样的记录。构造一个有100行记录的表,其中c1列上有100个唯一值,然后收集统计信息(注意,不要收集列上的直方图信息,因为在有直方图时,其计算逻辑和方法都要复杂得多,这里,我们只用列上的非直方图的统计信息)。操作过程如下:
SQL> insert into test_tab1 select 6+rownum id,lpad(rownum+1,10,'X') c1 from dual connect by rownum<=94;
94 rows created.
SQL> commit;
Commit complete.
SQL> exec dbms_stats.gather_table_stats('DEMO','TEST_TAB1',method_opt=>'for columns c1 size 1');
PL/SQL procedure successfully completed.
SQL> select count(*) cnt,count(distinct c1) cnt_c1 from test_tab1;
CNT CNT_C1
---------- ----------
100 100
分别来验证一下施加单个条件时,CBO的估算结果
看看是否与前边的理解是吻合的:
SQL> set autot on exp
SQL> select *
from test_tab1
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3
ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 70 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 5 | 70 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01
00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
如上所示,对条件“to_date(c1,‘yyyy-mm-dd’) > date’2020-11-01’”返回行数的估算是5行。由于表中总共有100行,所以,选择率是5/100=5%。与我们的理解是吻合的。
再来看对NOT LIKE的选择率:
SQL> set autot traceonly exp
SQL> select *
from test_tab1
where c1 like 'X%'; 2 3
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" LIKE 'X%')
SQL> select *
from test_tab1
where c1 NOT like 'X%'; 2 3
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 99 | 1386 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 99 | 1386 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" NOT LIKE 'X%')
如上所示,我们看到对LIKE和NOT LIKE的估算,与我们的理解也是吻合的。 如果我们”先执行过滤性好的条件“的猜测是正确的,那么这种情形下,显然,条件“to_date(c1,‘yyyy-mm-dd’) > date’2020-11-01’”的过滤性(5%)要好过条件“c1 NOT like ‘X%’”的过滤性(99%),所以,会先执行前者。 我们来验证一下:
SQL> set autot traceonly
SQL> select *
from test_tab1
where c1 not like 'X%'
and to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3 4
ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 70 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 5 | 70 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "C1" NOT LIKE 'X%')
Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
4 consistent gets
0 physical reads
0 redo size
434 bytes sent via SQL*Net to client
520 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
那我们再来验证一下,如果可以让条件“c1 NOT like ‘X%’”的选择率低于5%,那么我们就可能让CBO选择先执行该条件了。即1-1/n<0.05,显然,N要小于1.053,由于N表示的是唯一值的数量,所以,一定是个整数,即N只能是1了。
为了满足这个条件,我们将表中C1列的值,全部更新为同一个值:‘XXXXXXXXX1’后,收集统计信息后,如下所示:
SQL> set autot off
SQL> update test_tab1 set c1='XXXXXXXXX1';
100 rows updated.
SQL> commit;
Commit complete.
SQL> exec dbms_stats.gather_table_stats('DEMO','TEST_TAB1',method_opt=>'for columns c1 size 1');
PL/SQL procedure successfully completed.
我们先来验证一下前述两个条件的选择性是否如我们所愿,已经发生了改变:
SQL> set autot traceonly exp
SQL> select *
from test_tab1
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 70 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 5 | 70 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01
00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
SQL> select *
from test_tab1
where c1 like 'X%'; 2 3
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100 | 1400 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 100 | 1400 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" LIKE 'X%')
SQL> select *
from test_tab1
where c1 NOT like 'X%'; 2 3
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" NOT LIKE 'X%')
如上所示,条件“to_date(c1,‘yyyy-mm-dd’) > date’2020-11-01’”的选择率未变,仍然是5%,但条件“c1 NOT like ‘X%’”的选择率已经低于5%,目前估算只有大约1行记录满足该条件。
那么我们再次执行测试SQL,看看结果如何:
SQL> select *
from test_tab1
where c1 not like 'X%'
and to_date(c1,'yyyy-mm-dd') > date'2020-11-01'; 2 3 4
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" NOT LIKE 'X%' AND
TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
SQL> select *
from test_tab1
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01'
and c1 not like 'X%'; 2 3 4
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" NOT LIKE 'X%' AND
TO_DATE("C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
SQL>
如上所示,这时,CBO已经先执行条件“c1 NOT like ‘X%’”了。 同理,即使这时我们执行最初的两层SQL,其也应该是先执行条件“c1 NOT like ‘X%’”。验证一下:
SQL> select *
from (
select *
from test_tab1
where c1 not like 'X%'
)
where to_date(c1,'yyyy-mm-dd') > date'2020-11-01' ; 2 3 4 5 6 7
Execution Plan
----------------------------------------------------------
Plan hash value: 1698440217
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB1 | 1 | 14 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C1" NOT LIKE 'X%' AND
TO_DATE("TEST_TAB1"."C1",'yyyy-mm-dd')>TO_DATE(' 2020-11-01 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))
果然是这样。
附录:提供上述模拟数据的生成脚本
SQL> create table test_tab1 (id number,c1 varchar2(32));
Table created.
SQL> insert into test_tab1 select rownum id,to_char(sysdate-dbms_random.value(1,100),'yyyy-mm-dd') c1 from dual connect by rownum<=5;
5 rows created.
SQL> insert into test_tab1 select 5+rownum id,lpad(rownum,10,'X') c1 from dual connect by rownum<=1;
1 row created.
SQL>commit;
SQL> exec dbms_stats.gather_table_stats('DEMO','TEST_TAB1');
PL/SQL procedure successfully completed.
墨天轮原文链接:https://www.modb.pro/db/42008(复制到浏览器中打开或者点击“阅读原文”立即查看)
推荐阅读:144页!分享珍藏已久的数据库技术年刊
推荐下载:2020数据技术嘉年华PPT下载
2020数据技术嘉年华近50个PPT下载、视频回放已上传墨天轮平台,可在“数据和云”公众号回复关键词“2020DTC”获得!
视频号,新的分享时代,关注我们,看看有什么新发现?
下一篇: 同时执行多条SQL的MySQL存储过程
推荐阅读
-
奇奇怪怪的ORA-01841错误,分析处理过程(全)
-
纯干货分享 | 研发效能提升——敏捷需求篇-而敏捷需求是提升效能的方式中不可或缺的模块之一。 云智慧的敏捷教练——Iris Xu近期在公司做了一场分享,主题为「敏捷需求挖掘和组织方法,交付更高业务价值的产品」。Iris具有丰富的团队敏捷转型实施经验,完成了企业多个团队从传统模式到敏捷转型的落地和实施,积淀了很多的经验。 这次分享主要包含以下2个部分: 第一部分是用户影响地图 第二部分是事件驱动的业务分析Event driven business analysis(以下简称EDBA) 用户影响地图,是一种从业务目标到产品需求映射的需求挖掘和组织的方法。 在软件开发过程中可能会遇到一些问题,比如大家使用不同的业务语言、技术语言,造成角色间的沟通阻碍,还会导致一些问题,比如需求误解、需求传递错误等;这会直接导致产品的功能需求和要实现的业务目标不是映射关系。 但在交付期间,研发人员必须要将这些需求实现交付,他们实则并不清楚这些功能需求产生的原因是什么、要解决客户的哪些痛点。研发人员往往只是拿到了解决方案,需要把它实现,但没有和业务侧一起去思考解决方案是否正确,能否真正的帮助客户解决问题。而用户影响地图通常是能够连接业务目标和产品功能的一种手段。 我们在每次迭代里加入的假设,也就是功能需求。首先把它先实现,再逐步去验证我们每一个小目标是否已经实现,再看下一个目标要是什么。那影响地图就是在这个过程中帮我们不断地去梳理目标和功能之间的关系。 我们在软件开发中可能存在的一些问题 针对这些问题,我们如何避免?先简单介绍做敏捷转型的常规思路: 先做团队级的敏捷,首先把产品、开发、测试人员,还有一些更后端的人员比如交互运维的同学放在一起,组成一个特训团队做交付。这个团队要包含交付过程中所涉及的所有角色。 接着业务敏捷要打通整个业务环节和研发侧的一个交付。上图中可以看到在敏捷中需求是分层管理的,第一层是业务需求,在这个层级是以用户目标和业务目标作为输入进行规划,同时需要去考虑客户的诉求。业务人员通过获取到的业务需求,进一步的和团队一起将其分解为产品需求。所以业务需求其实是我们真正去发布和运营的单元,它可以被独立发布到我们的生产环境上。我们的产品需求其实就是产品的具体功能,它是我们集成和测试的对象,也就是我们最终去部署到系统上的一个基本单元。产品需求再到了我们的开发团队,映射到迭代计划会上要把它分解为相应的技术任务,包括我们平时所说的比如一些前端的开发、后端的开发、测试都是相应的技术任务。所以业务敏捷要达到的目标是需要去持续顺畅高质量的交付业务价值。 将这几个点串起来,形成金字塔结构。最上层我们会把业务目标放在整个金字塔的塔尖。这个业务目标是通过用户的目标以及北极星指标确立的。确认业务目标后再去梳理相应的业务流程,最后生产。另外产品需求包含了操作流程和业务规则,具需求交付时间、工程时间以及我们的一些质量标准的要求。 谈到用户影响的地图,在敏捷江湖上其实有一个传说,大家都有一个说法叫做敏捷需求的“任督二脉”。用户影响地图其实就是任脉,在黑客马拉松上用过的用户故事地图其实叫督脉。所以说用户影响地图是在用户故事地图之前,先帮我们去梳理出我们要做哪些东西。当我们真正识别出我们要实现的业务活动之后,用户故事地图才去梳理我们整个的业务工作流,以及每个工作流节点下所要包含的具体功能和用户故事。所以说用户影响地图需要解决的问题,我们包括以下这些: 首先是范围蔓延,我们在整张地图上,功能和对应的业务目标是要去有一个映射的。这就避免了一些在我们比如有很多干系人参与的会议上,那大家都有不同想法些立场,会提出很多需求(正确以及错误的需求)。这个时候我们会依据目标去看这些需求是否真的是会影响我们的目标。 这里提到的错误需求,比如是利益相关的人提出的、客户认为产品应该有的、某个产品经理需求分析师认为可以有的....但是这些功能在用户影响地图中匹配不到对应目标的话,就需要降低优先级或弃掉。另外,通常我们去制定解决方案的时候,会考虑较完美的实现,导致解决方案括很多的功能。这个时候关键目标至关重要,会帮助我们梳理筛选、确定优先级。 看一下用户影响到地图概貌 总共分为一个三层的结构: 第一层why,你的业务目标哪个是最重要的,为什么?涉及到的角色有哪些? 第二层how ,怎样产生影响?影响用户角色什么样的行为? (不需要去列出所有的影响,基于业务目标) 第三层what,最关键的是在梳理需求时不需一次把所有细节想全,这通常团队中经常遇到的问题。 我们用这个例子来看一下 这是一个客服中心的影响地图,业务目标是 3个月内不增加客服人数的前提下能支持1.5倍的用户数。此业务目标设定是符合 smart 原则的,specific非常的具体,miserable 是可以衡量的,action reoriented是面向活动的, real list 也是很实际的。 量化的目标会指引我们接下来的行动,梳理一个业务目标,尽量去量化,比如 :我们通过打造一条什么样的流水线,能够提高整个部署的效率,时间是原来的 1/2 。这样才是一个能量化的有意义的目标。 回到这幅图, how 层级识别出来的内容,客服角色:想要对它施加的影响,把客户引导到论坛上,帮助客户更容易的跟踪问题,更快速的去定位问题。初级用户:方论坛上找到问题。高级用户:在论坛上回答问题。通过我们这些用户角色,进行活动,完成在不增加客户客服人数的前提下支持更多的用户数量。 最后一个层级,才是我们日常接触比较多的真正的功能的特性和需求,比如引导到客户到论坛上,其实这个产品就需要有一个常见问题的论坛的链接。这个层次需要我们团队进一步地在交付,在每个迭代之前做进一步的梳理,细化成相应的用户故事。 这个是云智慧团队中,自己做的影响地图的范例,可以看下整个的层级结构。序号表示优先级。 那我们用户影响地图可以总结为:
-
windows下进程间通信的(13种方法)-摘 要 本文讨论了进程间通信与应用程序间通信的含义及相应的实现技术,并对这些技术的原理、特性等进行了深入的分析和比较。 ---- 关键词 信号 管道 消息队列 共享存储段 信号灯 远程过程调用 Socket套接字 MQSeries 1 引言 ---- 进程间通信的主要目的是实现同一计算机系统内部的相互协作的进程之间的数据共享与信息交换,由于这些进程处于同一软件和硬件环境下,利用操作系统提供的的编程接口,用户可以方便地在程序中实现这种通信;应用程序间通信的主要目的是实现不同计算机系统中的相互协作的应用程序之间的数据共享与信息交换,由于应用程序分别运行在不同计算机系统中,它们之间要通过网络之间的协议才能实现数据共享与信息交换。进程间通信和应用程序间通信及相应的实现技术有许多相同之处,也各有自己的特色。即使是同一类型的通信也有多种的实现方法,以适应不同情况的需要。 ---- 为了充分认识和掌握这两种通信及相应的实现技术,本文将就以下几个方面对这两种通信进行深入的讨论:问题的由来、解决问题的策略和方法、每种方法的工作原理和实现、每种实现方法的特点和适用的范围等。 2 进程间的通信及其实现技术 ---- 用户提交给计算机的任务最终都是通过一个个的进程来完成的。在一组并发进程中的任何两个进程之间,如果都不存在公共变量,则称该组进程为不相交的。在不相交的进程组中,每个进程都独立于其它进程,它的运行环境与顺序程序一样,而且它的运行环境也不为别的进程所改变。运行的结果是确定的,不会发生与时间相关的错误。 ---- 但是,在实际中,并发进程的各个进程之间并不是完全互相独立的,它们之间往往存在着相互制约的关系。进程之间的相互制约关系表现为两种方式: ---- (1) 间接相互制约:共享CPU ---- (2) 直接相互制约:竞争和协作 ---- 竞争——进程对共享资源的竞争。为保证进程互斥地访问共享资源,各进程必须互斥地进入各自的临界段。 ---- 协作——进程之间交换数据。为完成一个共同任务而同时运行的一组进程称为同组进程,它们之间必须交换数据,以达到协作完成任务的目的,交换数据可以通知对方可以做某事或者委托对方做某事。 ---- 共享CPU问题由操作系统的进程调度来实现,进程间的竞争和协作由进程间的通信来完成。进程间的通信一般由操作系统提供编程接口,由程序员在程序中实现。UNIX在这个方面可以说最具特色,它提供了一整套进程间的数据共享与信息交换的处理方法——进程通信机制(IPC)。因此,我们就以UNIX为例来分析进程间通信的各种实现技术。 ---- 在UNIX中,文件(File)、信号(Signal)、无名管道(Unnamed Pipes)、有名管道(FIFOs)是传统IPC功能;新的IPC功能包括消息队列(Message queues)、共享存储段(Shared memory segment)和信号灯(Semapores)。 ---- (1) 信号 ---- 信号机制是UNIX为进程中断处理而设置的。它只是一组预定义的值,因此不能用于信息交换,仅用于进程中断控制。例如在发生浮点错、非法内存访问、执行无效指令、某些按键(如ctrl-c、del等)等都会产生一个信号,操作系统就会调用有关的系统调用或用户定义的处理过程来处理。 ---- 信号处理的系统调用是signal,调用形式是: ---- signal(signalno,action) ---- 其中,signalno是规定信号编号的值,action指明当特定的信号发生时所执行的动作。 ---- (2) 无名管道和有名管道 ---- 无名管道实际上是内存中的一个临时存储区,它由系统安全控制,并且独立于创建它的进程的内存区。管道对数据采用先进先出方式管理,并严格按顺序操作,例如不能对管道进行搜索,管道中的信息只能读一次。 ---- 无名管道只能用于两个相互协作的进程之间的通信,并且访问无名管道的进程必须有共同的祖先。 ---- 系统提供了许多标准管道库函数,如: pipe——打开一个可以读写的管道; close——关闭相应的管道; read——从管道中读取字符; write——向管道中写入字符; ---- 有名管道的操作和无名管道类似,不同的地方在于使用有名管道的进程不需要具有共同的祖先,其它进程,只要知道该管道的名字,就可以访问它。管道非常适合进程之间快速交换信息。 ---- (3) 消息队列(MQ) ---- 消息队列是内存中独立于生成它的进程的一段存储区,一旦创建消息队列,任何进程,只要具有正确的的访问权限,都可以访问消息队列,消息队列非常适合于在进程间交换短信息。 ---- 消息队列的每条消息由类型编号来分类,这样接收进程可以选择读取特定的消息类型——这一点与管道不同。消息队列在创建后将一直存在,直到使用msgctl系统调用或iqcrm -q命令删除它为止。 ---- 系统提供了许多有关创建、使用和管理消息队列的系统调用,如: ---- int msgget(key,flag)——创建一个具有flag权限的MQ及其相应的结构,并返回一个唯一的正整数msqid(MQ的标识符); ---- int msgsnd(msqid,msgp,msgsz,msgtyp,flag)——向队列中发送信息; ---- int msgrcv(msqid,cmd,buf)——从队列中接收信息; ---- int msgctl(msqid,cmd,buf)——对MQ的控制操作; ---- (4) 共享存储段(SM) ---- 共享存储段是主存的一部分,它由一个或多个独立的进程共享。各进程的数据段与共享存储段相关联,对每个进程来说,共享存储段有不同的虚拟地址。系统提供的有关SM的系统调用有: ---- int shmget(key,size,flag)——创建大小为size的SM段,其相应的数据结构名为key,并返回共享内存区的标识符shmid; ---- char shmat(shmid,address,flag)——将当前进程数据段的地址赋给shmget所返回的名为shmid的SM段; ---- int shmdr(address)——从进程地址空间删除SM段; ---- int shmctl (shmid,cmd,buf)——对SM的控制操作; ---- SM的大小只受主存限制,SM段的访问及进程间的信息交换可以通过同步读写来完成。同步通常由信号灯来实现。SM非常适合进程之间大量数据的共享。 ---- (5) 信号灯 ---- 在UNIX中,信号灯是一组进程共享的数据结构,当几个进程竞争同一资源时(文件、共享内存或消息队列等),它们的操作便由信号灯来同步,以防止互相干扰。 ---- 信号灯保证了某一时刻只有一个进程访问某一临界资源,所有请求该资源的其它进程都将被挂起,一旦该资源得到释放,系统才允许其它进程访问该资源。信号灯通常配对使用,以便实现资源的加锁和解锁。 ---- 进程间通信的实现技术的特点是:操作系统提供实现机制和编程接口,由用户在程序中实现,保证进程间可以进行快速的信息交换和大量数据的共享。但是,上述方式主要适合在同一台计算机系统内部的进程之间的通信。 3 应用程序间的通信及其实现技术 ---- 同进程之间的相互制约一样,不同的应用程序之间也存在竞争和协作的关系。UNIX操作系统也提供一些可用于应用程序之间实现数据共享与信息交换的编程接口,程序员可以通过自己编程来实现。如远程过程调用和基于TCP/IP协议的套接字(Socket)编程。但是,相对普通程序员来说,它们涉及的技术比较深,编程也比较复杂,实现起来困难较大。 ---- 于是,一种新的技术应运而生——通过将有关通信的细节完全掩盖在某个独立软件内部,即底层的通讯工作和相应的维护管理工作由该软件内部来实现,用户只需要将通信任务提交给该软件去完成,而不必理会它的具体工作过程——这就是所谓的中间件技术。 ---- 我们在这里分别讨论这三种常用的应用程序间通信的实现技术——远程过程调用、会话编程技术和MQSeries消息队列技术。其中远程过程调用和会话编程属于比较低级的方式,程序员参与的程度较深,而MQSeries消息队列则属于比较高级的方式,即中间件方式,程序员参与的程度较浅。 ---- 4.1 远程过程调用(RPC)