mysql common interview questions

appendix:

https://mp.weixin.qq.com/s/pC0_Y7M7BkoUmlRwneZZdA

 

First, why use an auto-increment primary key

1, if we define the primary key (PRIMARY KEY), then InnoDB will select as the primary key clustered index.

If the primary key is not explicitly defined, it selects the first InnoDB a unique index that does not contain a NULL value as there are primary key index.

If there is no such unique index, the built InnoDB selects ROWID 6 bytes long as aggregated implicit index (writing rows ROWID as the primary key of the record is incremented, the ORACLE ROWID like may be cited as the ROWID is implied).

2 the leaf node, recording the data itself is stored in the main index (a B + Tree), which requires a leaf node within the same pieces of data (the size of a memory page or pages disk) storing records by primary key sequence

Thus whenever a new record is inserted into, MySQL based on its primary key inserted into an appropriate position of the node and, if the load factor reaches a page (the InnoDB default 15/16), then open a new page (node)

3, if the table using the increment primary key, each time a new record is inserted, it will record a subsequent sequence added to the current position of the index node, when a filled, it will automatically open up a new page

4, if the non-increment primary key (if the ID number or student number, etc.), because each insertion primary key value is approximated by a random, each time a new record is inserted into an existing index page must have a location intermediate

At this point MySQL had to order a new record into the proper position and movement data, even the target page may have been written back to disk and cleared from the cache, then you have to read back from the disk, which adds a lot of overhead

At the same time frequent moves, paging operation caused a lot of debris, was not compact index structure, the follow-up had to rebuild the table by OPTIMIZE TABLE and optimize fill the page.

Second, why use the data to improve efficiency index

  1. The index data storage is ordered

  2. In the case ordered by the index data is a query without having to traverse the index records

  3. In extreme cases, the data query efficiency index for dichotomy search efficiency approaches log2 (N)

Three differences, B + tree index and a hash index

B + Tree is a balanced tree more, the difference in height from the root to each leaf node is no more than 1, and has a pointer to each other links between nodes in the same hierarchy, are ordered, as shown below:

The hash index is to use a certain hash algorithm, the key terms of the new hash values, similar to a B + tree does not need to look like step by step from the root to the leaf node is retrieved with a single hash algorithm can be, is disordered, as shown below:

Fourth, hash indexes advantages:

Equivalent queries, hash index has an absolute advantage (the premise is: not a lot of duplicate key values, if a large number when duplicate key values, hash index is inefficient because there is so-called hash collision)

Fifth, the hash index is not applicable scenarios:

  1. It does not support range queries

  2. It does not support indexing complete sequencing

  3. It does not support the joint index of the most left-prefix matching rule

Usually, B + tree index structure suitable for most scenes, such as the following scene with hash indexes are more advantages:

In HEAP table, if the data stored repeat is very low (that is to say a large base), equivalent to the column data query-based, there is no scope of inquiry, there is no sort of time, particularly suitable for hash index, for example, this SQL:

# Only equivalent query

select id, name from table where name='李明'; 

The commonly used InnoDB the default engine using B + tree index, an index on the table it will use real-time monitoring.

If you believe that the establishment hash index can improve query efficiency, in memory of "adaptive hash index buffer" is automatically created hash indexes (default open in InnoDB adaptive hash index).

By observing the search mode, MySQL will use prefix index key hash index is established, if a table almost most of the buffer pool, then build a hash index to speed up the equivalent queries.

Note : In some workloads, look for performance improvements brought about much greater than the additional search index monitoring the situation and keeping the cost of a hash table structure brought about by the hash index.

But sometimes, under high load conditions, the adaptive hash index added read / write lock will bring competition, such as high concurrency join operation. like operations and% wildcard operation is not applicable to the adaptive hash index, you may want to turn off the adaptive hash index.

Sixth, the difference between B-tree and B + Tree

