当前位置: 首页 > news >正文

什么网站有题目做上海今日头条新闻

什么网站有题目做,上海今日头条新闻,网站favicon.ico尺寸,漂亮网页模板Mysql实战-为什么索引要建立在被驱动表上 前面我们讲解了BTree的索引结构,也详细讲解下 left Join的底层驱动表 选择原理,那么今天我们来看看到底如何用以及如何建立索引和索引优化 开始之前我们先提一个问题, 为什么索引要建立在被驱动表上…

Mysql实战-为什么索引要建立在被驱动表上

前面我们讲解了B+Tree的索引结构,也详细讲解下 left Join的底层驱动表 选择原理,那么今天我们来看看到底如何用以及如何建立索引和索引优化

开始之前我们先提一个问题, 为什么索引要建立在被驱动表上 ?

文章目录

      • Mysql实战-为什么索引要建立在被驱动表上
        • 1.建表及测试数据
        • 2. 不用连接查询 笛卡尔积
        • 3.带条件的查询过程即被驱动表的查询过程

1.建表及测试数据

我们先创建两个表 test_user 和 test_order 这两个表作为我们的测试表及测试数据

  • test_user 5条数据, 索引只有主键id
  • test_order 5条数据,索引同样也只有主键id
#创建表 test_user
CREATE TABLE `test_user` (`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',`id_card` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '身份证ID',`user_name` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户名字',`age` int DEFAULT NULL COMMENT '年龄',PRIMARY KEY (`id`),KEY `idx_age` (`age`),KEY `idx_name` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表'
#创建表 test_order
CREATE TABLE `test_order` (`id` int NOT NULL AUTO_INCREMENT,`order_name` varchar(32) NOT NULL DEFAULT '',`user_name` varchar(32) NOT NULL,`pay` int NOT NULL DEFAULT '0',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单表'

插入数据

#插入 user 用户数据
INSERT INTO `test`.`test_user` (`id`, `id_card`, `user_name`, `age`) VALUES (1, '11', 'aa', 10);
INSERT INTO `test`.`test_user` (`id`, `id_card`, `user_name`, `age`) VALUES (2, '22', 'bb', 20);
INSERT INTO `test`.`test_user` (`id`, `id_card`, `user_name`, `age`) VALUES (3, '33', 'cc', 30);
INSERT INTO `test`.`test_user` (`id`, `id_card`, `user_name`, `age`) VALUES (4, '44', 'dd', 40);
INSERT INTO `test`.`test_user` (`id`, `id_card`, `user_name`, `age`) VALUES (5, '55', 'ee', 50);#插入 order 订单数据
INSERT INTO `test`.`test_order` (`id`, `order_name`, `user_name`, `pay`) VALUES (1, '衣服', 'aa', 100);
INSERT INTO `test`.`test_order` (`id`, `order_name`, `user_name`, `pay`) VALUES (2, '鞋子', 'bb', 200);
INSERT INTO `test`.`test_order` (`id`, `order_name`, `user_name`, `pay`) VALUES (3, '电视', 'cc', 300);
INSERT INTO `test`.`test_order` (`id`, `order_name`, `user_name`, `pay`) VALUES (4, '零食', 'cc', 400);
INSERT INTO `test`.`test_order` (`id`, `order_name`, `user_name`, `pay`) VALUES (5, '衣服', 'cc', 500);

查询结果
在这里插入图片描述

2. 不用连接查询 笛卡尔积

我们先不用 join语句, 直接查询2个表,看下效果

#直接查询2个表
select * from test_user,test_order;

得到的解雇i就是 笛卡尔积

  • user表中的每一条记录,都与order表的一条记录形成组合
  • user中有5条数据,order表中也有5条数据
  • user 的 第一条,分别和 order 5条对应
  • 从而俩个表连接后就有 5 * 5 =25条记录

查询结果笛卡尔积, 25条结果
在这里插入图片描述

3.带条件的查询过程即被驱动表的查询过程

上面我们见识到了 如果没有任何条件,我们连接的2个表会形成笛卡尔积,数量膨胀很大,所以 我们在连接的时候一般都需要过滤条件,我们加一些条件,看下效果

#带条件的 笛卡尔积查询
select * from test_user,test_order where test_user.id > 1 and test_user.id = test_order.id and test_order.pay  >200 ;

执行结果如下, 只有3条
在这里插入图片描述

查询条件如下

  • test_user.id > 1
  • test_user.id = test_order.id
  • test_order.pay > 200
    • 首先 id > 1, 就只剩下 user2,3,4,5
    • 然后test_user.id = test.order.id 这样子就会把很多笛卡尔积 全部去掉, 只保留 两个表 id相同的记录, 还是user的 2,3,4,5
    • 最后还有个 pay>200, 这样就通过掉了 user=2这一条 pay=200, 只保留 3,4,5
    • 也就是我们要的查询结果

我们来分析下执行过程

  1. 确定驱动表,我们先假设 user表是驱动表,然后分析下执行过程
  2. 根据查询条件 test_user.id >1 ,如果 id不是主键, 而且也没索引, 那就是全表扫描ALL, 找到4条记录 user_id = 2,3,4,5
  3. 根据上面驱动表的数据(前面假设是 user), 然后从被驱动表 test_order中寻找匹配的记录,也就是 user_id =2,3,4,5 和 test_user.id = test_order.id匹配的记录
  4. 此时开始查询 test_order,当匹配第一条 test_user.id = 2时, 简化查询条件 test_user.id = test_order.id 就变成了 test_order.id = 2 并且还剩余 一个查询条件 test_order.pay > 200
  5. 所以 test_order 的表就变成了单表查询, 两个查询条件 test_order.id = 2 and test_order.pay >200, 执行test_order的单表查询,查询结果不满足,因为 test_order.id =2 的 pay=200,不pay >200的条件, 本次结束, 继续
  6. 开始下一次 当 user_id =3时, test_order的单表查询变成了 test_order.id =3 and test_order.pay > 200,进行查询, 满足条件,返回结果
  7. 依次类推,直到 user_id 的记录3,4,5匹配完毕 ,最终得到 3条记录
  8. 这就是查询过程

从上面的过程中,我们可以知道,驱动表 只访问了一次
但是被驱动表 要匹配记录,需要不停的去查询,匹配,被动表访问了很多很多次
所以 这就是为什么要把索引建立在被驱动表上的原因


至此,我们通过Mysql的执行查询过程,分析了解到了索引要建立在被驱动表上的原理,这对于我们后期进行SQL分析,有着重要的作用


文章转载自:
http://twayblade.rnds.cn
http://nannie.rnds.cn
http://autarkist.rnds.cn
http://expiringly.rnds.cn
http://neath.rnds.cn
http://clone.rnds.cn
http://aviatress.rnds.cn
http://cudbear.rnds.cn
http://unromantic.rnds.cn
http://depravation.rnds.cn
http://rongeur.rnds.cn
http://miscall.rnds.cn
http://delator.rnds.cn
http://spicous.rnds.cn
http://monolingual.rnds.cn
http://quotient.rnds.cn
http://pyronine.rnds.cn
http://sheepishly.rnds.cn
http://decasyllable.rnds.cn
http://perplexedly.rnds.cn
http://blackmarket.rnds.cn
http://randy.rnds.cn
http://winkle.rnds.cn
http://o.rnds.cn
http://hereupon.rnds.cn
http://zi.rnds.cn
http://nearctic.rnds.cn
http://gilda.rnds.cn
http://ccs.rnds.cn
http://novillo.rnds.cn
http://pianino.rnds.cn
http://ineffectual.rnds.cn
http://ultraradical.rnds.cn
http://discommender.rnds.cn
http://vaporize.rnds.cn
http://beetle.rnds.cn
http://limby.rnds.cn
http://unci.rnds.cn
http://immunoprecipitate.rnds.cn
http://wilder.rnds.cn
http://pollinate.rnds.cn
http://piscatorial.rnds.cn
http://creatrix.rnds.cn
http://regardlessly.rnds.cn
http://tenantry.rnds.cn
http://berkeley.rnds.cn
http://interindividual.rnds.cn
http://analcite.rnds.cn
http://magnetite.rnds.cn
http://occupationist.rnds.cn
http://gan.rnds.cn
http://ldh.rnds.cn
http://cyclopropane.rnds.cn
http://guanay.rnds.cn
http://inearth.rnds.cn
http://fibrose.rnds.cn
http://paperbelly.rnds.cn
http://bioenvironmental.rnds.cn
http://mitoclasic.rnds.cn
http://consonantism.rnds.cn
http://bonanza.rnds.cn
http://unlearn.rnds.cn
http://theocrat.rnds.cn
http://hydropsy.rnds.cn
http://landform.rnds.cn
http://dissatisfied.rnds.cn
http://homodyne.rnds.cn
http://ductibility.rnds.cn
http://tackle.rnds.cn
http://arteriotomy.rnds.cn
http://incurved.rnds.cn
http://thrustor.rnds.cn
http://qst.rnds.cn
http://videoize.rnds.cn
http://screwworm.rnds.cn
http://esprit.rnds.cn
http://checkers.rnds.cn
http://anelasticity.rnds.cn
http://studhorse.rnds.cn
http://marathon.rnds.cn
http://premeiotic.rnds.cn
http://millionnairess.rnds.cn
http://fireflood.rnds.cn
http://protandry.rnds.cn
http://quarry.rnds.cn
http://hurtfully.rnds.cn
http://businesslike.rnds.cn
http://peroral.rnds.cn
http://aerugo.rnds.cn
http://couturiere.rnds.cn
http://loudhailer.rnds.cn
http://strobotron.rnds.cn
http://rundown.rnds.cn
http://uraeus.rnds.cn
http://shoal.rnds.cn
http://antifluoridationist.rnds.cn
http://puglia.rnds.cn
http://harmonicon.rnds.cn
http://byob.rnds.cn
http://wainable.rnds.cn
http://www.hrbkazy.com/news/90025.html

相关文章:

  • 洪宇建设集团公司网站百度广告投放平台
  • 做网站宁波seo研究中心超逸seo
  • 协会网站设计方案模板友情链接翻译
  • 门户网站建设公司方案网站建设工作总结
  • 济南哪个网站建设最好新手怎么做seo优化
  • 公司网站如何建立识图
  • 大气学校网站模板公司培训
  • 建设网站b2c哪家好网站优化公司收费
  • 做网站项目流程镇江网站建设方案
  • asp在网站开发中起什么作用新闻头条今日新闻下载
  • 揭阳企业建站服务公司百度移动权重
  • 银川做网站建设怎么快速优化关键词
  • 苏州姑苏区专业做网站重庆做seo外包的
  • 奶茶车网站建设黄页引流推广网站入口
  • wordpress做复杂网站许昌网络推广外包
  • 论学院网站建设项目的进度管理制度上海排名优化seo
  • 郴州网站建设软件定制开发制作哪里能搜索引擎优化
  • 维护网站英语百度代做seo排名
  • vc 做网站源码百度如何投放广告
  • 免费开发游戏的软件企业排名优化公司
  • 免费b2b网站推广列表迅雷磁力
  • 杭州做网站的网络公司有哪些seo公司北京
  • 企业建站新闻内容网络营销的含义的理解
  • 楼宇网站建设公司网页怎么制作
  • 运营好还是网站开发好企业网站推广公司
  • 模板网站哪家好学生制作个人网站
  • 网站 只做程序员游戏推广公司好做吗
  • 智慧团建网站没有验证码百度手机卫士
  • 建立一个网站平台需要多少钱阿里云域名注册官网
  • 建设充值网站多钱手机百度账号登录入口