在Read uncommitted transaction中,选择语句hold S lock

时间:2014-07-06 04:47:23

标签: mysql transactions locking

我将select语句事务更改为未提交读取但仍保留共享锁并使用其他更新语句参与死锁,

选择语句是whatsnew(uid,friend_news_id,actor_news_id)SP

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;

 set @actor_news_limit = 2;
 set @player_news_limit = 6;


if (select
    n.id

from
    news n 
inner join
    news_user_xrf nu
on 
    n.id = nu.news_id
inner join
    friends f
 on
    nu.user_id = f.friend_id
 inner join 
    user u
 on 
    u.id = f.friend_id
 where
    f.user_id = uid
 and
    n.id > friend_news_id
 order by
    n.points desc,n.newsDate desc
 limit 0,1) is null

then



select * from
(select
    n.id,
    'whatshot_friendsnews' as type,
    n.content,
    '0' as actor_id,
    u.id as player_id,
    n.newsType,
    n.like_text,
    n.link,
    n.newsDate,
    u.imgurl,
    u.firstname,
    u.lastname,
    n.points
from
    news n 
inner join
    news_user_xrf nu
on 
    n.id = nu.news_id

 inner join 
    user u
 on 
    u.id = nu.user_id

 where
    u.id <> uid

 and
    n.id > friend_news_id
 order by
    n.newsDate desc
 limit 0,3) a order by newsDate;

else


select * from
(select
    n.id,
    'whatshot_friendsnews' as type,
    n.content,
    '0' as actor_id,
    u.id as player_id,
    n.newsType,
    n.like_text,
    n.link,
    n.newsDate,
    u.imgurl,
    u.firstname,
    u.lastname,
    n.points
from
    news n 
inner join
    news_user_xrf nu
on 
    n.id = nu.news_id
inner join
    friends f
 on
    nu.user_id = f.friend_id
 inner join 
    user u
 on 
    u.id = f.friend_id
 where
    f.user_id = uid
 and
    n.id > friend_news_id
 order by
    n.points desc,n.newsDate desc
 limit 0,3) a order by newsDate;



end if;

select * from
(select 
    n.id,
    'whatshot_actornews' as type,
    n.content,
    na.actor_id,
    "0" as player_id,
    n.newsType,
    n.like_text,
    n.link,
    n.newsDate,
    n.newsImg as imgurl,
    '' as firstname,
    ''  as lastname,
    n.points

from
    news n
inner join
    news_actor_xrf na
on 
    n.id = na.news_id
where 
    newsType = 2
and
    n.id > actor_news_id
order by 
    points desc,
    newsDate desc
limit 
    0,3
)  a order by newsDate;

COMMIT ;

END

现在我有另一个更新语句,其中我更新相同的表和此语句等待X锁但它不能拥有它,因为whatsnew sp在同一个表上持有S锁,     下面是Update sp

set SQL_SAFE_UPDATES=0;

update user
set networth = (select sum(p.avgcostprice * p.quantity) 
from portfolio p where p.user_id = user.id) +
(select truncate(sum((a.currentvalue - p.avgcostprice)*p.quantity),2)  
from actor a inner join portfolio p on a.id = p.actor_id where p.user_id = user.id) +
user.balance;


update user u
  set u.rank = (select count(*) from friends f 
 where ifnull(friend_networth,0) > user_networth
and f.user_id = u.id) + 1;

update user u
set u.rank_text = (select concat(u.rank,'/',(select
count(*)+1 from friends f where f.user_id = u.id)));

现在这两个存储过程都涉及死锁,但问题是甚至当我使用read uncommitted时为什么选择语句  持有行上的共享锁,它应该从中读取(脏读)  表。这里是SHOW ENGINE INNODB STATUS输出

