//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
      Access DB & ADO
      General SQL Server & Access Articles
      MySQL
      Other Articles
      Schema Tutorials
      Sql Server 2000
      Sql Server 2005
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
sqlserver2000.databases.aspfaq.com

ASP FAQ Tutorials :: Databases :: Sql Server 2000 :: Can I create an index on a BIT column?


Can I create an index on a BIT column?

Try this. In Enterprise Manager, right-click the Tables view, and select New Table. Create a BIT column named "MyBitColumn", and then click on the "Manage Indexes / Keys..." button to create a new index: 
 
 
 
When you do this, you are offered a "New" button. Click it, and under "Column name" try to select the "MyBitColumn" column. It's not there! Why? Because in this interface, Enterprise Manager does not offer you the ability to create an index on a BIT column. When you try to type the name yourself, you are rewarded with an annoying barrage of error dialogs. Each time you click "No" the first dialog reappears, and every time you try to click OK on the second, the same thing happens. 
 
 
 
So, can you create an index on a BIT column? Of course. You can even still do it in Enterprise Manager. Save your table, and then right-click it in the Tables view and choose All Tasks > Manage Indexes... give your index a name, check the "MyBitColumn" column, and hit OK. Voila! 
 
 
 
Another way is to create the index using T-SQL, which is the preferred way of generating DDL anyway: 
 
CREATE TABLE dbo.blat 

    MyBitColumn BIT 

GO 
 
CREATE INDEX MyBitIndex ON dbo.blat(MyBitColumn) 
GO
 
Now, the question is, do you really WANT an index on a BIT column? We're going to run some experiments, but in general, it is highly unlikely that you will get much use out of such an index. The exception is when the data is heavily weighted towards, say, 1 (e.g. 95-99% of the table), and you are searching for 0. Or vice-versa. 
 
What I'd like to test is the effect on execution time and the execution plan if you run different queries against large tables with a BIT column whose values are evenly distributed, or weighted heavily (in this case 97% - 3%), and in both cases, compare clustered to nonclustered to no index. 
 
So, let's create six different tables in their own database: 
 
CREATE DATABASE Splunge 
GO 
 
USE Splunge 
GO 
 
-- 50/50, no index 
CREATE TABLE dbo.Test1 

    myBit BIT NOT NULL 

 
-- 50/50, nonclustered index 
CREATE TABLE dbo.Test2 

    myBit BIT NOT NULL 

 
CREATE INDEX bitIndex ON dbo.Test2(myBit) 
 
-- 50/50, clustered index 
CREATE TABLE dbo.Test3 

    myBit BIT NOT NULL 

 
CREATE CLUSTERED INDEX bitIndex ON dbo.Test3(myBit) 
 
-- 97/3, no index 
CREATE TABLE dbo.Test4 

    myBit BIT NOT NULL 

 
-- 97/3, nonclustered index 
CREATE TABLE dbo.Test5 

    myBit BIT NOT NULL 

 
CREATE INDEX bitIndex ON dbo.Test5(myBit) 
 
-- 97/3, clustered index 
CREATE TABLE dbo.Test6 

    myBit BIT NOT NULL 

 
CREATE CLUSTERED INDEX bitIndex ON dbo.Test6(myBit)
 
And let's populate each with 100,000 rows, the first three will have 50,000 of each value (0,1), and the second three will have 97,000 0's and 3,000 1's. 
 
DECLARE 
    @i INT, 
    @ff BIT, -- 50/50 flag 
    @nf BIT -- 97/3 flag 
 
SELECT 
    @i = 1, 
    @ff = 0, 
    @nf = 0 
 
WHILE @i <= 100000 
BEGIN 
    IF @i > 50000 
        SET @ff = 1 
 
    IF @i > 97000 
        SET @nf = 1 
 
    INSERT dbo.Test1(myBit) SELECT @ff 
    INSERT dbo.Test2(myBit) SELECT @ff 
    INSERT dbo.Test3(myBit) SELECT @ff 
 
    INSERT dbo.Test4(myBit) SELECT @nf 
    INSERT dbo.Test5(myBit) SELECT @nf 
    INSERT dbo.Test6(myBit) SELECT @nf 
 
    SET @i = @i + 1 
END
 
On my system, this took roughly seven minutes. Your mileage may vary. 
 
So now that we have the data in there, let's run the following sets of queries. 
 
SELECT COUNT(*) FROM dbo.Test1 
SELECT COUNT(*) FROM dbo.Test2 
SELECT COUNT(*) FROM dbo.Test3 
SELECT COUNT(*) FROM dbo.Test4 
SELECT COUNT(*) FROM dbo.Test5 
SELECT COUNT(*) FROM dbo.Test6
 
SELECT COUNT(*) FROM dbo.Test1 WHERE MyBit=0 
SELECT COUNT(*) FROM dbo.Test1 WHERE MyBit=1 
SELECT COUNT(*) FROM dbo.Test2 WHERE MyBit=0 
SELECT COUNT(*) FROM dbo.Test2 WHERE MyBit=1 
SELECT COUNT(*) FROM dbo.Test3 WHERE MyBit=0 
SELECT COUNT(*) FROM dbo.Test3 WHERE MyBit=1 
SELECT COUNT(*) FROM dbo.Test4 WHERE MyBit=0 
SELECT COUNT(*) FROM dbo.Test4 WHERE MyBit=1 
SELECT COUNT(*) FROM dbo.Test5 WHERE MyBit=0 
SELECT COUNT(*) FROM dbo.Test5 WHERE MyBit=1 
SELECT COUNT(*) FROM dbo.Test6 WHERE MyBit=0 
SELECT COUNT(*) FROM dbo.Test6 WHERE MyBit=1
 
