oracle频繁update同一张表,对于同一张表update带来的效率问题

news/2024/7/4 22:24:57

需求:

存在一张表

create table tmp_report

(

...

tmp_domain varchar(20),

report_status int

...

);

其中2列的值为:

tmp_domain report_status

a3

a2

a1

b4

希望update report_status 的值为3所对应 tmp_domain 值相同的 report_status 都改成3,即:

tmp_domain report_status

a3

a3

a3

b4

开始用以下sql报错:

update tmp_report a set a.report_status = 3 where a.tmp_domain in

(select b.tmp_domain from tmp_report b where b.report_status =3);

-- ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause

更新:

update 前先备份tmp_report表

方法一:

root@localhost db1> update tmp_report a,tmp_report b set a.report_status = 3

->  where a.tmp_domain=b.tmp_domain and b.report_status=3;

Query OK, 2048 rows affected (11.32 sec)

Rows matched: 3072  Changed: 2048  Warnings: 0

-- 慢查询中的记录,扫描的行数大概是笛卡尔积

# Query_time: 11.318816  Lock_time: 0.000205 Rows_sent: 0  Rows_examined: 3146752

SET timestamp=1348214131;

update tmp_report a,tmp_report b set a.report_status = 3

where a.tmp_domain=b.tmp_domain and b.report_status=3;

恢复备份表:

root@localhost db1>delete from tmp_report;

Query OK, 3072 rows affected (0.09 sec)

root@localhost db1>insert into tmp_report select * from tmp_reportbak;

Query OK, 3072 rows affected (0.12 sec)

Records: 3072  Duplicates: 0  Warnings: 0

方法二:

root@localhost db1> update tmp_report set report_status = 3 where tmp_domain in (select tmp_domain from (select tmp_domain from tmp_report where report_status =3) tmp);

Query OK, 2048 rows affected (0.15 sec)

Rows matched: 3072  Changed: 2048  Warnings: 0

对于大表的时候,这两种方法都很慢。分开语句会好很多~


http://www.niftyadmin.cn/n/4261530.html

相关文章

python中字符串异或_python实现了字符串的按位异或和php中的strpad函数

近期在写自己主动化測试,因为开发加密中用到strpad和字符串的按位异或,而python中没有这种函数和功能,所以必须自己写一套,要不自己主动化測试无法进行,所以就用python实现了一下,因为在写字符串的按位异或…

Linux 修改SSH端口 和 禁止Root远程登陆

SSH 端口默认是22. 但从安全方面考虑,建议修改这个端口。 端口的取值范围是 0 - 65535(即2的16次方),0到1024是系统使用的端口,如 http服务的端口80。我们可以使用的端口范围:1024到65535。这个是socket规定的。 一. Linux修改ssh…

Javascript的IE和Firefox兼容性汇编[转帖.收藏]

最近作浏览器兼容性方面的工作,发现此篇文章,太好了,转帖收藏了 Javascript的IE和Firefox兼容性汇编 作者:yaosansi 日期:2006-11-14 1. document.form.item 问题 (1)现有问题: 现有代码中存在许多 document.formName.…

oracle读取注释,读取oracle注释

# codingutf-8__author__ jspdbau读取oracle注释import cx_Oracle # 导入模块SQLr"""select * from user_col_comments t where 11and t.comments is not nulland t.table_name TABLE"""words["custom","code","t…

pythonpandas画图代码_使用matplotlib的savefig保存从python pandas生成的绘图(AxesSubPlot)...

我正在使用pandas从数据帧生成绘图,我希望将其保存到文件中:dtf pd.DataFrame.from_records(d,columnsh)fig plt.figure()ax dtf2.plot()ax fig.add_subplot(ax)fig.savefig(~/Documents/output.png)似乎最后一行,使用matplotlib的savefi…

编程刺绣

怎么会突然把编程和刺绣两个八杆子打不到一起的概念放在一块作比较呢?这源于.net的初学。这两天我一直带着矛盾排斥的心理,痛苦万分得草读着.net完全手册,虽然稍看目录就了解哪几块内容可以略过,哪几块需要重点尝试,厚…

06_Hive分桶机制及其作用

1.Clustered By 对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分。 Hive也是针对某一列进行桶的组织。Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放在…

oracle+comment+on+column,Oracle 游标总结+整理

createorreplacefunctionfun_config_glide_num(p_classvarchar2,p_columnvarchar2)/*** 流水帐号配置_oracle* author:ai bo 2010.02.23* p_table varchar2 表名* p_website varchar2 自定义开头字符*/returnvarchar2asv_curIdinteger;--v_seq varchar2(50);--v_seq_va…