Table of Contents
回复内容:
前端
接口
后端服务
一般来说都是前端限制下,比如button按钮在后端响应结束之前置灰;一般需求这样做就足够了
Home Backend Development PHP Tutorial mysql - php高并发下如何防止数据的重复写入

mysql - php高并发下如何防止数据的重复写入

Jun 06, 2016 pm 08:18 PM
mysql php

前端请求php写入数据的接口太频繁,导致mysql中出现大量重复数据,如何处理

回复内容:

前端请求php写入数据的接口太频繁,导致mysql中出现大量重复数据,如何处理

这个问题从三个方面来回答题主:

前端

前端的话要对请求进行限制,通过disabled提交按钮或者是设置定时器来禁止用户多重提交。

接口

接口一定要做好limit,但是这个并不是解决这个问题的关键点,因为你不可能设置一个limit为1 time per min。为什么关注这个点是因为有时候用户并不是一个人,可能是恶意程序。

后端服务

个人觉得这个是重点,它包括PHP端和Mysql。我不清楚你的数据是怎样的,我举例新浪微博。如果一个用户写好微博然后快速点击了两次发送,你会看到什么?两条相同微博么?不可能,出现了你来找我好了。最简单的做法,要对上次数据缓存,然后校验是否是重复提交的。Mysql结构要设计好,如果你的数据要唯一,那么你的字段和主键设计时候要设置确保唯一性。

先码这么多,解决方法有好多种,可以去搜索一下。

高并发还行吧,防止重复一般都是从数据上去处理的,前端也会做限制,因为重复数据肯定是重复请求,那么页面上防止重复点击肯定是要做的,这肯定不是什么特别有效的方式,加上唯一的约束肯定是最好的方式,无论是请求时第三方的唯一约束,还是时间或者其他你们数据能称作唯一的肯定是最好的。不过重复感觉很难避免,百度贴吧或者微信,qq有时候也会出现重复,尤其是服务器性能降低卡顿的时候。

前端的请求是冗余的吗,是的话前端要做请求防重

最佳解决思路为:将并行转换为串行。

转换的方法有很多,常见的就是队列形式。

首先你可以用UNIQUE INDEX给相应的field防止重复写入

<code>比如 ALTER TABLE thetable ADD UNIQUE INDEX(pageid, name);</code>
Copy after login

其次,要你的选择可以是:

<code>// 选择一:覆盖之前同id的词条
INSERT INTO thetable (pageid, name, somefield)
VALUES (1, "foo", "first")
ON DUPLICATE KEY UPDATE (somefield = 'first')

// 选择二:添加重复计数器
INSERT INTO thetable (pageid, name)
VALUES (1, "foo"), (1, "foo")
ON DUPLICATE KEY UPDATE (pagecount = pagecount + 1)</code>
Copy after login

最后,可以在写入之前先查找,求count(*),不过这种效率比较低。

好像没有有效的方式完美解决这个问题,可以设置一个缓存判断,执行过写入一次,就不再执行下次同一写入

熟悉linux可参考sudo锁;

缓存吧 内存建key 插入关系数据库 延迟销毁 key

好奇怪你们难道都不知道mysql的共享锁吗

加锁啊,共享锁

一般来说都是前端限制下,比如button按钮在后端响应结束之前置灰;一般需求这样做就足够了

七目鱼

前端限制 -- 过滤频繁的请求,如按钮变灰,或者控制一个标志变量来判断是否执行ajax请求 。。这个只是体验,并不能真的限制~~
关键是后端限制
比如: select for update

整个请求的过程都可以做放置重复提交的验证。

  1. 在前端控制,比如按钮置灰等

  2. 在后端业务逻辑中,通过查询,判断该内容是否已经提交过(通过缓存来记录之类的)

  3. 在数据库层通过唯一键的方式来限制重复提交的记录。对于重复提交的内容自动被过滤。

在高并发的情况下,其实还要考虑下db 是不是能够扛得住并发,所以增加队列的限制,使得并发在进入db 的时候是一个串行的操作,当然这个还得看时机情况了。

MYSQL 的话用事务处理吧。。。

楼上回答的,有没有看问题啊。这种情况基本上只能靠前端做限制,或者API规定同一个唯一标识的请求频率。

需要说明什么数据重复了,重复了有什么影响?业务逻辑上要做避免重复的检查吗?先把业务逻辑定义清楚,才能有针对性的出解决方案。

前端先过滤掉重复刷新的。然后服务端再去过滤。

在表单里生成一个唯一的标识,提交的时候判断这个标识的内容有没有提交过了。

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 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.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles