SQL Performance – Are Bools/Bits Faster Than Integers?

performancesql

As the title suggests I'm wondering whether bools or bits are faster than using integers (1 and 0)? Specifically I thinking about their speed in CASE statements, even more specifically in T-SQL (although it would be interesting to hear about experiences from other languages).

For some context, I have a pretty big SELECT statement over a large table which has to transform a lot of data based on integer being either 1 or 0, and was considering converting the integers to bits for a performance boost.

EDIT:

Thank you all for your responses. It kind of seemed like you were all giving me pretty much the same advice worded slightly differently or with different levels of detail.

Best Answer

One could go and make wild speculations here, or even educated guesses, but the truth is that it probably doesn't matter, and if it does, it's going to depend on a lot of other factors. The only way to find out is to profile, and when you do, first be sure to rule out other factors (system load, fragmentation, etc.).

My bet would be that the difference is going to be negligible, and that there's much more to win elsewhere - set up sensible indexes, profile joins vs. subqueries, use caching where possible, experiment with many small queries vs. few large queries, avoid unnecessary trips to the database, buffer accumulated results, etc.

The thing is, an integer vs. bool optimization will never give you more than a linear speed increase, and the increase won't be large - if you get 5%, consider yourself lucky. Those 5% will get smashed if your query does an outer join requiring full scans on both joined tables when one table is really large and you add one row to the other.