PHP还是MySQL错误?
我正在通过Webserver API执行infinite curl,以远程grep数百万数据。使用PHP和MySQL 其背后的机制是,它通过API和以下数据的每个往返查询编号尝试存储到本地服务器,睡眠几秒钟(如下所述),然后重新执行间隔。到目前为止,所有的过程都没有取得好的结果。它要么慢,要么失败 到底什么是这里的表演停顿?如何在合理的时间范围内顺利执行此卷曲 试验依据如下:- *它是以秒为单位的PHP还是MySQL错误?,php,mysql,curl,Php,Mysql,Curl,我正在通过Webserver API执行infinite curl,以远程grep数百万数据。使用PHP和MySQL 其背后的机制是,它通过API和以下数据的每个往返查询编号尝试存储到本地服务器,睡眠几秒钟(如下所述),然后重新执行间隔。到目前为止,所有的过程都没有取得好的结果。它要么慢,要么失败 到底什么是这里的表演停顿?如何在合理的时间范围内顺利执行此卷曲 试验依据如下:- *它是以秒为单位的 -------------------------------------------------
------------------------------------------------------------------------------------------
| No. of Data Query | Sleep | Results |
------------------------------------------------------------------------------------------
| 100 per trip | 5s | Smooth but slow (imagine, few millions of data to DL |
------------------------------------------------------------------------------------------
| 1000 per trip | 2s | After a few intervals it stopped for unknown reason.. |
------------------------------------------------------------------------------------------
| 10000 per trip | 2s | After a few intervals it stopped for unknown reason...|
------------------------------------------------------------------------------------------
| 10000 per trip | 5s | After a few intervals it stopped for unknown reason...|
------------------------------------------------------------------------------------------
代码:-
PS:如果上述问题需要进一步阐述,请告诉我。你只是想让我们猜一下吗???或者你真的需要帮助和必要的可操作信息?你想让我们在不看到代码的情况下进行调试?为什么它需要睡眠?通常,当我这样做时,我使用curl\u multi\ux一次性获取16个并行curl实例,代码很好,因为它正确地存储在数据库中。但是,通过web api的查询因速度慢或未知原因而失败。我怎样才能深入到推理和原因?你可以先向我们展示你在做什么。造成这种情况的原因有很多,低效的SQL查询、错误使用cURL、超时、错误报告干扰输出。。。我们不可能不看就知道。
<?php
ini_set('max_execution_time', 0); // We will set php execution time to unlimited
include_once __DIR__ . "/../bm/custom/xxx/xxx.php";
include_once __DIR__ . "/../bm/util/xxx.php";
use bm\custom\xxx\xxx;
use bm\util\xxx;
$uclim = new xxx();
$client = new xxxx();
// Database Credentials
$host = 'localhost';
$user = 'xxx';
$password = 'xxxx';
$db = 'xxxx';
$db_conn = new mysqli($host, $user, $password, $db);
// Initialize result variable to be null
$result = null;
$allSensors = array(
'SENSOR_xxx'=>'xxxx',
'SENSOR_xxxa'=>'xxxx',
'SENSOR_xxxb'=>'xxxx',
'SENSOR_xxxc'=>'xxxx',
'SENSOR_xxxd'=>'xxxx',
'SENSOR_xxxe'=>'xxxx',
'SENSOR_xxxf'=>'xxxx',
'SENSOR_xxxg'=>'xxxx'
);
$configs = array(
'API_KEY'=>'xxxxxx',
'HOST'=>'https://xxx',
'API_STR'=>'xxxx',
'TYPE_STR_ORGANIZATION_LIST'=>'xxxx',
'TYPE_STR_DATASTORE_SEARCH'=>'xxxx',
'TYPE_STR'=>'xxxx'
);
/* Here we specified which data to acquired */
foreach($allSensors as $tableName=>$tableResourceId){
$url = null;
$offset = 0;
while(true){ // Here we start infinite loop until the end of the results
$data = array(
'resource_id'=>$tableResourceId,
'limit'=>1000, // Numbers of Query each interval.
'offset'=>$offset
);
//if(empty($result->result->_links->next)){
$url = $configs['HOST'] . $configs['API_STR'] . $configs['TYPE_STR'];
//}
$data = json_encode($data);
$headers_in = array(
'Authorization: ' . $configs['API_KEY'],
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
);
$result = $client->curlUsingPost($url, $data, $headers_in);
$result = json_decode($result);
if( !empty($result->result->_links->next) && empty($result->result->records)){ break; } // Here we check if empty records sent from the api then we will break the loops
// We gather all the fields from the Sensors for database table creation
if(!empty($result->result->fields)){
$allFields = null;
$i = 0;
$len = count($result->result->fields);
$checkPriKey = 0;
foreach($result->result->fields as $fieldName){
if($fieldName->id == "_id"){
$checkPriKey = 1;
}
if($i == $len - 1){ // To check if it is last row
$allFields .= $fieldName->id." ".$fieldName->type.", PRIMARY KEY (_id)";
}else{
$allFields .= $fieldName->id." ".$fieldName->type.",";
}
$i++;
}
// Merge the collected fields name into sql format
$sql = "CREATE TABLE IF NOT EXISTS $tableName ( $allFields )";
$db_conn->query($sql);
}
// We gather fieldnames pattern from records
if(!empty($result->result->records)){
$recordsField = array();
$sqlTableField = null;
$o = 0;
$fieldsLen = count((array)$result->result->records[0]);
foreach($result->result->records[0] as $key=>$value){
array_push($recordsField, $key);
if($o == $fieldsLen - 1){
$sqlTableField .= $key;
}else{
$sqlTableField .= $key.",";
}
$o++;
}
// We will insert values from records into table.
$multipleInsert = null;
$recordCounter = 0;
$recordLen = count((array)$result->result->records);
foreach($result->result->records as $key=>$record){
$insertStr = null;
$u = 0;
$recordsLen = count($recordsField);
foreach($recordsField as $fieldName){
if($u == $recordsLen - 1){
$insertStr .= '"'.$record->$fieldName.'"';
}else{
$insertStr .= '"'.$record->$fieldName.'",';
}
$u++;
}
if($recordCounter == $recordLen - 1){
$multipleInsert .= "(".$insertStr.");";
}else{
$multipleInsert .= "(".$insertStr."),";
}
$recordCounter++;
}
// Insert api requested value into DB
$compiledSql = "INSERT INTO $tableName ($sqlTableField) VALUES $multipleInsert";
$db_conn->query($compiledSql);
} // End of gather fieldnames pattern from records
//Increament offset value
$offset = $offset + 1000;
} // End of infinite while loop
}
?>