Why doesn't Firebird use index in my NOT something query?


It because indexes in Firebird are used to locate records that meet the criteria. When you specify that you want records that do not meet the criteria the index is useless: it would find you a record you don't want and then fetch all the other records using NATURAL table scan. Therefore it is much better to use full table scan at once.

Perhaps you have a situation where you only have a few records that do not meet the criteria, or only a few of them that do, but Firebird doesn't know that - it just knows the index selecivity and the query you supply. So, in short, the following would use an index:

where x = 1
where x > 10
where x IS NULL
where x between 1 and 10

And the following won't:

where x <> 1
where x not > 10
where x IS NOT NULL
where x NOT BETWEEN 1 and 10

As you can see, NOT expressions with 'less than' and 'greater than' operators could be rewritten to use index. Instead of:

where x not > 10

write:

where x <= 10



Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.

If you are a commercial tool maker and your tool features a great way to handle the issue written about in this FAQ, please check out our advertisement page.



All contents are copyright © 2007-2017 FirebirdFAQ.org unless otherwise stated in the text.


Links   Firebird   News   FlameRobin   Home Inventory powered by FB  
Add content   Advertise   About  

Categories
 Newbies
 SQL
 Installation and setup
 Backup and restore
 Performance
 Security
 Connectivity and API
 HOWTOs
 Errors and error codes
 Miscellaneous