If you observe the execution plan and statistics, you will see that those with the table scan (Test1 and Test4) require the least amount of reads and percentage of work. 
 
However, these results change ever so slightly if you are performing grouping and aggregates in the same query: 
 
SELECT MyBit, COUNT(*) FROM dbo.Test1 GROUP BY MyBit 
SELECT MyBit, COUNT(*) FROM dbo.Test2 GROUP BY MyBit 
SELECT MyBit, COUNT(*) FROM dbo.Test3 GROUP BY MyBit 
SELECT MyBit, COUNT(*) FROM dbo.Test4 GROUP BY MyBit 
SELECT MyBit, COUNT(*) FROM dbo.Test5 GROUP BY MyBit 
SELECT MyBit, COUNT(*) FROM dbo.Test6 GROUP BY MyBit
 
Here we see that the clustered index has a slight edge in query cost, but slightly higher I/O cost: 
 
 
And in SQL Server 2005, the clustered index scan has an even greater advantage: 
 
 
 
So, the answer to this one is yes, you can create a clustered index on a BIT column. 
 
However, as for whether you SHOULD—as with so many other choices—it depends. 
 
Jeff Gray correctly points out that the optimizer will do a little bit better if you explicitly tell it that you are dealing with a BIT (since the engine assumes INT), e.g.: 
 
WHERE MyBit = CONVERT(BIT, 0) 
--or 
WHERE MyBit = CAST(0 AS BIT)
 
Though that can come with a trade-off as well; namely, remembering to explicitly convert values on every statement. Whereas, if you use a CHAR(1) constrained to 'T'/'F', for example, no explicit conversion is necessary for the proper index to be used. 

Now clean up, because you probably don't want this data to hang around: 
 
DROP TABLE 
    dbo.Test1, dbo.Test2, dbo.Test3, 
    dbo.Test4, dbo.Test5, dbo.Test6
 
Or, just: 
 
DROP DATABASE Splunge

Related Articles

Are there tools available for auditing changes to SQL Server data?
Can I have optional parameters to my stored procedures?
Can I implement an input mask in SQL Server?
Can I make SQL Server format dates and times for me?
Can I start IDENTITY values at a new seed?
Can SQL Server tell me which row was inserted most recently?
How can I learn more about undocumented SQL Server stored procedures?
How can I make my SQL queries case sensitive?
How do I audit changes to SQL Server data?
How do I connect to a non-default instance of SQL Server?
How do I connect to SQL Server on a port other than 1433?
How do I create a cross-tab (or "pivot") query?
How do I determine if a table exists in a SQL Server database?
How do I drop a SQL Server database?
How do I find all the available SQL Servers on my network?
How do I get a list of SQL Server tables and their row counts?
How do I get rid of Named Pipes / DBNMPNTW errors?
How do I get the correct date/time from the msdb.sysjob* tables?
How do I get the nth row in a SQL Server table?
How do I get the result of dynamic SQL into a variable?
How do I handle REPLACE() within an NTEXT column in SQL Server?
How do I hide system tables in SQL Server's Enterprise Manager?
How do I know which version of SQL Server I'm running?
How do I limit the number of rows returned in my resultset?
How do I load text or csv file data into SQL Server?
How do I manage changes in SQL Server objects?
How do I monitor SQL Server performance?
How do I prevent linked server errors?
How do I reclaim space in SQL Server?
How do I recover data from SQL Server's log files?
How do I search for special characters (e.g. %) in SQL Server?
How do I start SQL Server Agent from ASP?
How do I time my T-SQL code?
How do I upsize from Access to SQL Server?
How do I upsize my MSDE database to full-blown SQL Server 2000?
How do I use a variable in a TOP clause in SQL Server?
How do I use GETDATE() within a User-Defined Function (UDF)?
How should I store an IP address in SQL Server?
Schema: How do I find all the foreign keys in a database?
SQL Server & MSDE
What are reserved Access, ODBC and SQL Server keywords?
What are the capacities of Access, SQL Server, and MSDE?
What are the main differences between Access and SQL Server?
What do I need to know about SQL Server 2000 SP4?
Where else can I learn about SQL Server?
Where is SP4 for SQL Server 2000?
Why am I having problems with SQL Server 2000 SP3 / SP3a?
Why can't I install SQL Server on Windows Server 2003?
Why can't I install SQL Server on Windows XP?
Why can't I use LIKE '%datepart%' queries?
Why do I get "Login failed for user '\'."?
Why do I get 'object could not be found' or 'invalid object name'?
Why do I get errors about master..spt_values?
Why do I get script errors in Enterprise Manager's 'taskpad' view?
Why do I get SQLSetConnectAttr Failed errors?
Why do I have problems with views after altering the base table?
Why does EM crash when I get an error in a stored procedure?
Why does Enterprise Manager return 'Invalid cursor state'?
Why does my DELETE query not work?
Why does sp_spaceused return inaccurate values?
Why is Enterprise Manager slow at expanding my database list?
Why is my app slow after upgrading from SQL Server 7 to 2000?
Why is tempdb full, and how can I prevent this from happening?
Why should I consider using an auxiliary calendar table?
Why should I consider using an auxiliary numbers table?

 

 


Created: 9/21/2004 | Last Updated: 6/1/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (231)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...