当前位置:网站首页>Interview MySQL

Interview MySQL

2022-06-22 19:00:00 LXMXHJ

review

Database mind map
MySQL Mind mapping

MySQL summary

Oracle and MySQL difference

All two are

difference MySQLOracle
company The Swedish MySQL AB company
Has been sun Acquisition
Has been Oracle Acquisition
Oracle
type Small and medium databases Large databases
cost Free open source charge
Default port number 33061521
Database and user relationship One user to one or more databases One database corresponds to multiple users
Field auto increment Set field to auto increment Create sequence sequence, add to The sequence of .nextval()
character varcharvarchar2
Numeric type smallint(2 byte )
int(4 byte )
integer(int A synonym for )
bigint(8 byte )
number
Pagination select * from table limit(start-1)*limit,limit
start Page number ,limit It's the number of bars per page
Use pseudo Columns rownum

Yes MySQL Understanding of the framework ?

MySQl Can be divided into Server layer and Storage engine Two parts .

  • Server layer :
    cover MySQL Most of the core functions of , And all the built-in functions , All cross engine functions are implemented at this layer ;
  • Storage engine :
    Data storage and extraction .
    Components are plug-in . The storage engine has InnorDB、MyISAM、memory;
    MySQL5.5.5 The default is InnorDB, Can pass engine = MyISAM To specify the storage engine ;
     Insert picture description here
    Server Introduction to layer components :
member effect
The connector Manage connections , Authority verification
The query cache If hit, the result will be returned directly
analyzer Lexical analysis 、 Syntax analysis
Optimizer Execution plan generation , Index selection
actuator Operate the engine , Return results

One SQL Statement execution flow in the database framework ?

Execution process :

  • The application puts the query SQL Statement is sent to the server for execution ;
  • Check whether the query exists in the cache . There is , Returns the result in the cache ; otherwise , Perform the next step ;
  • Conduct SQL Parsing 、 Syntax checking and preprocessing , Then the optimizer generates the corresponding execution plan ;
  • MySQL According to the execution plan , Call the interface of storage engine to query ;
  • Finally, the query result is returned to the client .

What are the three paradigms of a database ?

name meaning explain
First normal form (1NF) All domains should be atomic
Each column is indivisible
Each column of a database table is an indivisible atomic data item , It's not a collection 、 Array 、 Records and other non atomic data items
When an attribute of an entity has multiple values , Must be split into different properties
Second normal form (2NF) Non primary key columns are completely dependent on primary key columns , Not dependent on other non primary keys It is required that each instance or record in the database table can be uniquely distinguished
Third normal form (3NF) Any non primary attribute does not depend on other non primary attributes It is required that one relationship does not contain the non primary key word information already contained in other relationships
give an example : Department information table , Primary key = Department number , Other information includes the name of the Department 、 Department profile, etc ; After the department number is listed in the employee information table , No longer list Department names 、 Department indirect information

Professional term or key word

DB:Database database
DBMS:Database Management System Database management system
RDBMS:Relative Database Management System Relational database management system
database : Is a collection of tables , With relevant data ;
surface : A table is a matrix of data ;
Column ( Field ): One column ( data elements ) Contains the same type of data ;
That's ok ( Record ): A set of relevant data ;

database Basic operation

see 、 establish 、 modify 、 Delete database .

Understanding of horizontal and vertical segmentation ?

  • Database splitting principle :
    Through certain conditions , According to a certain dimension , Distribute the data in the same database to multiple databases ( host ), So as to achieve decentralized single database ( host ) Effect of load ;
  • Split mode :
    vertical ( The longitudinal ) Split 、 Horizontal split .

Split Vertically : It's special for the special warehouse

Introduce explain
Concept Classify the table by business , Distributed to different databases
Problem solving Reduce the load on a single node database
Can't solve the problem Shrinkage table , That is, the amount of data in each database has not changed
advantage After the split, the business is clear , The split rules are clear
Integration or expansion between systems is easy
Data maintenance is simple
shortcoming Some business tables cannot join, Only through interface , Increased system complexity
There is a single library performance bottleneck due to different restrictions of each business , Not easy to expand data and improve performance .
The transaction is complex

Horizontal split : Sub database and sub table

  • Concept :
    Split the same table into different databases ;
    It's not about classifying tables , It's distributed to multiple libraries according to certain rules of a field , Each table contains some data ;
    Simple understanding = According to the segmentation of data rows , Splitting some rows into other databases ;
  • misunderstand :
    The data table sliced horizontally must be saved in different places MySQL Node ;
    In fact, the data table from horizontal segmentation can also be saved in a MySQL Node ;
  • Why does horizontal segmentation not necessarily require multiple MySQL Node ?
    MySQL It comes with a data partition technology , You can put the data of a table , According to special rules , Sharding is stored in different directories .
    If given Linux The host is attached with multiple hard disks , You can use MySQL Partition technology , The data of a table is partitioned and stored on multiple hard disks .
    In this way, the original hard disk is limited IO Ability , Upgraded to multiple disk enhanced IO.

data type

value type 、 String type 、 Escape character 、 date / Time 、 Binary type 、 Variable .

char and varchar The difference between ?

difference charvarchar
length Fix
char(10) character string "abc" Storage 10 Bytes , among 7 One is a space
variable
varchar(10) character string "abc" Only three bytes
efficiency Higher The lower
Save a space Than char Save a space
Storage type varchar yes oracle Developed data types
Industry standards can store empty strings ;oracle It can also store NULL value

varchar(10) and varchar(20) The difference between ?

varchar(10) Put at most 10 Characters , therefore varchar(10) and varchar(20) Storage 10 And 10 Less than characters occupy the same space ;
But the latter consumes more memory when sorting , because order by col use fixed_length Calculation col length .
fixed_length Fixed length ;

