Mysql – the strategy of query evaluation in DBMS when Limit is used in query

database-designdatabase-recommendationMySQL

I have following tables in database

post(post_id , user_id , post_text , parsed_text) ;
Comment(comment_id , post_id , user_id , comment_text) ;
friend(id , user_id , friend_id ) ;
group (id , user_id ,group_id ) ;
like  (id, user_id , page_id ) ;

SQL to retrieve data from posts

 $posts =  mysql_query("select * from post where user_id in 
(select f_id  from ((select friend_id as f_id from friend  where user_id = 'a') 
union (select group_id as f_id  from group  where user_id = 'a') 
union (select page_id as f_id   where user_id = 'a'  ))) order by post_id limit 0 , 30");

In these table there are BTREE index on post_id in post table , comment_id in comment table and id in like , friend , share table ?

How DBMS evaluates this query to get 30 rows .

Is there any way to retrieve data from database from bottom of the table ? Bottom in the sense id is auto increment so i need to retrieve data from database as

If table has id 'N' then it should start retrieving data as 
first 'n-1' th'tupple ,second  'n-2' th tupple ,third 'n-3' th tupple and so on 
without using 'ORDER BY' .

This can reduce time complexity for above query .

Best Answer

Simplifying down to the post table, for discussion.

Create a unique index on the post_id.

CREATE UNIQUE INDEX recent_post_id ON post(post_id) 

Then query the data as such:

SELECT * 
FROM post 
WHERE user_id IN (your criteria) 
ORDER BY post_id DESC
LIMIT 30

Of course, the most overhead may well be your nested collection of rows that you UNION together to get the criteria for recent posts.