Use like or instr in Oracle

Source: Internet
Author: User
Instr and like are all functions implemented by Oracle. In strict terms, instr is an internal function and like is an SQL standard, which is highly efficient. However, I do not know how to implement them yet. But both

Instr and like are all functions implemented by Oracle. In strict terms, instr is an internal function and like is an SQL standard, which is highly efficient. However, I do not know how to implement them yet. But both

I saw an article: Using instr in the Oracle database instead of like (see). I am very skeptical after seeing it. The following is a simple test I did to compare and verify the original article.

1. before creating an index, SQL> select count (1) from t; COUNT (1) ---------- 11905920 Elapsed: 00:00:11. 38SQL> select count (1) from t where instr (object_name, 'A')> 0; COUNT (1) ---------- 3947520 Elapsed: 00:00:10. 46SQL> select count (1) from t where object_name like '% A %'; COUNT (1) ---------- 3947520 Elapsed: 00:00:12. 31SQL> select count (1) from t where instr (object_name, 'A') = 0; COUNT (1) ---------- 7958400 Elapsed: 00:00:10. 39SQL> select count (1) from t where object_name not like '% A %'; COUNT (1) ---------- 7958400 Elapsed: 00:00:10. 94

The above results show that the efficiency of instr and like is similar when no index is created, and the efficiency of instr is slightly higher, but it is not a huge difference mentioned in this article.

2. after an index is created, SQL> create Index t_ I on t (object_name); index created. elapsed: 00:02:08. 92SQL> select count (1) from t; COUNT (1) ---------- 11905920 Elapsed: 00:00:11. 07SQL> select count (1) from t where instr (object_name, 'A')> 0; COUNT (1) ---------- 3947520 Elapsed: 00:00:12. 04SQL> select count (1) from t where object_name like '% A %'; COUNT (1) ---------- 3947520 Elapsed: 00:00:07. 33SQL> select count (1) from t where instr (object_name, 'A') = 0; COUNT (1) ---------- 7958400 Elapsed: 00:00:11. 57SQL> select count (1) from t where object_name not like '% A %'; COUNT (1) ---------- 7958400 Elapsed: 00:00:06. 47

From the test above, we can see that after adding an index, like is more efficient than instr, and takes half of instr time, which can be described as a huge difference.

Summary

Instr and like are all functions implemented by Oracle. In strict terms, instr is an internal function and like is an SQL standard, which is highly efficient. However, I do not know how to implement them yet. However, the performance difference between the two depends on the specific database environment, such as the table structure, data distribution, index, database version, and current load of the database. In addition, high efficiency also comes at the cost of database and server resource consumption. When the time is the same order of magnitude or the user permits, how to control the efficiency is the key.

From this, we can see that database performance optimization is an art that requires continuous learning and practice. In this way, we just declare the journey of database performance optimization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.