title: Optimal database table design for the getting latest (most recent timestamp records) in PostgreSQL
body: <p>Please find the below as a problem scenario:</p>
  
  <ol>
  <li><p>Current DB design: I'm having 40 tables (currently each table is having 5 billion records) and these records are not having the same result which means each table consists of unique records in their own table format. Each table has a primary key (pk) "timestamp" which is in UTC with timezone in ISO format. Around 1000 records are getting inserted in each respective tables for every 5 min.</p></li>
  <li><p>Implementation: Now I have to get the most recent timestamp data from every table. 
  I tried using for every table <code>select * from table_name where timestamp = (select timestamp from table_name order by timestamp desc limit 1)</code> which returns the most recent records but it took some time to get the results. After this query, I tried using <code>select * from table_name where timestamp = (select max(timestamp) from table_name)</code> but eventually this also took some to get the data.</p></li>
  </ol>
  
  <p>So, How to minimize the query time to get the data from the database? 
  (all the design and query are welcome)?</p>
  
  <p>Thank you.</p>
  
  <p>FYI, Im using Python3.6 and <a href="https://pypi.org/project/psycopg2/" rel="nofollow noreferrer">psycopg2</a></p>