新聞中心
本篇文章為大家展示了MySQL中如何創(chuàng)建Key分區(qū)表,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
我們提供的服務有:成都網(wǎng)站設計、成都網(wǎng)站建設、外貿(mào)網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、黃岡ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的黃岡網(wǎng)站制作公司
按照KEY進行分區(qū)類似于按照HASH分區(qū),除了HASH分區(qū)使用的用戶定義的表達式,而KEY分區(qū)的 哈希函數(shù)是由MySQL 服務器提供。MySQL 簇(Cluster)使用函數(shù)MD5()來實現(xiàn)KEY分區(qū);
對于使用其他存儲引擎的表,服務器使用其自己內(nèi)部的 哈希函數(shù),這些函數(shù)是基于與PASSWORD()一樣的運算法則。
Key分區(qū)與Hash分區(qū)很相似,只是Hash函數(shù)不同,定義時把Hash關(guān)鍵字替換成Key即可,同樣Key分區(qū)也有對應與線性Hash的線性Key分區(qū)方法。
語法為PARTITION BY LINEAR KEY(列名)
創(chuàng)建key分區(qū)表舉例如下:
mysql> CREATE TABLE `dsf_data` (
-> `id` bigint(20) NOT NULL AUTO_INCREMENT,
-> `SH` varchar(32) DEFAULT NULL COMMENT '稅號',
-> `KPJH` varchar(32) DEFAULT NULL COMMENT '開票機號',
-> `ZFJH` varchar(32) DEFAULT NULL COMMENT '主分機號',
-> `MONTH` varchar(10) DEFAULT NULL,
-> `STATUS` varchar(255) DEFAULT NULL COMMENT '解析狀態(tài)標識',
-> `CREATE_TIME` datetime DEFAULT NULL COMMENT '插入時間',
-> `UPDATE_TIME` datetime DEFAULT NULL COMMENT '更新時間',
-> `FP_DATA` mediumtext COMMENT '發(fā)票數(shù)據(jù)',
-> PRIMARY KEY (`id`,`SH`),
-> KEY `index_sh` (`SH`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=1173560 DEFAULT CHARSET=utf8 PARTITION BY LINEAR KEY (SH) PARTITIONS 8;
Query OK, 0 rows affected (0.11 sec)
備注:如果分區(qū)字段中有主鍵或者唯一索引的列,那么所有主鍵列和唯一索引列都必須包含進來,因此上一步必須有兩個主鍵PRIMARY KEY (`id`,`SH`)存在。
插入數(shù)據(jù):
mysql> insert into dsf_data select * from test.fp_data;
Query OK, 202632 rows affected, 1 warning (18.96 sec)
Records: 202632 Duplicates: 0 Warnings: 1
mysql> explain partitions select sh from dsf_data; --全表掃描共訪問了8個分區(qū)(p0--p7)
+----+-------------+----------+-------------------------+-------+---------------+----------+---------+------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------------------------+-------+---------------+----------+---------+------+------+-------------+
| 1 | SIMPLE | dsf_data | p0,p1,p2,p3,p4,p5,p6,p7 | index | NULL | index_sh | 98 | NULL | 8 | Using index |
+----+-------------+----------+-------------------------+-------+---------------+----------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain partitions select sh from dsf_data where sh='130202568907641'; --值被隨機分到了p0分區(qū)
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | SIMPLE | dsf_data | p0 | ref | index_sh | index_sh | 98 | const | 1 | Using where; Using index |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> explain partitions select sh from dsf_data where sh='440300683797687'; --值被隨機分到了p4分區(qū)
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | SIMPLE | dsf_data | p4 | ref | index_sh | index_sh | 98 | const | 1 | Using where; Using index |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> explain partitions select sh from dsf_data where sh='91500107784224861G'; --sh的值被隨機分到了p6分區(qū)
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | SIMPLE | dsf_data | p6 | ref | index_sh | index_sh | 98 | const | 452 | Using where; Using index |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
值被隨機分到各個分區(qū),說明分區(qū)表創(chuàng)建成功。
上述內(nèi)容就是MySQL中如何創(chuàng)建Key分區(qū)表,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:MySQL中如何創(chuàng)建Key分區(qū)表
當前網(wǎng)址:http://fisionsoft.com.cn/article/pdhgid.html