Forum Moderators: coopster

Message Too Old, No Replies

MYSQL calls or PHP arrays?

what's more demanding on the server?

         

StoutFiles

6:12 am on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have 5000 widgets, each with five qualities, A, B, C, D, and E. The url will only contain A.

www.example.com/widgets/A

To get B,C,D, and E, I have two options. The simple route would be to pull them from my database. However, this database will be accessed a lot if I do this.

The other route would be to upload all the info in a php array and have that array on every page where there would be a choice. include("widgetarray.php")...then just pull the 4 values where A is.

Which do you think would be better? The load on the database or opening a separate page and searching an array?

StoutFiles

3:13 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No opinions?

coopster

3:35 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One way or another you have to get the data into an array in the first place so your question is a bit confusing. The array is only in memory for as long as the request is being processed. You may need to offer some clarification ...

henry0

3:50 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is StoutFiles thinking about a non dynamic array?
but that will call for tons of update!

So why not implementing a cache with that array
OR
Using Stored Procedures

rocknbil

4:18 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



include("widgetarray.php")

Which means, if I get you, as you modify these options in the database you'll have to update this file, correct? With that logic, code it in HTML. :-)

Kidding aside, if your options are in separate tables from the product, and optimized, I don't see a problem with reading those as often as required.

StoutFiles

4:23 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way or another you have to get the data into an array in the first place so your question is a bit confusing. The array is only in memory for as long as the request is being processed. You may need to offer some clarification ...

I'd have widget array updated (with mysql) whenever I update the database. So...abou once a week. Mysql once a week instead of a ton everyday.

tbarbedo

8:45 pm on Feb 18, 2009 (gmt 0)

10+ Year Member



If your database is not updated daily then I would suggest going with the array.

IanKelley

2:43 am on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The included array might use less CPU time but, because you're loading all the info for all products on each request (despite the page being only for a single product) the array is going to use more memory and more disk IO time than accessing the DB would.

If the OS caches the products array file then the disk problem is solved (as it is in MySQL provided cache settings are high enough) but you'll still end up using more memory.

It would make more sense to use the database... one query for each page.

StoutFiles

5:38 am on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the advice all.

VarX

8:17 am on Feb 20, 2009 (gmt 0)

10+ Year Member



i have a caching solution if interested.

whoisgregg

2:45 pm on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd do both options and measure how well each does, then pick one. :)

StoutFiles

3:47 pm on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll hear your caching solution. Always looking for an edge.

VarX

3:20 am on Feb 22, 2009 (gmt 0)

10+ Year Member



its a flat file with an assigned TTL when ever we pass a query in to RDBMS it caches the result in a flat file when the resultset retrieved from the cache it is considerably fast. if you want ill send you the whole product currently it can be used with MySQL and ODBC

brotherhood of LAN

5:41 am on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Shared memory [uk2.php.net] functions may be useful, PHP arrays can take up more space than you'd think. Some of the PHP "bug" submissions go into details as to why arrays take more space than minimally required.

If you're using shared memory, for ease you'd probably want to make all records/rows fixed length for ease of access.

rob7591

5:48 am on Feb 22, 2009 (gmt 0)

10+ Year Member



I'd much rather use MySQL queries.. If you use the array, you're going to be loading 5,000 items on every page. Instead of loading one with a MySQL query..

If it was better for resources to use the array, MySQL would be pointless.

mikhaill

12:05 am on Feb 26, 2009 (gmt 0)

10+ Year Member



Honestly, the simplest thing to do is to have it in a DB and then have this code in place:

//setup a cache system so not every call is queried to the DB
$file = 'totalfaq_cache.txt';
$expire = 86400; // 24 hours

if (file_exists($file) &&
filemtime($file) > (time() - $expire)) {
$data = unserialize(file_get_contents($file));
}
else
{

$sql = "select x,y,z from database";
$data = YoutDBCall($sql);

$OUTPUT = serialize($data);
$fp = fopen($file,"w");
fputs($fp, $OUTPUT);
fclose($fp);
} // end else

now the $data has the array... mostly from the flat file but every 24hrs its refreshed from the DB.