Using union in stream query
This commit is contained in:
parent
ed79cb5ded
commit
03e37d2186
3 changed files with 19 additions and 3 deletions
11
db/migrate/20110405170101_fix_stream_queries.rb
Normal file
11
db/migrate/20110405170101_fix_stream_queries.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class FixStreamQueries < ActiveRecord::Migration
|
||||
def self.up
|
||||
change_column :posts, :type, :string, :limit => 40
|
||||
remove_index :posts, :type
|
||||
end
|
||||
|
||||
def self.down
|
||||
add_index :posts, :type
|
||||
change_column :posts, :type, :string, :limit => 127
|
||||
end
|
||||
end
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20110331004720) do
|
||||
ActiveRecord::Schema.define(:version => 20110405170101) do
|
||||
|
||||
create_table "aspect_memberships", :force => true do |t|
|
||||
t.integer "aspect_id", :null => false
|
||||
|
|
@ -245,6 +245,7 @@ ActiveRecord::Schema.define(:version => 20110331004720) do
|
|||
end
|
||||
|
||||
add_index "posts", ["author_id"], :name => "index_posts_on_person_id"
|
||||
add_index "posts", ["created_at"], :name => "index_posts_on_created_at"
|
||||
add_index "posts", ["guid"], :name => "index_posts_on_guid", :unique => true
|
||||
add_index "posts", ["mongo_id"], :name => "index_posts_on_mongo_id"
|
||||
add_index "posts", ["status_message_id", "pending"], :name => "index_posts_on_status_message_id_and_pending"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ module Diaspora
|
|||
opts[:hidden] ||= false
|
||||
order_with_table = 'posts.' + opts[:order]
|
||||
opts[:limit] = opts[:limit].to_i * opts[:page].to_i if opts[:page]
|
||||
select_clause ='posts.id, posts.updated_at AS updated_at, posts.created_at AS created_at'
|
||||
|
||||
posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id})
|
||||
posts_from_self = self.person.posts
|
||||
|
|
@ -30,8 +31,11 @@ module Diaspora
|
|||
posts_from_self = posts_from_self.joins(:aspect_visibilities).where(:aspect_visibilities => {:aspect_id => opts[:by_members_of]})
|
||||
end
|
||||
|
||||
post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first}
|
||||
post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first}
|
||||
posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]).order(order_with_table)
|
||||
posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).order(order_with_table)
|
||||
|
||||
all_posts = "(#{posts_from_others.to_sql}) UNION (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}"
|
||||
post_ids = Post.connection.execute(all_posts).map{|r| r.first}
|
||||
|
||||
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table)
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue