Programmers interview prepare papers: 18 classic MySQL interview topics resolution, dry goods share

Programmers interview prepare papers: 18 classic MySQL interview topics resolution, dry goods share

1. What is the database three paradigms?

  1. The first paradigm (1NF): the field is atomic, can not be divided. (Relational database systems are all in first normal form database table fields are a single property, can not be divided)
  2. Second Normal Form (2NF) is established on the basis of first normal form (1NF) on that second normal form (2NF) must be met first normal form (1NF). Each instance requires a database table or row must be the only sub-region. Typically requires adding a column to the table, to uniquely identify each instance storage. This unique property is called the primary key column or primary key.
  3. Third normal form (3NF) must satisfy the second paradigm (2NF). Briefly, a third paradigm (3NF) requires a database table comprising a non-primary key information is not already included in other tables. > Third paradigm it has the following characteristics: 1 >> >> Each column 2 is only one value for each row can be distinguished. >> 3. Each table contains no non-primary key information already contained in other tables.

2. What experience have database optimization?

  1. Use PreparedStatement, generally higher than the performance Statement: a sql to the server to perform, involves the steps of: syntax checking, semantic analysis, compile, cache.
  2. There are foreign key constraints will affect the insert and delete performance, if the program is to ensure data integrity in the design of the database that will remove the foreign key.
  3. Then allow appropriate redundancy table, for example, the number of posts of a reply and last reply time than 4. UNION ALL UNION faster, therefore, it was confirmed that if the two combined result set does not contain duplicate data and does not require the time of ordering , then use UNIONALL. >> UNION and UNION ALL keywords are the result of combining the two into one, but both the use and efficiency from it are different. > 1 repeats the processing of the results: After UNION making table will filter out duplicate links records, Union All does not remove duplicate records. > 2 sort processing: Union will be sorted in order of the fields; UNION ALL simply merge the two results after return.

3. Please describe briefly what kind commonly used index?

  1. Ordinary Index: namely to create an index for a database table
  2. The only index: Similar to the general index, is different: the value of the MySQL database index columns must be unique, but allow free value
  3. Primary key index: It is a special unique index, does not allow nulls. Usually at the same time create a primary key index when construction of the table
  4. Composite index: In order to further the efficiency of extraction of MySQL, we should consider the establishment of a composite index. Multiple fields in a database table coming together as a composite index.

4. What is the working mechanism of the index and in the mysql database is?

  • Database indexing, database management system, a sort of data structure to assist in rapid query, update data in a database table. It is generally used to achieve an index B-tree and B + Tree variants

5.MySQL the basis of the operation command:

  1. If MySQL is running: Run the command service mysqlstatus on Debian, run the command service mysqld status on RedHat
  2. Start and stop MySQL service: Run the command service mysqld start to open service; Run service mysqld stop command to stop the service
  3. Shell Login MySQL: Run the command mysql -u root -p
  4. List all databases: Run the command show databases;
  5. Switch to a database and work on it: Run the command database use name; enter the name database database name of
  6. Lists all tables in a database: show tables;
  7. Get the name and type of all Field objects in the table: describe table_name;

6.mysql replication principle and process.

Mysql built-in replication is the basis for building large, high-performance applications. Mysql mechanism distribution system up to a plurality of data, this distribution is again achieved by copying the data of one host to another host Mysql (slaves), and re-executed. * During replication server acts as a primary server, while one or more other servers act as from the server.

A master index server writes binary log files will be updated, and maintain files to track log cycle. These log records can be sent from the server to update. When a connection from the master server, it notifies the position of the last successful update the master server reads in the log.

Since then, the server receives any updates from happening, and then blocked the main server and waits for notification of new updates. Follows

1. The primary server to update the records to the binary log file.

2. From the server logs the master copy to their binary relay log (replay log) in. 3. Time relay redo logs from the server, the update is applied to its own database.

