我有一个假日公园可用性数据表 一个例子是:
<table>
<thead>
<tr>
<th>id</th>
<th>api_key</th>
<th>date</th>
<th>category_id</th>
<th>sites_available</th>
<th>price</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>asfe4t4gs</td>
<td>2019-11-08</td>
<td>1</td>
<td>2</td>
<td>100</td>
</tr>
<tr>
<td>2</td>
<td>ydfhswre3354</td>
<td>2019-12-08</td>
<td>2</td>
<td>3</td>
<td>150</td>
</tr>
</thead>
</table>
我遇到的问题是,我们发送数据的JSON结果以更新或插入此表中。它需要检查api_key,date和category_id是否已在表中,并使用其他字段(price和sites_available)对其进行更新,否则需要将它们作为新条目插入。
这是一个Wordpress插件。
我们尝试遍历所有内容并确定其是否存在,但是由于传入的数据量太大,因此速度太慢并且超时(成千上万的条目)。我们还尝试使用$ wpdb-> replace方法,但是由于每次都在添加新条目,因此它似乎不起作用。
foreach ($result['categories'] as $category) {
if (empty($category['category_id']))
throw new Exception('category_id is empty');
if (empty($category['dates']))
throw new Exception('dates is empty');
foreach ($category['dates'] as $date) {
if(empty($date['date']))
throw new Exception('date is empty');
$wpdb->replace(
$availability_table_name,array(
"sites_available" => $date['sites_available'],"price" => $date['price'],'api_key' => $result['api_key'],'instance_id' => $result['instance_id'],'category_id' => $category['category_id'],"date_availability" => $date['date'],)
);
}
}