我目前正在尝试针对 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) 我在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) 当在超过 1 亿行的表上的另一列上使用 order by 时,我试图获得简单的全文匹配,以便更快地进行。基础是一张包含两列全文的表,我想搜索数据库,但按主要(最少/最近)或流行度对其进行排序。是否可以通过另一个索引列上的 order by 快速创建全文?下面的 SQL Fiddle 包含架构并解释了所有查询:
到目前为止,非常快的是在单独的表和连接中对搜索列进行非规范化,但如果没有必要,我宁愿不使用另一个表。下面的 SQL Fiddle(最后是非规范化查询):