简体   繁体   English

太多连接用于在mysql中插入数据

[英]Too many connections are used for insert data in mysql

I am aksing about too many connections in mysql server and phpmyadmin, as data speed per hour ~1000 from source in my database. 我想知道在mysql服务器和phpmyadmin中有太多的连接,因为我的数据库中的数据速度每小时约为1000。 When I TOP on server, mysqld shows 100-145% cpu utilization also lots of php threads are running there , as data insertion through php script. 当我在服务器上运行TOP时,mysqld显示出100-145%的cpu利用率,并且有很多php线程正在运行,这是通过php脚本插入数据的过程。 My question is, is there any mechanism through which connections are not overflow and mysql server utilize cpu as low as possible. 我的问题是,有没有机制可以防止连接溢出并且mysql服务器使用的CPU越低越好。 since I am using VPS server but it is not so effective to use another application on vps, because mysql server takes 100% cpu. 因为我使用的是VPS服务器,但在vps上使用另一个应用程序却不太有效,因为mysql服务器占用100%的cpu。 Sometime phpmyadmin crashes and shows some folk error. 有时phpmyadmin崩溃并显示一些民间错误。 I have read query cache mechanism, but it is used for caching same select queries, should i use this for insert or is there any other optimal solution kind of queue mechanism . 我已经阅读了查询缓存机制,但是它用于缓存相同的选择查询,应该将其用于插入还是有其他最佳解决方案类型的队列机制 Also, i can't say about on which time insert data speed is low or high, if it knows then i can try queue mechanism. 另外,我不能说插入数据的速度是低还是高,如果知道的话我可以尝试队列机制。 Thanks in advance! 提前致谢!

Check your codes and see if you have something like 检查您的代码,看看是否有类似的东西

for($i = 0; $i < 10000; $i++) // basicly long loop with inserts
    mysql_query("INSERT INTO table_name (id, value) VALUES ({$i}, 100)");

This should be altered to something like 应该将其更改为类似

$arr_values = array();
for($i = 0; $i < 10000; $i++)
    $arr_values[] = "({$i}, 100)";

$str_values = implode(', ', $arr_values);

mysql_query("INSERT INTO table_name (id, value) VALUES {$str_values}");

Basicly be aware of unneeded queries in your code. 基本上要注意代码中不需要的查询。

您应该从以下链接mysql_query缓存中获得有关“ QUERY CACHE”的基本概念

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM