索引的大小對於我的網店我有一個表,我使用了搜索: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