上篇粗略记录当mysql字段类型是string,传入int类型参数后失效当问题。

现在测试下mysql字段是int类型,传参string类型会发生什么。

题外话,最近膝盖手术后还在家养伤中,只怪自己以前骑车不注意休息保养,经常长途骑行出去玩,把膝盖骑费了(抽取积液+切除膝盖囊肿手术),搞得现在哪都去不了,已经一周没下楼走走。

【索引失效】

二. 单字段索引:字段是INT类型,传入string类型参数

MySQL [test_db]> show create table test_users\G;
*************************** 1. row ***************************
Table: test_users
Create Table: CREATE TABLE `test_users` (
`uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` char(15) NOT NULL,
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(11) unsigned NOT NULL DEFAULT '',
PRIMARY KEY (`uid`),
KEY `testindex` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1306001 DEFAULT CHARSET=utf8mb4
1 row in set (0.05 sec) ERROR: No query specified #开启profile
MySQL [test_db]> set profiling =1;
Query OK, 0 rows affected, 1 warning (0.03 sec) MySQL [test_db]> select * from test_users where user_id = '';
Empty set (0.03 sec) MySQL [test_db]> select * from test_users where user_id = 899242;
Empty set (0.03 sec)

 MySQL [test_db]> set profiling=0;
  Query OK, 0 rows affected, 1 warning (0.04 sec)

MySQL [test_db]> show profiles;
+----------+------------+---------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------------------------+
| 1 | 0.00034000 | select * from test_users where user_id = '' |
| 2 | 0.00034850 | select * from test_users where user_id = 899242 |
+----------+------------+---------------------------------------------------+
2 rows in set, 1 warning (0.04 sec)
#可以看到两种查询耗时基本持平
MySQL [test_db]> explain select * from test_users where user_id = 899242;
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test_users | NULL | ref | testindex | testindex | 4 | const | 1 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.03 sec) MySQL [test_db]> explain select * from test_users where user_id = '';
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test_users | NULL | ref | testindex | testindex | 4 | const | 1 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.04 sec)
#再用explain分析下,可见两种查询都是key=testindex

上面针对的都是单字段索引,现在我们使用组合索引,对比下会有什么不一样。

三. 组合索引