Data sheet Basic operation

establish 、 see 、 Modify data table structure .

operation Table data

Inquire about 、 increase 、 modify 、 Delete 、 Clear the table record .

How to optimize query performance ?

  • Reduce the amount of data requested
    Only necessary columns are returned : Avoid using select * sentence ;
    Just go back to the necessary lines : Use limit Statement to limit the data returned ;
    Cache data from repeated queries ;

  • Reduce the number of lines scanned by the server
    Overwriting queries by index ;

sql tuning

  • Create index :
    Try to avoid full scan , Consideration should be given where or order by
  • Avoid using calculations on indexes
    stay where In the sentence , If the index column is part of a calculation or function ,DBMS The optimizer will not use indexes but full table queries
  • Use precompiled queries
    The program is usually executed dynamically according to the user's input SQL, Use parameterization as much as possible SQL, You can avoid SQL Inject holes ;
    The database will parameterize SQL Precompile , You can directly use the precompiled results in the future .
  • adjustment where Join order in clause
    DBMS It is generally analyzed from top to bottom where Clause ;
  • Try to put as many as possible sql The statement is compressed into one sql in
    Every time you execute sql You have to go through the following process : Set up a network connection 、 Verify authority 、 Conduct SQL Statement query optimization 、 Send the execution result
    This process is very time consuming .
  • use where Clause substitution Having Clause
    Avoid using having, because having The result set is filtered only after all records are retrieved , and where Is before the aggregation ;
    If you can use where Reduce the number of records , You can reduce the cost ;
  • Use the alias of the table
    sql Join multiple tables , Use the alias of the table It can reduce the parsing time And reduce the syntax ambiguity of column names with the same name in different tables
  • use union all Replace union
  • use varchar/nvarchar Replace char / nchar
    Variable length fields have small storage space , You can save storage space ;
    For queries , Searching in a relatively small field is efficient ;

View

establish 、 see 、 modify 、 Delete view .

Indexes

Create index instance 、 General index 、 unique index 、 primary key 、 Composite index 、 Full-text index .

Talk about your understanding of index ?

  • effect :
    Index appears to improve query efficiency , It is equivalent to the directory of the database ;
    It is the most effective way to optimize query performance ;

  • Concept :
    stay MySQL Also known as “ key ”, It is a data structure used by the storage engine to quickly find records ;

  • principle :
    Filter out the final desired results by constantly narrowing the range of data you want to obtain , At the same time, it turns random events into sequential events ;

  • classification :
    hash Index of type : Quick to query a single item , Range query is slow ;
    b-tree Index of type :b+ Trees , The more layers , Exponential growth in data volume ;

  • shortcoming :
    Creating and maintaining indexes takes time ;
    Add to data 、 Delete 、 Change time , Indexes also need to be maintained dynamically , Reduce the speed of data maintenance ;
    As the amount of data increases , The more physical space an index needs to occupy ;

  • Establish principles :
    Frequently queried fields :where In clause ;
    In the grouped fields : That is to say group by In clause ;
    Joint query between dependent child table and parent table , Primary key or foreign key field ;
    Set unique integrity constraint fields ;

  • Not suitable for indexing :
    Columns that are rarely used in queries perhaps Columns with more duplicate values , It's not good to index ;
    Some special data types , It's not good to index . such as : The text field (text) etc. .

What data structure is used at the bottom of the index ?

The data structure of the index and Specific storage engine The realization of ;
MySQL The indexes used more often in are Hash Indexes 、B+ Trees Index, etc. ;
Then the underlying layer of the corresponding index is Hash surface 、 B + Trees ;

Talk to you about B+ Trees The understanding of the ?

Icon :
 Insert picture description here

explain The root node Branch nodes
Grubbing node 、 Leaf node
Leaf node
The number of sons Yes M A son has m Elements Yes M A son has m Elements
Store content keyword ( Indexes )、 The pointer keyword ( Indexes )、 The pointer keyword 、 data
Storage All root nodes 、 Branch nodes exist in child nodes , Is the maximum or minimum value
Include all keywords 、 A pointer to a data record
Leaf nodes themselves are linked in descending order according to keywords

effect :
advantage 1 = More efficient single element lookup .

To cluster index 、 Sparse index The understanding of the ?

  • Cluster index :
    It is to reorganize the actual data on the disk , According to the specified value of one or more columns Sorting algorithm .
    The feature is that the order of storing data is consistent with the order of index .
    In general, the primary key creates a clustered index by default , And only one cluster index is allowed in a table .

Cluster index and Nonclustered index ( Sparse index ) difference :
Cluster index : That is, the leaf node contains all complete row records , The leaf node contains the index and all other field information , That is, the data node ;InnoDB Storage engine built with primary key index B+tree That is, the cluster index ,
Nonclustered index : The others are sparse indexes ; The leaf node is still the index node ;

  • Sparse index :
    Secondary indexes 、 Joint index 、MyISAM The indexes of the storage engine are all sparse indexes .

Understanding of hash index ?

Understanding of overlay indexes ?

Classification of indexes ?

Understanding of the leftmost prefix principle ?

How to know if the created index has been used ? Or how can we know the reason why this statement runs so slowly ?

When the index will fail ? That is, the query does not go through the index ?

Business

Storage index

MyISAM、InnoDB、Memory.

Why? InnorDB The storage index adopts B+ Trees instead of B Trees ?

InnoDB and MyISAM Comparison ?

constraint

Primary key constraint 、 Foreign key constraints 、 Unique constraint 、 Check constraint 、 Non empty constraint 、 Default constraint .

20 Which three threads are involved in master-slave replication ?

原网站

版权声明
本文为[LXMXHJ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221732055508.html