Oracle的timestamp字段更新实验 结论:只有逐条更新才能保证timestamp字段有差别,批量更新只会得到一致的时间,此操作无关时间精度.
有这么一张表:
create table hy_testtime( id number(6,0) not null primary key, name nvarchar2(20) not null, utime timestamp(6) )
如果这样给它充值:
insert into hy_testtime select rownum, dbms_random.string('*',dbms_random.value(1,20)), sysdate from dual connect by level<100 commit;
那么充值完毕后,其utime字段都会是一样的.(SQL:select to_char(utime,'yyyy-mm-dd hh24:mi:ss.ff6') from hy_testtime)
然后,用传统的反连接找utime最新的记录,会把所有99条结果都找出来.
select count(*) from hy_testtime a where not exists (select null from hy_testtime b where b.utime>a.utime)
这就不是我们期待的一条记录了.
让我们换一张表:
create table hy_testtime2( id number(6,0) not null primary key, name nvarchar2(20) not null, utime timestamp(6) )
这样给它充值:
insert into hy_testtime2 select rownum, dbms_random.string('*',dbms_random.value(1,20)), systimestamp from dual connect by level<100
这回换用了systimestamp,但时间还是一样的.
这当然还不是我们想要的.于是我们用以下更新语句去更新:
update hy_testtime2 set utime=systimestamp where id<10; commit;
再查一下:
select to_char(utime,'yyyy-mm-dd hh24:mi:ss.ff6') from hy_testtime2 where id<10
看来,只要批量更新,就会造成utime是一样的结果.
但我们去一条条更新,如下所示:
update hy_testtime2 set utime=systimestamp where id=11; update hy_testtime2 set utime=systimestamp where id=12;
然后再查查:
select to_char(utime,'yyyy-mm-dd hh24:mi:ss.ff6') from hy_testtime2 where id in(11,12)
这就可以发现更新时间是不一致了.
然后再查一下utime最新记录有几条:
select count(*) from hy_testtime2 a where not exists (select null from hy_testtime2 b where b.utime>a.utime)
再看看是否是id=12的那条:
select a.* from hy_testtime2 a where not exists (select null from hy_testtime2 b where b.utime>a.utime)
果然是.
结论: 如果是批量更新timestamp字段,那更新上去的值必然一致,无论是用sysdate还是systimestamp都是一样.
如果要拉开时间,必须一条条去更新,如果不是取Oracle的systimestamp,那么使用Java中的Timsstamp类保证精度也是一样效果.
当然,在并行环境里,utime字段还有可能会重复! 这时可行方案是发现重复再取一次时间,再设置回去. 流程是:更新utime,检查utime有无重复,发现重复则再次更新utime.
参考资料:http://www.itpub.net/thread-1931266-4-1.html
--2020-02-25--
分类:
Oracle.权衡比较Sql文
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2015-02-25 【高中数学/极值问题】已知:a,b皆为正实数,且2a+b=1,求:a/(2-2a)+b/(2-b)的最小值?
2014-02-25 【Canvas与光阑】立方体六彩光阑