您好,登录后才能下订单哦!
这篇文章给大家介绍怎么在PostgreSQL中对删除更新进行优化,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
删除缓慢的原因:主要是约束的问题。(数据库在有约束的时候,进行操作,会根据约束对相关表进行验证,可想而知,20W的数据验证要耗费多久的时间)。其次就是sql的编写。(sql如果查询中包含子查询等的可以优化的where会影响匹配的速度<查询的话就不多逼逼了>)。索引的问题 请看下面的 补充部分
具体解决方法:
ALTER TABLE tableName DISABLE TRIGGER ALL; delete 目标语句 ALTER TABLE tableName ENABLE TRIGGER ALL;
利用mybatis批量更新,第一张图是原本的批量更新的写法,发现在本地还可以的但是放在服务器会由于时间太久出现IO错误。
后来换成第二张图的方式来更新。当然也可以java代码for循环来单条更新来达到批量的目的。(对于这三种方式,第二种最快)
<update id="batchUpdate2" parameterType="java.util.List"> UPDATE lt_dzheinfo <trim prefix="set" suffixOverrides=","> <trim prefix="dzzhao =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerBarcode} </foreach> </trim> <trim prefix="xming =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerName} </foreach> </trim> <trim prefix="ztai =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerState} </foreach> </trim> <trim prefix="mima =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerPsw} </foreach> </trim> <trim prefix="dzlxid =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerType} </foreach> </trim> <trim prefix="zjlx =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.identityType} </foreach> </trim> <trim prefix="zjhma =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.identityId} </foreach> </trim> <trim prefix="qyrqi =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then TO_TIMESTAMP(#{reader.enableDate}, 'YYYY-MM-DD HH24:MI:SS') </foreach> </trim> <trim prefix="jzrqi =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then TO_TIMESTAMP(#{reader.disableDate}, 'YYYY-MM-DD HH24:MI:SS') </foreach> </trim> <trim prefix="libid =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerOfLibCode} </foreach> </trim> <trim prefix="xingbie =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then <choose> <when test="reader.readerSex=='男'.toString()"> true </when> <otherwise> false </otherwise> </choose> </foreach> </trim> <trim prefix="userid =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerOperatorId} </foreach> </trim> <trim prefix="beizhu =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then #{reader.readerRemark} </foreach> </trim> <trim prefix="bzrqi =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then TO_TIMESTAMP(#{reader.enableDate}, 'YYYY-MM-DD HH24:MI:SS') </foreach> </trim> <trim prefix="optime =case" suffix="end,"> <foreach collection="list" item="reader" index="index"> when dzid=#{reader.readerId} then TO_TIMESTAMP(#{reader.enableDate}, 'YYYY-MM-DD HH24:MI:SS') </foreach> </trim> </trim> where dzid in <foreach collection="list" index="index" item="reader" separator="," open="(" close=")"> #{reader.readerId} </foreach> </update>
<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" index="index" item="reader" separator=";"> UPDATE lt_dzheinfo SET dzzhao = #{reader.readerBarcode}, xming = #{reader.readerName}, ztai = #{reader.readerState}, mima = #{reader.readerPsw}, dzlxid = #{reader.readerType}, zjlx = #{reader.identityType}, zjhma = #{reader.identityId}, qyrqi = TO_TIMESTAMP(#{reader.enableDate}, 'YYYY-MM-DD HH24:MI:SS'), jzrqi = TO_TIMESTAMP(#{reader.disableDate}, 'YYYY-MM-DD HH24:MI:SS'), libid = #{reader.readerOfLibCode}, xingbie = <choose> <when test="reader.readerSex=='男'.toString()"> true </when> <otherwise> false </otherwise> </choose>, userid = #{reader.readerOperatorId}, beizhu = #{reader.readerRemark}, bzrqi = TO_TIMESTAMP(#{reader.enableDate}, 'YYYY-MM-DD HH24:MI:SS'), optime = TO_TIMESTAMP(#{reader.enableDate}, 'YYYY-MM-DD HH24:MI:SS') <where> dzid = #{reader.readerId} </where> </foreach> </update>
补充:数据库突然变慢...索引作怪!
本人在最近发现,有些比容量较大的SQL数据库,在使用一段时间后,会突然变很慢很慢。特别体现在一些经常进行操作及查询的大表中。经过深入研究,这一般是索引的问题!如果把此表的索引重建一次,问题即可解决。
为更好地解决此问题,建意在SQL中新建一个数据库维护计划,选中“重新组织数据各索引页”选项,并设置每天运行一次。
/* 刷新数据库视图.sql */ ALTER PROC P_RefreshView as DECLARE @vName sysname DECLARE refresh_cursor CURSOR FOR SELECT Name from sysobjects WHERE xtype = 'V' order by crdate FOR READ ONLY OPEN refresh_cursor FETCH NEXT FROM refresh_cursor INTO @vName WHILE @@FETCH_STATUS = 0 BEGIN --刷新指定视图的元数据。由于视图所依赖的基础对象的更改,视图的持久元数据会过期。 exec sp_refreshview @vName PRINT '视图' + @vName + '已更新' FETCH NEXT FROM refresh_cursor INTO @vName END CLOSE refresh_cursor DEALLOCATE refresh_cursor GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
关于怎么在PostgreSQL中对删除更新进行优化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。