jadent
jadent

Reputation: 3894

Persistent MySQL Query Results w/ PHP

Is there any way to persist the query results using PHP so you can use it another call to the web server?

i know there is no way to persist a resource in php but looking for an alternative way to accomplish this:

i have a reporting server and some queries will take several seconds and return 10,000's of records (could be 10's of MB in size). i would like to be able to use the resource mysqli_stmt::data_seek to page through the results when the user requests a new page instead of requering each time and using limit & offsets

is there any technology that would allow me to do this?

some thoughts would be to put a php socket server inbetween request the query through the socket server which can keep the resource alive for say 5 minutes for additional page requests. if the resource is no longer available it will then run the query w/ a limit & offset

any ideas or other thoughts would be greatly appreciated!

Upvotes: 3

Views: 278

Answers (3)

Andris
Andris

Reputation: 6113

You could also look into using PHP's shared memory.

One bad thing about is that it only allows you to save strings so you would have to serialize your data. Not sure how that would impact the performance but at least you'd be writing to and reading from RAM, which is always fast.

Another drawback is that as with every memory operation you need to specify how many bytes you want to reserve in advance, it's not dynamic like Memcache.

You could probably initially reserve much more than you need (e.g. 20MB instead of 10MB) and store the size of the currently stored data in the first 8 or so bytes. E.g. if your data was 123456 bytes long you could store the serialized data as "00123456{serialized data}" and then read it by defining the offset:

$size = (int) shmop_read($shmid, 0, 8);
shmop_read($shmid, 8, $size);

Another thing to remember is it only works on Windows but it shouldn't be a problem if you have a decent server backend :)

Upvotes: 1

WWW
WWW

Reputation: 9860

If you serialized your data you could use APC or Memcached to persist an object / array that contains your data. Memcached would require that your server runs the actual memcached server along with having the memcached extension installed, while APC doesn't require anything special besides the extension.

Upvotes: 1

webbiedave
webbiedave

Reputation: 48897

You'll want to look into memcached

PHP has an interface for it.

Upvotes: 1

Related Questions