我将以下类用于插入数据:
class DB {
protected static $connection;
public $num = 0;
public $insertID = 0;
public $data = "";
public $select = "";
public $error = "";
public function connect() {
if(!isset(self::$connection)) {
self::$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
mysqli_query(self::$connection, "SET NAMES 'utf8'");
}
if(self::$connection === false) {
echo 'Not Connect To Database !<br>'.self::$connection->connect_error;
return false;
}
return self::$connection;
}
public function query($query) {
$this->select = $query;
$q = $this->connect()->query($query);
$this->connect()->commit();
return $q;
}
public function insert($table, $params){
$before = $after = '';
foreach($params as $field => $value){
$before .= "`$field`,";
$after .= "'$value',";
}
$before = substr($before,0,-1);
$after = substr($after,0,-1);
$sql = "INSERT INTO `$table` ($before) VALUES ($after)";
if($this->query($sql)){
$this->insertID = mysqli_insert_id($this->connect());
}
return true;
}
}
这是我的桌子:
这是我的代码:
$array = array( 'basketPrice'=>$detail['price'],
'basketOrder'=>$orderId,
'basketGift'=>$userGift,
'basketUseCredit'=>$cr,
'basketProduct'=>$product,
'basketFinish'=>$detail['buyPrice'],
'basketExSoon'=>$detail['minus']);
$db = new DB();
$db->insert('basket', $array);
在我的数据库中保存大约80.000条记录。运行代码后,我超时并插入到表中不起作用
basketId,basetGift和basketOrder是索引 为什么?
答案 0 :(得分:-1)
尝试将记录成束插入。这不会导致超时。尝试将记录拆分为每个呼叫100个插入内容。由于您具有与列关联的索引,因此很明显为什么插入会花费时间。拥有大量索引会影响在DB中插入的性能。因此,明智地选择索引。