all groups > sql server programming > september 2004 >
You're in the

sql server programming

group:

Create Index on BIT column?


Re: Create Index on BIT column? Adam Machanic
9/12/2004 5:41:39 PM
sql server programming:
Yes, you will have to create it through Query Analyzer. However, you should
note that indexes on BIT columns are not generally very useful unless you
have an extremely high percentage of either 1 or 0, and you happen to be
searching for the value that has the lower percentage. For instance, if 99%
of the rows are 1 and you're searching for 0, the index may be useful; if
the percentage goes much higher, the index will not be used. I've read
figures from 1% to 5%, but have not verified myself. Test thoroughly to
make sure the index is not blocking other operations.


[quoted text, click to view]

Re: Create Index on BIT column? Adam Machanic
9/12/2004 7:36:23 PM

[quoted text, click to view]

Thanks for the great tip, Erland. I've never even thought about that
before.

Create Index on BIT column? Lucas Tam
9/12/2004 9:23:19 PM
I read on Google Groups that you can create an index on a BIT column in SQL
Server 2000.

I don't have the option to create a BIT column in Enterprise Manager...

Do I have to create BIT column indexs through Query Analyzer?

Thanks.

--
Lucas Tam (REMOVEnntp@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
Re: Create Index on BIT column? Steve Kass
9/12/2004 9:26:50 PM
Erland,

While your suggestion is best, since it leaves less up to the
processor, another option is to write

WHERE bitcol = 0x01 -- where the literal is any nonzero value
or
WHERE bitcol = 0x

depending on which value you seek. Binary has the lowest precedence of
any data type, so the column will not be converted, and the index will
be available.

Steve Kass
Drew University

[quoted text, click to view]
Re: Create Index on BIT column? David Portas
9/12/2004 10:44:28 PM
Certainly you can create an index on a BIT column. Use the CREATE INDEX
statement (in Query Analyzer rather than EM). A single-column index on a bit
column is next to useless because of the obvious low cardinality but maybe
it makes sense as part of a compound key.

--
David Portas
SQL Server MVP
--

Re: Create Index on BIT column? Erland Sommarskog
9/12/2004 10:49:01 PM
Lucas Tam (REMOVEnntp@rogers.com) writes:
[quoted text, click to view]

That's correct.

[quoted text, click to view]

I have no idea what EM is able to or not. I only use Query Analyzer for
such tasks. And, as an advice, it pays off in the long run.

Anyway, to add to David's and Adam's posts, there is a gotcha with
index on bit columns. If you say:

SELECT * FROM tbl WHERE bitcol = 1

SQL Server will not use the index, even if there are just a handful of
rows with bitcol = 1. (With a 50/50 split, the index would be useless
anyway.) However, this works:

SELECT * FROM tbl WHERE bitcol = convert(bit, 1)

This is because in the first example there is an implicit conversion going
in the wrong direction for the index to be available.




--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
Re: Create Index on BIT column? Lucas Tam
9/13/2004 5:03:57 AM
Erland Sommarskog <esquel@sommarskog.se> wrote in
news:Xns956380B7C91EYazorman@127.0.0.1:

[quoted text, click to view]

Thanks for the info...

I didn't realize I needed to convert the cast the bit column explictly.

When you said there is an implicit conversation... what can the SQL
processor convert to besides 1 (or a non-zero value)???

--
Lucas Tam (REMOVEnntp@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
Re: Create Index on BIT column? Uri Dimant
9/13/2004 8:12:37 AM
Hi,Steve
I did this test on my workstation and it is still using table scan in both
cases or I don't understand something
select top 20000 o.orderid, cast (1 as bit)as d_bit into table_test from
orders o
cross join orders
go
select * from table_test
go
set rowcount 10000
update table_test set d_bit=0
set rowcount 0
go
create index dd on table_test (d_bit)
go
sp_helpindex table_test
go
DBCC DROPCLEANBUFFERS
go
select * from table_test where d_bit=convert(bit,1)
go
drop table table_test

[quoted text, click to view]

Re: Create Index on BIT column? Hugo Kornelis
9/13/2004 11:17:43 AM
[quoted text, click to view]

Hi Uri,

That's because the index statistics indicate (correctly) that
approximately half the rows in table_test would match - a table scan makes
sense in this case.

If you change the set rowcount 10000 to set rowcount 19990 (so that only
ten rows match the where clause), you'll see SQL Server switch to an index
seek + bookmark lookup.

Best, Hugo
--

Re: Create Index on BIT column? Uri Dimant
9/13/2004 12:36:17 PM
Hugo
Thanks, I have already got it.


[quoted text, click to view]

Re: Create Index on BIT column? Erland Sommarskog
9/13/2004 10:23:43 PM
Lucas Tam (REMOVEnntp@rogers.com) writes:
[quoted text, click to view]

Not should what you mean, but look up Datatypes in the T-SQL Reference
of Books Online, and particularly check Datatype Precedence.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
AddThis Social Bookmark Button