当前位置:网站首页>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 :
边栏推荐
- Fastjson deserialization randomness failed
- Development trend of China's power carrier communication industry and Research Report on the 14th five year plan 2022 ~ 2028
- How to use the find command
- Some points to pay attention to when closing mongodb services (as well as related commands when opening)
- C language and the creation and use of database
- Analysis report on demand and investment forecast of global and Chinese flame retardant hydraulic oil market from 2022 to 2028
- 2022-2028 global industrial TFT LCD industry survey and trend analysis report
- Oracle - data query
- Unity technical manual - getKey and getaxis and getbutton
- Which PHP open source works deserve attention
猜你喜欢

民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系

2022爱分析· IT运维厂商全景报告

2022-2028 global industrial TFT LCD industry survey and trend analysis report
![Lecture 14 of the Blue Bridge Cup -- number theory [exercises]](/img/96/0971909c8bf25820c2d4f520bb83fb.jpg)
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]

2022-2028 global iridium electrode industry research and trend analysis report

荣耀推出积分商城,支持兑换各种荣耀产品

Use of local stack in flask

Why is BeanUtils not recommended?

Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)

建立自己的网站(15)
随机推荐
oracle -- 表操作
2022-2028 global TFT LCD touch screen industry research and trend analysis report
[eosio] eos/wax signature error is_ Canonical (c): signature is not canonical
一位博士在华为的22年
Flex & Bison 开始
NRM source switching tool
Thinking while walking
Flex & Bison 開始
Why can't the mobile phone be used and the computer be connected
ES6 learning -- let
Flex & Bison Start
Common MySQL database functions and queries
MySQL数据库索引
万亿热钱砸向太空经济,真的是一门好生意?
2022-2028 global vacuum jacket system industry survey and trend analysis report
剑指 Offer 46. 把数字翻译成字符串(DP)
1281_FreeRTOS_vTaskDelayUntil实现分析
Unity的Ping类使用
2、一个向量乘它的转置,其几何意义是什么?
为什么OpenCV计算的帧率是错误的?