title: How to fetch latest timestamp values from table using DynamoDB PHP
body: <p>I'm new to DynamoDB.<br>
  In my application, every registered user has some sensors on his account and those sensors will send some data every 5 mins to our database. </p>
  
  <p>Now I want to show "If user has logged into his account, I should need to display all his sensors with latest timestamp values for each sensor".  </p>
  
  <p>I did this with three queries like "I fetched all devices under his account -> Original sensor IDs -> each sensor latest value " so this process taking very long time to execute.</p>
  
  <p>And my tables look like this:</p>
  
  <p>users:</p>
  
  <pre><code>---------------------
  |sno  | userId      |
  ---------------------
  | 1   | naveenkumar |
  |--------------------
  | 2   | abc         |
  |--------------------
  | 3   | xyz         |
  |--------------------
  </code></pre>
  
  <p>sensor_devices:</p>
  
  <pre><code>---------------------
  | id | sensorId     |
  ---------------------
  | 1  | sensor1      |
  |--------------------
  | 2  | sensor2      |
  |--------------------
  | 3  | sensor3      |
  |--------------------
  | 4  | sensor4      |
  ---------------------
  | 5  | sensor5      |
  ---------------------
  </code></pre>
  
  <p>And </p>
  
  <p>users-sensors:</p>
  
  <pre><code>----------------------
  | userId | sensorId  |
  ----------------------
  | 1      |  1        |
  |---------------------
  | 1      |  2        |
  |---------------------
  | 1      |  4        |
  |---------------------
  | 2      |  5        |
  |---------------------
  </code></pre>
  
  <p>data_tbl:</p>
  
  <pre><code>---------------------------------------------
  | sensorId  | value    | timestamp          |
  ---------------------------------------------
  | sensor1   | 4.3      | 2014-01-21 11:21:00|
  |--------------------------------------------
  | sensor2   | 5.0      | 2014-01-21 11:22:00|
  |--------------------------------------------
  | sensor3   | 10.0     | 2014-01-21 11:19:00|
  |--------------------------------------------
  | sensor4   | 6.3      | 2014-01-21 11:25:00|
  |--------------------------------------------
  | sensor1   | 8.3      | 2014-01-21 11:26:00|
  |--------------------------------------------
  | sensor2   | 6.0      | 2014-01-21 11:27:00|
  |--------------------------------------------
  | sensor3   | 9.0      | 2014-01-21 11:24:00|
  |--------------------------------------------
  | sensor4   | 6.3      | 2014-01-21 11:30:00|
  |--------------------------------------------
  </code></pre>
  
  <p>And Final Output table which will see the user in his homepage:</p>
  
  <pre><code>---------------------------------------------
  | sensorId  | value    | timestamp          |
  ---------------------------------------------
  | sensor1   | 8.3      | 2014-01-21 11:26:00|
  |--------------------------------------------
  | sensor2   | 6.0      | 2014-01-21 11:27:00|
  |--------------------------------------------
  | sensor4   | 6.3      | 2014-01-21 11:30:00|
  |--------------------------------------------
  </code></pre>
  
  <p>And my query looks like this and it's taking long time to execute, so please provide me a better solution:</p>
  
  <p>Let us assume $_SESSION['login_id'] = 1;</p>
  
  <pre><code>&lt;?php
    $aws = Aws::factory('config.php');
    $client = $aws-&gt;get("dynamodb");
  
    $tableName = "data_tbl";
    echo "&lt;table border=1&gt;";
    echo "&lt;tr&gt;";
  
    echo "&lt;th&gt;Sensor ID&lt;/th&gt;";
    echo "&lt;th&gt;value&lt;/th&gt;";
    echo "&lt;th&gt;Timestamp&lt;/th&gt;";
    echo "&lt;/tr&gt;";
  
    //Query to get user devices
    $devices = $client-&gt;query(array(
      "TableName" =&gt; "users-sensors",
      "KeyConditions" =&gt; array(
        "userId" =&gt; array(
          "ComparisonOperator" =&gt; ComparisonOperator::EQ,
          "AttributeValueList" =&gt; array(
            array(Type::STRING =&gt; $_SESSION['login_id'])
          )
        )
      )
    ));
    foreach ($devices['Items'] AS $key=&gt;$value) {
      $id = $value['sensorId']['S'];
      //Query to get original sensorId from sensor serial number
      $device = $client-&gt;query(array(
        "TableName" =&gt; "sensor_devices",
        "KeyConditions" =&gt; array(
          "id" =&gt; array(
            "ComparisonOperator" =&gt; ComparisonOperator::EQ,
            "AttributeValueList" =&gt; array(
              array(Type::STRING =&gt; $id)
            )
          )
        )
      ));
      $dids =  $device['Items'][0]['sensorId'][Type::STRING];
      //Query to get all latest values of sensors
      $response = $client-&gt;query(array(
        "TableName" =&gt; $tableName,
        "KeyConditions" =&gt; array(
          "sensorId" =&gt; array(
            "ComparisonOperator" =&gt; ComparisonOperator::EQ,
            "AttributeValueList" =&gt; array(
              array(Type::STRING =&gt; $dids)
            )
          )
        ),
        "Select" =&gt; "SPECIFIC_ATTRIBUTES",
        "AttributesToGet" =&gt; array("sensorId","value","timestamp"),
        "ScanIndexForward" =&gt; false,
        "Limit" =&gt; 1  
      ));
      foreach ($response['Items'] AS $key=&gt;$value) {
        echo "&lt;tr&gt;";
        $link = "graph.php?id=".$id;
        echo "&lt;td&gt;&lt;a href='$link'&gt;".$value['sensorId']['S']."&lt;/a&gt;&lt;/td&gt;";
        echo "&lt;td&gt;&lt;a href='$link'&gt;".$value['value']['S']."&lt;/a&gt;&lt;/td&gt;";
        $epoch = $value['timestamp']['S'];
        $timestamp = date('Y-m-d H:i:s', $epoch);
        echo "&lt;td&gt;&lt;a href='$link'&gt;".$timestamp."&lt;/a&gt;&lt;/td&gt;";
        echo "&lt;/tr&gt;";
      }
    }
    echo "&lt;/table&gt;";
  ?&gt;
  </code></pre>
  
  <p>If I would get answer for this I would get complete my job.
  Thanks</p>