title: PHP select the biggest `id` value from MySQL table
body: <p>I have a table with more than 300 000 rows and I need to select the highest value for the column 'id'. Usually, I will do like this:</p>
  
  <pre><code>SELECT id FROM my_table ORDER BY id DESC
  </code></pre>
  
  <p>... but this will cause slow queries and I don't want to use it. Is there a different way to solve this situation? <code>id</code> is auto increment and primary key.</p>
  
  <p><strong>Later Edit:</strong> It seems my full code is quite bad written, as I deduct from your comments. Below I posted a sample of the code I'm working and the tables. Can you suggest a proper way to insert the last ID+1 of table_x in two tables (including table_x itself). I have to mention that the script will be running more than once.</p>
  
  <pre><code>TABLE_X          TABLE_Y
  ------------     ----------
  id_x | value     id_y | id_x
  ------------     ----------
     1 | A            1 | 3
     2 | B            
     3 | C
  
  &lt;?php
  for($i=0; $i&lt;10; $i++){
      $result_x = mysql_query('SELECT id_x FROM table_x ORDER BY id_x DESC');
      $row_x = mysql_fetch_array($result_x);
  
      $next = $row_x['id_x'] + 1;
      mysql_query('INSERT INTO table_x(id_x) VALUES("'.$next.'")');
      mysql_query('INSERT INTO table_y(id_x) VALUES("'.$next.'")');
  }
  ?&gt;
  </code></pre>