Table of Contents
回复内容:
Home Backend Development PHP Tutorial php高并发下的疑问。

php高并发下的疑问。

Jun 06, 2016 pm 08:22 PM
php

在高并发下,如果我插入一条数据,返回一个insert_id,那么这个insert_id会不会为别人所用呢。如果不被别人所用,php是怎么做到访问隔离的。

回复内容:

在高并发下,如果我插入一条数据,返回一个insert_id,那么这个insert_id会不会为别人所用呢。如果不被别人所用,php是怎么做到访问隔离的。

楼主的理解有问题。。。
MySQL的LAST_INSERT_ID()或者说PHP的mysql_insert_id(connection)。
请注意,Connection
每次插入数据,MySQL会返回数据给【当前的数据库连接】,告知是插入到第几个位置(ID)的。

根本不存在什么"访问隔离","为别人所用"之类的问题。

——————————————————————————————————————————————————

数据库的存在,本身就是为了解决并发的问题。
查询是可以并发的(同时读取文件不会有问题)。
插入、修改、删除必须是队列方式(同时修改同一个文件???)。

楼主的理解有问题,一个等一个就会一卡一卡的?
举个栗子,现在的一般的机械硬盘的写入速度30-50M/s。
一次SQL写入请求,平均写入3-5KB数据(你TM在逗我,怎么可能这么多),那么理论上,也可以支持10000/s的并发量。
什么叫一卡一卡的 = =

—————————————————————————————————————————————————

回想当年……2333333……
当初我还不会PHP的时候被同学拉去做网站,报名参加活动的那种只有几个网页的网站。
数据提交之后,在PHP里,我就自己定义了一个格式,一条数据一行,把数据全都保存到一个txt文件里。
(在相当长一段时间里,我都觉得,数据库貌似也没那么重要啊,直接读写文件不就完了)
直到后来……
同时对一个文件进行写操作就会发生异常(很底层的问题,操作系统级别的)。
同时对一个文件加锁(防止同时写操作),也可能发生异常(还是操作系统级别的问题)。
对txt的指定位置进行修改,删除,必须从文件里全部读入内存,然后处理,然后写到文件里。
想修改格式,查询,排序……全都要全部读取全部写入。
这时候遇到并发的问题……彻底爆炸,数据各种丢失错误混乱。。。

数据库已经基本是相当完美的解决了这些乱七八糟的问题了。。。

嗯,是的,每个请求的链接都是独立的,10个并发就是10个链接对象,所以insert_id永远不会被其他请求的链接所用!!!

php层面的“隔离”
这个就是PHP的“特性”---每个请求都是一个进程,php-fpm每次都会分配一个对应的worker来处理这个请求。
每个操作都在对应的php-fpm下属的worker下进行,内存管理也是一样。
处理完这个进程就关闭。
你插入的返回id是在各自请求对应的php-fpm下属的worker内存中,不会出现你说的情况。

mysql层面的“隔离”
跟引擎有关咯。简单说引擎对应的写入处理不同。MyIASM是表锁,Innodb是行锁,都可以在一定程度上避免并发问题。再高的并发,就要用其他的处理方式了,比如队列等等。

php即便是高并发,单进程连数据库还是单个连接的。
insert id是根据mysql连接的上一条insert语句返回的。
所以对于一个php进程,一个mysql连接,返回的insert id是不会混乱的

兄弟啊,你需要看一本书叫数据库原理,数据库里面有一个概念叫ACID,事务就两种状态,成功和失败。你这边都insert进去了,那就是成功了,就last insert id也是这个事务的返回值。
MySQL本身也支持并发,开N个connection去同步的insert,也不会有问题,最多就是慢一点。

PS:PHP能写出来多高的并发??

php中一个请求一般新生成一个mysql连接,多个session同时请求mysql插入操作如何同步,这是mysql解决的问题。mysql 插入操作是加锁的,保证了原子性。

@MrGeneral 说的有部分问题,在这里贴出来,大家补充下:
---每个请求都是一个进程,每次都会启动一个php-fpm。
这句话,每个请求都是一个进程,没错,但是每次都会启动一个php-fpm?

这个是不对的,每次都会启动一个fastcgi的进程,但是这个进程是不是每一次都重新创建的,那倒未必,可能是从php-fpm维护的”进程池“里面取的,也就是说,php-fpm负责进程的管理,部分优化,比如cache住啊,避免了每次都创建进程的开销。而php-fpm本身是常驻的。

求反驳~

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles