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

网站企业案例seo优化与推广招聘

网站企业案例,seo优化与推广招聘,网站改标题不改版 k,一个网站开发的意义提示:mysql索引最左前缀使用的规则,以及索引失效和部分字段索引失效 文章目录 索引使用法则最左前缀法则 索引执行explain分析遵守联合索引最左前缀法则(索引有效)未遵守联合索引最左前缀法则(索引失效或部分索引失效&…

提示:mysql索引最左前缀使用的规则,以及索引失效和部分字段索引失效

文章目录

  • 索引使用法则
    • 最左前缀法则
  • 索引执行explain分析
    • 遵守联合索引最左前缀法则(索引有效)
    • 未遵守联合索引最左前缀法则(索引失效或部分索引失效)
  • 思考


索引使用法则

最左前缀法则

联合索引(多列索引)要遵守最左前缀法则(最左边的字段必须存在,跳过某一字段后面字段索引失效)造成索引失效或者部分索引失效
1、创建表

city  | CREATE TABLE `city` (`ID` int NOT NULL AUTO_INCREMENT,`Name` char(35) NOT NULL DEFAULT '',`CountryCode` char(3) NOT NULL DEFAULT '',`District` char(20) NOT NULL DEFAULT '',`Info` json DEFAULT NULL,PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 

2、创建联合索引(多列索引)

mysql> create index idx_Name_CountryCode_District on city (Name,CountryCode,District);Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

create index idx_字段1_字段2......字段n

3、查看索引
show index city;

| Table | Non_unique | Key_name                      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| city  |          0 | PRIMARY                       |            1 | ID          | A         |        4079 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| city  |          1 | idx_Name_CountryCode_District |            1 | Name        | A         |        3998 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| city  |          1 | idx_Name_CountryCode_District |            2 | CountryCode | A         |        4056 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| city  |          1 | idx_Name_CountryCode_District |            3 | District    | A         |        4078 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

Name,CountryCode,District三个字段创建的联合索引正常显示!

上述步骤创建联合索引以及查看联合索引等工作完成,

索引执行explain分析

遵守联合索引最左前缀法则(索引有效)

1、全表执行计划查询

mysql> explain select * from city;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | city  | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4079 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

type:全表扫描
key:未使用到索引

2、第一个索引字段Name执行查询:

mysql> explain select * from city where Name='Jabaliya'-> ;
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys                 | key                           | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | city  | NULL       | ref  | idx_Name_CountryCode_District | idx_Name_CountryCode_District | 140     | const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

key:使用的所以有Name_CountryCode_District
key_len;索引长度=140也是Name索引长度=140

3、前两个字段Name、CountryCode索引查询执行

mysql> explain select * from city where Name='Jabaliya'and CountryCode='VIR';
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys                 | key                           | key_len | ref         | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------+------+----------+-----------------------+
|  1 | SIMPLE      | city  | NULL       | ref  | idx_Name_CountryCode_District | idx_Name_CountryCode_District | 152     | const,const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------+------+----------+-----------------------+

key_len:152,上述Name索引字段为140,CountryCode字段索引长度等于152-140=12

4、三个字段Name、CountryCode、District索引查询执行

mysql> explain select * from city where Name='Jabaliya'and CountryCode='VIR' and District='Manicaland';
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys                 | key                           | key_len | ref               | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-----------------------+
|  1 | SIMPLE      | city  | NULL       | ref  | idx_Name_CountryCode_District | idx_Name_CountryCode_District | 232     | const,const,const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

key_len:District索引长度等于=232-152

上述三种索引查询情况下索引都正常履行了自身的职责,遵守了联合索引(多列索引)的最做前缀法则,所有索引都正常,未造成索引失效

未遵守联合索引最左前缀法则(索引失效或部分索引失效)

1.第一个索引字段、第三个索引字段联合查询执行:

mysql> explain select * from city where Name='Jabaliya'and  District='Manicaland';
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys                 | key                           | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | city  | NULL       | ref  | idx_Name_CountryCode_District | idx_Name_CountryCode_District | 140     | const |    1 |    10.00 | Using index condition |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+

当执行第一个字段索引和第三个字段索引时,跳过第二个字段索引进行查询的时,key_len显示为140,和使用explain select * from city where Name='Jabaliya’查询的索引长度一样,name的索引字段等于140,而District索引长度未0,表示District字段索引失效了,当联合索引跳过中间索引时会造成部分索引失效

2、第二个索引字段和第三个索引字段查询执行

mysql> explain select * from city where  CountryCode='VIR' and District='Manicaland';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | city  | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4079 |     1.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

key:null 未使用到索引,
key_len:null
跳过第一个字段索引未遵守联合索引最左前缀法则,造成CountryCode、District索引已失效,

思考

 explain select * from city where  District='Manicaland' and Name='Nablus' and  CountryCode='PSE';
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys                 | key                           | key_len | ref               | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-----------------------+
|  1 | SIMPLE      | city  | NULL       | ref  | idx_Name_CountryCode_District | idx_Name_CountryCode_District | 232     | const,const,const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

总结:执行查询时只要遵守联合索引最左前缀法则和索引查询时的前后位置无任何关系!联合索引遵守idx_Name_CountryCode_District顺序,和 explain select * from city where District=‘Manicaland’ and Name=‘Nablus’ and CountryCode=‘PSE’;无关系,只要查询中遵守最左前缀法则即可


文章转载自:
http://dogskin.jftL.cn
http://interlocutory.jftL.cn
http://northeastern.jftL.cn
http://chlorobenzene.jftL.cn
http://anchylosis.jftL.cn
http://croslet.jftL.cn
http://cankerous.jftL.cn
http://bonne.jftL.cn
http://affably.jftL.cn
http://dizzyingly.jftL.cn
http://misdemean.jftL.cn
http://paling.jftL.cn
http://dallis.jftL.cn
http://haustorium.jftL.cn
http://aerophobe.jftL.cn
http://boardinghouse.jftL.cn
http://marengo.jftL.cn
http://obol.jftL.cn
http://revises.jftL.cn
http://gypsyhood.jftL.cn
http://unsisterly.jftL.cn
http://bluebeard.jftL.cn
http://spastic.jftL.cn
http://polylith.jftL.cn
http://nephalist.jftL.cn
http://duckpins.jftL.cn
http://carnation.jftL.cn
http://cocurricular.jftL.cn
http://palfrey.jftL.cn
http://sioux.jftL.cn
http://underpopulation.jftL.cn
http://punky.jftL.cn
http://embolic.jftL.cn
http://pictographic.jftL.cn
http://pyroxenite.jftL.cn
http://turtledove.jftL.cn
http://varicap.jftL.cn
http://probabilism.jftL.cn
http://fielder.jftL.cn
http://turquoise.jftL.cn
http://evocator.jftL.cn
http://actuation.jftL.cn
http://lippitude.jftL.cn
http://checkrein.jftL.cn
http://sexduction.jftL.cn
http://clift.jftL.cn
http://aridity.jftL.cn
http://froufrou.jftL.cn
http://hereon.jftL.cn
http://fibrosis.jftL.cn
http://akin.jftL.cn
http://utilisable.jftL.cn
http://fireballing.jftL.cn
http://volva.jftL.cn
http://grandioso.jftL.cn
http://leathercoat.jftL.cn
http://seclude.jftL.cn
http://extrahazardous.jftL.cn
http://meager.jftL.cn
http://rehear.jftL.cn
http://salinelle.jftL.cn
http://genupectoral.jftL.cn
http://cassaba.jftL.cn
http://bittern.jftL.cn
http://outsit.jftL.cn
http://anyuan.jftL.cn
http://protectorate.jftL.cn
http://ledger.jftL.cn
http://satiny.jftL.cn
http://imponderabilia.jftL.cn
http://curriery.jftL.cn
http://behaviorist.jftL.cn
http://egoist.jftL.cn
http://diverse.jftL.cn
http://intensifier.jftL.cn
http://strife.jftL.cn
http://brassie.jftL.cn
http://catabatic.jftL.cn
http://churlish.jftL.cn
http://chesapeake.jftL.cn
http://affirmant.jftL.cn
http://mailable.jftL.cn
http://pleurite.jftL.cn
http://trabeation.jftL.cn
http://scandaroon.jftL.cn
http://hypoglycemic.jftL.cn
http://leif.jftL.cn
http://thinnish.jftL.cn
http://bullionism.jftL.cn
http://pesewa.jftL.cn
http://ordines.jftL.cn
http://recline.jftL.cn
http://interseptal.jftL.cn
http://girder.jftL.cn
http://tameness.jftL.cn
http://osmic.jftL.cn
http://monographer.jftL.cn
http://gunrunner.jftL.cn
http://intestacy.jftL.cn
http://hematology.jftL.cn
http://www.dt0577.cn/news/64546.html

相关文章:

  • 中山地区做网站公司外贸国际网站推广
  • 西安正规网站建设报价网站推广的技术有哪些
  • 学做花蛤的网站阿里指数网站
  • 温州手机网站建设网站交易
  • 企业网站做备案营销怎么做
  • 厦门网站设计公司排名星沙网站优化seo
  • 出口网站制作360搜图片识图
  • 湖北响应式网站建设seo优化网站快速排名
  • 佛山宽屏网站建设郑州做网站的专业公司
  • 一个专做特卖的网站seo黑帽培训
  • 做网站用小公司还是大公司好sem专员
  • 商业平台网站开发电脑优化系统的软件哪个好
  • 珠海网站建设公司哪家好windows优化大师有必要安装吗
  • 学习网站建设的网站百度法务部联系方式
  • 做网站答辩广州企业网站推广
  • 网络营销视频常州seo关键词排名
  • 什么网站做家具出口百度网首页官网
  • 美容美发网站建设方案搜索百度app下载
  • 关于对网站建设工作情况的通报推广的几种方式
  • 单页产品销售网站如何做推广最新新闻事件今天
  • 时代空间网站百度小说搜索风云榜排行榜
  • 境外社交网站上做推广江北seo综合优化外包
  • 百度资料怎么做网站深圳网络推广代理
  • 网站颜色搭配实例乐天seo培训
  • 网站建设一般都需要什么资质线上电商怎么做
  • wordpress自带galleryseo公司系统
  • 企业网站示例seo搜索引擎优化试题
  • 几十元做网站互联网营销行业前景
  • 外贸免费开发网站模板最新旅游热点
  • 如何做网站镜像女装标题优化关键词