=====================================
2014-07-06 03:44:57 2ad677681700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 2 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 32811 srv_active, 0 srv_shutdown, 89228 srv_idle
srv_master_thread log flush and writes: 122039
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 22814
OS WAIT ARRAY INFO: signal count 21778
Mutex spin waits 6627, rounds 508985, OS waits 15885
RW-shared spins 5876, rounds 176305, OS waits 5659
RW-excl spins 44, rounds 38079, OS waits 1237
Spin rounds per wait: 76.80 mutex, 30.00 RW-shared, 865.43 RW-excl
------------------------
LATEST DETECTED DEADLOCK
------------------------
2014-07-06 02:01:41 2ad677681700
*** (1) TRANSACTION:
TRANSACTION 1925770, ACTIVE 0 sec fetching rows
mysql tables in use 2, locked 2
LOCK WAIT 25 lock struct(s), heap size 3112, 909 row lock(s)
MySQL thread id 28709, OS thread handle 0x2ad677640700, query id 349542 ec2-54-226-145-7.compute-1.amazonaws.com 54.227.116.3 Database1 Searching rows for update
update user u
set u.rank_text = (select concat(u.rank,''/'',(select
count(*)+1 from friends f where f.user_id = u.id)))
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 19303 page no 29 n bits 104 index `PRIMARY` of table `Database1`.`user` trx id 1925770 lock_mode X waiting
Record lock, heap no 31 PHYSICAL RECORD: n_fields 25; compact format; info bits 0
 0: len 8; hex 8000000000000416; asc         ;;
 1: len 6; hex 0000001d214a; asc     !J;;
 2: len 7; hex 6f0000018d08f9; asc o      ;;
 3: len 8; hex 80005af4001478e8; asc   Z   x ;;
 4: len 5; hex 5269736869; asc Rishi;;
 5: len 5; hex 506174656c; asc Patel;;
 6: len 23; hex 7269736869706174656c5f3939407961686f6f2e636f6d; asc rishi_9999@yahoo.com;;
 7: len 30; hex 43414146336348376866544942414a434844466b5a42664937544a554643; asc CAAF3cH7hfTIBAJCHDFkZBfI7TJUFC; (total 201 bytes);
 8: len 8; hex 800000005405f359; asc     T  Y;;
 9: len 9; hex 80000000000007d619; asc          ;;
 10: len 1; hex 01; asc  ;;
 11: len 30; hex 313034383539303739377c3130303030303937303735313938317c313030; asc 1048590797|100000970751981|100; (total 58 bytes);
 12: len 30; hex 68747470733a2f2f67726170682e66616365626f6f6b2e636f6d2f313030; asc https://graph.facebook.com/100; (total 50 bytes);
 13: SQL NULL;
 14: len 4; hex 80000002; asc     ;;
 15: len 4; hex 466f4145; asc FoAE;;
 16: SQL NULL;
 17: len 4; hex 00000000; asc     ;;
 18: len 3; hex 322f35; asc 2/5;;
 19: SQL NULL;
 20: len 8; hex 8000000000000007; asc         ;;
 21: len 4; hex 05c58045; asc    E;;
 22: SQL NULL;
 23: len 8; hex 8000000000000000; asc         ;;
 24: len 8; hex 8000000000000007; asc         ;;

*** (2) TRANSACTION:
TRANSACTION 1925761, ACTIVE 22 sec starting index read
mysql tables in use 4, locked 4
14 lock struct(s), heap size 3112, 4 row lock(s)
MySQL thread id 28723, OS thread handle 0x2ad677681700, query id 349517 ec2-54-91-34-208.compute-1.amazonaws.com 54.91.34.208 Database1 Sending data
call whatsnew(uid,friend_news_id,actor_news_id)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 19303 page no 29 n bits 104 index `PRIMARY` of table `Database1`.`user` trx id 1925761 lock mode S locks rec but not gap
Record lock, heap no 31 PHYSICAL RECORD: n_fields 25; compact format; info bits 0
 0: len 8; hex 8000000000000416; asc         ;;
 1: len 6; hex 0000001d214a; asc     !J;;
 2: len 7; hex 6f0000018d08f9; asc o      ;;
 3: len 8; hex 80005af4001478e8; asc   Z   x ;;
 4: len 5; hex 5269736869; asc Rishi;;
 5: len 5; hex 506174656c; asc Patel;;
 6: len 23; hex 7269736869706174656c5f3939407961686f6f2e636f6d; asc rishi_9999@yahoo.com;;
 7: len 30; hex 43414146336348376866544942414a434844466b5a42664937544a554643; asc CAAF3cH7hfTIBAJCHDFkZBfI7TJUFC; (total 201 bytes);
 8: len 8; hex 800000005405f359; asc     T  Y;;
 9: len 9; hex 80000000000007d619; asc          ;;
 10: len 1; hex 01; asc  ;;
 11: len 30; hex 313034383539303739377c3130303030303937303735313938317c313030; asc 1048590797|100000970751981|100; (total 58 bytes);
 12: len 30; hex 68747470733a2f2f67726170682e66616365626f6f6b2e636f6d2f313030; asc https://graph.facebook.com/100; (total 50 bytes);
 13: SQL NULL;
 14: len 4; hex 80000002; asc     ;;
 15: len 4; hex 466f4145; asc FoAE;;
 16: SQL NULL;
 17: len 4; hex 00000000; asc     ;;
 18: len 3; hex 322f35; asc 2/5;;
 19: SQL NULL;
 20: len 8; hex 8000000000000007; asc         ;;
 21: len 4; hex 05c58045; asc    E;;
 22: SQL NULL;
 23: len 8; hex 8000000000000000; asc         ;;
 24: len 8; hex 8000000000000007; asc         ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 19303 page no 29 n bits 104 index `PRIMARY` of table `Database1`.`user` trx id 1925761 lock mode S locks rec but not gap waiting
