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

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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

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

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

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

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