7.mysql support copy type?

  1. Statement-based replication: SQL statements that are executed on the primary server, execute the same statement from the server. The default MySQL statement-based replication, more efficient. Once clone not found, a copy is automatically selected based on the row.
  2. Row-based replication: copy the content changes in the past, rather than execute commands from the server again from the beginning mysql5.0 support.
  3. Mixed types of replication: defaults based replication statement, if it is found based on statements can not be precisely replicated, will use row-based replication.

8.mysql distinguish myisam with the innodb?

  1. Transaction Support>  MyISAM: it emphasizes that performance, every time the query is atomic, which performs faster than InnoDB type, but does not provide transaction support. InnoDB: provides advanced database features Affairs supports transactions, foreign keys and so on. With a transaction (commit), transaction-safe rollback (rollback) and crash repair capacity (crash recovery capabilities) of (transaction-safe (ACID compliant) ) type table.
  2. InnoDB supports row-level locking, and support MyISAM table-level locking. >> myisam users in the operation table, select, update, delete, insert statements are automatically locked to the table, in the case of concurrent insert table meet later if locked, you can insert new data at the end of the table.
  3. InnoDB support MVCC, but MyISAM does not support
  4. Support InnoDB foreign keys, and MyISAM does not support
  5. Table primary key>  MyISAM: Allow any index table and not the presence of the primary key index is stored in the address line. The InnoDB: If no non-null primary key or unique index, will automatically generate a 6-byte main key (not visible to users), the data part of the main index, the index is stored an additional value of the main index.
  6. InnoDB does not support full-text indexing, and MyISAM support.
  7. Portability, backup and restore>  MyISAM: data is stored as a file, it will be very convenient for data transfer across the platform. It can be operated separately for a table at the time of backup and recovery. InnoDB: free program can copy data files, backup binlog, or use mysqldump, the amount of data on tens of G when relatively pain
  8. Storage structure>  MyISAM: Each MyISAM storage into three files on disk. The name of the first file name of the table to begin the extension indicates the file type. .frm file stores the table. Extended data file named .MYD (MYData). Extension index file is .MYI (MYIndex). InnoDB: all the tables are stored in the same data file (may also be a plurality of files, or a separate file table space), the size of the table is restricted only in the InnoDB operating system file size, typically 2GB.

Meaning the difference between varchar and char in the 9.mysql and (50) 50 representatives of varchar?

  1. Varchar difference and the char: char is a type of fixed length, varchar is a variable length type.
  2. Meaning VARCHAR (50) in 50: Up to 50 bytes of storage
  3. Meaning as in int (20) 20 is: M indicates int (M) in the maximumdisplay width (the maximum width of the display) for integer types The maximumlegal display width is 255..

10.MySQL in InnoDB transaction isolation level supported by four names, as well as the differences between progressive?

  1. Read Uncommitted (Uncommitted read content) >> In this isolation level, all transactions can see the results of other uncommitted transactions. This isolation level is rarely used in practice, because its performance is not much better than the other levels. Uncommitted read data, also called a dirty read (Dirty Read).
  2. Read Committed (read submission) >> This is the default isolation level for most database systems (MySQL, but not the default). It meets the simple definition of isolation: a transaction can only see the change has been submitted firms do. This isolation level also supports so-called non-repeatable read (Nonrepeatable Read), because other instances of the same transaction in the example of the process during which there may be a new commit, so select the same may return different results.
  3. Multiple instances (can be re-read) >> This is the default MySQL transaction isolation level, it ensures that the same transaction Repeatable Read concurrent reads data at the time, you will see the same data row. But in theory, it will lead to another thorny issue: Magic Reading (PhantomRead). Simply put, phantom read means that when a user reads a range of data row, another transaction and insert a new row within the range, when the user re-read the range of data rows, you will find a new " Phantom "line. InnoDB and Falcon storage engine through a multi-version concurrency control (MVCC, Multiversion Concurrency Control gap lock) mechanism to solve the problem. Note: In fact, multi-version only the problem of non-repeatable read problem, plus clearance lock (that is, here it is called concurrency control) before we can solve the problem of phantom read.
  4. Serializable (serializable) >> This is the highest isolation level, transaction ordering by forcing it to make it impossible to conflict with each other, so as to solve the problem phantom read. In short, it is to add a shared lock on each row of data read. At this level, it could lead to a lot of timeouts and lock contention.

