MyISAM

【BATJ】面试必问MySQL索引实现原理

霸气de小男生 提交于 2019-11-26 12:07:42
BATJ面试题剖析 1、 为什么需要使用索引? 2、 数据结构 Hash 、平衡二叉树、 B 树、 B+ 树 区别? 3、 机械 硬盘、固态硬盘区别? 4、 M yisam与 I nnodb B+ 树的区别? 5、 MySQL 中的索引什么数据结构? 6、M ySQL 数据库优化方案? 1.为什么需要使用索引? MySQL官方对索引的定义为:索引(Index)是帮助 MySQL 高效 获取数据的数据结构。 也就是说:索引就像书的目录一样可以非常快速的定位到书的页码。 如果向 mysql 发出一条 sql 语句请求,查询的字段没有创建索引的话,可能会导致全表扫描,这样查询效率非常低 2.1 数据结构 Hash 算法 哈希表(Hash table,也叫散列表),是根据关 键码值(Key value) 而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做 散列函数 ,存放记录的数组叫做 散列表 。 优点 : 通过字段的值计算的hash值,定位数据非常快, 查找可以直接根据 key 访问。 缺点 : 因为底层数据结构是散列的,无法进行比较大小, 不能进行范围查找 index=Hash(key) 2.2数据结构 平衡二叉树算法 平衡二叉查找树,又称 AVL树。 它除了具备二叉查找树的基本特征之外,还具有一个非常重要的特点:它

mysql常见面试题

白昼怎懂夜的黑 提交于 2019-11-26 11:51:29
[SELECT *] 和[SELECT 全部字段]的 2 种写法有何优缺点? 1. 前者要解析数据字典,后者不需要 2. 结果输出顺序,前者与建表列顺序相同,后者按指定字段顺序。 3. 表字段改名,前者不需要修改,后者需要改 4. 后者可以建立索引进行优化,前者无法优化 5. 后者的可读性比前者要高所以, 尽量使用后者来查询 若一张表中只有一个字段 VARCHAR(N)类型,utf8 编码,则 N 最大值为多少? 由于 utf8 的每个字符最多占用 3 个字节。而 MySQL 定义行的长度不能超过65535(text和blob不计算在内), 因此 N 的最大值计算方法为:(65535-1-2)/3。 减去 1 的原因是实际存储从第二个字节开始,减去 2 的原因是因为要在列表长度存储实际的字符长度(长度大于256用两个字节存储),除以 3 是因为 utf8 限制:每个字符最多占用 3 个字节。 MySQL 中 InnoDB 引擎的行锁是通过加在什么上完成(或称实现)的? InnoDB 行锁是通过给索引上的索引项加锁来实现的,这一点 MySQL 与Oracle 不同,后者是通过在数据块中对相应数据行加锁来实现的。 InnoDB 这种行锁实现特点意味着:只有通过索引条件检索数据,InnoDB 才使用行级锁,否则,InnoDB 将使用表锁 mysql 中 myisam 与 innodb

MyIsam engine transaction support

好久不见. 提交于 2019-11-26 11:26:06
问题 I was testing transaction support in innoDB tables, and just for the curriosity I tried to run the same transaction on MyIsam table, and surprisingly it worked. I am assuming that on myIsam table queries are executed one after another not in one atomic operation and I don\'t get any errors from START TRANSACTION and COMMIT and ROLLBACK operations. I am interested, is MyIsam engine just ignoring this operations or does it perform some actions? 回答1: MyISAM effectively works in auto-commit mode

How to lock a single row

岁酱吖の 提交于 2019-11-26 11:09:28
问题 I have a user table with field lastusedecnumber . I need to access and increment lastusedecnumber . During that accessing time I need to lock that particular user row (not the entire table). How do I do this? The table type is MyISAM . 回答1: MySQL uses only table-level locking from MyISAM tables. If you can, switch to InnoDB for row-level locking. Here's a link to the MySQL site describing Locks set by SQL Statements for InnoDB tables. http://dev.mysql.com/doc/refman/5.0/en/innodb-locks-set

02.日志系统:一条SQL更新语句是如何执行的?

