- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经尝试了所有我能从类似问题中找到的建议,但似乎都没有帮助。
我正在运行一个 PHP 脚本来将一些数据添加到数据库中——只有大约 1000 行。这是我正在使用的完整代码:
<?php
header('Content-Type: text/plain');
$t_start = microtime(true);
require('../php/connect.php');
$data = json_decode(file_get_contents('base/en/stringlist.js'));
foreach ($data as $cat => $values) {
$category = mysql_real_escape_string($cat);
foreach ($values as $key => $info) {
$name = mysql_real_escape_string($key);
$text = mysql_real_escape_string($info->text);
$tip = mysql_real_escape_string(isset($info->tip) ? $info->tip : '');
$query = "INSERT INTO locale_strings (name,text,tip,category) VALUES ('$name','$text','$tip','$category')";
if (!mysql_query($query)) {
echo mysql_error() . "\n";
}
}
}
echo 'Time: ' . round(microtime(true) - $t_start, 4) . ' seconds.';
?>
(我为 mysql_
语法道歉)。我已经在 3 台 PC 上使用过它,它们都运行 Win7/8,并安装了相当新的 XAMPP。在其中两台机器上,该脚本执行大约需要 2-3 秒,而在第三台机器上,它会在 30 秒后超时(添加约 900 行后)。什么会导致它运行速度慢 10 倍?
如果我注释掉 mysql_query
行,运行需要 0.012 秒,所以代码本身不是问题。数据库位于本地主机上,并包含在 etc/hosts
文件中 - 与其他机器上的相同。
这是表结构:
CREATE TABLE IF NOT EXISTS `locale_strings` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`category` varchar(255) NOT NULL,
`text` text NOT NULL,
`locked` int(11) NOT NULL DEFAULT '0',
`tip` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4833 DEFAULT CHARSET=utf8;
ALTER TABLE `locale_strings`
ADD PRIMARY KEY (`id`);
ALTER TABLE `locale_strings`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4833;
最佳答案
InnoDB 存储引擎是 ACID合规。每个查询都以自动提交模式运行,并且会使用磁盘的输入输出操作,这是相当昂贵的。这意味着每个查询都会强制硬盘驱动器 (HDD) 写入一条记录(而不是让操作系统安排它)。您的 HDD(如果是机械硬盘)每秒大约有 300 个 I/O。这意味着对于大约 900-1000 条记录,写下它们可能需要 2-3 秒。
相比之下,正如我提到的,使用 MyISAM 存储引擎可以让操作系统安排写入。记录不会立即写入磁盘;他们是buffered在保存到磁盘之前在内存中。虽然这意味着 MyISAM 更快,但代价是数据丢失的风险(如果系统崩溃或断电,而缓冲区中的数据尚未写入磁盘)。
要对此进行优化,通常的方法是将多个插入包装到一个事务中。一种方法是使用 PDO ,准备语句,启动事务并在完成 1000 次插入后提交。这使得硬盘驱动器可以在一次操作中写下大量数据。
您使用 PDO 重写的代码将像这样流动(注意 - 没有测试,不要复制粘贴,仅供引用):
// connect.php - note that the code around PDO should go in the file connect.php as per example
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try
{
$pdo = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
//===================================================================
// The rest of the code
//===================================================================
// Decode JSON
$data = json_decode(file_get_contents('base/en/stringlist.js'));
// Check if there were any errors decoding JSON
if(empty($data))
{
echo "Something went wrong, error: " . json_last_error_msg();
exit;
}
try
{
// Prepare the statement - it's prepared once, used multiple times
$stmt = $pdo->prepare("INSERT INTO locale_strings (name, text, tip, category) VALUES (:name, :text, :tip, :category)");
// Start the transaction
$pdo->beginTransaction();
// Loop through the data, bind parameters, execute and when done - commit
foreach ($data as $cat => $values)
{
foreach ($values as $key => $info)
{
$stmt->bindValue(':name', $key, PDO::PARAM_STR);
$stmt->bindValue(':text', $info->text, PDO::PARAM_STR);
$stmt->bindValue(':tip', isset($info->tip) ? $info->tip : '', PDO::PARAM_STR);
$stmt->bindValue(':category', $cat, PDO::PARAM_STR);
$stmt->execute();
}
}
// And finally, tell the HDD to write down the info.
// Note - for this example, we issued all the contents in a single commit.
// That might not be the sweet spot so you can optimize later on with checking
// how many records are optimal go do down to the disk in a single I/O
$pdo->commit();
}
catch(PDOException $e)
{
if($pdo->inTransaction())
{
$pdo->rollBack();
}
// I assume you know how to handle exceptions, messages, trace etc.
}
关于PHP+MySQL 在一台机器上速度太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444280/
我在想出一个算法时遇到了麻烦... 我有一系列 GPS 数据,以 1 秒为间隔记录时间、速度、距离。假设距离是米,速度是米/秒。可能有超过 2 小时的数据,或 7200 个点。这里的“时间”字段主要是
使用java排序器,即: Collections.sort(myArrayList, new Comparator() { @Override public int c
有什么区别吗 SELECT * FROM my_table 和 SELECT my_column_id FROM my_table 地点: my_table 有百万行 网站上有大量并发用户进行sql查
有2个样本。 在第一个示例中,使用 orderby 可以更快地获得结果。 (根据 phpmyadmin 速度报告) 在另一个例子中,我没有使用 order by,它给出的结果较慢。 (根据 phpmy
我注意到,如果我将训练数据加载到内存中并将其作为 numpy 数组提供到图中,与使用相同大小的 shuffle 批次相比,速度会有很大差异,我的数据有大约 1000 个实例。 使用内存 1000 次迭
我在 python 中使用破折号。我正在绘制记录到 SQLite 数据库中的实时数据,目前,我正在绘制单个值与时间线图。我计划再添加 20 个图表,但目前,随着时间的增加, plotly 变慢,我认为
我试图调用 hasNext Velocity 模板中的方法,以便根据 foreach 循环中的位置影响行为 - 仅 hasNext没有按照文档工作。 这是 Velocity 用户指南的片段,关于 ha
在我正在制作的游戏中,我有两个点,pt1 和 pt2,我想计算出它们之间的角度。我已经在较早的计算中计算出距离。显而易见的方法是对垂直距离上的水平距离进行反正切 (tan(theta) = opp/a
我经常遇到字符串值不存在和/或为空的情况。这是测试这种情况的最佳方法吗? #if( $incentive.disclaimer && $!incentive.disclaimer != '' )
我想将一个模板nested包含在其他模板cont1,cont2和cont3中。 并且嵌套模板应仅对cont1隐藏一个特定控件。 在包含在cont1中之前,我想为一些标志变量$hideMyControl
是否可以更改从“Windows Azure Media Encoder”输出的音频的播放速度? 我正在使用配置为“WMA High Quality Audio”的“Windows Azure Medi
我使用速度将String(template)与字段合并 hi there I'am ${name}, And I'am ${age} old. velocity将字段${name}和${age}与一种
我使用的是 LockedBitmap 类,它简化了 C# 中位图数据的处理。目前它正在将数据复制到本地 byte[] 数组中,然后通过其类方法访问该数组以获取/设置像素颜色值。 这比直接通过指针访问锁
我尝试在 VM_global_library.vm 文件中添加一堆 #set($x=abc) 语句,但这些变量在我的 VM 模板中不可用。 我想为图像的基本路径等设置一个全局变量。这可能吗? 最佳答案
我的项目结构: -src --main ---java ----makers -----SomeClass ---resources ----htmlPattern.vm 如何告诉 SomeClass
我正在尝试从 Velocity 中的字符串中删除不需要的字符(换行符可以,但不能像 EM 和 CAN ASCII 控制字符那样)。 #set($cleanScreen = $cleanScreen.r
我想在日.月.年之间的点处分割日期。例如:2015 年 1 月 14 日至 {14, 01, 2015}这是我使用的代码:dates3.get(0) 包含我从页面的文本字段获取的字符串“14.01.2
之后,从 1.5 升级到速度引擎 1.7 出现了 1.5 没有的问题。为了解释这个问题,我必须展示一个代码片段: #foreach($someVariable in $someCollection)
我想知道从表中选择所有字段是否更快: SELECT * 或只选择您真正需要的: SELECT field1, field2, field3, field4, field5... 假设表有大约 10 个
我正在尝试模仿照片应用程序的行为,在该应用程序中,用户用手指平移照片并且照片具有一定的速度。由于我不会深入的原因,我不能将 UIScrollView 与它的缩放 UIImageView 一起使用,而是
我是一名优秀的程序员,十分优秀!