只是想看看你们对这场表演的看法...
我有一个英特尔i7 ivybridge四通道,16 i7 DDR3内存,7200RPM驱动器用于服务器/mysql驱动器等...
我对1000条记录进行了简单的更新:$q2 = "UPDATE msg_inbox SET receivedtime='$epochTime‘WHERE id='$id’LIMIT 1 ";
假设这一切都是基于当前的foreach循环(在返回的每个id上)触发的。但是如果我注释掉这个更新查询,页面将花费.0300秒来加载全部1000条记录。
但是使用update查询,在页面完全完成之前延迟了57秒…我使用PHP microtime进行了检查。
这是正常的和预期的性能,还是有什么问题?(也许是我的机器)
问候
表信息:定义:
CREATE TABLE `msg_inbox` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sender` varchar(20) NOT NULL,
`sender_name` varchar(100) NOT NULL,
`receiver` varchar(20) NOT NULL,
`receiver_name` varchar(20) NOT NULL,
`msg` tinytext NOT NULL,
`senttime` int(10) NOT NULL,
`receivedtime` int(10) NOT NULL,
`operator` varchar(20) NOT NULL,
`device_name` varchar(50) NOT NULL,
`msgtype` varchar(20) NOT NULL,
`read_by` varchar(20) NOT NULL COMMENT 'marks a message to be read or unread with ID of who read it',
`folder_id` int(11) DEFAULT '0',
`owner` int(11) DEFAULT '0',
`prefix` varchar(10) NOT NULL,
`raw_gwdata` text,
`stat` enum('normal','important','deleted') NOT NULL DEFAULT 'normal',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8528 DEFAULT CHARSET=latin1;目前表中总共有7887行。
发布于 2013-05-09 18:32:13
猜测id列已被编入索引...我要说的是,你需要看看你的MySql连接限制,HERE。
我想说你的机器和查询都很好,你需要仔细检查你的mysql / php配置。
PS:我见过4 in内存的双核在不到30秒的时间内完成1000次插入。
发布于 2013-05-10 22:26:30
加快速度的可能解决方案是批量更新。Id是惟一的,如果您只打算更新现有的id (而不打算尝试更新一个不存在的id),那么您可以使用INSERT.....ON复制键更新类型查询。
这样做的好处是,您可以在一条语句中使用新值更新数百行,而且速度非常快。
在执行此操作时,我有时会使用一个类来跟踪记录,它可以确定何时执行插入操作。
下面是一个示例(使用php) (没有直接测试,所以可能有一些奇怪的拼写错误):
<?php
$InsertObject = new generic_insert($db_object, 'msg_inbox', array('id', 'receivedtime'), 1000);
foreach ($DetailsArray AS $DetailItem)
{
InsertObject->InsertItem("($DetailItem[id], $DetailItem[receivedtime])");
}
unset($InsertObject);
class generic_insert
{
var $db;
var $InsertArray = array();
var $TableName = '';
var $InsertBatchSize = 1000;
var $UpdateClause = '';
function __CONSTRUCT($db, $TableName, $ColumnArray, $InsertBatchSize = 1000)
{
$this->db = $db;
$this->TableName = $TableName;
$this->ColumnArray = $ColumnArray;
$this->InsertBatchSize = $InsertBatchSize;
foreach (array_slice($this->ColumnArray, 1) AS $ColumnItem)
{
$UpdateArray = "$ColumnItem=VALUES($ColumnItem)";
}
if (count($UpdateArray) > 0)
{
$this->UpdateClause = " ON DUPLICATE KEY UPDATE SET ".implode(',', $UpdateArray);
}
}
function __DESTRUCT()
{
if (count($this->InsertArray) > 0)
{
$this->PerformInsert();
}
}
public function InsertItem($InItem)
{
$this->InsertArray[] = $InItem;
if (count($this->InsertArray) > $this->InsertBatchSize)
{
$this->PerformInsert();
}
}
private function PerformInsert()
{
$query = "INSERT INTO ".$this->TableName." (`".implode('`, `', $this->ColumnArray)."`) VALUES ".implode(",", $this->InsertArray)." ".$this->UpdateClause;
$stmtId = $this->db->query($query);
if ($stmtId === false)
{
die("Insert To ".$this->TableName." Failed - ".$this->db->error());
}
else
{
$this->db->free_result($stmtId);
}
$this->InsertArray = array();
}
}
?>该类接受一些参数,包括数据库对象、表名、列名的数组(假设第一个是主键)和一次完成的最大更新次数(在本例中为1000)。它应该是相当通用的。
https://stackoverflow.com/questions/16459320
复制相似问题