mysql - Get Relation of Followed or Public User -
i working on social app. have users can have private accounts. users can follow each other. fastest way using activerecord or pure sql fetch records of has_many on user either belong following or belong public user. in pseudo code:
user.get_all_posts_for_users_being_followed_by(me) + user.get_all_posts_for_public_users
i have this:
select `posts`.* `posts` ( user_id in (select id users visibility = 'all' union select followable_id follows followable_type = "user" , follower_type = "user" , follower_id = 4 , follows.status = 1) )
but hoping there might faster way handle that, or way rails query methods.
you can perform clear query activerecord, recommend use pure version for_now, because it's easy modificate now. need pay attention on this:
the query might faster, if add indexes
add_index :users, :visibility, :name => 'visibility_ix'
selecting columns * wildcard cause query's meaning , behavior change if table's schema changes, , might cause query retrieve data.
in() , not in() subqueries poorly optimized. mysql executes subquery dependent subquery each row in outer query. frequent cause of serious performance problems in mysql 5.5 , older versions. query should rewritten join or left outer join, respectively.
Comments
Post a Comment