使用 ORDER BY 和 MATCH AGAINST 优化选择查询

发布于 2024-11-05 12:51:08 字数 5674 浏览 4 评论 0原文

以下查询需要 25 秒,有 2 个表连接。第一个 posts 表有 150,00 行,topics 表有 50,000 行。任何人都知道我如何加快速度。

SELECT SQL_NO_CACHE
  post_search.post_id,
  topic_search.topic_id,
  topic_search.topic_title,
  topic_search.topic_last_post_time,
  MATCH(post_search.post_text,topic_search.topic_title) AGAINST('search_terms' IN BOOLEAN MODE) AS score
FROM bb_posts_fulltext_search post_search
LEFT JOIN bb_topics_fulltext_search topic_search
  ON post_search.topic_id = topic_search.topic_id
WHERE MATCH(post_search.post_text,topic_search.topic_title) AGAINST('search_terms' IN BOOLEAN MODE)
GROUP BY topic_search.topic_id
ORDER BY score DESC
LIMIT 0,6

描述

mysql> DESCRIBE bb_posts_fulltext_search;
+-----------+------------+------+-----+---------+-------+
| Field     | Type       | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| post_id   | bigint(20) | NO   | PRI | NULL    |       |
| post_text | longtext   | YES  | MUL | NULL    |       |
| topic_id  | bigint(20) | YES  | MUL | NULL    |       |
+-----------+------------+------+-----+---------+-------+

描述解释

mysql> DESCRIBE bb_topics_fulltext_search
    -> ;
+----------------------+--------------+------+-----+---------+-------+
| Field                | Type         | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+-------+
| topic_id             | int(11)      | NO   | PRI | NULL    |       |
| topic_title          | varchar(255) | YES  | MUL | NULL    |       |
| topic_posts          | bigint(20)   | YES  |     | NULL    |       |
| topic_poster_name    | varchar(40)  | YES  |     | NULL    |       |
| topic_last_post_id   | bigint(20)   | YES  |     | NULL    |       |
| forum_id             | int(11)      | YES  |     | NULL    |       |
| parent_group_id      | int(11)      | YES  |     | NULL    |       |
| child_group_id       | int(11)      | YES  |     | NULL    |       |
| topic_last_post_time | datetime     | YES  | MUL | NULL    |       |
+----------------------+--------------+------+-----+---------+-------+

更新

+----+-------------+--------------+--------+---------------+---------+---------+----------------------------------+--------+---------------------------------+
| id | select_type | table        | type   | possible_keys | key     | key_len | ref                              | rows   | Extra                           |
+----+-------------+--------------+--------+---------------+---------+---------+----------------------------------+--------+---------------------------------+
|  1 | SIMPLE      | post_search  | ALL    | NULL          | NULL    | NULL    | NULL                             | 158972 | Using temporary; Using filesort |
|  1 | SIMPLE      | topic_search | eq_ref | PRIMARY       | PRIMARY | 4       | wordpress.post_search.topic_id   |      1 | Using where                     |
+----+-------------+--------------+--------+---------------+---------+---------+----------------------------------+--------+---------------------------------+

:索引

+--------------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                    | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| bb_posts_fulltext_search |          0 | PRIMARY   |            1 | post_id     | A         |      158972 |     NULL | NULL   |      | BTREE      |         |               |
| bb_posts_fulltext_search |          1 | topic_id  |            1 | topic_id    | A         |       52990 |     NULL | NULL   | YES  | BTREE      |         |               |
| bb_posts_fulltext_search |          1 | post_text |            1 | post_text   | NULL      |           1 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+--------------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

CREATE 1

