Home  >  Article  >  Database  >  MySQL lock mechanism and PHP lock mechanism

MySQL lock mechanism and PHP lock mechanism

巴扎黑
巴扎黑Original
2016-11-08 13:39:021101browse

Simulation preparation--how to simulate high concurrent access to a script: bin/ab.exe of the apache installation file can simulate the amount of concurrency -c - how much concurrency is simulated -n how many times in total http:// requests are requested for the script
For example: cmd: apache installation path/bin/ab.exe -c 10 -n 10 http://web.test.com/test.php

【Get to the point】
Locks in MYSQL:
Syntax:
LOCK TABLE table name 1 READ |WRITE, table name 2 READ|WRITE ............. [Lock table]
UNLOCK TABLES [Release table]


Read: read lock|shared lock: all Clients can only read this table but not write this table
Write: write lock|exclusive lock: All currently locked clients can operate this table, other clients can only block
Note: You can only operate during the process of locking the table If you want to operate a locked table, all tables to be operated must be locked!

File lock in PHP (the file is locked, not the table)
What is the relationship between the file locked and the table? : It has no relation at all. It is similar to a token. Whoever gets it will operate it. So the table is not locked at all.
When testing, as long as there is a file, it doesn’t matter what the name is.

Summary:
You should only use file locks in PHP in the project, and try to avoid table locks, because if the table is locked, then everything in the entire website related to this table All functions have been slowed down (for example: many users at the front desk have been placing orders, the product table MySQL locks the table, and other operations related to the product table have been blocked [cannot read the product table], because one function slows down the entire website. slow).

One of my projects is O2O takeout. 12-2pm and 6pm are the times when order concurrency is high. In this case, MySQL lock is obviously not considered, and the user experience is too poor. In fact, depending on actual needs, there is no need to design inventory for takeaways. Of course, in addition to the flash sale activity module, PHP file lock is still required.

Application scenarios:
1. When placing orders with high concurrency, locking is required when reducing inventory
2. When ordering and grabbing tickets with high concurrency, use

MySQL lock sample code:

<?php
/**
模拟秒杀活动-- 商品100件
CREATE TABLE a
(
    id int comment &#39;模拟100件活动商品的数量&#39;
);
INSERT INTO a VALUES(100);
模仿:以10的并发量访问这个脚本!    使用apache自带的ab.exe软件
 */
error_reporting(0);
mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;admin123&#39;);
mysql_select_db(&#39;test&#39;);
# mysql 锁
mysql_query(&#39;LOCK TABLE a WRITE&#39;);// 只有一个客户端可以锁定表,其他客户端阻塞在这
$rs = mysql_query(&#39;SELECT id FROM a&#39;);
$id = mysql_result($rs, 0, 0);
if($id > 0)
{
    --$id;
    mysql_query(&#39;UPDATE a SET id=&#39;.$id);
}
# mysql 解锁
mysql_query(&#39;UNLOCK TABLES&#39;);

PHP file lock sample code:

<?php
/**
模拟秒杀活动-- 商品100件
CREATE TABLE a
(
    id int comment &#39;模拟100件活动商品的数量&#39;
);
INSERT INTO a VALUES(100);
模仿:以10的并发量访问这个脚本!    使用apache自带的ab.exe软件
 */
error_reporting(0);
mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;admin123&#39;);
mysql_select_db(&#39;test&#39;);
# php中的文件锁
$fp = fopen(&#39;./a.lock&#39;, &#39;r&#39;); // php的文件锁和表没关系,随便一个文件即可
flock($fp, LOCK_EX);// 排他锁
$rs = mysql_query(&#39;SELECT id FROM a&#39;);
$id = mysql_result($rs, 0, 0);
if($id > 0)
{
    --$id;
    mysql_query(&#39;UPDATE a SET id=&#39;.$id);
}
# php的文件锁,释放锁
flock($fp, LOCK_UN);
fclose($fp);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn