简体   繁体   English

我应该如何在SQL Server 2005中使用BIT

[英]How I should use BIT in SQL Server 2005

Regarding to SQL performance. 关于SQL性能。

I have a scalar valued function for checking some specific condition in base, it returns BIT value for True or False. 我有一个标量值函数用于检查base中的某些特定条件,它返回BIT值为True或False。

I now do not know how I should fill @BIT parameter 我现在不知道如何填写@BIT参数

If I write. 如果我写。

set @bit = convert(bit,1)

or 要么

set @bit = 1

or 要么

set @bit='true'

function will work anyway but I do not know which method is recommended for daily use. 函数无论如何都会工作,但我不知道建议日常使用哪种方法。

Another question, I have table in my base with around 4 million records, daily insert is about 4K records in that table. 另一个问题,我在我的基地有大约400万条记录,每日插入大约是该表中的4K记录。

Now I want to add CONSTRAINT on that table with scalar valued function that I mentioned already 现在我想在该表上添加带有标量值函数的CONSTRAINT,我已经提到过了

Something like this 像这样的东西

ALTER TABLE fin_stavke
ADD CONSTRAINT fin_stavke_knjizenje CHECK ( dbo.fn_ado_chk_fin(id)=convert(bit,1))

where "id" is the primary key of table fin_stavke and dbo.fn_ado_chk_fin looks like 其中“id”是表fin_stavke的主键,dbo.fn_ado_chk_fin看起来像

create FUNCTION fn_ado_chk_fin
(
    @stavka_id int
)
RETURNS bit
AS
BEGIN
declare @bit bit

if exists (select * from fin_stavke where id=@stavka_id and doc_id is null and protocol_id is null)
        begin
            set @bit=0


        end
    else
        begin
            set @bit=1
        end
return @bit;
END
GO

Will this type and method of checking constraint will affect badly performance on my table and SQL at all ? 这种类型和检查约束的方法会不会影响我的表和SQL的性能?

If there is also better way to add control on this table please let me know. 如果还有更好的方法来增加对此表的控制,请告诉我。

I might be wrong but from the looks of it, it seems you only want to check that not both doc_id and protocol_id are NULL ? 我可能错了,但从它的外观来看,似乎你只想检查doc_idprotocol_id都不是NULL

You can add a table constraint to achieve this. 您可以添加表约束来实现此目的。

ALTER TABLE fin_stavke
ADD CONSTRAINT fin_stavke_knjizenje CHECK ( doc_id IS NOT NULL OR protocol_id IS NOT NULL)

I'd use 我用了

 set @bit = 1

It's safe (assigning true to a bit feels wrong) and convert just seems pointless. 这是安全的(分配真的有点感觉不对)转换似乎毫无意义。

I've always seen bit used as 1 or 0. I'd stick with that. 我总是看到比特用作1或0.我坚持认为。 Everyone will know what you're doing. 每个人都会知道你在做什么。

That constraint is going to affect the performance of your inserts but not by much since you're be seeking to the primary key of the table. 这个约束会影响插入的性能,但是因为你正在寻找表的主键。 It's probably the cheapest lookup you can do. 它可能是您可以做的最便宜的查找。

A bit variable can be set with an integer value: 可以使用整数值设置位变量:

set @bit = 1

Sometimes you want an actual bit value to avoid the implicit conversion, then you can do an excplicit conversion: 有时你想要一个实际的比特值来避免隐式转换,然后你可以进行一个明确的转换:

set @bit = cast(1 as bit)

When assigning it to a variable there is no practical difference, but in a query the conversion would happen when the query is parsed rather than when it's executed. 将其分配给变量时没有实际差异,但在查询中,转换将在解析查询时发生,而不是在执行时发生。 I have used it a few times and got actual performance differences. 我已经使用了几次并且得到了实际的性能差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM