存储过程中变量类型:number,pls_integer,small integer
2015-05-27 11:18
295 查看
今天在查看批量提交脚本时发现,很多存储过程变量定义整形时都是使用PLS_INTEGER,不禁疑惑它跟number有什么不同。
查看一些资料:
PLS_INTEGER Datatype
You use the PLS_INTEGER datatype to store signed integers. Its magnitude range is -2147483648 to 2147483647, represented in 32 bits. PLS_INTEGER values require less storage than NUMBER values and NUMBER subtypes. Also, PLS_INTEGER operations use hardware arithmetic, so they are faster than NUMBER operations, which use library arithmetic. For efficiency, use PLS_INTEGER for all calculations that fall within its magnitude range. For calculations outside the range of PLS_INTEGER, you can use the INTEGER datatype.
Benefits of Using PLS_INTEGER Datatype in PL/SQL
If you have a whole-number counter, for example in a loop or record counter, consider using a datatype ofPLS_INTEGER instead of INTEGER or NUMBER. When declaring an integer variable, PLS_INTEGER is the most efficient numeric datatype because its values require less storage than INTEGER or NUMBER values, which are represented internally as 22-byte Oracle numbers. Also, PLS_INTEGER operations use machine arithmetic, so they are faster than BINARY_INTEGER, INTEGER, or NUMBER operations, which use library arithmetic.
TIPS:
1)pls_integer类型也是数字类型,但和number类型不同,number可以存储实数,而pls_integer只能存储-2147483647到+2147483647之间的整数,如果使用pls_integer类型时发生溢出,系统将会报错。
2)binary_integer与pls_integer类似,在9.2版本以前大量使用,从9.2以后,从Oracle内部一些组件可以看的出,大有被pls_integer取代之势(pls_integer比binary_integer具有更少的存储开销和更好的访问性能,所以Oracle从9.2以后推荐你尽量能使用pls_integer就使用pls_integer)。它也是只能存储-2147483647到+2147483647之间的整数。
3)在oracle 11g中,又增加了一个新的类似的数据类型simple_integer,不过simple_integer不能包含空值,它的取值范围是[-2147483648..2147483647]。在11g中,simple_integer相对pls_integer在性能上又有所提高,如果在实际的pl/sql中既不需要overflow检查也不会包含null值,Oracle建议你使用simple_integer.
做了一个简单的测试,1个包含123w条数据表某个字段的更新操作。
declare
rnt number(4):= 0; ------------此处变换类型为 pls_integer,small integer
begin
for idx in (select rowid rid from autotest.photo_test ) loop
update test.photo_test set point = point * 100 where rowid = idx.rid;
rnt := rnt + 1;
if rnt = 2000 then
rnt := 0;
commit;
end if;
end loop;
commit;
end;
消耗的时间分别为
[b][b]simple_integer :00:06:49.97[/b]
[/b]
pls_integer :00:08:08.41
[b]number :00:08:19.05[/b]
number(4) :00:03:37.89
因为只是个简单的测试,所以没有重复测试,测试结果可能有些偏差。不过大致可以看出,消耗的时间跟变量的类型有关。
同为34位时,效率上使用[b]simple_integer>[b]pls_integer>[b][b]number,但是指定number位数以后可以看到速度有明显提升,所以尽量明确整形类型的精度,少占空间才是王道。[/b][/b][/b][/b]
查看一些资料:
PLS_INTEGER Datatype
You use the PLS_INTEGER datatype to store signed integers. Its magnitude range is -2147483648 to 2147483647, represented in 32 bits. PLS_INTEGER values require less storage than NUMBER values and NUMBER subtypes. Also, PLS_INTEGER operations use hardware arithmetic, so they are faster than NUMBER operations, which use library arithmetic. For efficiency, use PLS_INTEGER for all calculations that fall within its magnitude range. For calculations outside the range of PLS_INTEGER, you can use the INTEGER datatype.
Benefits of Using PLS_INTEGER Datatype in PL/SQL
If you have a whole-number counter, for example in a loop or record counter, consider using a datatype ofPLS_INTEGER instead of INTEGER or NUMBER. When declaring an integer variable, PLS_INTEGER is the most efficient numeric datatype because its values require less storage than INTEGER or NUMBER values, which are represented internally as 22-byte Oracle numbers. Also, PLS_INTEGER operations use machine arithmetic, so they are faster than BINARY_INTEGER, INTEGER, or NUMBER operations, which use library arithmetic.
TIPS:
1)pls_integer类型也是数字类型,但和number类型不同,number可以存储实数,而pls_integer只能存储-2147483647到+2147483647之间的整数,如果使用pls_integer类型时发生溢出,系统将会报错。
2)binary_integer与pls_integer类似,在9.2版本以前大量使用,从9.2以后,从Oracle内部一些组件可以看的出,大有被pls_integer取代之势(pls_integer比binary_integer具有更少的存储开销和更好的访问性能,所以Oracle从9.2以后推荐你尽量能使用pls_integer就使用pls_integer)。它也是只能存储-2147483647到+2147483647之间的整数。
3)在oracle 11g中,又增加了一个新的类似的数据类型simple_integer,不过simple_integer不能包含空值,它的取值范围是[-2147483648..2147483647]。在11g中,simple_integer相对pls_integer在性能上又有所提高,如果在实际的pl/sql中既不需要overflow检查也不会包含null值,Oracle建议你使用simple_integer.
做了一个简单的测试,1个包含123w条数据表某个字段的更新操作。
declare
rnt number(4):= 0; ------------此处变换类型为 pls_integer,small integer
begin
for idx in (select rowid rid from autotest.photo_test ) loop
update test.photo_test set point = point * 100 where rowid = idx.rid;
rnt := rnt + 1;
if rnt = 2000 then
rnt := 0;
commit;
end if;
end loop;
commit;
end;
消耗的时间分别为
[b][b]simple_integer :00:06:49.97[/b]
[/b]
pls_integer :00:08:08.41
[b]number :00:08:19.05[/b]
number(4) :00:03:37.89
因为只是个简单的测试,所以没有重复测试,测试结果可能有些偏差。不过大致可以看出,消耗的时间跟变量的类型有关。
同为34位时,效率上使用[b]simple_integer>[b]pls_integer>[b][b]number,但是指定number位数以后可以看到速度有明显提升,所以尽量明确整形类型的精度,少占空间才是王道。[/b][/b][/b][/b]
相关文章推荐
- oracle中integer、pls_integer和number类型
- 为什么在存储过程中局部变量不能申明text,ntext,image类型的,高手帮我解决一下啊?(急)
- ExecuteNonQuary接收存储过程的输出类型的变量的值
- oracle 存储过程中date类型变量声明中to_date赋值丢失时分秒问题
- ibatis调用存储过程:返回NUMBER,对应java类型的解决办法
- 执行字符串SQL语句--带有参数的存储过程以及 int类型的字符串变量注意事项
- 存储过程变量类型定义与表字段一致
- PHP内核探索:变量存储与类型
- 在Asp中使用存储过程数值类型值
- C++变量的存储类型
- pls_integer类型
- PHP内核探索:变量存储与类型
- C语言变量的类型和存储位置
- ORACLE存储之NUMBER类型
- SQL Server中使用表类型参数批量添加和修改的存储过程
- ORACLE数值类型详解--NUMBER、FLOAT、BINARY_FLOAT、BINARY_DOUBLE、BINARY_INTEGER
- 存储过程变量影响时间
- [转]一个利用Sql Server 20005的 ROW_NUMBER Function 的分页存储过程.
- Double与Float,Integer,Long封装类型的变量,比较大小时,
- 变量名‘@cardno1’已声明。变量名在插叙批次或存储过程内部必须唯一