Java Road to God: Java Interview Preparation (15)

Six, SpringBoot framework

1. What is SpringBoot

SpringBoot is simplified based on the original Spring framework, reducing our workload

2. Advantages of SpringBoot

1. Independent operation

SpringBoot embeds various servlet containers, such as Tomcat, we don’t need to mark the project as a war package and deploy it to the container, only need to be an executable jar package to run independently

2. Simplify configuration

spring-boot-starter-web starter automatically depends on other components, reducing maven configuration

3. No code generation and XML configuration

No code is generated during the SpringBoot configuration process, and all configuration tasks can be completed without the need for an XML configuration file

3. What is the core annotation of SpringBoot

The @SpringBootApplication annotation on the startup class is the core annotation of SpringBoot. Mainly contains the following 3 notes

  • @SpringBootConfiguration: combined with @COnfiguration annotations to realize the function of configuration files
  • @EnableAutoConfiguration: Turn on the automatic configuration function, or turn off a certain automatic configuration option
  • @ComponentScan: Scanning of Spring components

4. Several ways to run SpringBoot

1. Execute the main method directly

2. Run as a jar package

3. Maven plugin operation

5. How to understand Starters in SpringBoot

Starters can be understood as a starter, which contains a series of dependency packages that can be integrated into the application, and can integrate Spring and other technologies in one stop.

Seven, MySql database

1. Basic knowledge of database

1.1 What is a stored procedure? What are the advantages?

A stored procedure is a collection of SQL statements that have been compiled and stored in the database in advance

advantage:

1. Reuse: It can be reused, thereby reducing the workload of database developers

2. Reduce network traffic: the stored procedure is located on the server, and only the name and parameters of the stored procedure need to be passed when calling, which reduces the amount of data transmitted over the network

3. Security: parameterized stored procedures can prevent SQL injection attacks

Disadvantages:

1. Stored procedures are often customized to specific databases. Because of the different programming languages ​​supported, the original stored procedures need to be rewritten when switching database systems from other vendors.

1.2 Three paradigms

First normal form:

Attributes cannot be divided. Each field is at the atomic level. For example, in a Student table, it cannot be said that id, name, and class number are put in one field.

Second normal form:

On the basis of satisfying the first paradigm, part of the dependence is eliminated. Non-primary attributes are completely dependent on the primary attributes. For example, in a Student table, id and name are used as primary keys, then the id and name attributes belong to the primary attributes, and the other fields belong to non-primary attributes. Non-primary attributes must depend on both id and name, not only on one of them

Third Normal Form:

On the basis of satisfying the second paradigm, eliminate transitive dependence. Non-primary attributes directly depend on the primary attributes, rather than indirectly dependent on the primary attributes.

1.3 The execution process of a SQL statement

After MySQL receives a query request, it will first look at the query cache. The previously executed statements and their results may be directly cached in memory in the form of key-value pairs. The key is the query statement, and the value is the query result. If your query can directly find the key in this cache, then the value will be directly returned to the client.

But the hit rate of the general query cache is not particularly high, because the update operation of a table will delete the query cache of this table, and Mysql8.0 deletes the query cache part.

1.4 What kind of view, the usage scenario of the view, and the advantages and disadvantages

A view is a table derived from one or several basic tables. It is different from the basic table in that it is a virtual table. There is no real data. Only the definition of the view is stored in the database, and the data corresponding to the view is not stored. The data is still in the original basic table.

The view can be understood as a window, through the window to see the data, the essence of the view is a query statement.

Create view syntax

create view 视图名 as 查询语句

View use cases

//查询姓张的学生名和专业名
select stuname,mojarname from stuinfo s inner join major m on s.majorid=m.id where s.stuname like '张%';

//创建视图
 create view v1 as select stuname,mojarname from stuinfo s inner join major m on s.majorid=m.id ;
 
//利用视图()
select * from v1 where stuname like='张%';

When the same query statement is used in multiple places, and the query syntax is very complicated, it is recommended to use the view

advantage

1. Simplify user operations

2. Appropriate use of views can express queries more clearly

Disadvantage

1. Query data from database views, performance may be slower

2, the view depends on the table structure, when the table structure changes, the view will also change

1.5 explain keyword in mysql

1.5.1 id

The query sequence number contains a set of numbers that can be repeated, indicating the order in which SQL statements are executed in mysql. Generally, there are three situations:

1. The ids are all the same, the execution order in this case is from top to bottom

2. The id is different. In this case, MySQL will give priority to the one with the larger id number.

