- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在内容表中的标题和关键字上搜索请求的术语 ($q),同时也搜索模型,这些模型位于另一个表中并由中间的表链接。另外,我需要获取另一个表中的 View 数。
这是我到目前为止一直在处理的查询,结果很好,但太慢了(当我在 PhpMyAdmin 中运行它时,平均 0.6 秒......我们每月有数百万访问者)
SELECT DISTINCT SQL_CALC_FOUND_ROWS
c.*,
cv.views,
(MATCH (c.title) AGAINST ('{$q}') * 3) Relevance1,
MATCH (c.keywords) AGAINST ('{$q}') Relevance2,
(MATCH (a.`name`) AGAINST ('{$q}') * 2) Relevance3
FROM
content AS c
LEFT JOIN
content_actors AS ca ON ca.content = c.record_num
LEFT JOIN
actors AS a ON a.record_num = cm.actor
LEFT JOIN
content_views AS cv ON cv.content = c.record_num
WHERE
c.enabled = 1
GROUP BY c.title, c.length
HAVING (Relevance1 + Relevance2 + Relevance3) > 0
ORDER BY (Relevance1 + Relevance2 + Relevance3) DESC
表架构如下所示:
content
record_num title keywords
1 Video1 Comedy, Action, Supercool
2 Video2 Comet
content_actors
content model
1 1
1 2
2 1
actors
record_num name
1 Jennifer Lopez
2 Bruce Willis
content_views
content views
1 160
2 312
这是我通过执行 SHOW INDEX FROM tablename 找到的索引:
Table Column_Name Seq_in_index Key_name Index_type
---------------------------------------------------------------------------
content record_num 1 PRIMARY BTREE
content keywords 1 keywords FULLTEXT
content keywords 2 title FULLTEXT
content title 1 title FULLTEXT
content description 1 description FULLTEXT
content keywords 1 keywords_2 FULLTEXT
content_actors content 1 content BTREE
content_actors actor 2 content BTREE
content_actor actor 1 actor BTREE
actors record_num 1 PRIMARY BTREE
actors name 1 name BTREE
actors name 1 name_2 FULLTEXT
content_views content 1 PRIMARY BTREE
content_views views 1 views BTREE
这是查询的解释:
ID SELECT_TYPE TABLE TYPE POSSIBLE_KEYS KEY ROWS EXTRA
1 SIMPLE c ref enabled_2, enabled enabled 29210 Using where; Using temporary; Using filesort
1 SIMPLE ca ref content content 1 Using index
1 SIMPLE a eq_ref PRIMARY PRIMARY 1
1 SIMPLE cv eq_ref PRIMARY PRIMARY 1
我使用 GROUP BY 来避免重复内容,但单独使用该 group by 似乎会使处理查询所需的时间增加一倍。
编辑 好吧,在玩了一下查询之后,我意识到,如果我删除 GROUP BY,我会得到重复项,如果我让 GROUP BY 在那里,它不会不采用正确的 Relevance3 值(如果没有 GROUP BY,一个会返回 Relevance3 的值,而另一个则不会...)
最佳答案
将MATCH
(或运算)添加到WHERE
- 这将显着减少SQL_CALC_FOUND_ROWS
中要处理的行数code> 并消除对 HAVING...
的需要。
而不是
cv.views,
...
LEFT JOIN content_views AS cv ON cv.content = c.record_num
做
( SELECT views FROM content_views ON content = c.record_num ) AS views,
编辑
需要LEFT
和GROUP BY
,因为actors
是可选的,并且可能有多个actors
。由于您根本不需要 Actor 姓名,因此您可以通过执行以下操作来摆脱它
WHERE ... AND ( EXISTS SELECT *
FROM content_actors
JOIN actors AS a ON ...
WHERE MATCH (a.`name`) AGAINST ('{$q}')
AND ca...
)
但这并不能让您在 ORDER BY
中包含相关性。
因此,您需要使用 UNION DISTINCT
构建一个子查询。将有 2 个SELECT
:
选择#1:
SELECT c.id,
3 * MATCH(c.title) AGAINST ('{$q}')
+ MATCH(c.keywords) AGAINST ('{$q}') AS relevance
FROM Content AS c
WHERE MATCH(c.title, c.keywords) AGAINST ('{$q}')
(并且有 FULLTEXT(title, keywords))这将有效地获取有用的
content` 行的 id。
选择#2:
SELECT c.id,
2*MAX(MATCH(a.actor) AGAINST ('{$q}') AS actor_rel) AS relevance
FROM content AS c
JOIN content_actors ca ON ca.content = c.record_num
JOIN actors a ON a.record_num = ca.actor
WHERE MATCH(a.actor) AGAINST ('{$q}')
GROUP BY c.id;
确保有content_actors: INDEX(actor)
和content: INDEX(record_num)
。此SELECT
将有效地从actors
开始并返回到content
。请注意,当两个参与者 MATCH
时,它会执行与您的代码不同的操作;希望我的 MAX
是一个更好的解决方案。
现在,让我们把事情放在一起......
选择#3:
SELECT id, SUM(rel) AS relevance
FROM ( ... select #1 ... )
UNION ALL
( ... select #2 ... )
GROUP BY id
但这还不是全部......
选择#4:
SELECT c.*,
( ... views ... ) AS views
FROM ( ... select #3 ... ) AS u
JOIN content c ON c.id = u.id
我建议您手动运行每个步骤来验证它们,逐渐将所有部分组合在一起。是的,它很复杂,但应该相当快。
关于mysql - 优化跨多个表的全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35317501/
目前我正在构建相当大的网络系统,我需要强大的 SQL 数据库解决方案。我选择 Mysql 而不是 Postgres,因为一些任务需要只读(MyISAM 引擎)而其他任务需要大量写入(InnoDB)。
我在 mysql 中使用如下命令。当它显示表格数据时,它被格式化为一个非常干净的表格,间距均匀且 |作为列分隔符。 SELECT * FROM TABLE_NAME; 当我从 CLI 运行命令时,如下
我知道这个问题之前已经被问过好几次了,我已经解决了很多问题,但到目前为止没有任何效果。 MySQL 试图将自身安装到的目录 (usr/local/mysql) 肯定有问题。关于我的错误的奇怪之处在于我
以下是我的 SQL 数据结构,我正在尝试如下两个查询: Select Wrk_ID, Wrk_LastName, Skill_Desc from Worker, Skill where
我们有一个本地 mysql 服务器(不在公共(public)域上),并希望将该服务器复制到我们拥有的 google 云 sql 实例。我的问题是:1.这可能吗?2.我们的本地服务器只能在本地网络上访问
我有一个表(test_table),其中一些字段值(例如字段 A、B 和 C)是从外部应用程序插入的,还有一个字段(字段 D),我想从现有表(store_table)插入其值,但在插入前者(A、B 和
我想创建一个 AWS RDS 实例,然后使用 terraform 管理数据库用户。因此,首先,我创建了一个 RDS 实例,然后使用创建的 RDS 实例初始化 mysql 提供程序,以进一步将其用于用户
当用户在我的网站上注册时,他们会在我的一个数据库中创建自己的表格。该表存储用户发布的所有帖子。我还想做的是也为他们生成自己的 MySql 用户——该用户仅有权从他们的表中读取、写入和删除。 创建它应该
我有一个关于 ColdFusion 和 Mysql 的问题。我有两个表:PRODUCT 和 PRODUCT_CAT。我想列出包含一些标记为:IS_EXTRANET=1 的特殊产品的类别。所以我写了这个
我想获取 recipes_id 列的值,以获取包含 ingredient_id 的 2,17 和 26 条目的值。 假设 ingredient_id 2 丢失则不获取记录。 我已经尝试过 IN 运算符
在 Ubuntu 中,我通常安装两者,但 MySQL 的客户端和服务器之间有什么区别。 作为奖励,当一个新语句提到它需要 MySQL 5.x 时,它是指客户端、服务器还是两者兼而有之。例如这个链接ht
我重新访问了我的数据库并注意到我有一些 INT 类型的主键。 这还不够独特,所以我想我会有一个指导。 我来自微软 sql 背景,在 ssms 中你可以 选择类型为“uniqeidentifier”并自
我的系统上有 MySQL,我正在尝试确定它是 Oracle MySQL 还是 MySQL。 Oracle MySQL 有区别吗: http://www.oracle.com/us/products/m
我是在生产 MySQL 中运行的应用程序的新维护者。之前的维护者已经离开,留下的文档很少,而且联系不上了。 我面临的问题是执行以下请求大约需要 10 秒: SELECT COUNT(*) FROM `
我有两个位于不同机器上的 MySQL 数据库。我想自动将数据从一台服务器传输到另一台服务器。比方说,我希望每天早上 4:00 进行数据传输。 可以吗?是否有任何 MySQL 内置功能可以让我们做到这一
有什么方法可以使用 jdbc 查询位于 mysql 根目录之外的目录中的 mysql 表,还是必须将它们移动到 mysql 根目录内的数据库文件夹中?我在 Google 上搜索时没有找到任何东西。 最
我在 mysql 数据库中有两个表。成员和 ClassNumbers。两个表都有一个付费年份字段,都有一个代码字段。我想用代码数字表中的值更新成员表中的付费年份,其中成员中的代码与 ClassNumb
情况:我有 2 台服务器,其中一台当前托管一个实时 WordPress 站点,我希望能够将该站点转移到另一台服务器,以防第一台服务器出现故障。传输源文件很容易;传输数据库是我需要弄清楚如何做的。两台服
Phpmyadmin 有一个功能是“复制数据库到”..有没有mysql查询来写这个函数?类似于将 db A 复制到新的 db B。 最佳答案 首先创建复制数据库: CREATE DATABASE du
我有一个使用 mySQL 作为后端的库存软件。我已经在我的计算机上对其进行了测试,并且运行良好。 当我在计算机上安装我的软件时,我必须执行以下步骤: 安装 mySQL 服务器 将用户名指定为“root
我是一名优秀的程序员,十分优秀!