Oracle fuzzy query (3.2 to improve the efficiency of fuzzy query from the perspective of using functions and sql syntax 2) The performance comparison between Like and Instr fuzzy query in ORACLE

Source: http://blog.csdn.net/haiross/article/details/12974851
Comments:

The performance of Like and Instr fuzzy query in ORACLE is compared to the fuzzy query performance

of Like and Instr in ORACLE





instr(title, 'manual')> 0 is equivalent to title like '%manual%'



instr(title,'manual')=1 is equivalent to title like 'manual%'



instr(title,'manual')=0 is equivalent to title not like '%manual%'



t table There are nearly 11 million data in the database. Many times, we want to perform string matching. In SQL statements, we usually use like to achieve our search goals. But after the actual test, it is found that the efficiency of like is quite different from that of the instr function. Here are some test results:

SQL> set timing on
SQL> select count(*) from t where instr(title,'manual')>0;

  COUNT(*)
----------
     65881

Elapsed: 00 :00:11.04
SQL> select count(*) from t where title like '%Manual%';






SQL> select count(*) from t where instr(title,'manual')=0;

  COUNT(*)
----------
  11554580

Elapsed: 00:00:11.31
SQL> select count(*) from t where title not like '%Manual%';

  COUNT(*)
----------
  11554580

In addition, I am building another table with more than 200 million, using 8 parallel, using like query for a long time There is no result, but using instr, the search is completed in 4 minutes, and the performance is quite good. These little tricks can be used well, and the work efficiency will be improved a lot. The above test shows that some of the built-in functions of ORACLE have been optimized to a considerable extent.



instr(title,'aaa')>0 is equivalent to like

instr(title,'aaa')=0 is equivalent to not like



special usage:



select id, name from users where instr('101914, 104703', id) > 0;
  It is equivalent to
select id, name from users where id = 101914 or id = 104703;










1.select * from tb where name like '%XX%';
2.select * from tb where instr(name,'XX')>0;

If there is no index on the name field, the efficiency of the two is similar, and there is basically no difference .

In order to improve efficiency, we can add a non-unique index to the name field:
create index idx_tb_name on tb(name); In

this way, use a statement like

select * from tb where instr(name,'XX')>0;

to query , the efficiency can be improved a lot, and the greater the amount of table data, the greater the difference between the two. However, it is also necessary to take into account the effect that the DML statement will reorder the index data after the name field is added to the index.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327006716&siteId=291194637