English 中文(简体)
是否在MySQL原子中增加一个领域?
原标题:Is incrementing a field in MySQL atomic?

我在制作一个网站时想在标准MyISAM表格中增加反射。

简化实例:

UPDATE votes SET num = num + 1;

如果多种联系正在同样地进行,或MySQL将予以照顾,并锁定桌子或东西以确保没有冲突,这是否会造成问题?

最佳回答

MyISAM表格使用表格的锁定。 这意味着,整个表格将在执行你最新询问时锁定。 因此,你简化使用案例的答案是:是的,这已经变得安全。 但是,如果你使用另一个储存引擎或更新包括多个表格,情况可能并非如此。

这里是MySQL手册的引文,以便更清晰:

Table locking enables many sessions to read from a table at the same time, but if a session wants to write to a table, it must first get exclusive access. During the update, all other sessions that want to access this particular table must wait until the update is done.

如果符合你的设计,你也可以考虑使用自动加固栏、交易或外部同步。

Cheers!

问题回答

文字是原样,但增量也需要读写。 因此,问题是:你是否相信读物是安全的,换言之,你是否确保了另一个正在做增量工作的人不会最终获得同样的价值? 我有疑虑。 这样做的正确方法是100%。

-- begin transaction here

select counter from myCounters where counter_id = 1 FOR UPDATE;

-- now the row is locked and nobody can read or modify its values

update myCounters set counter = ? where id = 1;

-- set ? to counter + 1 programmatically

commit; -- and unlock...

是的,表格(或非行格式数据库中的浏览量)在你进行更新询问时自动锁定。

原子为<编码>。 其他形式的<代码>UPDATE可以通过使用SlectT ...... FOR UPD进行原子处理。

如果存在同样的问题,尽管询问更为复杂:

UPDATE CLB_SYNC_T SET PENDING_MESSAGES = PENDING_MESSAGES + ? WHERE USER_ID = ?

利用MyISAM作为违约发动机,我没有帮助。 SlectT FOR UPDATE use.

由于我的SQL没有锁定整个桌子,因此通过<<>选择通用<>/strong>提高绩效约10倍,以更新行文。

在使用InnoDB时,在多个栏目中使用unique index:

Table Sessions { unique_key(browser_session_id,profile_id) // ensures that inserting 1 entry per session will occur once }

<slection(browser_session_id) from Sessions

由于不允许每个用户举行多次会议,将保证举行独特的会议。

<><>Conclusions

  • 优点

    Each insert does require a pre-select.

  • 缺点

    这并不适合所有案件。

    可能降低写作业绩,需要额外管理





相关问题
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 ...

热门标签