I've got a twitter-like enter my web application. I have attempted to optimize the query whenever possible however it still freezes when it is loading the "tweets" from PostgresSQL database.

Twitter, github, facebook, feeds are extremely smooth and fast.

What's the easiest method to accomplish this?

Regards.

**Edit: the code may be the one out of railstutorial.org. http://railstutorial.org/chapters/following-users#sec:scopes_subselects_and_a_lambda

# Return microposts from the users being followed by the given user.
  scope :from_users_followed_by, lambda { |user| followed_by(user) }

  private

    # Return an SQL condition for users followed by the given user.
    # We include the user's own id as well.
    def self.followed_by(user)
      followed_ids = user.following.map(&:id).join(", ")
      where("user_id IN (#{followed_ids}) OR user_id = :user_id",
            { :user_id => user })
    end


# Table name: microposts
#
#  id         :integer         not null, primary key
#  content    :string(255)
#  user_id    :integer
#  created_at :datetime
#  updated_at :datetime
#

Sorry because of not supplying enough information.**

Have you put any index in your tables?