Mybatis-SQL analysis component | Jingdong logistics technical team

background

One of the biggest hidden dangers in preparing for the big promotion is slow sql, which is the most destructive, and it is also the biggest hidden danger that often causes the entire application to jitter in daily work, and there are certain technical requirements for evaluating the quality of sql. Lack of experience or lack of carefulness caused a bad sql to go online successfully. When it was discovered, it either caused online impact, alarm, or post-slow sql collection discovery. At this time, it is generally impossible to stop the loss quickly and needs to be modified. The code goes online, or the database index is adjusted.

Core pain points:

1. It is impossible to detect slow SQL in advance, and may deteriorate into slow SQL statements

2. After slow SQL appears online, it is impossible to quickly stop the loss

Solutions

1. To solve the problem before going online, the best way is to find out whether a sql is good or bad during the testing phase, or even during the development phase

2. After the slow SQL is found online, in addition to changing the code to go online and adjusting the database table index, it supports hot update to replace the SQL statement .

Within the department, most of the database frameworks currently use mybatis, and then develop a mybatis component based on the implementation mechanism of mybatis itself, which can automatically extract and analyze running sql, customize a set of default analysis rules, and let sql in the development environment When executing with the test environment, you can make a preliminary evaluation and expose the problematic slow sql at this stage; at the same time, it has the sql replacement function. When there is a problem sql online, you can quickly complete a sql through ducc ​​configuration. Online replacement greatly reduces the stop loss time for online problems.

Research on Open Source Solutions

At present, the core functions of the mainstream SQL analysis components are mainly placed in two directions: 1. Slow SQL analysis and optimization suggestions; Integrate with application code. It is also impossible to realize our core pain points, slow sql early analysis warning and dynamic sql replacement.

Design

Core functions: SQL analysis and early warning capabilities, SQL replacement capabilities

detailed design

Mainly divided into 8 functional modules

Module 1: core is mainly responsible for the connection of components to mybatis, and the arrangement and calling of other modules

Module 2: config is mainly responsible for the initialization of component configuration information

Module 3: extrat mainly extracts the complete sql to be executed by parsing mybatis related objects

Module 4: analysis mainly splices analysis statements, executes explain analysis statements and obtains analysis results

Module 5: Loading and initialization of rule sql analysis rules, support custom rules

Current default rules (continuous expansion):

1. Query unmatched index

2. The filtering effect of matching index is poor

3. Too many rows returned

4. File sorting is used

Module 6: score matches and scores based on the analysis results and configured scoring rules, and optimizes the assembly of suggestions

Module 7: out output module, output the output results, currently there are two output methods: error log and MQ

Module 8: replace module, which can dynamically replace SQL statements based on ducc ​​configuration

Instructions

1. Introduce dependent jar packages

<dependency>
    <groupId>com.jd.sql.analysis</groupId>
    <artifactId>sql-analysis</artifactId>
    <version>1.2-SNAPSHOT</version>
</dependency>

2. Configuration component xml

<configuration>
    <plugins>
        <plugin interceptor="com.jd.sql.analysis.core.SqlAnalysisAspect" >
            <!-- 开启sql分析功能最简配置 -->
            <property name="analysisSwitch" value="true"/>

            <!-- 开启sql替换功能最简配置 -->
            <property name="sqlReplaceModelSwitch" value="true"/>
            <property name="duccAppName" value="workbench-backend"/>
            <property name="duccUri" value="ucc://workbench-backend:[email protected]/v1/namespace/workbench_backend/config/default/profiles/test?longPolling=60000&necessary=false"/>
            <property name="duccMonitorKey" value="refundBugFlag"/>
        </plugin>
    </plugins>
</configuration>

3. Core configuration items

Attributes use Is it required? Defaults Remark
analysisSwitch Whether to enable the analysis function yes false  
onlyCheckOnce Whether to analyze a sqlid only once No true  
checkInterval Analysis interval per sqlid No 300000 milliseconds onlyCheckOnce is false to take effect
exceptSqlIds Ssqlid that needs to be filtered and not analyzed No    
sqlType Analyzed sql type No Default select, update support
scoreRuleLoadClass Scoring rule loader for extending custom rules No    
outModel Default output method No Default: LOG Support LOG, MQ two ways
outputClass Scoring result output class, used to extend custom result output methods No    
sqlReplaceModelSwitch Whether the sql replacement module is enabled No default false  
duccAppName Application name of ducc ​​configuration (jdos) No    
to lead ducc uri configuration No    
duccMonitorKey SQL replaces the key corresponding to the configuration file No    

4. Default analysis effect display

4.1, slow sql analysis effect

4.2, sql dynamic replacement effect

5. Practical usage plan

5.1, slow sql analysis - log output + keyword alarm

<configuration>
    <plugins>
        <plugin interceptor="com.jd.sql.analysis.core.SqlAnalysisAspect" >
            <property name="analysisSwitch" value="true"/>
        </plugin>
    </plugins>
</configuration>

5.2, slow sql analysis - log output + mq output + es storage + Kibana analysis

<configuration>
    <plugins>
        <plugin interceptor="com.jd.sql.analysis.core.SqlAnalysisAspect" >
            <property name="appName" value="workbench-backend"/>
            <property name="analysisSwitch" value="true"/>
            <property name="outputModel" value="mq"/>
            <property name="mqApp" value="qlstation"/>
            <property name="mqUser" value="qlstation"/>
            <property name="mqPassword" value="D1BCC547"/>
            <property name="mqAddress" value="jmq-testcluster.jd.local:50088"/>
            <property name="mqTopic" value="jdl_kds_key_node_log"/>
        </plugin>
    </plugins>
</configuration>

final effect

5.3, slow sql replacement -ducc configuration dynamic update sql statement

<configuration>
    <plugins>
        <plugin interceptor="com.jd.sql.analysis.core.SqlAnalysisAspect" >
            <property name="sqlReplaceModelSwitch" value="true"/>
            <property name="duccAppName" value="workbench-backend"/>
            <property name="duccUri" value="ucc://workbench-backend:[email protected]/v1/namespace/workbench_backend/config/default/profiles/test?longPolling=60000&necessary=false"/>
            <property name="duccMonitorKey" value="sqlReplaceConfig"/>
        </plugin>
    </plugins>
</configuration>

found slow sql

ducc configuration

Online sql is dynamically replaced

Note: After the function is officially repaired, this configuration needs to be removed. This function is only for urgent online problems, and it is not recommended to be used as a function for a long time

Performance Testing

In the test environment, thousands of ordinary SQL queries were performed, and 5 tests were performed for each scenario

Time spent without plugin enabled: 11108ms, 10237ms, 9482ms, 7938ms, 8196ms

Time-consuming to enable sql analysis: 16619ms, 17333ms, 16321ms, 19057ms, 18164ms

Actual configuration, only the first execution or interval execution, a single impact of about 10ms)

Time-consuming to enable sql replacement: 10642ms, 8803ms, 8353ms, 8830ms, 9170ms

basically no effect

Applicable scene

1. Slow SQL prevention

2. Stop loss for online problems

Advantage

1. Core advantage: Analyzing sql during execution, which is different from the traditional time-consuming evaluation of slow sql relying on sql execution, and pre-analyzing directly based on syntax and indexes, which can not only prevent some bad sql from being found to be slow sql after going online, but also Can give sql optimization suggestions, which can avoid slow sql online to the greatest extent. Support dynamic replacement of online sql, which can quickly stop loss of online problems.

2. Performance: Based on performance and different usage scenarios, customized configuration is supported, whether each SQL is checked only once, or configured at a certain time interval. SQL replacement is almost lossless.

3. Expansion: Based on the expansion of subsequent sql scoring rules and the consideration of outputting analysis results in different ways, it supports custom extensions of scoring rules and output methods.

4. Cost: low access cost, no code intrusion.

Author: JD Logistics Hu Haitao

Source: JD Cloud Developer Community Ziyuanqishuo Tech

Graduates of the National People’s University stole the information of all students in the school to build a beauty scoring website, and have been criminally detained. The new Windows version of QQ based on the NT architecture is officially released. The United States will restrict China’s use of Amazon, Microsoft and other cloud services that provide training AI models . Open source projects announced to stop function development LeaferJS , the highest-paid technical position in 2023, released: Visual Studio Code 1.80, an open source and powerful 2D graphics library , supports terminal image functions . The number of Threads registrations has exceeded 30 million. "Change" deepin adopts Asahi Linux to adapt to Apple M1 database ranking in July: Oracle surges, opening up the score again
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10086660
Recommended