3. If the id is the same but different, mysql will first execute the one with the larger id, and then execute it from top to bottom according to the order of id

1.5.2 select_type

select_type represents the type of query, which is mainly used to distinguish between ordinary queries, joint queries, and nested queries. The types are as follows

  • simple: simple query, which means that there are no sub-queries in the query, nesting and other operations
  • primary: The query contains any sub-queries, and the outermost query is marked as primary
  • subquery: represents a subquery
  • derived: mark the subquery as derived, and store the query results in a temporary table
  • union: If the second SELECT appears after UNION, it will be marked as UNION; if UNION is included in the subquery of the FROM clause, the outer SELECT will be marked as DERIVED
  • union result: get the result from the union table

1.5.3 type

Types, the effects are sorted from good to bad as follows

system>const>eq_ref>ref>range>index>ALL

1. System: There is only one row of records in the table. This is a special case of the const type. It does not usually appear and can be ignored.

2. const: It means that it can be found once by index, and const is used to compare primary key or unique index. Because it only matches one row of data, it is fast. If the primary key is placed in the where list, MySQL converts the query into a constant

3. Eq_ref: unique index scan. For each index key, there is only one piece of data in the table that matches it. Commonly used in primary key or unique index scans

4. ref: non-unique index scan, returns all rows matching a single value, essentially an index access, it returns all rows matching a single value, however, he may find multiple eligible Line, so he should belong to a mixture of search and scan

5. Range: Only retrieve rows in a given range, and use an index to select rows. The key column shows which index is used. Generally, queries such as between, <, >, in appear in your where statement. This range scan index is better than a full table scan because it only needs to start at a certain point of the index. , Ending at another point, without scanning all indexes

6, index: full index scan, the difference between Index and all is that index only traverses the index tree, which is usually faster than all, because the index file is usually smaller than the data file

7, all: full table scan, will traverse the whole table to find matching rows

Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref

1.5.4 possible_key

Indicates the index that may be used in the query, but it may not be used in practice

1.5.5 keys

Indicates the index actually used in the query statement, or null if it is not used

1.5.6 key_len

Indicates the number of bytes used in the index. The index length used in the query is calculated by key_len. Without loss of accuracy, the shorter the index length, the better

1.5.7 ref

Shows which column or constant of the index is used to find the value of the index

1.5.8 rows

According to the statistical information of the table and the selection of the index, roughly estimate the number of rows read to find the required record

1.5.9 extra

  • Using filesort: indicates that mysql reads data using an external index to sort, instead of reading according to the index in the table, mysql cannot use the sort completed by the index to become a file sort
  • Using temporary: A temporary table is used to save the intermediate results, and mysql uses a temporary table when sorting the query results
  • Using index: Indicates that the covering index is used
  • Using where: Indicates that where filtering is used
  • Using join buffer: indicates that the link cache is used

1.6 mysql architecture

1.7 How to perform fuzzy query without like

We usually use like statements when performing fuzzy queries, and the performance of like statements is a bit worse.

select * from user where name like '%张%'

The following methods can be used instead

1, locate statement

select * from user where locate('张',name)>0

2、position

select * from user where position('张' in name)

3. Instr statement

select * from user where instr(name,'张')>0

1.8 Store pages in mysql

1.9

1.10 SQL statement optimization

1. Create an index in the table, giving priority to the fields used by where and group by

2. Try to avoid using select *, the returned useless fields will reduce the query effect

3. Try to avoid using in or not in, which will cause the database engine to abandon the index

4. Try to avoid using or, which will cause the database engine to abandon the index

5. Try to avoid using fuzzy query at the beginning of the field, which will cause the database engine to abandon the index for full table scan

6. Try to avoid the null value judgment, which will cause the database engine to abandon the index

1.11 When designing a database, is the higher the paradigm level, the better

The answer is definitely no.

1.12 What are super keys, candidate keys, primary keys, and foreign keys?

  • Super key: The attribute set that uniquely identifies the tuple in the relationship is called the super key of the relationship mode. An attribute can be used as a super key, and multiple attributes can also be used as a super key. Super key Pokémon candidate key and primary key
  • Candidate key: is the smallest super key, that is, the super key without redundant elements
  • Primary key: A combination of data columns or attributes in a database table that uniquely and completely identify the stored data object. A data column can only have one primary key, and the value of the primary key cannot be missing or null
  • Foreign key: the primary key of another table exists in one table, which is called the foreign key of this table

