English 中文(简体)
我的我的我的我的我的我的我的SQL僵局情景——需要咨询意见
原标题:my MySQL deadlock scenario - advice needed

I need a expert advice on this.

Background PHP/ Codeigniter MYSQLi Innodb table - fares_table Stored Procedure

Backend - A cronjob php script is written to insert/update data(from a service) into the fares_table every few minutes. (normal sql query)

Frontend - Users will be able to read these data (the query is written in a stored procedure form because it involves joining of many tables, hence my stored procedure has create temporary table from select statements from fares_table and joined to other tables)

<><>Problem

Deadlock found when trying to get lock; try restarting transaction

A deadlock may occur if a user happen to stumble upon the frontend while the fares_table are updating/inserting. Deadlocks happen to the update statement

The deadlock is caused by stored procedure trying to wait for lock to released while trying to create temporary tables with select statements from fares_table while the backend doing an insert or update trying to wait for lock to release.

LATEST DETECTED DEADLOCK
------------------------
110408  9:05:45
*** (1) TRANSACTION:
TRANSACTION 0 203543446, ACTIVE 0 sec, OS thread id 6584 fetching rows
mysql tables in use 2, locked 2
LOCK WAIT 761 lock struct(s), heap size 60736, 30170 row lock(s)
MySQL thread id 86268, query id 135039790 XXXXXXX Copying to t
CREATE TEMPORARY TABLE tmp_tb1 AS SELECT MIN( fare ) as cheapest_fare,flighttype origin,destination ....
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 18433 n bits 240 index `PRIMARY` of table `db_name`.`fares_table`
Record lock, heap no 85 PHYSICAL RECORD: n_fields 18; compact format; info bits 0
0: len 4; hex 8025d996; asc  %  ;; 1: len 6; hex 00000c21d3a9; asc    !  ;; 2: len 7; hex 0000000b031

*** (2) TRANSACTION:
TRANSACTION 0 203543465, ACTIVE 0 sec, OS thread id 3080 updating or deleting, thread declared inside
mysql tables in use 1, locked 1
3 lock struct(s), heap size 320, 2 row lock(s), undo log entries 1
MySQL thread id 85631, query id 135039816 XXXXX Updating
UPDATE `fares_table` SET `fare` = 2552.85, `currency` =  AUD ..
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 18433 n bits 240 index `PRIMARY` of table `db_name`.`fares_table`
Record lock, heap no 85 PHYSICAL RECORD: n_fields 18; compact format; info bits 0
0: len 4; hex 8025d996; asc  %  ;; 1: len 6; hex 00000c21d3a9; asc    !  ;; 2: len 7; hex 0000000b031

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 2086 n bits 600 index `flighttype_idx` of table `db_name`.`fares_table`
Record lock, heap no 218 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
0: len 7; hex 4f6e6520776179; asc One way;; 1: len 3; hex 424e45; asc BNE;; 2: len 8; hex 8000124a588

*** WE ROLL BACK TRANSACTION (2)

www.un.org/Depts/DGACM/index_spanish.htm 我的临时固定地址:

Catch database error 1213 and retry the updating query. It works now , but i would like to find a much better solution of preventing deadlock instead. Any expertise advice?

• 如何改变防止僵局的命令或复制<代码>飞行类型_idx索引 帮助?

问题回答

由于设立临时表格的问询,由于总职能<地雷编码>(农场)而基本上将表的主要部分锁定下来,而且最新情况需要等待完成,因此没有可以解决僵局的简单重新排序。

It would be better to surround the contention by an explicit locking mechanism, perhaps on a lock table just for this purpose, rather than let the transactions compete and then have to roll back. In particular, the create table statement cannot be rolled back, strange as that may seem. See the LOCK TABLE documentation.

为了辅助执行,将更新说明变成一个储存程序,并储存更新和设定时间表的程序,以便检查和设置一个锁,开展工作,然后锁定。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签