mysql索引的藝術 單列索引與多列索引的區別

來源:互聯網
上載者:User

標籤:update   div   weight   多個   var   區別   har   let   sql   

CREATE TABLE `security_role_resource` (  `id` varchar(50) NOT NULL DEFAULT ‘‘,  `role_id` varchar(50) DEFAULT NULL,  `resource_id` varchar(50) DEFAULT NULL,  `date_create` timestamp NULL DEFAULT NULL,  `date_update` timestamp NULL DEFAULT NULL,  `date_delete` timestamp NULL DEFAULT NULL,  `parent_shop_code` varchar(50) DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `index_role_id` (`role_id`),  KEY `index_resource_id` (`resource_id`),  KEY `index_parent_shop_code` (`parent_shop_code`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `security_role_resource2` (  `id` varchar(50) NOT NULL DEFAULT ‘‘,  `role_id` varchar(50) DEFAULT NULL,  `resource_id` varchar(50) DEFAULT NULL,  `date_create` timestamp NULL DEFAULT NULL,  `date_update` timestamp NULL DEFAULT NULL,  `date_delete` timestamp NULL DEFAULT NULL,  `parent_shop_code` varchar(50) DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `index_parent_shop_code_role_id_resource_id` (`parent_shop_code`,`role_id`,`resource_id`),  KEY `index_role_id` (`role_id`),  KEY `index_resource_id` (`resource_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

兩個表的區別在於第二表是複合式索引,組合了`parent_shop_code`,`role_id`,`resource_id`三列

單獨的每個有索引的列查詢沒有區別,但是如果多個條件一起查詢

explain select * from security_role_resource2 where parent_shop_code=‘002142‘ and resource_id=‘PNqiYZbiNN‘ and role_id=‘GBcvY87pCq‘ ;

mysql的處理方式不一樣了,它會選擇一個最嚴格的索引來進行檢索,可以理解為檢索能力最強的那個索引來檢索,另外幾個索引利用不上了,這樣效果就不如多列索引了。

現在同樣的查詢條件對第一個表查詢

explain select * from security_role_resource where parent_shop_code=‘002142‘ and resource_id=‘PNqiYZbiNN‘ and role_id=‘GBcvY87pCq‘ ;

下面這樣的查詢用不到複合式索引:

explain select * from security_role_resource where parent_shop_code=‘002142‘ or role_id=‘GBcvY87pCq‘;

 

mysql索引的藝術 單列索引與多列索引的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.