php导入大量数据到mysql性能优化
在mysql中我们结合php把一些文件导入到mysql中,下面我来分享我对15000条记录进行导入时分析与优化,各位朋友可参考。
之前有几篇文章,说了最近tiandi在帮朋友做一个小项目,用于统计电话号码的,每次按需求从数据库里随机生成打包的电话号码,然后不停地让人打这些电话号码推销产品(小小鄙视一下这样的行为)。但是朋友要求帮忙,咱也不能不帮啊,是吧。程序两个星期前已经做好,测试完毕交工。前几天朋友来电说,每天导入电话号码的时间越来越长,有时候一万条记录就要半个小时以上,看看能不能想办法提高一下这个速度。
我理了一下思路,数据库结构很简单,可以认为就两个字段,一个字段存电话号码,另一字段存类别,类别分别为c,d,e等等,分别代表已经拨通过此电话,未拨通过此电话,未拨打过此电话等等状态,而整个程序逻辑是这样的:
■拿到一个txt文件,里面存的是电话号码
■通过程序将txt文件导入到mysql里
■导入的时候,检测txt里的电话号码是否和mysql里的重复,如果不重复,直接插入新记录,如果重复,就需要按照判断电话号码所属类别来进行更新。
由于每个txt里的电话号码导入时,都需要做一次比较,所以程序肯定会耗时一些,这里我们先撇开这个原因,因为本文章的标题是优化写入速度,那么程序什么时候会写入记录呢?通过上面的逻辑得知,在匹配数据库时,没有发现存在记录时会发生写入数据库操作(当然update也算,只是这里只讨论insert)。那么将上述逻辑转化为代码,差不多如下:
代码如下 | 复制代码 |
//$array为txt文件explode出来的数组,每一个为一个电话号码, $str为类型 |
以上代码完全正确,但是效率低下,当txt文件里包含了上万个电话号码时,即会有上万次的插入数据库操作,虽然每次的数据库写入操作都是很快的,但是上万条累计下来,这个执行时间不容忽视。tiandi简单的测试了一下插入15000万条记录,耗时差不多5分钟。如果再加上之前的逻辑判断等等过程,那么半个小时还真得不算少了。这样可不行,必须减少数据库库写入次数才对,于是上面代码变更为以下:
代码如下 | 复制代码 |
$sql2="INSERT INTO ".$usertable." (tel,type,updatetime) VALUES"; |
这样,整个写入操作只有1次,大大地缩短了执行时间,差不多10秒就搞定了15000条记录。好了,本文到此结束,如果你也遇上写入大量数据到mysql耗时长的问题时,不如试试本文的优化方式。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

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

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.
