Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
PHP
ini_set('max_execution_time', 15000);
ini_set("memory_limit","1028M");

mysql_query('TRUNCATE TABLE scrip_average');

$sql_all = mysql_query("select * From all_scrip limit 8000");
  while($row_all = mysql_fetch_array($sql_all))
  { 

  $SC_CODE=$row_all['SC_CODE'];
  
  $sql_last = mysql_query("select * From scrip where SC_CODE='$SC_CODE' ORDER BY ID DESC  LIMIT 1");
  while($row_last = mysql_fetch_array($sql_last))
  { 
$LAST_OPEN=$row_last['OPEN'];
$LAST_HIGH=$row_last['HIGH'];
$LAST_CLOSE=$row_last['CLOSE'];
$LAST_LOW=$row_last['LOW'];

}

  
 
  for ($n = 200; $n >0; $n--)
{
  $total_OPEN=0;
  $total_HIGH=0;
  $total_LOW=0;
  $total_CLOSE=0;

  $result_open=mysql_query("SELECT SUM(OPEN) AS total_value FROM scrip where SC_CODE='$SC_CODE' ORDER BY ID DESC  LIMIT $n");
$total_OPEN=mysql_result($result_open,0,0);

 $result_high=mysql_query("SELECT SUM(HIGH) AS total_value FROM scrip where SC_CODE='$SC_CODE' ORDER BY ID DESC  LIMIT $n");
  $total_HIGH=mysql_result($result_high,0,0);

 $result_low=mysql_query("SELECT SUM(LOW) AS total_value FROM scrip where SC_CODE='$SC_CODE' ORDER BY ID DESC  LIMIT $n");
 $total_LOW=mysql_result($result_low,0,0);

 $result_close=mysql_query("SELECT SUM(CLOSE) AS total_value FROM scrip where SC_CODE='$SC_CODE' ORDER BY ID DESC  LIMIT $n");
$total_CLOSE=mysql_result($result_close,0,0);

  $total_OPEN=$total_OPEN/$n;
  $total_HIGH=$total_HIGH/$n;
  $total_LOW=$total_LOW/$n;
  $total_CLOSE=$total_CLOSE/$n;
  
$LOW_status='negetive';
if($total_LOW>$LAST_LOW)
{
$LOW_status='possitive';
}
$HIGH_status='negetive';
if($total_HIGH>$LAST_HIGH)
{
$HIGH_status='possitive';
}
$OPEN_status='negetive';
if($total_OPEN>$LAST_OPEN)
{
$OPEN_status='possitive';
}
$CLOSE_status='negetive';
if($total_CLOSE>$LAST_CLOSE)
{
$CLOSE_status='possitive';
}

   $data=array(
   'date'=>$n,
		'SC_CODE'=>$SC_CODE,
		'OPEN'=>$total_OPEN,
		'HIGH'=>$total_HIGH,
		'LOW'=>$total_LOW,
		'CLOSE'=>$total_CLOSE,
		'LOW_status'=>$LOW_status,
		'HIGH_status'=>$HIGH_status,
		'OPEN_status'=>$OPEN_status,
		'CLOSE_status'=>$CLOSE_status);
				//'img_postdate'    =>date("y,m,d"),
	  	$query=insert("scrip_average",$data);	
		


}
}


What I have tried:

It takes too much time in execution please help me to reduce execution time
Posted
Updated 27-Jul-18 9:07am
v2

There is no way to give an accurate assessment since there is no way to know how many records are in your table. Assuming your queries would give you the correct values.

If the amount of data is very large then you need to apply an INDEX to the fields by which you sort to speed up the sorting (a lot). For example, you ORDER BY ID DESC means you should put an INDEX on the ID field.

Another less standard option, which I don't whole heartily recommend, is to do a sort by ID into a temp table and then do queries on that - having to only do the sort once. This may or may not help - bet even this will work out much better if you have an INDEX on ID.
 
Share this answer
 
Quote:
I have done my code using photo mysql but I take more than 7 hours in execution please guide me for reduce time

You wonder the reason why your code takes so much time to execute?
The reason is very simple: cascading
- you do 1 query with up 8000 records
- for each record you loop 200 time
- and then you have 4 query
- and then a single insert
Total select = 8000 * 200 * 4 = 6,400,000 selects
Total insert = 8000 * 200 = 1,600,000 inserts

You need to really learn how SQL works and how to optimize queries.
Rather than the 200*4 queries in the for loop, I would do a single query of raw data outside of the for loop, and then building the sums inside the for loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900