MySQL [test_db]> show create table test_log\G;
*************************** 1. row ***************************
Table: test_log
Create Table: CREATE TABLE `test_log` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`log_id` int(11) unsigned NOT NULL DEFAULT '',
`rand_name` char(15) NOT NULL,
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`country` varchar(50) NOT NULL DEFAULT '',
`short_country_name` char(5) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `c` (`country`),
KEY `log` (`log_id`,`rand_name`,`country`)
) ENGINE=InnoDB AUTO_INCREMENT=6601004 DEFAULT CHARSET=utf8mb4
1 row in set (0.04 sec) ERROR: No query specified MySQL [test_db]> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.03 sec) MySQL [test_db]> select * from test_log where log_id = '';
Empty set (0.04 sec) MySQL [test_db]> select * from test_log where log_id = 987371;
+--------+--------+--------------+---------------------+--------------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+--------+--------+--------------+---------------------+--------------------+--------------------+
| 948373 | 987371 | 1ae53be9c1df | 2020-01-16 12:01:09 | 中国澳门特区 | MO |
+--------+--------+--------------+---------------------+--------------------+--------------------+
1 row in set (0.04 sec) MySQL [test_db]> set profiling =0;
Query OK, 0 rows affected, 1 warning (0.03 sec) MySQL [test_db]> show profiles;
+----------+------------+---------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------------------------+
| 1 | 0.00034000 | select * from test_users where user_id = '' |
| 2 | 0.00034850 | select * from test_users where user_id = 899242 |
| 3 | 0.00464450 | select * from test_log where log_id = '' |
| 4 | 0.01399875 | select * from test_log where log_id = 987371 |
+----------+------------+---------------------------------------------------+
4 rows in set, 1 warning (0.03 sec) #有没有发现什么不对劲的地方?
#没错 该组合索引里面 log_id是int类型,string类型参数比int类型参数快,到底哪里出问题? #explain分析下
MySQL [test_db]> explain select * from test_log where log_id = '';
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test_log | NULL | ref | log | log | 4 | const | 1 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.05 sec) MySQL [test_db]> explain select * from test_log where log_id = 987371;
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test_log | NULL | ref | log | log | 4 | const | 1 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.03 sec) #explain得到的结果都是一模一样!!! 那为什么有快慢之分,为了方便数据比较,我们重连mysql连接l。 Database changed
MySQL [test_db]> set profiling = 1;
Query OK, 0 rows affected, 1 warning (0.04 sec) MySQL [test_db]> select * from test_log where log_id = '';
+---------+---------+--------------+---------------------+--------------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+--------------------+--------------------+
| 5941183 | 5990180 | 970eb4c2f8e8 | 2020-01-17 13:20:37 | 吉尔吉斯斯坦 | KG |
+---------+---------+--------------+---------------------+--------------------+--------------------+
1 row in set (0.05 sec) MySQL [test_db]> select * from test_log where log_id = '';
+---------+---------+--------------+---------------------+-----------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+-----------------+--------------------+
| 2950083 | 2999080 | 805c2b1fbab1 | 2020-01-17 13:02:40 | 所罗门群岛 | Sb |
+---------+---------+--------------+---------------------+-----------------+--------------------+
1 row in set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = '';
+---------+---------+--------------+---------------------+-----------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+-----------+--------------------+
| 3851245 | 3900242 | f19fcdd1172e | 2020-01-17 13:08:21 | 菲律宾 | PH |
+---------+---------+--------------+---------------------+-----------+--------------------+
1 row in set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = '';
+---------+---------+--------------+---------------------+--------------------------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+--------------------------------+--------------------+
| 4391245 | 4440242 | fd03f30dfc3e | 2020-01-17 13:11:20 | 圣文森特和格陵纳丁斯 | VC |
+---------+---------+--------------+---------------------+--------------------------------+--------------------+
1 row in set (0.04 sec) MySQL [test_db]> select * from test_log where log_id = 23440242;
Empty set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = 2344042;
+---------+---------+--------------+---------------------+--------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+--------------+--------------------+
| 2295045 | 2344042 | bc89598c0d10 | 2020-01-17 12:58:47 | 格鲁吉亚 | GE |
+---------+---------+--------------+---------------------+--------------+--------------------+
1 row in set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = 5314042;
+---------+---------+--------------+---------------------+--------------------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+--------------------------+--------------------+
| 5265045 | 5314042 | 452e5ecf5fa5 | 2020-01-17 13:16:44 | 特立尼达和多巴哥 | TT |
+---------+---------+--------------+---------------------+--------------------------+--------------------+
1 row in set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = 5614092;
+---------+---------+--------------+---------------------+--------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+--------------+--------------------+
| 5565095 | 5614092 | 78adae2b40a3 | 2020-01-17 13:18:31 | 马约特岛 | YT |
+---------+---------+--------------+---------------------+--------------+--------------------+
1 row in set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = 5900392;
+---------+---------+--------------+---------------------+---------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+---------+---------+--------------+---------------------+---------+--------------------+
| 5851395 | 5900392 | e89ccadae397 | 2020-01-17 13:20:19 | 挪威 | NO |
+---------+---------+--------------+---------------------+---------+--------------------+
1 row in set (0.04 sec) MySQL [test_db]> set profiling = 0;
Query OK, 0 rows affected, 1 warning (0.03 sec) MySQL [test_db]> show profiles;
+----------+------------+-------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------+
| 1 | 0.01717650 | select * from test_log where log_id = '' |
| 2 | 0.00281950 | select * from test_log where log_id = '' |
| 3 | 0.00167475 | select * from test_log where log_id = '' |
| 4 | 0.00431675 | select * from test_log where log_id = '' |
| 5 | 0.00032000 | select * from test_log where log_id = 23440242 |
| 6 | 0.00387325 | select * from test_log where log_id = 2344042 |
| 7 | 0.00461725 | select * from test_log where log_id = 5314042 |
| 8 | 0.00485450 | select * from test_log where log_id = 5614092 |
| 9 | 0.00476200 | select * from test_log where log_id = 5900392 |
+----------+------------+-------------------------------------------------+
9 rows in set, 1 warning (0.04 sec)
#通过对比发现字段为int类型是,参数是int还是string对耗时影响不大
#耗时差别不大,我们猜测一下这里mysql默认将string类型转换成int类型了
#不相信的话,我们来验证下就知道了 MySQL [test_db]> select * from test_log where log_id = 'a666f';
Empty set (0.04 sec) MySQL [test_db]> select * from test_log limit 1;
+----+--------+--------------+---------------------+--------------------------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+----+--------+--------------+---------------------+--------------------------------+--------------------+
| 1 | 1 | c4ca4238a0b9 | 2020-01-16 11:54:25 | 中立区(沙特-伊拉克间) | NT |
+----+--------+--------------+---------------------+--------------------------------+--------------------+
1 row in set (0.04 sec) MySQL [test_db]> update test_log set log_id=0 where id = 1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0 MySQL [test_db]> select * from test_log where log_id = 'a666f';
+----+--------+--------------+---------------------+--------------------------------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+----+--------+--------------+---------------------+--------------------------------+--------------------+
| 1 | 0 | c4ca4238a0b9 | 2020-01-16 11:54:25 | 中立区(沙特-伊拉克间) | NT |
+----+--------+--------------+---------------------+--------------------------------+--------------------+
1 row in set (0.03 sec) MySQL [test_db]> select * from test_log where log_id = '12a1a';
+----+--------+--------------+---------------------+---------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+----+--------+--------------+---------------------+---------+--------------------+
| 12 | 12 | c20ad4d76fe9 | 2020-01-16 11:54:25 | 英国 | UK |
+----+--------+--------------+---------------------+---------+--------------------+
1 row in set (0.04 sec) MySQL [test_db]> select * from test_log where log_id = '02a1a';
+----+--------+--------------+---------------------+---------+--------------------+
| id | log_id | rand_name | created_time | country | short_country_name |
+----+--------+--------------+---------------------+---------+--------------------+
| 2 | 2 | c81e728d9d4c | 2020-01-16 11:54:25 | 中国 | CN |
+----+--------+--------------+---------------------+---------+--------------------+
1 row in set (0.03 sec)