Record lock, heap no 26 PHYSICAL RECORD: n_fields 25; compact format; info bits 0
 0: len 8; hex 800000000000040b; asc         ;;
 1: len 6; hex 0000001d214a; asc     !J;;
 2: len 7; hex 6f0000018d0878; asc o     x;;
 3: len 8; hex 80005af401790e35; asc   Z  y 5;;
 4: len 3; hex 52616a; asc Raj;;
 5: len 5; hex 506174656c; asc Patel;;
 6: len 22; hex 706174656c3039303972616a407961686f6f2e636f6d; asc patel@yahoo.com;;
 7: len 30; hex 434141463363483768665449424145726535315545495475473766556652; asc CAAF3cH7hfTIBAEre51UEITuG7fUfR; (total 199 bytes);
 8: len 8; hex 8000000054060653; asc     T  S;;
 9: len 9; hex 800000000000098440; asc         @;;
 10: len 1; hex 01; asc  ;;
 11: len 30; hex 313034383539303739377c313432343530303835307c3130303030303937; asc 1048590797|1424500850|10000097; (total 85 bytes);
 12: len 30; hex 68747470733a2f2f67726170682e66616365626f6f6b2e636f6d2f313030; asc graph.facebook.com/10000; (total 50 bytes);
 13: SQL NULL;
 14: len 4; hex 80000001; asc     ;;
 15: SQL NULL;
 16: SQL NULL;
 17: len 4; hex 00000000; asc     ;;
 18: len 3; hex 312f37; asc 1/7;;
 19: len 4; hex 00000000; asc     ;;
 20: len 8; hex 8000000000000018; asc         ;;
 21: len 4; hex 6bdb7e45; asc k ~E;;
 22: SQL NULL;
 23: len 8; hex 8000000000000000; asc         ;;
 24: len 8; hex 8000000000000018; asc         ;;

*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS
------------
Trx id counter 1929046
Purge done for trx''s n:o < 1929045 undo n:o < 0 state: running but idle
History list length 1711
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 28864, OS thread handle 0x2ad677681700, query id 357530 122.169.237.65 Database1 init
SHOW ENGINE INNODB STATUS
---TRANSACTION 1929043, not started
MySQL thread id 28863, OS thread handle 0x2ad677640700, query id 357529 ec2-54-226-145-7.compute-1.amazonaws.com 54.227.116.3 Database1 creating table
create table temp_ceiling (actor_id int, p_change float, cval float, fval float)
---TRANSACTION 1929042, not started
MySQL thread id 28859, OS thread handle 0x2ad675ee3700, query id 357510 localhost 127.0.0.1 rdsadmin cleaning up
---TRANSACTION 1929045, ACTIVE 0 sec committing foreign key definitions
5 lock struct(s), heap size 1248, 0 row lock(s), undo log entries 8
MySQL thread id 28863, OS thread handle 0x2ad677640700, query id 357529 ec2-54-226-145-7.compute-1.amazonaws.com 54.227.116.3 Database1 creating table
create table temp_ceiling (actor_id int, p_change float, cval float, fval float)
TABLE LOCK table `SYS_TABLES` trx id 1929045 lock mode IX
TABLE LOCK table `SYS_COLUMNS` trx id 1929045 lock mode IX
TABLE LOCK table `SYS_TABLESPACES` trx id 1929045 lock mode IX
TABLE LOCK table `SYS_DATAFILES` trx id 1929045 lock mode IX
TABLE LOCK table `SYS_INDEXES` trx id 1929045 lock mode IX
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o''s: 0, sync i/o''s: 0
Pending flushes (fsync) log: 0; buffer pool: 0
946 OS file reads, 122080 OS file writes, 61052 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 4000.00 writes/s, 5000.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 612301, node heap has 33 buffer(s)
0.00 hash searches/s, 5.00 non-hash searches/s
---
LOG
---
Log sequence number 502059361
Log flushed up to   502055320
Pages flushed up to 502053286
Last checkpoint at  502053286
0 pending log writes, 0 pending chkp writes
39359 log i/o''s done, 1.00 log i/o''s/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 316588032; in additional pool allocated 0
Dictionary memory allocated 269802
Buffer pool size   18880
Free buffers       11148
Database pages     7699
Old database pages 2822
Modified db pages  16
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 886, created 6813, written 73085
0.00 reads/s, 4000.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 7699, unzip_LRU len: 0
I/O sum[0]:cur[1], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Main thread process no. 18631, id 47100585178880, state: sleeping
Number of rows inserted 43271, updated 151830, deleted 2009, read 7297650700
0.00 inserts/s, 14.49 updates/s, 0.00 deletes/s, 167000.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
'

请帮助

0 个答案:

没有答案