search
HomeBackend DevelopmentPHP Tutoriallinux - 大家对PHP多进程与MySQL的高并发瓶颈是怎么处理的

需求

  • 每天执行一个定时任务,把一个具有千万条数据的日志分割成一些小的日志文件;
  • 然后启动多个PHP进程,分别对这些小日志文件中的每行数据进行处理,处理过程是这样的:每个PHP进程从各自负责的日志文件中逐行读取,然后根据该行中的一个标识去数据库中查找是否有了该记录,如果没有则插入该行,如果已经存在了就更新(该行的数据与数据库中的数据进行合并);

引发的问题

这样就出现了一个问题,就是可能存在多个PHP进程对同一条数据都在做处理,如果用Mysql锁机制可以避免这个问题,但是这样的话,就出现另一个问题,就是MySQL的高并发瓶颈,因为我是多个PHP进程同时在进行,一个进程要等待另一个进程解锁后,才能操作数据库。

这样我前面所做的分割日志、启动多个进程这些操作就失去意义了,因为我在前面做日志分析时,启的进程再多,运行的再快,到最后全都会卡在高并发操作数据库这个瓶颈上,不知道大家对这类问题是怎么处理的?

回复内容:

需求

  • 每天执行一个定时任务,把一个具有千万条数据的日志分割成一些小的日志文件;
  • 然后启动多个PHP进程,分别对这些小日志文件中的每行数据进行处理,处理过程是这样的:每个PHP进程从各自负责的日志文件中逐行读取,然后根据该行中的一个标识去数据库中查找是否有了该记录,如果没有则插入该行,如果已经存在了就更新(该行的数据与数据库中的数据进行合并);

引发的问题

这样就出现了一个问题,就是可能存在多个PHP进程对同一条数据都在做处理,如果用Mysql锁机制可以避免这个问题,但是这样的话,就出现另一个问题,就是MySQL的高并发瓶颈,因为我是多个PHP进程同时在进行,一个进程要等待另一个进程解锁后,才能操作数据库。

这样我前面所做的分割日志、启动多个进程这些操作就失去意义了,因为我在前面做日志分析时,启的进程再多,运行的再快,到最后全都会卡在高并发操作数据库这个瓶颈上,不知道大家对这类问题是怎么处理的?

写过类似的东西,像这种需要多次更新同一账号数据的时候,我们的做法是处理完数据后不立即存入mysql,
而是放在了redis中,后面的数据更新都在redis修改,
在redis积累了数千条数据后在异步的写一次数据到mysql,效果不错
卤煮的场景多进程处理建议只负责数据处理,处理完的放redis,在单独用一个脚本定时检测redis
达到条件的时候(一定条数或时间)存一次数据到mysql

上面的两种方法都可行

建主键索引,使用 upsert 语句是最便捷的解决方法。如上 @felix021 大神所言。

另外,使用缓存(Memcache 或者 redis )异步写入数据也不错,只是开发代价要高一些。

加队列。做daemon执行队列操作。

如果只是日志数据的话,题主可以直接用你那个标记记录的唯一表示当做主键|唯一键,直接往数据里面插东西,啥也不用管。
这样的话既不会锁表也不用去检查这条记录有没有存在。

1,读加缓存。
2,写加队列。
3,推荐Redis。
4,分库分表分机器。

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
What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

What is the importance of setting the httponly flag for session cookies?What is the importance of setting the httponly flag for session cookies?May 03, 2025 am 12:10 AM

Setting the httponly flag is crucial for session cookies because it can effectively prevent XSS attacks and protect user session information. Specifically, 1) the httponly flag prevents JavaScript from accessing cookies, 2) the flag can be set through setcookies and make_response in PHP and Flask, 3) Although it cannot be prevented from all attacks, it should be part of the overall security policy.

What problem do PHP sessions solve in web development?What problem do PHP sessions solve in web development?May 03, 2025 am 12:02 AM

PHPsessionssolvetheproblemofmaintainingstateacrossmultipleHTTPrequestsbystoringdataontheserverandassociatingitwithauniquesessionID.1)Theystoredataserver-side,typicallyinfilesordatabases,anduseasessionIDstoredinacookietoretrievedata.2)Sessionsenhances

What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version