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:

  1. the query might faster, if add indexes

    add_index :users, :visibility, :name => 'visibility_ix' 
  2. selecting columns * wildcard cause query's meaning , behavior change if table's schema changes, , might cause query retrieve data.

  3. 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

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -