More efficient way to perform multiple MySQL queries in PHP -


I have an online store with thousands of orders and I am writing some inquiry so that each supplier (brand) is made on the site. Have given. I have the following questions, for each month of the selected year:

  $ filterJan = "$ filterYear-01"; $ QueryJan = "select value, volume Ordr_itms WHERE ProductID = '$ ProductID" and timestamp'% $ Filtrjan% ' "10000"; $ SuppliersQueryFilter = mysql_query ($ queryJan, $ connection) or die (mysql_error ()); while ($ rowF = mysql_fetch_assoc ($ suppliersQueryFilter)) {$ price = $ rowF [ "value"] * $ rowF [ "volume"]; $ Kuljan = $ Kuljan + $ value;}  
< P> ** and so on ** for each month **

takes time to load (we are talking for at least 60 seconds) and I know that it is any size or Not every master is skilled Are they searching these questions through thousands of records? Is there any more effective method or writing on this:

a) Probably reduce the amount of code for 1 query b) Raising the loading bar

$ filterYear as of year 2009.

So what does this query it is a selected year (which is assigned to the $ filter year) To select how much money has been made for each month, then the results will be generated. There is a table with January, February, March ... how much money has been made each month, so £ 2345, £ 2101 etc. ...

For best performance, you want to submit a query from the database like this:

  SELECT DATE_FORMAT (i .imestamp, '% I`productID = 'foo' and i.timestamp & gt; in the form of `month`, 'sum' (i.price * i.qty) as 'total' order_imit in the form of 'y-% m'). = 2013 -01-01 'and i.timestamp & lt; '2013-01-01' + INTERVAL 12 months by group DATE_FORMAT (i.timestamp, '% Y-% m')  

(It assumes that timestamp using column MySQL datatype TIMESTAMP , DATETIME or DATE )

exclude mysql_ interface, you want to avoid SQL Injection vulnerabilities using the mysql_real_escape_string function. (Better option would be to use mysqli or PDO interface, and must use a prepared statement with bind placeholders.)

We want to predict on BARE column timestamp, so MySQL Instead of requiring a full scan of each row in the table, the appropriate index available for a category scan operation can be used.

We also want to use the power of the server to quickly get a total, and instead of retrieving each flipping line, and processing each of those rows separately Instead of (RBAR = line by the executing row)

GROUP BY section and SUM (

with mysql_ interface, query Something like this will appear: $ querytext = "SELECT DATE_FORMAT (I.timestamp, '% Y-% m') as` month`, SUM (i.price * i.qty ) As Men` total 'order_items from i.productID =' '. Mysql_real_escape_string ($ thisProductID). "' And i.timestamp & gt; = ''. Mysql_real_escape_string ($ filters years)." -01-01 'and I. timestamp & lt; ' ". Mysql_real_escape_string ($ filters years)." -01-01' + m interval by 12 months group DATE_FORMAT (i.timestamp, '% Y-%') "; # for debugging #echo $ querytext ;


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -