索引的大小对于我的网店我有一个表,我使用了搜索:MySQL的减少巨额表
CREATE TABLE `store_search` (
`term` varchar(50) NOT NULL DEFAULT '',
`content_id` int(10) unsigned NOT NULL,
`type` enum('keyword','tag') NOT NULL DEFAULT 'keyword',
`random` int(10) unsigned NOT NULL,
`saving` int(10) unsigned NOT NULL,
PRIMARY KEY (`content_id`,`term`,`type`),
UNIQUE KEY `saving` (`term`,`saving`,`random`,`content_id`,`type`),
UNIQUE KEY `random` (`term`,`random`,`content_id`,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED
产品可以以两种方式列出:按随机顺序(基于栏random
)或折扣(基于列saving
)。过去的测试显示,使用UNIQUE
订单的约束比使用标准索引与ORDER BY
更有效。查询可以是这样的:
mysql> EXPLAIN SELECT content_id FROM store_search USE INDEX (random) WHERE term LIKE 'shirt%' AND type='keyword' LIMIT 2000,100;
+----+-------------+--------------+-------+---------------+--------+---------+------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+-------+---------------+--------+---------+------+---------+--------------------------+
| 1 | SIMPLE | store_search | range | random | random | 152 | NULL | 9870580 | Using where; Using index |
+----+-------------+--------------+-------+---------------+--------+---------+------+---------+--------------------------+
这样我就可以防止ORDER BY
条款(没有文件排序中,这种方法做)。
mysql> EXPLAIN SELECT DISTINCT x.content_id
-> FROM store_search x USE INDEX (saving)
-> INNER JOIN store_search y ON x.content_id=y.content_id
-> WHERE x.term LIKE 'shirt%' AND x.type='keyword' AND y.term LIKE 'blue%' AND y.type='keyword'
-> LIMIT 0,100;
+----+-------------+-------+-------+-----------------------+---------+---------+--------------+----------+-------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------------+---------+---------+--------------+----------+-------------------------------------------+
| 1 | SIMPLE | x | range | PRIMARY,saving,random | saving | 152 | NULL | 11449970 | Using where; Using index; Using temporary |
| 1 | SIMPLE | y | ref | PRIMARY,saving,random | PRIMARY | 4 | x.content_id | 20 | Using where; Using index; Distinct |
+----+-------------+-------+-------+-----------------------+---------+---------+--------------+----------+-------------------------------------------+
正如我所说的,这个解决方案是好的迄今:PRIMARY KEY
用于自搜索多个术语时加入。我现在的问题是:目前这张表(〜500mio行)非常庞大,索引不再适合内存。这导致,INSERT
和UPDATE
语句非常慢。数据需要23GB和索引消耗32GB,所以这张表的55GB。测试是可能的,但复制这张表时会消耗大量时间,但是有没有人可以减少索引大小? 我想将字符串列的排序转换为latin_1
,但我可以合并一些索引吗?
列** **中的不同值的数量限制为几个或者它们是否是真正的自由文本? – trincot
这些确实是免费的文字。长期限制为50个字符。 – rabudde
您实际需要向最终用户提供的记录数是否有限制?我的意思是,如果你得到10000场比赛,你是否真的需要全部提供? – trincot