数据库教程:mysql索引最左原则实例代码分享

前言

最近在看MySQL索引的知识,看到组合索引的时候,有一个最左侧原则,通过查找相关资料深入学习了下,下面话不多说了,来一起看看详细的介绍吧

建表

CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL, `age` tinyint(2) DEFAULT NULL, PRIMARY KEY (`id`), KEY `Index_user` (`name`,`age`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

测试sql

第一种

mysql> explain SELECT * FROM `user` where name="tom" G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: user partitions: NULL type: ref possible_keys: Index_user key: Index_user key_len: 43 ref: const rows: 1 filtered: 100.00 Extra: NULL

第二种

mysql> explain SELECT * FROM `user` where age=18 and name="tom" G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: user partitions: NULL type: ref possible_keys: Index_user key: Index_user key_len: 45 ref: const,const rows: 1 filtered: 100.00 Extra: NULL

第三种

mysql> explain SELECT * FROM `user` where age=18 G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: user partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3 filtered: 33.33 Extra: Using where 1 row in set, 1 warning (0.00 sec)

第四种

mysql> explain SELECT * FROM `user` where name="tom" and age=18 G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: user partitions: NULL type: ref possible_keys: Index_user key: Index_user key_len: 45 ref: const,const rows: 1 filtered: 100.00 Extra: NULL 1 row in set, 1 warning (0.00 sec)

上述就是数据库技术:mysql索引最左原则实例代码分享的全部内容,如果对大家有所用处且需要了解更多关于mysql数据库学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/944895.html

(0)
上一篇 2021年11月14日 下午11:48
下一篇 2021年11月14日

精彩推荐