1, B tree, each node storing key and data, that all the nodes in the tree, and the leaf node pointer to NUL, the leaf node does not contain any keyword information.

2、B+树,所有的叶子结点中包含了全部关键字的信息,及指向含有这些关键字记录的指针,且叶子结点本身依关键字的大小自小而大的顺序链接

所有的非终端结点可以看成是索引部分,结点中仅含有其子树根结点中最大(或最小)关键字。 (而B 树的非终节点也包含需要查找的有效信息)

七、为什么说B+比B树更适合实际应用中操作系统的文件索引和数据库索引?

1、B+的磁盘读写代价更低。

B+的内部结点并没有指向关键字具体信息的指针,因此其内部结点相对B树更小。

如果把所有同一内部结点的关键字存放在同一盘块中,那么盘块所能容纳的关键字数量也越多。一次性读入内存中的需要查找的关键字也就越多。相对来说IO读写次数也就降低了。

2、B+-tree的查询效率更加稳定。

由于非终结点并不是最终指向文件内容的结点,而只是叶子结点中关键字的索引。所以任何关键字的查找必须走一条从根结点到叶子结点的路。所有关键字查询的路径长度相同,导致每一个数据的查询效率相当。

八、MySQL联合索引

1、联合索引是两个或更多个列上的索引。

对于联合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。

例如索引是key index (a,b,c). 可以支持a 、 a,b 、 a,b,c 3种组合进行查找,但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效。

2、利用索引中的附加列,您可以缩小搜索的范围,但使用一个具有两列的索引不同于使用两个单独的索引。

复合索引的结构与电话簿类似,人名由姓和名构成,电话簿首先按姓氏对进行排序,然后按名字对有相同姓氏的人进行排序。

如果您知道姓,电话簿将非常有用;如果您知道姓和名,电话簿则更为有用,但如果您只知道名不知道姓,电话簿将没有用处。

九、什么情况下应不建或少建索引

1、表记录太少

2、经常插入、删除、修改的表

3、数据重复且分布平均的表字段,假如一个表有10万行记录,有一个字段A只有T和F两种值,且每个值的分布概率大约为50%,那么对这种表A字段建索引一般不会提高数据库的查询速度。

4、经常和主字段一块查询但主字段索引值比较多的表字段

十、什么是表分区?

表分区,是指根据一定规则,将数据库中的一张表分解成多个更小的,容易管理的部分。从逻辑上看,只有一张表,但是底层却是由多个物理分区组成。

十一、表分区与分表的区别

分表:指的是通过一定规则,将一张表分解成多张不同的表。比如将用户订单记录根据时间成多个表。

分表与分区的区别在于:分区从逻辑上来讲只有一张表,而分表则是将一张表分解成多张表。

十二、表分区有什么好处?

1、存储更多数据。分区表的数据可以分布在不同的物理设备上,从而高效地利用多个硬件设备。和单个磁盘或者文件系统相比,可以存储更多数据

2、优化查询。在where语句中包含分区条件时,可以只扫描一个或多个分区表来提高查询效率;涉及sum和count语句时,也可以在多个分区上并行处理,最后汇总结果。

3、分区表更容易维护。例如:想批量删除大量数据可以清除整个分区。

4、避免某些特殊的瓶颈,例如InnoDB的单个索引的互斥访问,ext3问价你系统的inode锁竞争等。

十三、分区表的限制因素

  1. 一个表最多只能有1024个分区

  2. MySQL5.1中,分区表达式必须是整数,或者返回整数的表达式。在MySQL5.5中提供了非整数表达式分区的支持。

  3. 如果分区字段中有主键或者唯一索引的列,那么多有主键列和唯一索引列都必须包含进来。即:分区字段要么不包含主键或者索引列,要么包含全部主键和索引列。

  4. 分区表中无法使用外键约束

  5. MySQL的分区适用于一个表的所有数据和索引,不能只对表数据分区而不对索引分区,也不能只对索引分区而不对表分区,也不能只对表的一部分数据分区。

