我目前正在尝试针对 Stack Overflow 评论的数据转储运行一些查询。这是架构的样子:
CREATE TABLE `socomments` (
`Id` int(11) NOT NULL,
`PostId` int(11) NOT NULL,
`Score` int(11) DEFAULT NULL,
`Text` varchar(600) NOT NULL,
`CreationDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`UserId` int(11) NOT NULL,
PRIMARY KEY (`Id`),
KEY `idx_socomments_PostId` (`PostId`),
KEY `CreationDate` (`CreationDate`),
FULLTEXT KEY `Text` (`Text`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Run Code Online (Sandbox Code Playgroud)
我对表运行了这个查询,它运行得非常慢(它确实有 2900 万行,但它有一个全文索引):
SELECT *
FROM socomments
WHERE MATCH (Text) AGAINST ('"fixed the post"' IN BOOLEAN MODE)
Run Code Online (Sandbox Code Playgroud)
所以我分析了它,结果是:
|| Status || Duration ||
|| starting || …Run Code Online (Sandbox Code Playgroud) 当我添加一些以布尔模式为条件的单词时,全文匹配忽略了它的索引。选择如下:
explain select * from seeds WHERE MATCH(text) AGAINST ("mount cameroon" IN BOOLEAN MODE);
Run Code Online (Sandbox Code Playgroud)
产出
+----+-------------+-------+----------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+----------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | seeds | fulltext | text | text | 0 | | 1 | Using where |
+----+-------------+-------+----------+---------------+------+---------+------+------+-------------+
Run Code Online (Sandbox Code Playgroud)
具有多个条件词的相同查询
explain select * from seeds WHERE MATCH(text) AGAINST ("mount cameroon" IN BOOLEAN MODE) = 4;
Run Code Online (Sandbox Code Playgroud)
产出
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id …Run Code Online (Sandbox Code Playgroud) 我有一个基本表:
create table fullTextTest
(
id INT(11) NOT NULL,
superText CHAR(255) NOT NULL,
superLongText TEXT NOT NULL,
primary key (`id`),
FULLTEXT KEY `superText` (`superText`),
FULLTEXT KEY `superLongtext` (`superLongtext`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into fullTextTest
set id=1,
superText="Hi guys, how is it goin'?",
superLongtext="Please give me some dummy text to search on!!!"
;
show index from fullTextTest;
| fullTextTest | 0 | PRIMARY | 1 | id | A | 1 | NULL | NULL | | BTREE | | …Run Code Online (Sandbox Code Playgroud) 这是我当前的查询:
SELECT BusinessID as ID,
111151.29341326*SQRT(pow(-6.186751-X(LatLong),2)+pow(106.772835-Y(LatLong),2)*0.98838574205337) AS Distance from
(
SELECT *
FROM
tableauxiliary
WHERE
MBRContains(
GeomFromText (
'MULTIPOINT(-6.1934985598076 106.76604791159,-6.1800034401924 106.77962208841)'
),
Latlong)=1
AND Prominent >15
) AS TA
Having Distance <= 18238
ORDER BY
Distance
LIMIT
0, 45
Run Code Online (Sandbox Code Playgroud)
请注意,他们我使用了子查询。它使用子查询的原因是因为我想要
MBRContains(
GeomFromText (
'MULTIPOINT(-6.1934985598076 106.76604791159,-6.1800034401924 106.77962208841)'
),
Latlong)=1
Run Code Online (Sandbox Code Playgroud)
首先要完成。这将查询时间从 19 秒减少到 0.9 秒。
有没有办法提示mysql查询优化器,这样我就不需要使用子查询
更新:
我试过:
SELECT BusinessID as ID,
111151.29341326*SQRT(pow(-6.186751-X(LatLong),2)+pow(106.772835-Y(LatLong),2)*0.98838574205337) AS Distance from tableauxiliary
USE Index (LatLong_2,FullTextSearch)
WHERE
MBRContains(
GeomFromText (
'MULTIPOINT(-6.1934985598076 106.76604791159,-6.1800034401924 106.77962208841)'
),
Latlong)
AND Prominent >15 …Run Code Online (Sandbox Code Playgroud) 我在https://serverfault.com/questions/353888/mysql-full-text-search-cause-high-usage-cpu 上提出了一个问题一些用户建议在这里提问。
我们建立了一个新闻网站。每天我们都会从web api输入数以万计的数据。
为了提供精准的搜索服务,我们的表使用了MyISAM,建立了全文索引(标题、内容、日期)。我们的网站正在测试 Godaddy VDS,内存为 2GB,空间为 30GB(无交换,因为 VDS 不允许构建交换)。CPU是Intel(R) Xeon(R) CPU L5609 @ 1.87GHz
运行一个 ./mysqltuner.pl
我们得到一些结果:
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.5.20
[OK] Operating on 32-bit architecture with less than 2GB RAM
-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 396M (Tables: 39)
[--] Data in InnoDB tables: 208K (Tables: 8)
[!!] …Run Code Online (Sandbox Code Playgroud)