青春壹個敷衍的年華 提交于 2019-11-26 09:19:30
我们还是从一个表的一条更新语句说起,我们创建下面一张表: create table T(ID int primary key, c int); 如果要将ID=2这一行c的值加1,SQL可以这么写: update T set c=c+1 where ID=2; 前一篇文章介绍过SQL语句基本的执行链路,可以确认的说,查询语句的那一套流程,更新语句也是同样会走一遍。在执行语句前要先连接数据库,这是连接器的工作。接下来,分析器会通过词法和语法解析知道这是一条更新语句,优化器决定要使用ID这个索引。然后执行器负责具体执行,找到这一行,然后更新。 与查询流程不一样的是,更新流程还涉及两个重要的日志模块:redo log(重做日志)和binlog(归档日志)。 redo log MySQL里面经常说到的WAL技术的全称是Write-Ahead Logging,它的关键点就是行写日志再写磁盘。具体来说,当有一条记录需要更新的时候,InnoDB引擎会先把记录写到redo log里面,并更新内存,这个时候更新就算完成了。同时InnoDB引擎会在适当的时候,将这个操作记录更新到磁盘里面,而这个更新往往是在系统比较闲的时候做。 InnoDB的redo log是固定在小的,比如可以配置为一组4个文件,每个文件的大小是1GB,那么总共就可以记录4GB的操作,如图所示: 从头开始写,写到末尾又回到开头循环写

How can I check MySQL engine type for a specific table?

匆匆过客 提交于 2019-11-26 08:57:36
问题 My MySQL database contains several tables using different storage engines (specifically myisam and innodb). How can I find out which tables are using which engine? 回答1: SHOW TABLE STATUS WHERE Name = 'xxx' This will give you (among other things) an Engine column, which is what you want. 回答2: To show a list of all the tables in a database and their engines, use this SQL query: SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbname'; Replace dbname with your

Is there a REAL performance difference between INT and VARCHAR primary keys?

℡╲_俬逩灬. 提交于 2019-11-26 03:17:21
问题 Is there a measurable performance difference between using INT vs. VARCHAR as a primary key in MySQL? I\'d like to use VARCHAR as the primary key for reference lists (think US States, Country Codes) and a coworker won\'t budge on the INT AUTO_INCREMENT as a primary key for all tables. My argument, as detailed here, is that the performance difference between INT and VARCHAR is negligible, since every INT foreign key reference will require a JOIN to make sense of the reference, a VARCHAR key

What's the difference between MyISAM and InnoDB? [duplicate]

荒凉一梦 提交于 2019-11-26 03:00:01
问题 This question already has answers here : MyISAM versus InnoDB [closed] (25 answers) Closed 6 years ago . I understand that this question has been asked before, but most of the time it is asked in relation to a specific database or table. I cannot find an answer on this site that describes the two engines and their differences without respect to someones specific database. I want to be able to make more informed decisions in the future with respect to designing a table or database, so am

How to properly create composite primary keys - MYSQL

≡放荡痞女 提交于 2019-11-26 01:37:14
问题 Here is a gross oversimplification of an intense setup I am working with. table_1 and table_2 both have auto-increment surrogate primary keys as the ID. info is a table that contains information about both table_1 and table_2 . table_1 (id, field) table_2 (id, field, field) info ( ???, field) I am trying to decided if I should make the primary key of info a composite of the IDs from table_1 and table_2 . If I were to do this, which of these makes most sense? ( in this example I am combining

it专业测试-解析

人走茶凉 提交于 2019-11-26 01:34:41
目录 1.继承thread类必须实现哪个方法 2.关于ArrayList 和LinkedList的说法错误的是 3.MySQL中语句的%和 _ 表达正确的是 4.下面那个关于接口的表述是错误的 5.下面哪项不是数据库的四大特征之一 6.以下对封装的描述正确的是 7.以下代码输出运行输出的是 8.下面那个HTTP的状态码表示数据永久重定向 9.下面哪项不是合法的bean的scope属性值 10.下面那个是线程安全的 11.关于变量的命名规范,说法正确的是 12.下面哪个命令用于测试网络连通性的 13.可以是用哪个工具查看java线程状态 14.java如何创建多线程 15.下面哪个关键词与AOP无关 16.在Java语言中,下列关于类的继承的描述哪个是正确的 17.Java类中静态integer变量的默认值是什么 18.下面哪种情况,不会影响普通索引生效 19.欲构造ArrayList类得出一个实例,继承List接口,下列哪个方法是正确的 20.OutOfMemory错误的常见原因不是下面哪个 21.类的构造函数是否可以被override?是否可以被overload? 22.下面哪个关键词可以防止类被override? 23.在Java中,下面对于构造函数的描述正确的是 24.Java接口修饰符可以为 25.下面哪项不是spring注入bean的方式 26