Skip to content

Commit 613df4f

Browse files
authored
Update 20160827_01.md
1 parent ec6e32c commit 613df4f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

201608/20160827_01.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ INSERT 0 1000000
3939
update | delete limit 用法如下
4040

4141
```
42-
postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10
42+
postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10 for update
4343
)
4444
delete from t where id in (select * from t1);
4545
DELETE 10
4646
47-
postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10
47+
postgres=# with t1 as (select id from t where id between 1 and 1000 limit 10 for update
4848
)
4949
update t set info='new' where id in (select * from t1);
5050
UPDATE 10
5151
5252
postgres=# explain with t1 as (select id from t where id between 1 and 1000
53-
limit 10)
53+
limit 10 for update)
5454
postgres-# update t set info='new' where id in (select *
5555
from t1);
5656
QUERY
@@ -76,15 +76,15 @@ width=4)
7676

7777
```
7878
postgres=# with t1 as (select ctid from t where id between 1 and 100000
79-
limit 10) delete from t where ctid in (select ctid from t1) and id between 1 and 100000; -- 一定要recheck防止ctid内容在此期间发生变化不符合查询条件, 或者使用RR模式
79+
limit 10 for update) delete from t where ctid in (select ctid from t1) and id between 1 and 100000; -- 一定要recheck防止ctid内容在此期间发生变化不符合查询条件, 或者使用RR模式
8080
DELETE 10
8181
8282
postgres=# with t1 as (select ctid from t where id between 1 and 100000
83-
limit 10) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
83+
limit 10 for update) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
8484
UPDATE 10
8585
8686
postgres=# explain with t1 as (select ctid from t where id between 1 and
87-
100000 limit 10) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
87+
100000 limit 10 for update) update t set info='new' where ctid in (select ctid from t1) and id between 1 and 100000;
8888
QUERY
8989
PLAN
9090
-----------------------------------------------------------------------------
@@ -111,10 +111,10 @@ PLAN
111111
使用如下的写法即可:
112112

113113
```
114-
postgres=# with t1 as (select ctid from test limit 10) delete from test where
114+
postgres=# with t1 as (select ctid from test limit 10 for update) delete from test where
115115
ctid = any (array(select ctid from t1)) and id between 1 and 100000;
116116
DELETE 10
117-
postgres=# with t1 as (select ctid from test limit 10) delete from test where
117+
postgres=# with t1 as (select ctid from test limit 10 for update) delete from test where
118118
ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
119119
id
120120
----
@@ -131,7 +131,7 @@ ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
131131
(10 rows)
132132
133133
DELETE 10
134-
postgres=# with t1 as (select ctid from test limit 10) delete from test where
134+
postgres=# with t1 as (select ctid from test limit 10 for update) delete from test where
135135
ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
136136
id
137137
----
@@ -149,7 +149,7 @@ ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
149149
150150
DELETE 10
151151
152-
postgres=# explain with t1 as (select ctid from test limit 10) delete from
152+
postgres=# explain with t1 as (select ctid from test limit 10 for update) delete from
153153
test where ctid = any (array(select ctid from t1)) and id between 1 and 100000 returning *;
154154
QUERY
155155
PLAN
@@ -174,15 +174,15 @@ create table test(id int, crt_time timestamp);
174174
insert into test select generate_series(1,1000000), clock_timestamp();
175175
create index idx_test_1 on test(crt_time);
176176
177-
postgres=# with t1 as (select ctid from test order by crt_time limit 1000)
177+
postgres=# with t1 as (select ctid from test order by crt_time limit 1000 for update)
178178
delete from test where ctid = any (array(select ctid from t1)) and id between 1 and 100000;
179179
DELETE 1000
180180
Time: 1.905 ms
181-
postgres=# with t1 as (select ctid from test order by crt_time limit 1000)
181+
postgres=# with t1 as (select ctid from test order by crt_time limit 1000 for update)
182182
delete from test where ctid = any (array(select ctid from t1)) and id between 1 and 100000;
183183
DELETE 1000
184184
Time: 1.470 ms
185-
postgres=# with t1 as (select ctid from test order by crt_time limit 1000)
185+
postgres=# with t1 as (select ctid from test order by crt_time limit 1000 for update)
186186
delete from test where ctid = any (array(select ctid from t1)) and id between 1 and 100000;
187187
DELETE 1000
188188
Time: 1.396 ms

0 commit comments

Comments
 (0)