Chapter 11 Database
1. What are the three paradigms of databases?
1. The column cannot be divided;
2. Each row of data only does one thing, only related to one column, the primary key;
3. Each attribute has a direct relationship with the primary key, not an indirect relationship;
The three paradigms are just the basic concepts for database design, and a database with less redundancy and a reasonable structure can be established. If there is a special complex, of course, it must be treated specially. The most important thing in database design is to look at requirements and performance, demand>performance>table structure.
Therefore, we cannot blindly pursue the three paradigms to build a database.
2. There are a total of 7 pieces of data in an auto-increment table, the last 2 pieces of data are deleted, the mysql database is restarted, and another piece of data is inserted. What is the id at this time?
In general, the table type we create is InnoDB.
Without restarting MySQL, if you add a new record, the id is 8;
restart, the ID is 6. Because the InnoDB table only records the largest ID of the auto-incrementing primary key in the memory, if you restart, the deleted largest ID will be lost.
If the table type is MyISAM, the maximum ID will not be lost after restarting, the ID is 8;
InnoDB must have a primary key (it is recommended to use an auto-incrementing primary key instead of a UUID, since the auto-incrementing primary key index query is efficient), support foreign keys, support transactions, and support row-level locks.
After the system crashes, MyISAM is difficult to recover;
Considering comprehensively, InnoDB is preferred, and MySQL is also InnoDB by default.
3. How to get the current database version?
//MySQL,,mysql -vselect version();//Oracle select * from v$version;
4. What is ACID?
ACID is the four basic elements of database transaction execution, including Atomicity, Consistency, Isolation, and Durability.
All operations in the entire transaction are either completed or not completely completed, and it is impossible to stop at a certain link in the middle. If an error occurs during the execution of the transaction, it will be rolled back to the state before the transaction started by roolback, as if the transaction had never been executed.
Transactions must always keep the system in a consistent state, no matter how many concurrent transactions are at any given time.
Executing transactions in an isolated state makes them appear to be the only operation performed by the system in a given time.
If there are two transactions that run at the same time and perform the same function, the isolation of the transaction ensures that each transaction in the system thinks that only it is using the system. This property is called serialization. In order to prevent confusion between transaction operations, requests must be serialized or serialized so that only one request is used for the same data at the same time.
A successful transaction will permanently change the state of the system.
5. What is the difference between char and varchar?
- The length of char is fixed, and the length of varchar is variable;
- The efficiency of char is higher than that of varchar;
- Char occupies more space than varchar, char needs to use trim when querying;
6. What is the difference between float and double?
1. What is the difference between float and double?
(1) The number of bytes occupied in the memory is different
Single-precision floating-point numbers occupy 4 bytes in memory;
Double-precision floating-point numbers occupy 8 bytes in memory;
(2) The number of significant digits is different
8 significant digits of single-precision floating-point number;
Double-precision floating-point number with 16 significant digits;
(3) The range of values is different
The representation range of single-precision floating-point numbers: -3.40E+38~3.40E+38
The representation range of double-precision floating-point numbers: -1.79E+308~-1.79E+308
(4) Different processing speeds in the program
Generally speaking, the CPU processes single-precision floating-point numbers faster than double-precision floating-point numbers
If it is not declared, the default decimal is of type double. If you want to use float, you must perform forced conversion;
2. For example
float f = 1.3; will compile and report an error, the correct way of writing is float f = (float)1.3; or float a = 1.3f; (f or F can be case-insensitive)
Float is eight significant digits, and the seventh digit will be rounded;
4. Interview questions
(1) What will 3*0.1==0.3 in java return? true or false?
Answer: Return false, because floating-point numbers cannot be expressed completely accurately, and accuracy will generally be lost;
(2) Is float f = 3.4; in java correct?
Answer: Not correct. Because 3.4 is a double-precision floating-point number, assigning double-precision to single-precision is a downcast and will cause precision loss. Therefore, it is necessary to force type conversion float=(float)3.4; or write float f = 3.4f;
Seven, Oracle paging sql
#不带排序的SELECT * FROM (SELECT ROWNUM AS rowno, t.* FROM worker t where ROWNUM <=20) table_alias WHERE table_alias.rowno > 10;
#带排序的SELECT * FROM (SELECT tt.*, ROWNUM AS rowno FROM ( SELECT t.* FROM worker t ORDER BY wkid aSC) tt WHERE ROWNUM <= 20) table_alias WHERE table_alias.rowno >= 10;
8. How does the database ensure the uniqueness of the primary key
1. Primary key constraints
No two rows on the primary key column have the same value (ie duplicate value), and NULL is not allowed;
2. Uniqueness constraint
Ensure that the data in a field or a group of fields is different from the corresponding data in other rows in the table. Unlike the primary key constraint, the unique constraint is allowed to be null, but there can only be one row;
3. Unique index
Rows with the same index value are not allowed, thereby prohibiting duplicate indexes and key values;
4. The difference between the three
- Constraints are used to check the correctness of the data;
- Indexes are used to optimize queries;
- Creating a unique constraint will create a constraint and a unique index;
- Creating a unique index will only create a unique index;
- Both primary key constraints and unique constraints will create a unique index.
Nine, how to design a database
1. The database design at least takes up more than 40% of the project development time
2. Database design does not just stay on the surface of the demo page
The fields required for the page content are only part of the database design. There are also fields required for system operation, module interaction, transfer data, connections between tables, etc., so database design is definitely not a simple basic data storage, but also logic data storage.
3. After the database design is completed, 80% of the design and development of the project must be stored in your mind
The design of each field must have its own meaning. It is necessary to clearly know how to use these fields in the program, and how the connection of multiple tables is reflected in the program.
4. Consider efficiency and optimization issues when designing a database
Large amounts of data indicate coarse-grained ones, and some necessary fields will be redundant, so that the least tables and the weakest table relationships are used to store massive amounts of data. Big data tables should be indexed for easy query. For requirements such as calculations, data interactions, and statistics, it is also necessary to consider whether it is necessary to use stored procedures.
5. Add necessary redundant fields
Fields such as creation time, modification time, operating user IP, and remarks should be included in each table. Some redundant fields are convenient for future maintenance, analysis, and expansion.
6. Design a reasonable table association
If the relationship between the two tables is complex, it is recommended to use the third mapping table to associate and maintain the relationship between the two tables to reduce the direct coupling between the tables.
7. When designing the table, do not add constraint associations such as primary and foreign keys, and add constraint associations after the system coding phase is completed
8. Choose a suitable primary key generation strategy
The difficulty of database design is actually much more difficult than pure technical implementation. It fully reflects a person's overall design ability and control ability. Finally, database design is very important and complicated.
10. Is gender suitable for indexing
Fields with low discrimination are not suitable for indexing, because index pages need to be costly and need to be stored, but such fields can be used as part of a joint index.
11. How to query duplicate data
1. Query repeated single fields (group by)
select 重复字段A, count(*) from 表 group by 重复字段A having count(*) > 1
2. Query multiple repeated fields (group by)
select 重复字段A, 重复字段B, count(*) from 表 group by 重复字段A, 重复字段B having count(*) > 1
3. Delete all duplicate data
-- 慎重考虑后执行，后悔记得及时回滚。 delete from table group by 重复字段 having count(重复字段) > 1
12. What optimization methods are generally adopted by the database?
1. Select the appropriate field attributes
- In order to obtain better performance, you can set the field width in the table as small as possible.
- Try to set the field to not null
- When executing a query, the database does not need to compare null values.
- For some provinces or gender fields, they are defined as enum types. Enum types are treated as numeric data, and numeric data is processed much faster than text type blocks.
2. Use join to replace subqueries
3. Use union to replace manually created temporary tables
Note: In union usage, the field types of the two select statements must match, and the number of fields must be the same.
Either all succeeded or all failed.
Can guarantee the consistency and completeness of the data in the database. The transaction starts with begin and ends with the commit keyword.
If an error occurs, the rollback command can restore the database to the state before begin.
Another important function of transaction is that when multiple users use the same data source at the same time, it can provide users with a safe access method by locking the database, so that it can ensure that the user's operation is not interfered by other users.
5. Lock the table
Although the transaction is a very good way to maintain the integrity of the database, but because of its exclusivity, sometimes affect the performance of the database, especially in large applications.
Since the database will be locked during the execution of the transaction, other users can only wait temporarily until the end of the transaction.
Sometimes you can use the method of locking the table to get better performance,
Shared lock: other users can only see but cannot modify
lock table person in share mode;
For the locks actively added through the lock table command, if you want to release them, you only need to issue the rollback command.
6. Use foreign keys
The method of locking the table can maintain the integrity of the data, but it cannot guarantee the relevance of the data. At this time, foreign keys can be used.
7, use the index
Index is a common method to improve database query speed, especially when the query statement contains max(), min(), order by commands, the performance improvement is more significant.
Generally speaking, indexes should be built on fields commonly used for join, where, and order by. Try not to index a large number of duplicate worthy fields in the database.
8. Optimized query statement
Try not to use functions for operations on indexed fields.
Try not to use like keywords and wildcards. This is very simple, but at the expense of performance.
Avoid automatic type conversion in the query, because type conversion will also invalidate the index.
13. How to define the index and which types
- B-tree index, if the index is not established, Oracle will automatically add a B-tree index to each column;
- normal: normal index
- unique: unique index
- bitmap: bitmap index, bitmap index is specific to the situation where there are only a few enumeration values, such as the gender field;
- Function-based index
14. What is the difference between inner connection, left connection and right connection of mysql?
- Inner connection, showing all the data in the two tables that are connected;
- Left link, take the left table as a reference, display all data, if there is no in the right table, it will be displayed as null
- Right link, use the right table as a reference to display the data, and if there is none in the left table, it will be displayed as null
Chapter 12 RabbitMQ
1. What are the usage scenarios of RabbitMQ?
1. Solve the asynchronous problem
For example, when users register, send emails and SMS to report successful registration, RabbitMQ message queues can be used, and users do not need to wait for feedback.
2. Decoupling between services
The RabbitMQ message queue is added to the order system and the inventory system. When the inventory system has problems, the order system can still be used normally, reducing the coupling between services.
3. The spike system
Use the maximum value of RabbitMQ to realize the spike system.
2. What are the important roles of RabbitMQ? What are the important components?
1. What are the important roles of RabbitMQ?
Client, RabbitMQ, server.
2. What are the important components?
(1) connectionFactory (connection manager)
The manager that establishes the connection between the application and RabbitMQ.
The channel used for message push.
(3) RoutingKey (routing key)
Used to distribute the data of the producer to the switch.
(4) Exchange (switch)
Used to receive and distribute messages.
(5) BindKey (binding key)
Used to bind messages from the exchange to the queue
Used to store producer messages.
3. What is the role of vhost in RabbitMQ?
Vhost can be understood as the mini version of RabbitMQ, which contains independent switches, bindings, and queues. The most important thing is that it has an independent authority system that can be controlled by users within the scope of vhost. Considering the overall situation of RabbitMQ, different applications can run on different vhosts as a means of isolating different permissions.
Chapter 13 JVM
1. What are the main components of jvm? And its role?
JVM includes class loading subsystem, heap, method area, stack, local method stack, program counter, direct memory, garbage collector, and execution engine.
1. Class loading subsystem
The class loading subsystem is responsible for loading class information, and the loaded class information is stored in the method area.
2. Direct memory
Direct memory is the memory space outside the Java heap and directly applied to the system. The speed of accessing direct memory is due to the Java heap. For performance considerations, direct memory may be considered for frequent reads and writes.
3. Garbage collector
The garbage collector can reclaim the heap, method area, and direct memory.
4. Execution engine
The execution engine is responsible for executing the bytecode of the virtual machine, and the virtual machine uses just-in-time compilation technology to compile the method into machine code before execution.
2. Talk about the jvm runtime data area?
The runtime data area includes heap, method area, stack, local method stack, and program counter.
The heap solves the problem of object instance storage, the main area managed by the garbage collector.
2. Method area
The method area can be considered as a part of the heap, used to store the information that has been loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler.
The stack solves the problem of program running. What is stored in the stack is a stack frame, and what is stored in the stack frame is information such as local variable table, operand stack, dynamic link, method export and so on.
(1) Stack frame
The process from invocation to execution of each method is the process of pushing a stack frame in the virtual machine stack to popping out of the stack.
(2) Local variable table
Used to save the parameters and local variables of the function.
(3) Operand stack
The operand stack is also called the operation stack. Most instructions pop data from here, perform operations, and then push the result back to the operand stack.
4. Local method stack
Similar to the stack function, the native method stack executes the native method, a Java interface for calling non-Java code.
5. Program counter (PC register)
Stored in the program counter is the number of lines of bytecode executed by the current thread. When the JVM works, it selects the next bytecode instruction to be executed by changing the value of this counter.
3. What is a class loader, and what are the class loader?
1. What is a class loader?
The class loader is responsible for loading all classes, and it generates a java.lang.Class instance object for all classes loaded into memory.
2. What are the class loaders?
There are three types of loaders for the JVM:
(1) Start the class loader
This class has no parent loader and is used to load the core classes of Java. The implementation of the startup class loader depends on the underlying operating system and is part of the implementation of the virtual machine. It does not inherit from java.lang.classLoader.
(2) Extended class loader
Its parent class is the startup class loader, and the extended class loader is a pure java class, a subclass of the ClassLoader class, and is responsible for loading the extension directory of the JRE.
(3) Application class loader
Its parent class is the extended class loader, which loads classes from the directory specified by the environment variable classpath or the system property java.lang.path. It is the parent loader of the custom class loader.
Fourth, talk about the execution process of class loading?
When the program actively uses a certain class, if the class has not been loaded into the memory, the JVM will load the class through the three steps of loading, connecting, and initializing.
Loading refers to reading the class file of the class into memory and creating a java.lang.Class object for it.
The loading of the class is completed by the class loader, which is provided by the JVM, and developers can also create their own class loader by inheriting the ClassLoader base class.
The binary data of a class can be loaded from different sources by using different class loaders. There are usually the following sources:
- Load from local file system
- Load from jar package
- Load via network
- Dynamically compile a Java source file and load it
When the class is loaded, the system generates a corresponding Class object for it, and then enters the connection phase, which is responsible for merging the binary data of the class into the JRE.
Class connection can be divided into three stages:
File format verification
Symbol reference verification
Allocate memory for static variables of the class and set default initial values.
Replace the symbolic references in the binary data of the class with direct references.
Assign initial values to static variables of the class.
5. What is the class loading mechanism of JVM?
There are three main types of JVM class loading mechanisms:
1. Fully responsible
When the class loader loads a certain class, other classes that the class depends on and references are also loaded by the class loader.
2. Parental delegation
Let the parent loader load the class first, and only consider loading it by itself when the parent loader cannot load it.
3. Cache mechanism
The caching mechanism ensures that all loaded classes will be cached. When a certain class is needed in the program, first search from the cache area. If it does not exist, then the binary data corresponding to the class will be read and converted into a class object. , Stored in the buffer area.
This is why after modifying the class, the JVM must be restarted so that the modifications made by the program will take effect.
6. What is the parental delegation model?
If a class receives a class loading request, it does not load it first, but delegates the request to the loader of the parent class. If the parent loader still has its parent loader, it further delegates upwards, recursively in turn , The request will eventually reach the top-level startup class loader. If the parent class loader can complete the parent loading task, it will return successfully. If the parent loader cannot complete the loading task, the child loader will try to load by itself. This is the parent delegation model.
Advantages of the parental delegation model:
- Avoid repeated loading;
- Taking into account security factors, the types defined in the java core API will not be replaced at will. Assuming that a class named java.lang.Integer is passed through the network and passed to the startup loader through the parental delegation mode, the startup loader is in the core Java API If the class with the same name is found in the file, it is found that the class has been loaded, it will not reload the Integer class passed by the network, but will directly return the loaded Integer.class, which can prevent the core API library from being tampered with at will.
7. How to judge whether the object can be recycled?
1. Reference counting algorithm
(1) Determine the number of references to the object
Determine whether the object can be recycled by judging the number of references to the object;
each object instance has a reference counter, which is referenced by +1 and completed by reference by -1;
any object instance with a reference count of 0 can be treated as garbage;
(2) Advantages and disadvantages
Advantages: high execution efficiency, less impact on the program;
disadvantages: unable to detect circular references, resulting in memory leaks;
2. Reachability analysis algorithm
Determine whether the object can be recycled by judging whether the reference chain of the object is reachable.
If the program can no longer refer to the object, then the object can definitely be recycled, and this state is called unreachable.
So how to judge the unreachable state?
The answer is GC roots, that is, the root object. If an object cannot reach the path of the root object, or the object cannot be referenced from the root object, the object is unreachable.
The following three types of objects are called GC roots in the JVM to determine whether an object can be recycled.
(1) The stack frame of the virtual machine stack
When each method is executed, JVM will create a corresponding stack frame (operand stack, local variable table, runtime constant pool reference), when the method is executed, the stack frame is popped from the stack, so , The exclusive temporarily created in the method does not exist, or there is no GC roots pointing to these temporary objects, these objects will be recycled in the next GC.
(2) Static attributes in the method area
The static attribute data type attribute does not belong to any instance, so the attribute will naturally serve as GC roots. This requires the class to exist, the object pointed to by the reference will always exist, and the class will be recycled.
When will the class be recycled?
- No instance of this class exists in the heap
- The classLoader that loaded the class has been recycled
- The java.lang.class object of this class is not referenced anywhere, which means that the information of this class cannot be accessed through reflection
(3) Objects referenced by the local method stack
8. What garbage collection algorithms does jvm have?
1. Whether the object is dead algorithm
- Reference counter algorithm
- Reachability analysis algorithm
2. GC algorithm
(1) Mark removal algorithm
If the object is cleared after being marked, it will bring a new problem-memory fragmentation. If there is a larger object instance that needs to allocate a larger memory space on the heap next time, it may not find enough contiguous memory and have to trigger garbage collection again.
(2) Copy algorithm (the new generation garbage collection algorithm in the Java heap)
- First mark the memory to be reclaimed and the memory not to be reclaimed;
- Copy the unreclaimed memory to the new memory area;
- The existing memory area can be fully reclaimed, and the new memory area is also continuous;
The disadvantage is that part of the system memory is lost, because part of the memory is freed up for copying.
(3) Marked compression algorithm (garbage collection algorithm of the old age in the Java heap)
For the young generation, most objects will not survive, so the replication algorithm is more efficient, but for the old generation, most objects may continue to survive. If the replication algorithm is used at this time, the efficiency will be reduced.
The mark compression algorithm is still mark first. The memory objects that do not need to be reclaimed are compressed to one end of the memory. At this time, the memory at the boundary can be cleared, which can avoid the efficiency problems caused by the copy algorithm and also avoid the problem of memory fragmentation.
The old age garbage collection algorithm is called "Major GC".
Nine, what garbage collectors does jvm have?
10. JVM stack concept, when to destroy objects
- The class will be loaded when the program is running, and the method will be loaded when it is executed. If there are no references, Java will automatically garbage collect. You can also use System.gc() to turn on the collector, but the collector is not necessarily Will be recycled immediately.
- Static variables are created when the class is loaded, and destroyed in order at the end of the entire program;
- Instance variables are created when the class instantiates the object, and destroyed when the object is destroyed;
- Local variables are created when used in the local scope, and destroyed when they exit the local scope;
11. What are the new-generation garbage collectors and the old-generation garbage collectors? What's the difference?
New generation collectors: Serial, ParNew, Parallel Scavenge
Old age collector: Serial Old, Parallel Old, CMS
The new-generation collector generally uses a copy algorithm, which is more efficient, but wastes memory;
The old generation collector generally uses a clearly marked algorithm, such as the most commonly used CMS;
11. Tell me about the CMS garbage collector in detail?
The CMS garbage collector is Concurrent Mark Sweep, which is a synchronous mark-sweep. CMS is divided into four stages:
Initial mark, mark the object that GC Root can directly associate to, it will trigger "Stop The World";
concurrent mark, judge whether the object is in use through GC Roots Tracing;
re-mark, the object will be judged again during the mark, the execution time is longer Short, it will trigger "Stop The World";
concurrent clearing, clearing objects, can be performed concurrently with user threads;
12. Briefly describe how the generational garbage collector works?
The generational collector is divided into the young generation and the old generation, the new generation accounts for about 1/3, and the old generation accounts for about 2/3;
The new generation includes Eden, From Survivor, and To Survivor; the space ratio between the Eden area and the two survivor areas is 8:1:1;
The execution process of the garbage collector:
- Put the surviving objects of Eden + From Survivor into the To Survivor area;
- Clear the Eden + From Survivor partition, exchange the From Survivor and To Survivor partitions;
- After each exchange, the age of the surviving object is +1, reaching 15 and upgrading to the old age, and the big object will directly enter the old age;
- In the old age, when the space reaches a certain proportion, it will trigger a global recovery. The old age generally adopts the mark-sweep algorithm;
Highlights from the past issue: