1.B
General READ SQL optimization for DB2 and Oracle
These techniques apply to both DB2 and Oracle, however, the rules appear to yield a better response time on DB2.
- Avoid using wildcard (%) at the beginning of a predicate.
The predicate LIKE '%abc' causes full table scan. For example:
SELECT * FROM TABLE1 WHERE COL1 LIKE '%ABC'
This is a known performance limitation in all databases.
select *
from dbo.v_R_System
where NETBIOS_NAME0 = 'COMPUTER'
select *
from dbo.v_R_System
where NETBIOS_NAME0 LIKE 'COMPUTER'
select *
from dbo.v_R_System
where NETBIOS_NAME0 LIKE 'COMPUT%'
select *
from dbo.v_R_System
where NETBIOS_NAME0 LIKE '%COMPUT%'
When you hit CTRL+L to get the execution plan, for each of these, and mouse-over the operator on the far left, you'll see the following:
Notice, the first query uses = instead of LIKE. It's got the lowest cost and uses an INDEX SEEK. Very fast and efficient..What we would expect.
The second query uses LIKE, and no wildcards. The cost is still pretty low and it uses an INDEX SEEK.
The third query uses LIKE, and a wildcard, but doesn't START with a wildcard. The cost is the same as the second and still uses INDEX SEEK.
The fourth query however, uses LIKE and starts with a wildcard. It still uses an index, but the cost is higher and it's an INDEX SCAN so it's going to be less efficient and slower.