mysql索引创建和使用细节(二)的更多相关文章

  1. mysql索引创建和使用细节

    最近困扰自己很久的膝盖积液手术终于做完,在家养伤,逛技术博客看到easyswoole开发组成员仙士可博客有关mysql索引方面的知识,自己打算重温下. 正常业务起步数据表数据量较少,不用考虑使用索引, ...

  2. Database基础(二):MySQL索引创建与删除、 MySQL存储引擎的配置

    一.MySQL索引创建与删除 目标: 本案例要求熟悉MySQL索引的类型及操作方法,主要练习以下任务: 普通索引.唯一索引.主键索引的创建/删除 自增主键索引的创建/删除 建立员工表yg.工资表gz, ...

  3. 索引-mysql索引创建、查看、删除及使用示例

    mysql索引创建.查看.删除及使用示例 1.创建索引: ALTER TABLE用来创建普通索引.UNIQUE索引或PRIMARY KEY索引. ALTER TABLE table_name ADD ...

  4. MySQl索引创建

    一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表 ...

  5. Mysql索引创建及删除

    1.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...

  6. Mysql索引(究极无敌细节版)

    参考了: https://www.jianshu.com/p/ace3cd6526c4 推荐up主https://space.bilibili.com/377905911 推荐书籍<mysql是 ...

  7. mysql索引 ->创建索引、修改索引、删除索引的命令语句

    查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...

  8. MYSQL 索引创建与使用

    可能用到索引的地方: where 子句,order by,group by 不需要创建索引的情况: 1. 表比较小 2.赋值有限的列(枚举),不要创建索引.创建的索引返回的行越少越好,此时区分度大. ...

  9. mysql索引创建&查看&删除

    1.索引作用 在索引列上,除了上面提到的有序查找之外,数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍. 例如,有 ...

随机推荐

  1. pycharm下的多个python版本共存(一)

    经历过IDLE,anaconda,和pycharn的编程环境,并进行了一段时间的项目编程后,决定使用pycharm作为以后的工作环境. 一方面因为项目组其他人推荐,另一方面在使用过程中比较顺手.当然很 ...

  2. git clone出现Permission denied (publickey)解决办法

    一.错误 git clone git@gitee.com:wangzaiplus/xxx.git, 出现Permission denied (publickey) 二.原因 无权限, 未将公钥添加至G ...

  3. HDU 2454"Degree Sequence of Graph G"(度序列可图性判断)

    传送门 参考资料: [1]:图论-度序列可图性判断(Havel-Hakimi定理) •题意 给你 n 个非负整数列,判断这个序列是否为可简单图化的: •知识支持 握手定理:在任何无向图中,所有顶点的度 ...

  4. 2019-8-4-自动更新所有-Git-仓库

    title author date CreateTime categories 自动更新所有 Git 仓库 lindexi 2019-08-04 14:44:59 +0800 2019-08-01 2 ...

  5. js基础——继承

    1.实现继承:原型链         function extend1() {//父类型           this.name = "张三";         }         ...

  6. 基于AutoIt3的运维工具

    #Region ;**** 编译指令由 AutoIt3Wrapper 选项编译窗口创建 **** #AutoIt3Wrapper_Icon=favicon.ico #AutoIt3Wrapper_Co ...

  7. 中和IOS七层架构和TCP/IP四层架构的五层架构

    五层架构分别为应用层.运输层.网络层.数据链路层.物理层. IOS架构把应用层又细分为应用层.表示层.会话层 TCP/IP把网络层改名网际层,数据链路层和物理层结合成网络接口层 其实只要学习五层协议, ...

  8. dotnet 修复找不到 System.ServiceProcess 定义

    本文告诉大家如果复制网上一段代码发现 System.ServiceProcess 提示找不到方法或定义,需要手动添加引用 例如下面一段代码 using System.ServiceProcess; p ...

  9. 【NOIP模拟赛】【数学】完全平方数

    问题描述 一个数如果是另一个整数的完全平方,那么我们就称这个数为完全平方数(Pefect Sqaure),也称平方数. 小A认为所有的平方数都是很perfect的~ 于是他给了小B一个任务:用任意个不 ...

  10. 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)

    牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...