Article directory
- 1. Which fields do you pay more attention to in the explain execution plan in MySQL?
- 2. What is the difference between char, varchar and text?
- 3. What is the difference between int(3) and int(11) queries?
- 4. What is the difference between NULL and an empty value in a field?
- 5. How to solve the circular dependency problem in spring?
- 6. What is the difference between jdk dynamic proxy and CGlib proxy?
- 7. What is the difference between strong references, soft references, weak references, and phantom references?
- 8. Implementation of custom annotations?
- 9. Redis scenario question: Query a certain fixed prefix key from massive data?
- Summarize
1. Which fields do you pay more attention to in the explain execution plan in MySQL?
mysql> explain select * from staff;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | staff | ALL | NULL | NULL | NULL | NULL | 2 | NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set
type (important)
This is one of the most important fields and shows what type was used for the query. The connection types from best to worst are:
system,const,eq_ref,ref,fulltext,ref_or_null,index_merge,unique_subquery,index_subquery,range,index,ALL
2. What is the difference between char, varchar and text?
- In MySQL, char, varchar, and text fields can all be used to store character-type data. Both char and varchar can specify the maximum character length, but text cannot.
- Storage and Retrieval
The storage and retrieval of data are also different. - According to the query speed:
char is the fastest, followed by varchar, and text is the slowest. - Storage length:
3. What is the difference between int(3) and int(11) queries?
The 3 or 11 here represents the specific length stored in the database. I always think that int(3) can only store numbers of 3 lengths, and int(11) can only store numbers of 11 lengths. In fact, when we are When you choose to use the int type, whether you choose int(3) or int(11), it will be stored in the database with a length of 4 bytes.
4. What is the difference between NULL and an empty value in a field?
-
Essentially different:
1. Null values do not take up space
2. Null values take up space -
In layman's terms:
a null value is like a vacuum cup with nothing, and a null value is a cup filled with air. Although they all look the same, they are fundamentally different.
5. How to solve the circular dependency problem in spring?
5.1 Redesign
Redesign the structure to remove circular dependencies.
5.2 Using the annotation @Lazy
One of the easiest ways to eliminate circular dependencies is through lazy loading. When injecting dependencies, inject the proxy object first, and then create the object to complete the injection when it is used for the first time.
@Component
public class CircularDependencyA {
private CircularDependencyB circB;
@Autowired
public CircularDependencyA(@Lazy CircularDependencyB circB) {
this.circB = circB;
}
}
After using @Lazy, run the code and you can see that the exception is eliminated.
5.3 Using Setter/Field Injection
One way the Spring documentation suggests is to use setter injection. Injection occurs when dependencies are finally used. Make few changes to the previous sample code to observe the test effect.
@Component
public class CircularDependencyA {
private CircularDependencyB circB;
@Autowired
public void setCircB(CircularDependencyB circB) {
this.circB = circB;
}
public CircularDependencyB getCircB() {
return circB;
}
}
@Component
public class CircularDependencyB {
private CircularDependencyA circA;
private String message = "Hi!";
@Autowired
public void setCircA(CircularDependencyA circA) {
this.circA = circA;
}
public String getMessage() {
return message;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
TestConfig.class })
public class CircularDependencyTest {
@Autowired
ApplicationContext context;
@Bean
public CircularDependencyA getCircularDependencyA() {
return new CircularDependencyA();
}
@Bean
public CircularDependencyB getCircularDependencyB() {
return new CircularDependencyB();
}
@Test
public void givenCircularDependency_whenSetterInjection_thenItWorks() {
CircularDependencyA circA = context.getBean(CircularDependencyA.class);
Assert.assertEquals("Hi!", circA.getCircB().getMessage());
}
}
5.4 Using @PostConstruct
@Component
public class CircularDependencyA {
@Autowired
private CircularDependencyB circB;
@PostConstruct
public void init() {
circB.setCircA(this);
}
public CircularDependencyB getCircB() {
return circB;
}
}
@Component
public class CircularDependencyB {
private CircularDependencyA circA;
private String message = "Hi!";
public void setCircA(CircularDependencyA circA) {
this.circA = circA;
}
public String getMessage() {
return message;
}
5.5 Implement ApplicationContextAware and InitializingBean
@Component
public class CircularDependencyA implements ApplicationContextAware, InitializingBean {
private CircularDependencyB circB;
private ApplicationContext context;
public CircularDependencyB getCircB() {
return circB;
}
@Override
public void afterPropertiesSet() throws Exception {
circB = context.getBean(CircularDependencyB.class);
}
@Override
public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
context = ctx;
}
}
@Component
public class CircularDependencyB {
private CircularDependencyA circA;
private String message = "Hi!";
@Autowired
public void setCircA(CircularDependencyA circA) {
this.circA = circA;
}
public String getMessage() {
return message;
}
}
6. What is the difference between jdk dynamic proxy and CGlib proxy?
- The jdk dynamic proxy uses the interceptor (must implement InvocationHandler) and the reflection mechanism to generate an anonymous class of the proxy interface, which is called before calling the specific method. InvokeHandler to handle.
- Use the ASM framework to load the class file generated by the proxy object class, and modify its bytecode to generate a subclass for processing.
7. What is the difference between strong references, soft references, weak references, and phantom references?
8. Implementation of custom annotations?
There are four meta-annotations in java: @Retention, @Inherited, @Documented, @Target
9. Redis scenario question: Query a certain fixed prefix key from massive data?
In Redis, if you want to query a certain data in a large amount of data, you should use Scan. Scan has the following characteristics:
- Scan can realize the matching function of keys;
- Scan is queried through a cursor and will not cause Redis to freeze;
- Scan provides the count parameter, which can specify the number of traversals;
- Scan will return the cursor to the client, and the user client will continue to traverse the query;
- The results returned by Scan may have duplicate data, which needs to be deduplicated by the client;
- A null value is returned once and the cursor is not 0, indicating that the traversal is not over yet;
- Scan can guarantee that the deleted elements will not be queried before starting the retrieval;
- If some elements are modified during the iteration process, Scan does not guarantee that the relevant elements can be queried.
Summarize
I hope it is helpful to you, welcome to discuss in the comment area.