十四、如何判断当前MySQL是否支持分区?

命令:show variables like '%partition%' 运行结果:

 

have_partintioning 的值为YES,表示支持分区。

十五、MySQL支持的分区类型有哪些?

  1. RANGE分区: 这种模式允许将数据划分不同范围。例如可以将一个表通过年份划分成若干个分区

  2. LIST分区: 这种模式允许系统通过预定义的列表的值来对数据进行分割。按照List中的值分区,与RANGE的区别是,range分区的区间范围值是连续的。

  3. HASH分区 :这中模式允许通过对表的一个或多个列的Hash Key进行计算,最后通过这个Hash码不同数值对应的数据区域进行分区。例如可以建立一个对表主键进行分区的表。

  4. KEY分区 :上面Hash模式的一种延伸,这里的Hash Key是MySQL系统产生的。

十六、四种隔离级别

  1. Serializable (串行化):可避免脏读、不可重复读、幻读的发生。

  2. Repeatable read (可重复读):可避免脏读、不可重复读的发生。

  3. Read committed (读已提交):可避免脏读的发生。

  4. Read uncommitted (读未提交):最低级别,任何情况都无法保证。

十七、关于MVVC

MySQL InnoDB存储引擎,实现的是基于多版本的并发控制协议——MVCC (Multi-Version Concurrency Control) 

:与MVCC相对的,是基于锁的并发控制,Lock-Based Concurrency Control

MVCC最大的好处:读不加锁,读写不冲突。在读多写少的OLTP应用中,读写不冲突是非常重要的,极大的增加了系统的并发性能,现阶段几乎所有的RDBMS,都支持了MVCC。

  1. LBCC:Lock-Based Concurrency Control,基于锁的并发控制

  2. MVCC:Multi-Version Concurrency Control

    基于多版本的并发控制协议。纯粹基于锁的并发机制并发量低,MVCC是在基于锁的并发控制上的改进,主要是在读操作上提高了并发量。

十八、在MVCC并发控制中,读操作可以分成两类:

  1. 快照读 (snapshot read):读取的是记录的可见版本 (有可能是历史版本),不用加锁(共享读锁s锁也不加,所以不会阻塞其他事务的写)

  2. 当前读 (current read):读取的是记录的最新版本,并且,当前读返回的记录,都会加上锁,保证其他事务不会再并发修改这条记录

十九、行级锁定的优点:

1、当在许多线程中访问不同的行时只存在少量锁定冲突。

2、回滚时只有少量的更改

3、可以长时间锁定单一的行。

二十、行级锁定的缺点:

  1. 比页级或表级锁定占用更多的内存。

  2. 当在表的大部分中使用时,比页级或表级锁定速度慢,因为你必须获取更多的锁。

  3. 如果你在大部分数据上经常进行GROUP BY操作或者必须经常扫描整个表,比其它锁定明显慢很多。

  4. 用高级别锁定,通过支持不同的类型锁定,你也可以很容易地调节应用程序,因为其锁成本小于行级锁定。

二十一、MySQL优化

  1. 开启查询缓存,优化查询

  2. explain你的select查询,这可以帮你分析你的查询语句或是表结构的性能瓶颈。EXPLAIN 的查询结果还会告诉你你的索引主键被如何利用的,你的数据表是如何被搜索和排序的

  3. 当只要一行数据时使用limit 1,MySQL数据库引擎会在找到一条数据后停止搜索,而不是继续往后查少下一条符合记录的数据

  4. 为搜索字段建索引

  5. 使用 ENUM 而不是 VARCHAR。如果你有一个字段,比如“性别”,“国家”,“民族”,“状态”或“部门”,你知道这些字段的取值是有限而且固定的,那么,你应该使用 ENUM 而不是VARCHAR

  6. Prepared StatementsPrepared Statements很像存储过程,是一种运行在后台的SQL语句集合,我们可以从使用 prepared statements 获得很多好处,无论是性能问题还是安全问题。

    Prepared Statements 可以检查一些你绑定好的变量,这样可以保护你的程序不会受到“SQL注入式”攻击

  7. 垂直分表

  8. 选择正确的存储引擎

二十二、key和index的区别

  1. key 是数据库的物理结构,它包含两层意义和作用,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查询用的)。包括primary key, unique key, foreign key 等

  2. index是数据库的物理结构,它只是辅助查询的,它创建时会在另外的表空间(mysql中的innodb表空间)以一个类似目录的结构存储。索引要分类的话,分为前缀索引、全文本索引等;

二十三、Mysql 中 MyISAM 和 InnoDB 的区别有哪些?

区别:

  1. InnoDB支持事务,MyISAM不支持

    对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成一个事务;

  2. InnoDB支持外键,而MyISAM不支持。对一个包含外键的InnoDB表转为MYISAM会失败;

  3. InnoDB是聚集索引,数据文件是和索引绑在一起的,必须要有主键,通过主键索引效率很高。

    但是辅助索引需要两次查询,先查询到主键,然后再通过主键查询到数据。因此主键不应该过大,因为主键太大,其他索引也都会很大。

    而MyISAM是非聚集索引,数据文件是分离的,索引保存的是数据文件的指针。主键索引和辅助索引是独立的。

  4. InnoDB不保存表的具体行数,执行select count(*) from table时需要全表扫描。而MyISAM用一个变量保存了整个表的行数,执行上述语句时只需要读出该变量即可,速度很快;

  5. Innodb不支持全文索引,而MyISAM支持全文索引,查询效率上MyISAM要高;

如何选择:

  1. 是否要支持事务,如果要请选择innodb,如果不需要可以考虑MyISAM;

  2. 如果表中绝大多数都只是读查询,可以考虑MyISAM,如果既有读写也挺频繁,请使用InnoDB

  3. 系统奔溃后,MyISAM恢复起来更困难,能否接受;

  4. MySQL5.5版本开始Innodb已经成为Mysql的默认引擎(之前是MyISAM),说明其优势是有目共睹的,如果你不知道用什么,那就用InnoDB,至少不会差。

二十四、数据库表创建注意事项

1、字段名及字段配制合理性

  • 剔除关系不密切的字段;

  • 字段命名要有规则及相对应的含义(不要一部分英文,一部分拼音,还有类似a.b.c这样不明含义的字段);

  • 字段命名尽量不要使用缩写(大多数缩写都不能明确字段含义);

  • 字段不要大小写混用(想要具有可读性,多个英文单词可使用下划线形式连接);

  • 字段名不要使用保留字或者关键字;

  • 保持字段名和类型的一致性;

  • 慎重选择数字类型;

  • 给文本字段留足余量;

2、系统特殊字段处理及建成后建议

  • 添加删除标记(例如操作人、删除时间);

  • 建立版本机制;

3、表结构合理性配置

  • 多型字段的处理,就是表中是否存在字段能够分解成更小独立的几部分(例如:人可以分为男人和女人);

  • 多值字段的处理,可以将表分为三张表,这样使得检索和排序更加有调理,且保证数据的完整性!

4、其它建议

  • 对于大数据字段,独立表进行存储,以便影响性能(例如:简介字段);

  • 使用varchar类型代替char,因为varchar会动态分配长度,char指定长度是固定的;

  • 给表创建主键,对于没有主键的表,在查询和索引定义上有一定的影响;

  • 避免表字段运行为null,建议设置默认值(例如:int类型设置默认值为0)在索引查询上,效率立显;

  • 建立索引,最好建立在唯一和非空的字段上,建立太多的索引对后期插入、更新都存在一定的影响(考虑实际情况来创建);

 

Guess you like

Origin www.cnblogs.com/williamjie/p/11081592.html