mysql> select count(distinct substring(email,1,10))/count(*) from tb_user;+------------------------------------------------+|count(distinct substring(email,1,10))/count(*)|+------------------------------------------------+|1.0000|+------------------------------------------------+1 row in set(0.00 sec)mysql>
9、电子邮件地址的前9个字符的唯一值的数量与总用户数量的比率
mysql> select count(distinct substring(email,1,9))/count(*) from tb_user;+-----------------------------------------------+|count(distinct substring(email,1,9))/count(*)|+-----------------------------------------------+|0.9583|+-----------------------------------------------+1 row in set(0.00 sec)mysql>
10、电子邮件地址的前8个字符与前9个字符在唯一性方面的表现是相似的
mysql> select count(distinct substring(email,1,8))/count(*) from tb_user;+-----------------------------------------------+|count(distinct substring(email,1,8))/count(*)|+-----------------------------------------------+|0.9583|+-----------------------------------------------+1 row in set(0.00 sec)mysql>
11、前 6 个字符的不重复数量占总行数的比例
mysql> select count(distinct substring(email,1,6))/count(*) from tb_user;+-----------------------------------------------+|count(distinct substring(email,1,6))/count(*)|+-----------------------------------------------+|0.9583|+-----------------------------------------------+1 row in set(0.00 sec)mysql>
12、前 5 个字符的不重复数量占总行数的比例
mysql> select count(distinct substring(email,1,5))/count(*) from tb_user;+-----------------------------------------------+|count(distinct substring(email,1,5))/count(*)|+-----------------------------------------------+|0.9583|+-----------------------------------------------+1 row in set(0.00 sec)mysql>
13、随着截取长度的减少,电子邮件地址前缀的唯一性也在减少
mysql> select count(distinct substring(email,1,4))/count(*) from tb_user;+-----------------------------------------------+|count(distinct substring(email,1,4))/count(*)|+-----------------------------------------------+|0.9167|+-----------------------------------------------+1 row in set(0.00 sec)mysql>
14、查看MySQL中tb_user表的索引
mysql> show index from tb_user;+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+| tb_user |0| PRIMARY |1| id | A |24|NULL|NULL|| BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |1| profession | A |16|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |2| age | A |22|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |3| status | A |24|NULL|NULL| YES | BTREE ||| YES |NULL|+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+4 rows in set(0.01 sec)mysql>
15、在tb_user表的email列上创建一个前缀索引,其中只包括email列的前5个字符
mysql> create index idx_email_5 on tb_user(email(5));
Query OK,0 rows affected(0.05 sec)
Records:0 Duplicates:0 Warnings:0mysql> show index from tb_user;+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+| tb_user |0| PRIMARY |1| id | A |24|NULL|NULL|| BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |1| profession | A |16|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |2| age | A |22|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |3| status | A |24|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_email_5 |1| email | A |23|5|NULL| YES | BTREE ||| YES |NULL|+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+5 rows in set(0.01 sec)mysql>
16、查询 email=‘daqiao666@sina.com’ 的用户
mysql> select * from tb_user where email='daqiao666@sina.com';+----+------+-------------+--------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+------+-------------+--------------------+------------+------+--------+--------+---------------------+|6| 大乔 |17799990005| daqiao666@sina.com | 舞蹈 |22|2|0|2001-02-0700:00:00|+----+------+-------------+--------------------+------------+------+--------+--------+---------------------+1 row in set(0.00 sec)mysql>
17、执行计划 email=‘daqiao666@sina.com’
mysql> explain select * from tb_user where email='daqiao666@sina.com';+----+-------------+---------+------------+------+---------------+-------------+---------+-------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+---------------+-------------+---------+-------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ref | idx_email_5 | idx_email_5 |23|const|1|100.00| Using where |+----+-------------+---------+------------+------+---------------+-------------+---------+-------+------+----------+-------------+1 row in set,1warning(0.00 sec)mysql>