Let the eye rest, and then continue it!

11. The table has a large field X (for example: text type), and the X field will not be updated frequently to read as main, this field is split into sub-table what the benefits?

If the field there are large fields (text, blob) type, and access to these fields is not much, this time together becomes a drawback. MYSQL database records stored in the storage rows, the smaller the data block size is fixed (16K), each record, the more the same block stored records. At this point it should be removed the large field, so when most small to cope with the query field, will be able to improve efficiency. When you need to query a large field, this time associated with the query is inevitable, but it is worth it. After the split open, UPDAE field necessary for the UPDATE plurality of tables

InnoDB row lock 12.MySQL the engine is done by adding on what (or realization) of?

InnoDB row lock is achieved by the index to the index items on the lock, which is different from MySQL and Oracle, which is locked by a corresponding data line in the data block to achieve. InnoDB row lock to achieve this characteristic means: only through index conditions to retrieve the data, only InnoDB row-level locking, otherwise, would use InnoDB table lock!

13.MySQL control memory allocation in the global parameters, what?

  1. Keybuffersize:> keybuffersize 指定索引缓冲区的大小,它决定索引处理的速度,尤其是索引读的速度。通过检查状态值Keyreadrequests 和 Keyreads,可以知道 keybuffersize 设置是否合理。比例 keyreads /keyreadrequests 应该尽可能的低,至少是1:100,1:1000 更好(上述状态值可以使用 SHOW STATUS LIKE‘keyread%'获得)。> keybuffersize 只对 MyISAM 表起作用。即使你不使用MyISAM 表,但是内部的临时磁盘表是 MyISAM 表,也要使用该值。可以使用检查状态值 createdtmpdisktables 得知详情。对于 1G 内存的机器,如果不使用 MyISAM表,推荐值是 16M(8-64M) > keybuffersize 设置注意事项 >>>1. 单个keybuffer 的大小不能超过 4G,如果设置超过 4G,就有可能遇到下面 3 个bug: >>>>> http://bugs.mysql.com/bug.php?id=29446 <br/> >>>>> http://bugs.mysql.com/bug.php?id=29419 <br/> >>>>> http://bugs.mysql.com/bug.php?id=5731 <br/> >>>2. 建议 keybuffer 设置为物理内存的 1/4(针对 MyISAM 引 擎),甚至是物理内存的 30%~40%,如果keybuffersize 设置太大,系统就会频繁的换页,降低系统性能。因为 MySQL 使用操作系统的缓存来缓存数据,所以我们得为系统留够足够的内存;在很多情况下数据要比索引大得多。>>>3. 如果机器性能优越,可以设置多个keybuffer,分别让不同的key*buffer 来缓存专门的索引
  2. innodbbufferpool_size > 表示缓冲池字节大小,InnoDB 缓存表和索引数据的内存区域。mysql 默认的值是 128M。最大值与你的CPU 体系结构有关,在 32 位操作系统,最大值是 4294967295(2^32-1) ,在 64 位操作系统,最大值为18446744073709551615 (2^64-1)。> 在 32 位操作系统中,CPU 和操作系统实用的最大大小低于设置的最大值。如果设定的缓冲池的大小大于 1G,设置innodbbufferpoolinstances 的值大于 1. > 数据读写在内存中非常快, innodbbufferpoolsize 减少了对磁盘的读写。当数据提交或满足检查点条件后才一次性将内存数据刷新到磁盘中。然而内存还有操作系统或数据库其他进程使用, 一般设置bufferpool 大小为总内存的 3/4 至 4/5。若设置不当, 内存使用可能浪费或者使用过多。对于繁忙的服务器, buffer pool 将划分为多个实例以提高系统并发性, 减少线程间读写缓存的争用。buffer pool 的大小首先受 innodbbuffer*pool_instances 影响,当然影响较小。
  3. querycachesize > 当 mysql 接收到一条 select 类型的 query时,mysql 会对这条query 进行 hash 计算而得到一个 hash 值,然后通过该 hash 值到 query cache 中去匹配,如果没有匹配中,则将这个hash 值存放在一个 hash 链表中,同时将 query 的结果集存放进cache 中,存放 hash 值的链表的每一个 hash 节点存放了相应query结果集在 cache 中的地址,以及该 query 所涉及到的一些 table 的相关信息;如果通过 hash 值匹配到了一样的 query,则直接将 cache 中相应的 query 结果集返回给客户端。如果 mysql 任何一个表中的任何一条数据发生了变化,便会通知query cache 需要与该 table 相关的query 的 cache 全部失效,并释放占用的内存地址。> query cache优缺点 >> 1. query 语句的 hash 计算和 hash 查找带来的资源消耗。mysql 会对每条接收到的 select 类型的 query 进行 hash 计算然后查找该query 的 cache 是否存在,虽然 hash 计算和查找的效率已经足够高了,一条query 所带来的消耗可以忽略,但一旦涉及到高并发,有成千上万条 query 时,hash计算和查找所带来的开销就的重视了;>> 2. query cache 的失效问题。如果表变更比较频繁,则会造成 query cache 的失效率非常高。表变更不仅仅指表中的数据发生变化,还包括结构或者索引的任何变化;>> 3. 对于不同 sql 但同一结果集的 query都会被缓存,这样便会造成内存资源的过渡消耗。sql 的字符大小写、空格或者注释的不同,缓存都是认为是不同的 sql(因为他们的 hash 值会不同);>> 4. 相关参数设置不合理会造成大量内存碎片,相关的参数设置会稍后介绍。
  4. readbuffersize >是 MySQL 读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL 会为它分配一段内存缓冲区。readbuffersize 变量控制这一缓冲区的大小。如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。

14.若一张表中只有一个字段 VARCHAR(N)类型,utf8 编码,则 N 最大值为多少(精确到数量级即可)?

Since each character utf8 occupy up to 3 bytes. And the length of the line defined MySQL not exceed 65535 the maximum value of N is calculated as: (65535-1-2) / 3. The reason minus 1 is the actual storage from the second byte, minus 2 reason is because you want to store the actual length of the list of characters in length, divided by 3 is because utf8 limit: Each character takes up to three bytes .

* 15. The [SELECT What are the advantages and disadvantages] and [SELECT all fields] of two kinds of writing? **

  1. The former to parse the data dictionary, which does not require
  2. Result output sequence, the former construction of the table columns in the same order, the order according to which the specified field.
  3. Field renamed does not require modification, which requires change
  4. The latter can be optimized indexing, the former can not be optimized
  5. The latter than the former high readability

16.HAVNG WHERE clause and the similarities and differences?

  1. Syntax: where column names with the table, having used the alias select results
  2. Results Effect range: where the rows in the table data is read, having the client returns the number of rows
  3. Index: where can use the index, having not use the index, the result can only be a temporary set of operation
  4. The back where you can not use aggregate functions, having a specialized use aggregate functions.

17.MySQL when there is no record insert, update when the record exists, the statement how to write?

INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEYUPDATE c=c+1;

18.MySQL the insert and update the select statement syntax

SQL insert into student (stuid,stuname,deptid) select 10,'xzm',3from student where stuid > 8;
update student a inner join student b on b.stuID=10 seta.
stuname=concat(b.stuname, b.stuID) where a.stuID=10 ;

Your concern is my forward momentum to move on, thank you for reading, appreciated.

Programmers interview prepare papers: 18 classic MySQL interview topics resolution, dry goods share
Welcome attention to micro-channel public number: architecture thousand words of notes Murong. Welcome attention to progress together.

Guess you like

Origin blog.51cto.com/14409100/2431515