with limit(20) it runs only the first 20 values of DB In this lines of code it selects only the first 0-20 but i want the first time select the first 0-20 and then the other(20-40) and other(40-60) and so on. How I can solve this problem?

 function something_cron()
      $query = db_select('watchdog', 'wa')
        -&gt;extend('PagerDefault')
        -&gt;orderBy('wid')
        -&gt;fields('wa', array('variables', 'type', 'severity',
      'message', 'wid', 'timestamp'))
        -&gt;limit(20);
      $result = $query-&gt;execute();

      // Loop through each item and add to $row.
      foreach ($result as $row) {
        someother();
      }
The common pattern is to store the last ID processed in a variable, using variable_set(). Then you can retrieve it using variable_get() with a default of 0 before running your query, and add a condition to the query for wid being greater:

$last_id = variable_get('mymodule_last_id', 0);
...
-&gt;condition('wid', $last_id, '&gt;')

...
$last_id = value from row
variable_set('mymodule_last_id', $last_id);