1.13 varchar和char

1. Fixed length and variable length

char means fixed length, fixed length, varchar means

2. Different storage capacity

For char, it can store up to 255 characters

The varchar can store up to 65532 characters

2. Indexes in Mysql

2.1 What is an index

  • According to the official introduction, the index is a data structure that helps MyISAM to efficiently obtain data. The index can be understood as a directory, which can speed up the speed of database queries.
  • Generally speaking, indexes include clustered indexes, covering indexes, composite indexes, etc. MySql uses B+Tree as the data structure of the index
  • Generally speaking, the index itself is also very large, and it is impossible to store all of it in memory. Therefore, the index is often stored on the disk, which can be a separate index file, or it may be stored in a data file together with the data.

SQL statement to create an index

create index index_name on table_name(字段名)

2.2 Advantages and disadvantages of indexing

Advantage:

  • Can improve the efficiency of data retrieval and reduce the IO cost of the database
  • Sorting data by index column reduces the cost of data sorting and also reduces CPU consumption

​ The index column will be automatically sorted, corresponding to the use of the Order by statement will be much faster

Disadvantages:

  • The index will take up disk space, so it is not recommended to use the index when the data in the table is relatively small, after all, it is time-consuming to read the disk.
  • Although the index will improve the query efficiency, but it will reduce the update efficiency of the table. You need to maintain the index for the addition, deletion, and modification of the table

2.3 Classification of Indexes

Single-column index

  • Ordinary index: The basic index type in Mysql, there are no restrictions, and it is allowed to insert duplicate values ​​and null values ​​in the column that defines the index
  • Unique index: The value in the index column must be unique, but null values ​​are allowed
  • Primary key index: a special unique index, no null values ​​are allowed

Composite index

  • Index created on a combination of multiple fields in the table
  • The use of composite index needs to follow the principle of the leftmost prefix
  • Under normal circumstances, it is recommended to use composite index instead of advanced index

2.4 What are the data structures that can be used as indexes

2.4.1 Binary Tree

2.4.2 Red-Black Tree

2.4.3 Hash table

2.4.4 B-tree

2.4.5 B+ Tree

2.5 Basic principles of indexing

Indexes are used to quickly query records that have specific values. If there is no index, generally the entire table is traversed when the query is executed. The principle of indexing is to make unordered data in order

1. Sort the contents of the indexed column

2. Generate an inverted list for the sorting results

3. Put the data address chain on the content of the inverted table

4. When querying, first get the contents of the inverted table, and then take out the data address chain, so as to get the specific data

2.6 Principles of Index Design

1. The column suitable for indexing is the column that appears in the where clause, or the column in the join clause

2. For classes with a small cardinality, the index effect is poor, and there is no need to create an index on this column

3. Use a short index. If you index a long string, you should specify a prefix length, which can save a lot of index space

4. Don't overuse indexes. Indexes require additional disk space and reduce the performance of write operations. When modifying the table, the index must be maintained

2.7 Principles of index creation

1. The leftmost prefix matching principle

2. Create an index more frequently as a query field

3. Frequently updated fields are not suitable for index creation

4. If it is a column that cannot effectively distinguish the data, it is not suitable to be an index column. Like gender

5. Expand the index as much as possible, don't create a new index.

6. Data columns with foreign keys must be indexed

7. For columns that are rarely involved in queries, do not create indexes for columns with more duplicate values

8. Do not create indexes for columns defined as text, image, and bit data types

2.8 Precautions for index creation

  • Non-empty field: the column should be specified as not null. In mysql, it is difficult to optimize the query for columns with null values
  • Fields with large discrete values: the field has less repeated values
  • The smaller the index field, the better: the smaller the field, the more indexes will be stored on the storage page

2.9 Clustered index/non-clustered index/covering index

The primary key index under Innodb is a clustered index, and the primary key index under MyISam is a non-clustered index

The leaf nodes of the clustered index store the primary key value and data rows

The leaf nodes of a non-clustered index store index data, and the data rows are stored separately in another file

Index coverage means that the execution of a query statement can be retrieved only from the index, not from the data table

2.10 Index push down

  • Index pushdown is an optimization on non-primary key indexes, which can effectively reduce the number of times back to the table and greatly improve the efficiency of queries

2.11 Index failure

  • Violation of the leftmost prefix principle
  • Use in and not in
  • There is no query condition, or the query condition does not create an index
  • No leading column is used in the query condition
  • The number of queries is the majority of large tables

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/113977313