我知道以前有人问过这种问题,但没有一个让我找到解决方案。
这是我的查询:
SELECT p.photoid, c.comment, p.path, p.smallfile, p.bigfile, c.userid, c.dateposted, u.username, c.likephoto
FROM bs_photocomments c, photos p, user u
WHERE c.active = 1
AND u.userid = c.userid
AND p.photoid = c.photoid
AND c.id = (SELECT ID FROM bs_photocomments WHERE photoid = p.photoid ORDER BY ID DESC LIMIT 1)
ORDER BY c.id DESC LIMIT 2
关于最后一个 AND 的一点解释:
基本上,我正在尝试获取 20 张独特照片的最新评论。例如,如果一张照片有 5 条评论,我只想显示最后一条。
ORDER BY c.id 是 killer 。只有 18 万条评论和 10 万张照片。如果我删除 ORDER BY 查询会立即返回,但显然不是我想要的结果。
索引如下:
+------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| bs_photocomments | 0 | PRIMARY | 1 | id | A | 183269 | NULL | NULL | | BTREE | |
| bs_photocomments | 1 | photocomments_idx1 | 1 | photoid | A | 183269 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx2 | 1 | active | A | 4 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx3 | 1 | dateposted | A | 183269 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx4 | 1 | userid | A | 4953 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx5 | 1 | puserid | A | 4072 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx6 | 1 | id | A | 183269 | NULL | NULL | | BTREE | |
| bs_photocomments | 1 | photocomments_idx6 | 2 | photoid | A | 183269 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx7 | 1 | photoid | A | 183269 | NULL | NULL | YES | BTREE | |
| bs_photocomments | 1 | photocomments_idx7 | 2 | userid | A | 183269 | NULL | NULL | YES | BTREE | |
+------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| photos | 0 | PRIMARY | 1 | photoid | A | 57736 | NULL | NULL | | BTREE | |
| photos | 1 | photos_idx1 | 1 | userid | A | 5773 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx2 | 1 | dateuploaded | A | 57736 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx3 | 1 | camera | A | 17 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx4 | 1 | focallength | A | 308 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx5 | 1 | category | A | 96 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx6 | 1 | active | A | 1 | NULL | NULL | YES | BTREE | |
| photos | 1 | photo_idx7 | 1 | aperture | A | 354 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx8 | 1 | lenstype | A | 1 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx9 | 1 | film | A | 1 | NULL | NULL | YES | BTREE | |
| photos | 1 | photos_idx10 | 1 | originalfile | A | 57736 | NULL | NULL | YES | BTREE | |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
有什么想法吗?谢谢!
最佳答案
此查询将返回(最多)20 张不同照片的单个“最新”评论(“recent-cy”由 bs_photocomments 表中的升序 ID 值标识)。返回的 20 张照片将是评论最“最近”的照片,并从最“最近”的评论开始排序。 (注意:只考虑 bs_photocomments 中具有 active=1 的行,bs_photocomments 中的所有其他行都将被忽略,就好像它们甚至不在表中一样。)
SELECT p.photoid
, c.comment
, p.path
, p.smallfile
, p.bigfile
, c.userid
, c.dateposted
, u.username
, c.likephoto
JOIN (
SELECT n.photoid
, MAX(n.id) AS max_id
FROM bs_photocomments n
WHERE n.active = 1
GROUP BY n.photoid
ORDER BY max_id DESC
LIMIT 20
) m
JOIN bs_photocomments c
ON c.id = m.max_id
JOIN photos p
ON p.photoid = c.photoid
JOIN user u
ON u.userid = c.userid
ORDER BY c.id DESC
LIMIT 20
“技巧”是使用内联 View 来获取每个 photoid 的“最新”评论(内联 View 的别名是 m)。完成后,只需从 bs_photocomments 获取行,以及从用户和照片获取相关行即可。
我相信原始查询中真正的性能“ killer ”是连接谓词中的相关子查询。就对 bs_photocomments 表中的每一行执行该查询,甚至对 photos 表中的每一行运行该查询而言,这将非常昂贵。
为了获得最佳性能,您需要“覆盖索引”,而不是每个单独列上的(无用的)索引。至少,您需要 bs_photocomments 上的索引以 photoid 作为前导索引,后跟 id。包含active列将意味着可以“使用索引”满足内联 View 查询,而无需访问任何数据页。
... ON bs_photocomments (photoid, id, active)
有了这个索引,MySQL 应该能够相当有效地获得每个 photoid 的最大 id。同样,有一个索引
... ON bs_photocomments (id, comment, userid, dateposted, likephoto)
将意味着对 bs_photocomments 表 (c) 的外部查询也可以“使用索引”得到满足。 (MySQL 可能能够满足索引中的 ORDER BY,而不需要排序操作。(您需要检查 EXPLAIN 输出以查看 Extra 中是否出现“Using filesort”专栏。)
此外,这些覆盖索引可能会略微提高查询的性能。 (如果在不访问基础表中的页面的情况下从索引满足查询,则 EXPLAIN 输出将显示“使用索引”。)
ON photos (photoid, path, smallfile, bigfile)
ON user (userid, username)
每个单独列上的大多数单独索引都是无用的;这些索引中的任何一个都不太可能被使用,因此它们所做的只是消耗资源。
关于Mysql查询Order By太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14284535/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我在Rails上使用带有ruby的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s
我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params
我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使
我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi
例如,假设我有一个名为Products的模型,并且在ProductsController中,我有以下代码用于product_listView以显示已排序的产品。@products=Product.order(params[:order_by])让我们想象一下,在product_listView中,用户可以使用下拉菜单按价格、评级、重量等进行排序。数据库中的产品不会经常更改。我很难理解的是,每次用户选择新的order_by过滤器时,rails是否必须查询,或者rails是否能够以某种方式缓存事件记录以在服务器端重新排序?有没有一种方法可以编写它,以便在用户排序时rails不会重新查询结果