Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial mysql表列复制,mysql连接时间和内存控制。

mysql表列复制,mysql连接时间和内存控制。

Jun 23, 2016 pm 02:10 PM

有两个表临时表temp_t,和正表table1,都是MyISAM,分别有28W条左右的记录。
cron定时每分钟从网络上获取一些信息,先存储到temp_t(频繁写入),
然后cron定时每10分钟从temp_t复制信息到正表table1(频繁读取,每10分钟写一次)。

temp_t字段 id(auto_increment),title(varchar(200)),content(varchar(500)),date(DATETIME), mykey(tinyint(1) Default 1, 这个来判别是否已经复制到正表,复制过后UPDATE为2)

table1字段 id(没有auto_increment),title(varchar(200)),content(varchar(500)),date(DATETIME)


28W记录,每个数据表大概有300MB,每次更新差不多要2K-3K条记录。
如果直接复制的话,MYSQL连接时间过长,导致MYSQL性能下降table1会出现类似于锁表的现象,查询table1的时间明显加长

require dirname(__FILE__) . '/../connection.php';mysql_select_db("news",$connextion);mysql_query("SET NAMES utf8");$query = mysql_query("SELECT * FROM temp_t where mykey = '1'");while($rows = mysql_fetch_array($query)){	mysql_query("UPDATE temp_t SET mykey='2' WHERE id='".mysql_real_escape_string($rows['id'])."'");	mysql_query("INSERT INTO table1 (id,title,content,date) values ('".mysql_real_escape_string($rows['id'])."','".mysql_real_escape_string($rows['title'])."','".mysql_real_escape_string($rows['content'])."','".mysql_real_escape_string($rows['date'])."'");}mysql_close($connextion);
Copy after login



于是想先把记录从temp_t里的数据先读出来,存为一个json数组,然后再复制数据到table1,但是有时会遇到Cannot allocate memory,然后导致整个服务器崩溃(是不是这个json数组太大,占了巨量的内存)。PS:已经在php.ini里将memory limit加到64M了(但总觉得治标不治本,这么一个复制过程,不需要那么多内存,关键还是代码不合理)。

require dirname(__FILE__) . '/../connection.php';mysql_select_db("news",$connextion);mysql_query("SET NAMES utf8");$query = mysql_query("SELECT * FROM temp_t where mykey = '1'");$jon = array();while($rows = mysql_fetch_array($query)){	$jon['a'] = $rows['id'];	$jon['b'] = $rows['title'];	$jon['c'] = $rows['content'];	$jon['d'] = $rows['date'];	$pjon .= json_encode($jon).',';}mysql_close($connextion);$njso = json_decode('['.substr($pjon,0,-1).']');foreach($njso as $nx){		if($nx->a){		require dirname(__FILE__) . '/../connection.php';		mysql_select_db("news",$connextion);		mysql_query("SET NAMES utf8");		mysql_query("UPDATE temp_t SET mykey='2' WHERE id='".mysql_real_escape_string($nx->a)."'");		mysql_query("INSERT INTO table1 (id,title,content,date) values ('".mysql_real_escape_string($nx->a)."','".mysql_real_escape_string($nx->b)."','".mysql_real_escape_string($nx->c)."','".mysql_real_escape_string($nx->d)."'");		mysql_close($connextion);	}}
Copy after login


所以求助大虾们,有没有更节省的代码用来复制表的信息?要求mysql连接时间短,内存消耗又少?谢谢大家。


回复讨论(解决方案)

依次执行这两句就可以了
INSERT INTO table1 (id,title,content,date) values
SELECT id, title,content,date from temp_t where mykey = '1'

UPDATE temp_t SET mykey='2' WHERE mykey = '1'

不需劳驾 php 转交

既然 temp_t 是临时存放,那么转移后的数据为何不删除呢?

依次执行这两句就可以了
INSERT INTO table1 (id,title,content,date) values
SELECT id, title,content,date from temp_t where mykey = '1'

UPDATE temp_t SET mykey='2' WHERE mykey = '1'

不需劳驾 php 转交

既然 temp_t 是临时存放,那么转移后的数据为何不删除呢?

temp_t的数据还不能删,insert时还有一个判断,就是title不重复(避免重复插入)
按照老大的意思,这样执行就可以了?还没测试,得停掉cron脚本。
弱弱的问一句,依老大的经验,300MB数据表,像这样一次插入2000-3000条记录,这个脚本大概要运行多长时间,耗费多少内存?是不是my.cnf里要增加某些值的内存,用来加速INSERT和SELECT的执行时间?再次感谢。

require dirname(__FILE__) . '/../connection.php';mysql_select_db("news",$connextion);mysql_query("SET NAMES utf8");$query = mysql_query("INSERT INTO table1 (id,title,content,date) values SELECT id, title,content,date from temp_t where mykey = '1'");$query = mysql_query("UPDATE temp_t SET mykey='2' WHERE mykey = '1'");}mysql_close($connextion);
Copy after login

1:首先临时表,又没对外读取?如果有为啥不memcache,然后table1在去读取memcache回来?

2:300m 不算大吖。别人文学站,冬不冬一个表就上10G的。一样复制来去。

3:以前公司做个项目,1个表就400万条数据,采用的方法,从临时表里,1条1条的读取,慢慢读,不用把所有事情挤在一起来出来,这样虽然是时时刻刻的运行着,总共比卡死好吖。

1:首先临时表,又没对外读取?如果有为啥不memcache,然后table1在去读取memcache回来?

内存本来就不够,还装memcache……人家是nginx服务器集群,俺这块是几十元的虚拟空间……

10G的表复制,是不是my.cnf里需要增加某些值的大小?
总感觉自己的表300M运行速度慢,还经常卡死。人家天文数字,频繁读写的照样工作顺畅。


1:首先临时表,又没对外读取?如果有为啥不memcache,然后table1在去读取memcache回来?

内存本来就不够,还装memcache……人家是nginx服务器集群,俺这块是几十元的虚拟空间……

10G的表复制,是不是my.cnf里需要增加某些值的大小?
总感觉自己的表300M运行速度慢,还经常卡死。人家天文数字,频繁读写的照样工作顺畅。


那你是用第三条好点,无论你多大多复杂的数据,1条1条的来。1-2秒1条,1天下来也很恐怖了。
没必要一次倒那么多出去的,如果非的一次倒,多大的集团公司也不敢这样做的。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles