当前位置:网站首页>Several optimization scenarios using like fuzzy retrieval in SQL
Several optimization scenarios using like fuzzy retrieval in SQL
2022-06-25 23:09:00 【bisal】
SQL We often encounter the use of LIKE The scene of fuzzy retrieval ,'%' The location of , It may affect the normal use of the index , I saw an article in Teacher Liu's official account , The transformation strategies of relevant scenes are introduced , Very useful , Recommended reading .
The test table t1,object_name Column creation index ,
create table t1 as select * from dba_objects;
create index idx_t1_01 on t1(object_name);Scene one ,'%' After
select object_name from t1 where object_name like 'BISAL%'; Obviously, you can use indexes , But be careful , In this case ,'%' The shorter the first string , The less selective the index is ,

Scene two ,'%' before
select object_name from t1 where object_name like '%BISAL' Because this index is based on object_name The positive order of columns is organized in an index , Fuzzy search of header , Cannot locate data directly through the index , Just because the search column only has object_name, So the index fast full scan is used , Actually, it is all the leaf nodes scanned ,

To make it clear , We search for object_id Column , He is not in the index ,
select object_id from t1 where object_name like '%BISAL'; therefore ,Oracle Lower cost full table scan selected ,

As a comparison , We are in accordance with the 'BISAL%', The scenario test that can use the index ,
select object_id from t1 where object_name like 'BISAL%';You can see , Index range scanning is used , obtain rowid, Go back to the table to get specific data , There is no need to scan the entire index or the entire table ,

Let's pull it back , If you have to '%BISAL' retrieval , You can create a object_name Reverse index of columns ,
create index idx_t1_02 on t1(reverse(object_name)); In the query statement LIKE The right value of is also used reverse function ,
select object_name from t1 where reverse(object_name) like reverse('%BISAL');here ,'%BISAL' Index is used , Careful friends may find that the implementation plan is slightly different from the above , There are many operations for returning tables , The reason is that the index is based on reverse(object_name) The organization's , But the search is object_name, Therefore, the table should be returned according to the index ,

Scene three , Before and after '%'
for example '%BISAL%', Can I use an index ?
There are three situations ,
(1) ABC Always appears at a fixed position starting with a string , You can create functional indexes to optimize .
(2) ABC Always appear at the end of a string , You can create a function composite index to optimize .
(3) ABC The position in the string is not fixed , It can be done by rewriting SQL To optimize .
Case one ,ABC Always appears at a fixed position starting with a string .
Can pass substr Function to intercept a string , Create function indexes .
for example ,BISAL From the fifth bit of the string , Create function indexes ,
create index idx_t1_03 on t1(substr(object_name, 5, 30));The implementation is as follows SQL, It is equivalent to intercepting from the fifth bit of the original string every time ,
select object_name from t1 where substr(object_name, 5, 30) like 'BISAL%'; You can use indexes ,

The second case ,ABC Always appear at the end of a string , You can create a function composite index to optimize .
It is equivalent to the need to intercept strings in reverse order , Can pass reverse and substr Composite function index , for example BISAL From the last five digits of the string , Create function indexes ,
create index idx_t1_04 on t1(reverse(substr(object_name, 1, length(object_name)-4))); When searching , Need to use reverse and substr Function combination ,like Right value '%BISAL', Can be realized ‘%BISAL%’ Search function ,
select object_name from t1 where reverse(substr(object_name, 1, length(object_name)-4)) like reverse ('%BISAL');The third case ,ABC The position in the string is not fixed , It can be done by rewriting SQL To optimize .
This needs to be rewritten , hypothesis object_name There is an index , The requirements are as follows ,
select object_name from t1 where object_name like '%BISAL%';We rewrite it as , Through a subquery , And conditions object_name relation ,
select object_name from t1 where object_name in (select object_name from t1 where object_name like '%BISAL%'); The execution plan at this time , As shown below , A combination of index fast full scan and index range scan ,

Even if we search object_id This field that is not in the index ,
select object_id from t1 where object_name in (select object_name from t1 where object_name like '%BISAL%');It also avoids full table scanning , Although we still need a quick full scan of the index , But at least the cost of scanning is reduced (1/N( The ratio of the number of index blocks to the number of data blocks )), On this alone , The bigger the watch , The more obvious the effect is . But this kind of IN rewrite , If the subquery returns a small number of records , Execution efficiency may be improved N times , But if more , Rewriting efficiency , It may not be much different from before ,

Although the above '%' There are various transformation schemes , But at least some of them have to be rewritten SQL, So I suggest , From the demand level , Be sure to use LIKE Is the scene of fuzzy retrieval reasonable , Whether his non functional indicators meet the requirements , Don't come up and change , Plan before you act , You may get twice the result with half the effort .
Recently updated articles :
《 Short track speed skating and speed skating 》
《InnoDB perform delete What did you do ?》
《 Recent problems 》
《 clone PDB Database operation 》
Article classification and indexing :
边栏推荐
- Unity technical manual - particle foundation main module attributes - upper
- As a programmer, how can we learn, grow and progress happily? (personal perception has nothing to do with technology)
- zabbix_server配置文件详解
- Interview shock 23: talk about thread life cycle and transformation process?
- The applet draws a simple pie chart
- Basic concepts of processor scheduling
- [invitation letter] on March 4, the platform enabled digital intelligence Innovation -- UFIDA BiP PAAS cloud platform IUAP digital intelligence hundred cities forum · Jinan Station
- 2022-2028 global vacuum jacket system industry survey and trend analysis report
- 字符串变形(字符串大小写切换和变现)
- 2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
猜你喜欢

Wpewebkit debugging MSE playback

Network security project questions of the first Henan vocational skills competition in 2022

多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%...

Fastjson deserialization randomness failed

【EOSIO】EOS/WAX签名错误 is_canonical( c ): signature is not canonical 问题

2022 love analysis · panoramic report of it operation and maintenance manufacturers

ES6 - numerical extension and object extension

1281_FreeRTOS_vTaskDelayUntil实现分析

OSPF - detailed explanation of GRE tunnel (including configuration command)

Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
随机推荐
Why absolute positioning overlaps
Unity技术手册 - 粒子基础主模块属性-上
记|一次exists关键字的学习记录
adb常用命令
腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级
Some points to pay attention to when closing mongodb services (as well as related commands when opening)
Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系
oracle -- 表操作
万亿热钱砸向太空经济,真的是一门好生意?
Oracle - data query
Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)
Unity technical manual - particle emission and life cycle velocity sub module
How to use JMeter for interface testing
The wisdom of questioning? How to ask questions?
2022-2028 global open source cloud storage industry research and trend analysis report
Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
Unity technical manual - getKey and getaxis and getbutton
22 years of a doctor in Huawei
2022-2028 global SiC igniter industry research and trend analysis report