- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在用 Rails (3) 构建一个博客类型的网站,其中“作者”可以关注其他作者,并且为了找到拥有最多关注者的作者,我写了这个范围:
scope :popular, all.sort { |a, b| a.followers.count <=> b.followers.count }
我知道这真的很低效,因为它必须对数据库中的每个作者执行多个查询,然后比较结果。我问这个问题是为了看看是否有更好的方法来做到这一点。相关代码如下:
class Author < ActiveRecord::Base
has_many :relationships, :dependent => :destroy, :foreign_key => "follower_id"
has_many :following, :through => :relationships, :source => :followed
has_many :reverse_relationships, :dependent => :destroy, :foreign_key => "followed_id", :class_name => "Relationship"
has_many :followers, :through => :reverse_relationships, :source => :follower
scope :popular, all.sort { |a, b| a.followers.count <=> b.followers.count }
end
“追随者”是通过一个单独的模型实现的:
class Relationship < ActiveRecord::Base
belongs_to :follower, :class_name => "Author"
belongs_to :followed, :class_name => "Author"
end
以及检索流行作者:
@popular_authors = Author.popular[0..9]
我希望能够编写类似 Author.popular.limit(10)
的东西,但我知道为了让它工作,Author.popular 必须返回一个对象ActiveRecord::Relation
类,而不是数组。
正如我提到的,这段代码确实有效,但效率低得可怕,发出数百个查询只是为了找到排名前 10 位的作者。任何帮助将不胜感激!
最佳答案
一种优化可能是使用预先加载。我怀疑你有很多查询说,SELECT * from
AuthorWHERE
follower_id= 7
预加载可以将您的数百个查询变成一个巨大的查询。这可能会生成一个慢速查询,但通常会更快,因为慢速查询的时间少于 5000 次快速查询。
我不是 SQL 专家,但您可能还想使用 LIMIT = 10
的 GROUP_BY。获得最受欢迎的 10 个。
尝试类似的东西
Authors.find(:all, :include => Authors, :group_by => " SELECT `count` as (some subquery I don't know to get the number of followers)", :limit => 10)
关于sql - "Popular"用户的 Rails 高效 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527492/
Find most "popular" follower for a specific person. The more followers someone has, the more "popula
Find most "popular" follower for the persons. The more followers someone has, the more "popular" the
假设有一个标签表,如stackoverflow问题标签: TagID (bigint), QuestionID (bigint), Tag (varchar) 使用LINQ获取25个最常用标签的最有效
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在尝试使用 Twitter 搜索 API 检索几条“流行”推文,但是当我使用“result_type =流行”尝试此操作时,我基本上最终得到一条“流行”推文。无论如何,我是否可以调整代码,以便获得
我有一个输入框,用户可以在提问时输入标签。它们以逗号分隔,如下所示: test1, test2, test3, test4 我的问题表结构是: 为了显示问题的标签,我只使用 explode(",",
我正在使用以下代码来生成某个 release_id 在表中出现了多少次的列表 所以 release_id 可以在我的表中出现多次。 SELECT release_id, COUNT( release_
我需要作业来获取数组中最“流行”的数字(出现频率最高的数字),如果有多个数字具有相同的显示次数,则随机获取一些数字。经过三个多小时的尝试,或者在网上搜索,这就是我得到的: public int get
好的,所以我正在构建一个网站,人们可以在其中发布新闻、评论、问题等。人们还可以对所有这些对象进行评分,最喜欢其中的大部分,分享它们等。该网站是 PHP+MySQL。我用 PHP 编写了一个脚本,它执行
这些是我的表格: 表:人物 id---name 1----John 2----Mike 3----George 表:访问 id---name---visitor 1----Paris--1 2----
我计划创建一个包含 x 千行数据的 mysql 5 (myISAM) 表。每行都会有一个计数字段,并且具有 20 个最高计数值的行将被检索很多,可能与每行更新一比一。通常不会检索不在这 20 行中的
ProductId | BrandId | Views 1 | 1 | 3 2 | 1 | 2 3 |
如何显示某个时期(例如过去一天)的“最受欢迎(文章|帖子|无论什么)”列表? (基本上复制了 Radioactivity Drupal 模块的功能。) 最佳答案 这是我会做的: 如果您还没有,请注册
基于v3 documentation我本以为是这样的: $ curl https://api.github.com/legacy/repos/search/python?language=Python
我想知道如何在我的应用程序的 SQLite 数据库中按受欢迎程度排序项目。 该应用程序连接到我的在线服务器并通过 JSON 自动同步信息。项目通常由用户在手机上保存,并添加到列表中,我想我可以将所有单
在 Google CSE 中,当我尝试获取 Popular Queries 时,我在 FireBug 控制台 中收到此错误: NetworkError: 400 Bad Request - http:
我有 2 个模型 Books 和 Categories 我想知道如何根据其中的书籍数量获得前 10 个类别。 我的模型是这样的 class Book(models.Model): """(Bo
考虑下表,其中包含字段 - id (int) 和 date_created (datetime): id date_created 1 2010-02-25 12:25:32
我正在用 Rails (3) 构建一个博客类型的网站,其中“作者”可以关注其他作者,并且为了找到拥有最多关注者的作者,我写了这个范围: scope :popular, all.sort { |a, b
我只是在做开源NLP工具的对比研究,对openNLP和coreNLP引擎的特性/服务有所了解。最近,我看到 openNLP 论坛没有任何贡献,而 coreNLP 论坛仍然活跃。所以我想了解stanfo
我是一名优秀的程序员,十分优秀!