Question

Can I specify what fields I want to receive from query when using HandlerSockets ?

Here is my sample table

CREATE TABLE pushed_media
(
    user_id BINARY(12) NOT NULL,
    story_id BINARY(12) NOT NULL,
    sent_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    PRIMARY KEY ( user_id, story_id )
);

Php code that queries it is below

$hs = new HandlerSocket($host, $port);
if (!($hs->openIndex(1, $dbname, $table, HandlerSocket::PRIMARY, 'user_id,story_id,sent_date')))
{
    echo $hs->getError(), PHP_EOL;
    die();
}

$user_id = pack('H*', substr(md5('ruslan'), 0, 24));
$story_id = pack('H*', substr(md5('story1'), 0, 24));

$retval = $hs->executeSingle(1, '=', array($user_id, $story_id), 1, 0);

All I need is sent_date because I already know two other values. Is it possible to not to transfer them over the network again?

Was it helpful?

Solution

You can specify the fields that you want to receive from the query by simply specifying them in the field parameter of the openIndex method.
As you can read here, this code:

<?php
$hs->openIndex(1, 'db', 'table', 'PRIMARY', 'k,v');
$ret = $hs->executeSingle(1, '>=', array('K1'));
var_dump($ret);
?>

Is equivalent to the following SQL statement:

SELECT k,v FROM table WHERE k >= 'K1' LIMIT 1

Back to your example, if you want to read only the sent_date field value:

$hs = new HandlerSocket($host, $port);
if (!$hs->openIndex(1, $dbname, $table, myHandlerSocket::PRIMARY, 'sent_date')) {
    die($hs->getError());
}

$user_id  = pack('H*', substr(md5('ruslan'), 0, 24));
$story_id = pack('H*', substr(md5('story1'), 0, 24));

$retval = $hs->executeSingle(1, '=', array($user_id, $story_id), 1, 0);
die("<pre>".print_r($retval,true)."</pre>");

The resulting output should be something like this:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2013-02-01 22:18:39
)

The HandlerSocket protocol documentation states:

Once an 'open_index' request is issued, the HandlerSocket plugin opens the specified index and keep it open until the client connection is closed. Each open index is identified by <indexid>. If <indexid> is already open, the old open index is closed. You can open the same combination of <dbname> <tablename> <indexname> multple times, possibly with different <columns>. For efficiency, keep <indexid> small as far as possible.

OTHER TIPS

What about executeMulti ?

$retval = $hs->executeMulti(array(
  array(1, '=', array($user_id), 1, 0),
  array(2, '=', array($story_id), 1, 0),
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top