9

If I have a php script which calls INSERT, UPDATE, DELETE, etc on a MySQL connection, and that script gets called at uncontrolled times by a POST operation, is it always "safe" (ie, will not result in corrupt tables or collisions during requests)?

For example, if 500 requests come during a 1-second period.

If so, how does php/mysql achieve this?

If not, what does one need to do to guarantee "serial" access or safe simultaneous access?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
SG1
  • 2,871
  • 1
  • 29
  • 41

6 Answers6

11

MySQL uses locking (table-level for MyISAM or row-level for InnoDB), which does not allow 2 processes (2 calls to the script) to modify the same row. So the table won't crash*, but it's possible that MySQL can't handle the number of request in reasanoble time and the requests will wait. You should always optimize your queries to be as fast as possible.

*MyISAM could crash on insert/update intensive applications, but it has automatic recovery. However keep in mind that in such application, InnoDB has far better performance

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • 1
    There were many excellent answers to this question; I can't believe the speed and response of the community in this case - thank you. I believe that some of the other answers may be more instructive for someone seeking to build a rock-solid, enterprise-level implementation - if that is you, see below. I, however, am trying to do as little as possible and simply wanted to know if mysql could handle lots of simultaneous requests. The answer is yes to anyone who is not being paid specifically to know another answer. Thank you Darhazer. – SG1 Mar 01 '12 at 22:06
2

is it always "safe" (ie, will not result in corrupt tables or collisions during requests)?

yes

If so, how does php/mysql achieve this?

table/row locks.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2

MySQL uses locks for Isoloation, and transactions for Atomicity. Transactions require InnoDB or BDB. InnoDB supports full ACID support.

Locks and transactions, combined, will resolve your concurrency issue.

By default, MySQL has implicit transactions.

Definitely learn about these features to see if they fit the bill. MyISAM uses table locking, whereas InnoDB provides row level locking, so one may serve your needs better than the other.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
1

Usually databases are solid to collisions, however there are important operations those must be completed or discarded. Think about a cash deposit on a bank account.

In order to achieve this result you could be interested in using transactions:

PHP + MySQL transactions examples

Community
  • 1
  • 1
Bedo
  • 925
  • 2
  • 14
  • 27
0

Use transactions... See this

Joey
  • 10,504
  • 16
  • 39
  • 54
0

I think the terminology you are looking for is transactions and isolation levels. If these are set according to your requirement, you don't need to worry about collisions. Here is tutorial on how it works.

kosa
  • 65,990
  • 13
  • 130
  • 167