我在数据库中有以下表格
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 ) ;从posts检索数据的SQL
$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");在这些表中,post_id上有BTREE索引,评论表中有comment_id,在like、朋友、共享表中有id?
DBMS如何评估此查询以获得30行。
是否有任何方法从表底部检索数据库中的数据?在这个意义上,id是自动增量,所以我需要从数据库中检索数据,如
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' .这可以降低上述查询的时间复杂度。
发布于 2014-05-14 15:26:01
简化到post表进行讨论。
在post_id上创建一个唯一的索引。
CREATE UNIQUE INDEX recent_post_id ON post(post_id) 然后以这样的方式查询数据:
SELECT *
FROM post
WHERE user_id IN (your criteria)
ORDER BY post_id DESC
LIMIT 30当然,最大的开销可能是您嵌套的行集合,您可以将这些行合并在一起,以获得最近发布的条件。
https://dba.stackexchange.com/questions/65108
复制相似问题