Situation:
I am trying to write a efficient query using "LIKE" statement to look after a piece of text into a column with short texts.
Model:
Table 'EVENTSGENERAL' : { ID (KEY), GENERATOR_ (FK), DATETIME, COMPUTERNAME, OSLOGIN, DBLOGIN, INFOTYPE, INFO }
Table 'EVENTSGENERATORS' : { ID (KEY), GENERATOR_ (FK), SHORTNAME, LONGNAME }
Table 'EVENTSINFOTYPES' : { ID (KEY), GENERATOR_ (FK), VERSION_, INFOTYPE, DESCRIPTION }
Indexes : EVENTSGENERAL.GENERATOR, EVENTSGENERAL.DATETIME, EVENTSINFOTYPES.INFOTYPE
All Ascending.
My Query:
SELECT FIRST @first SKIP @skip A.ID,B.LONGNAME, A.DATETIME, A.COMPUTERNAME,A.OSLOGIN, A.DBLOGIN, C.DESCRIPTION, A.INFO
FROM EVENTSGENERAL A JOIN EVENTSGENERATORS B ON B.GENERATOR_ = A.GENERATOR_
JOIN EVENTSINFOTYPES C ON C.GENERATOR_ = A.GENERATOR_ AND C.INFOTYPE = A.INFOTYPE
WHERE C.DESCRIPTION LIKE '%VALUE%'
Problem:
This query will run on very large databases. Is there any way I can improve it? I am using a Firebird database.
No, unfortunately not, because you use wildcards (
%) on both sides of theLIKEkeyword. The leading (beginning) wildcard means that no index can be used to help improve the search speed, and therefore every row has to be examined to see if it meets the criteria.You can speed things up somewhat by changing to
LIKE 'VALUE%'instead; an index can at least be used to limit the rows being searched to those starting withVALUE.