BIT类型是否适合建索引
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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器