DROP TABLE IF EXISTS `wordpress`.`bb_posts_fulltext_search`;
CREATE TABLE  `wordpress`.`bb_posts_fulltext_search` (
  `post_id` bigint(20) NOT NULL,
  `post_text` longtext,
  `topic_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`post_id`),
  KEY `topic_id` (`topic_id`),
  FULLTEXT KEY `post_text` (`post_text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE 2

DROP TABLE IF EXISTS `wordpress`.`bb_topics_fulltext_search`;
CREATE TABLE  `wordpress`.`bb_topics_fulltext_search` (
  `topic_id` int(11) NOT NULL,
  `topic_title` varchar(255) DEFAULT NULL,
  `topic_posts` bigint(20) DEFAULT NULL,
  `topic_poster_name` varchar(40) DEFAULT NULL,
  `topic_last_post_id` bigint(20) DEFAULT NULL,
  `forum_id` int(11) DEFAULT NULL,
  `parent_group_id` int(11) DEFAULT NULL,
  `child_group_id` int(11) DEFAULT NULL,
  `topic_last_post_time` datetime DEFAULT NULL,
  PRIMARY KEY (`topic_id`),
  KEY `topic_last_post_time` (`topic_last_post_time`),
  FULLTEXT KEY `topic_title` (`topic_title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

解决方案: http://forums.mysql.com/read.php?115 ,418955,418955#msg-418955

The following query takes 25 seconds with 2 table joins. The first posts table has 150,00 rows, topics table has 50,000 rows. Anyone know how I can speed this up.

SELECT SQL_NO_CACHE
  post_search.post_id,
  topic_search.topic_id,
  topic_search.topic_title,
  topic_search.topic_last_post_time,
  MATCH(post_search.post_text,topic_search.topic_title) AGAINST('search_terms' IN BOOLEAN MODE) AS score
FROM bb_posts_fulltext_search post_search
LEFT JOIN bb_topics_fulltext_search topic_search
  ON post_search.topic_id = topic_search.topic_id
WHERE MATCH(post_search.post_text,topic_search.topic_title) AGAINST('search_terms' IN BOOLEAN MODE)
GROUP BY topic_search.topic_id
ORDER BY score DESC
LIMIT 0,6

DESCRIBE

mysql> DESCRIBE bb_posts_fulltext_search;
+-----------+------------+------+-----+---------+-------+
| Field     | Type       | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| post_id   | bigint(20) | NO   | PRI | NULL    |       |
| post_text | longtext   | YES  | MUL | NULL    |       |
| topic_id  | bigint(20) | YES  | MUL | NULL    |       |
+-----------+------------+------+-----+---------+-------+

DESCRIBE

mysql> DESCRIBE bb_topics_fulltext_search
    -> ;
+----------------------+--------------+------+-----+---------+-------+
| Field                | Type         | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+-------+
| topic_id             | int(11)      | NO   | PRI | NULL    |       |
| topic_title          | varchar(255) | YES  | MUL | NULL    |       |
| topic_posts          | bigint(20)   | YES  |     | NULL    |       |
| topic_poster_name    | varchar(40)  | YES  |     | NULL    |       |
| topic_last_post_id   | bigint(20)   | YES  |     | NULL    |       |
| forum_id             | int(11)      | YES  |     | NULL    |       |
| parent_group_id      | int(11)      | YES  |     | NULL    |       |
| child_group_id       | int(11)      | YES  |     | NULL    |       |
| topic_last_post_time | datetime     | YES  | MUL | NULL    |       |
+----------------------+--------------+------+-----+---------+-------+

EXPLAIN

+----+-------------+--------------+--------+---------------+---------+---------+----------------------------------+--------+---------------------------------+
| id | select_type | table        | type   | possible_keys | key     | key_len | ref                              | rows   | Extra                           |
+----+-------------+--------------+--------+---------------+---------+---------+----------------------------------+--------+---------------------------------+
|  1 | SIMPLE      | post_search  | ALL    | NULL          | NULL    | NULL    | NULL                             | 158972 | Using temporary; Using filesort |
|  1 | SIMPLE      | topic_search | eq_ref | PRIMARY       | PRIMARY | 4       | wordpress.post_search.topic_id   |      1 | Using where                     |
+----+-------------+--------------+--------+---------------+---------+---------+----------------------------------+--------+---------------------------------+

UPDATE: Indexes

+--------------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                    | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| bb_posts_fulltext_search |          0 | PRIMARY   |            1 | post_id     | A         |      158972 |     NULL | NULL   |      | BTREE      |         |               |
| bb_posts_fulltext_search |          1 | topic_id  |            1 | topic_id    | A         |       52990 |     NULL | NULL   | YES  | BTREE      |         |               |
| bb_posts_fulltext_search |          1 | post_text |            1 | post_text   | NULL      |           1 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+--------------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

CREATE 1

DROP TABLE IF EXISTS `wordpress`.`bb_posts_fulltext_search`;
CREATE TABLE  `wordpress`.`bb_posts_fulltext_search` (
  `post_id` bigint(20) NOT NULL,
  `post_text` longtext,
  `topic_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`post_id`),
  KEY `topic_id` (`topic_id`),
  FULLTEXT KEY `post_text` (`post_text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE 2

DROP TABLE IF EXISTS `wordpress`.`bb_topics_fulltext_search`;
CREATE TABLE  `wordpress`.`bb_topics_fulltext_search` (
  `topic_id` int(11) NOT NULL,
  `topic_title` varchar(255) DEFAULT NULL,
  `topic_posts` bigint(20) DEFAULT NULL,
  `topic_poster_name` varchar(40) DEFAULT NULL,
  `topic_last_post_id` bigint(20) DEFAULT NULL,
  `forum_id` int(11) DEFAULT NULL,
  `parent_group_id` int(11) DEFAULT NULL,
  `child_group_id` int(11) DEFAULT NULL,
  `topic_last_post_time` datetime DEFAULT NULL,
  PRIMARY KEY (`topic_id`),
  KEY `topic_last_post_time` (`topic_last_post_time`),
  FULLTEXT KEY `topic_title` (`topic_title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

SOLUTION:
http://forums.mysql.com/read.php?115,418955,418955#msg-418955

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2024-11-12 12:51:08

OP在这里找到了答案,以防万一链接消失,以下是该链接中的文本:

DROP TABLE IF EXISTS `wordpress`.`bb_posts_fulltext_search`;
CREATE TABLE `wordpress`.`bb_posts_fulltext_search` (
`post_id` int(10) unsigned NOT NULL,
`post_text` longtext,
`topic_id` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`post_id`,`topic_id`) USING BTREE,
FULLTEXT KEY `post_text` (`post_text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `wordpress`.`bb_topics_fulltext_search`;
CREATE TABLE `wordpress`.`bb_topics_fulltext_search` (
`topic_id` int(11) NOT NULL,
`topic_title` varchar(255) DEFAULT NULL,
`topic_posts` bigint(20) DEFAULT NULL,
`topic_poster_name` varchar(40) DEFAULT NULL,
`topic_last_post_id` bigint(20) DEFAULT NULL,
`forum_id` int(11) DEFAULT NULL,
`parent_group_id` int(11) DEFAULT NULL,
`child_group_id` int(11) DEFAULT NULL,
`topic_last_post_time` datetime DEFAULT NULL,
PRIMARY KEY (`topic_id`),
KEY `topic_last_post_time` (`topic_last_post_time`),
FULLTEXT KEY `topic_title` (`topic_title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

SELECT
  topic_search.topic_id,
  topic_search.topic_title,
  topic_search.topic_posts,
  topic_search.topic_title,
  topic_search.topic_poster_name,
  topic_search.topic_last_post_id,
  topic_search.topic_last_post_time,
  MATCH(post_search.post_text,topic_search.topic_title) 
    AGAINST('searchterms' IN BOOLEAN MODE) AS score
FROM bb_posts_fulltext_search as post_search
LEFT JOIN bb_topics_fulltext_search as topic_search
  ON post_search.topic_id = topic_search.topic_id
WHERE 
  MATCH(post_search.post_text,topic_search.topic_title) 
    AGAINST('searchterms' IN BOOLEAN MODE)
GROUP BY topic_search.topic_id
ORDER BY score DESC
LIMIT 0,6 

The OP has found an answer here, just in case the link disappears, below is the text in that link:

DROP TABLE IF EXISTS `wordpress`.`bb_posts_fulltext_search`;
CREATE TABLE `wordpress`.`bb_posts_fulltext_search` (
`post_id` int(10) unsigned NOT NULL,
`post_text` longtext,
`topic_id` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`post_id`,`topic_id`) USING BTREE,
FULLTEXT KEY `post_text` (`post_text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `wordpress`.`bb_topics_fulltext_search`;
CREATE TABLE `wordpress`.`bb_topics_fulltext_search` (
`topic_id` int(11) NOT NULL,
`topic_title` varchar(255) DEFAULT NULL,
`topic_posts` bigint(20) DEFAULT NULL,
`topic_poster_name` varchar(40) DEFAULT NULL,
`topic_last_post_id` bigint(20) DEFAULT NULL,
`forum_id` int(11) DEFAULT NULL,
`parent_group_id` int(11) DEFAULT NULL,
`child_group_id` int(11) DEFAULT NULL,
`topic_last_post_time` datetime DEFAULT NULL,
PRIMARY KEY (`topic_id`),
KEY `topic_last_post_time` (`topic_last_post_time`),
FULLTEXT KEY `topic_title` (`topic_title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

SELECT
  topic_search.topic_id,
  topic_search.topic_title,
  topic_search.topic_posts,
  topic_search.topic_title,
  topic_search.topic_poster_name,
  topic_search.topic_last_post_id,
  topic_search.topic_last_post_time,
  MATCH(post_search.post_text,topic_search.topic_title) 
    AGAINST('searchterms' IN BOOLEAN MODE) AS score
FROM bb_posts_fulltext_search as post_search
LEFT JOIN bb_topics_fulltext_search as topic_search
  ON post_search.topic_id = topic_search.topic_id
WHERE 
  MATCH(post_search.post_text,topic_search.topic_title) 
    AGAINST('searchterms' IN BOOLEAN MODE)
GROUP BY topic_search.topic_id
ORDER BY score DESC
LIMIT 0,6 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文