Showing posts with label represent. Show all posts
Showing posts with label represent. Show all posts

Friday, February 10, 2012

bit mask with 300 flags

Hello,
I have 300 groups. Each user can or can not be associated with certain
group. I would like to represent group membership using corresponding flag
(on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
4....
If user is member of group of 1,2 and 3 it will be represented by number 7,
etc.
It is power of 2.With BIGINT data type I can only store up to 63 different
groups. I need to store up to 300 groups.
I could split groups into several integer fields but I was hoping to use
single VARBINARY field.
I am trying to store all flags in VARBINARY(300) field. The problem is
that when populating groups table (see below), SQL server can only perform
bitwise operations when one of the agruments is of integer type. I am trying
to loop through all 300 groups and populate them with power of 2.
I run into the same limitation (no more than 63 groups). Is this possible
to store 300 flags for each groups ina single field?
Thanks,
Igor
CREATE TABLE groups_bin
(
group_id SMALLINT ,
group_name VARCHAR(100),
group_bit_mask VARBINARY(2)
)
DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=300
WHILE @.intID<=@.intMaxID
BEGIN
INSERT INTO groups_bin(group_id,group_name,group_bit_mask)
VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
ENDYou really should normalize your data. The SQL Engine is optimized for
working with sets of data, not for manual parsing and manipulation of
individual bits in columns.
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7,
> etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different
> groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying
> to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
> Thanks,
> Igor
> CREATE TABLE groups_bin
> (
> group_id SMALLINT ,
> group_name VARCHAR(100),
> group_bit_mask VARBINARY(2)
> )
> DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
> SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=300
> WHILE @.intID<=@.intMaxID
> BEGIN
> INSERT INTO groups_bin(group_id,group_name,group_bit_mask)
> VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
> SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
> PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
> END
>
>|||"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7,
> etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different
> groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying
> to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
>
Is it really appropriate (or necessary) to enshrine the 300 groups in the
metadata?
The more usual model would be
create table Users(id int primary key, name varchar(50) not null)
create table Groups(id int primary key, name varchar(50) not null)
create table UserGroup
(
UserIDint not null references Users on delete cascade,
GroupID id not null references Groups on delete cascade,
constraint pk_UserGroup primary key (UserID,GroupID)
)
create index ix_UserGroup on UserGroup(GroupID)
If you do need to encode the 300 group memberships in a single row, either
because it's appropriate because the 300 groups are as unchanging as the
database schema, or necessary because of some crazy performance requirement,
why not store the 300 memberships in 300 columns?
create table Users
(
id int primary key,
name varchar(50) not null,
InGroup1 bit not null default 0,
InGroup2 bit not null default 0,
. . .
)
David|||Replied in microsoft.public.sqlserver.programming.
Please do not post the same question indepenedently in multiple groups.
--
David Portas
SQL Server MVP
--|||David,
I do not want to have 300 columns because of maintenance reason. Bitmask
approach will allow me to have certain degree of flexibility without need to
change schema (in case I will need to add new groups). For example, I can
create VARBINARY field with extra empty bits to reserve space for future
groups. My main objective is to save space. We will have millions of users.
UserGroup table will be huge if we use traditional relational approach.
Groups are very much static.
Any comments are welcome.
Igor
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:ef%23EjoBMFHA.3184@.TK2MSFTNGP09.phx.gbl...
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
>> Hello,
>> I have 300 groups. Each user can or can not be associated with certain
>> group. I would like to represent group membership using corresponding
>> flag
>> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
>> 4....
>> If user is member of group of 1,2 and 3 it will be represented by number
>> 7,
>> etc.
>> It is power of 2.With BIGINT data type I can only store up to 63
>> different
>> groups. I need to store up to 300 groups.
>> I could split groups into several integer fields but I was hoping to use
>> single VARBINARY field.
>> I am trying to store all flags in VARBINARY(300) field. The problem is
>> that when populating groups table (see below), SQL server can only
>> perform
>> bitwise operations when one of the agruments is of integer type. I am
>> trying
>> to loop through all 300 groups and populate them with power of 2.
>> I run into the same limitation (no more than 63 groups). Is this possible
>> to store 300 flags for each groups ina single field?
> Is it really appropriate (or necessary) to enshrine the 300 groups in the
> metadata?
> The more usual model would be
> create table Users(id int primary key, name varchar(50) not null)
> create table Groups(id int primary key, name varchar(50) not null)
> create table UserGroup
> (
> UserIDint not null references Users on delete cascade,
> GroupID id not null references Groups on delete cascade,
> constraint pk_UserGroup primary key (UserID,GroupID)
> )
> create index ix_UserGroup on UserGroup(GroupID)
>
> If you do need to encode the 300 group memberships in a single row, either
> because it's appropriate because the 300 groups are as unchanging as the
> database schema, or necessary because of some crazy performance
> requirement, why not store the 300 memberships in 300 columns?
> create table Users
> (
> id int primary key,
> name varchar(50) not null,
> InGroup1 bit not null default 0,
> InGroup2 bit not null default 0,
> . . .
> )
> David
>

bit mask with 300 flags

Hello,
I have 300 groups. Each user can or can not be associated with certain
group. I would like to represent group membership using corresponding flag
(on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
4....
If user is member of group of 1,2 and 3 it will be represented by number 7,
etc.
It is power of 2.With BIGINT data type I can only store up to 63 different
groups. I need to store up to 300 groups.
I could split groups into several integer fields but I was hoping to use
single VARBINARY field.
I am trying to store all flags in VARBINARY(300) field. The problem is
that when populating groups table (see below), SQL server can only perform
bitwise operations when one of the agruments is of integer type. I am trying
to loop through all 300 groups and populate them with power of 2.
I run into the same limitation (no more than 63 groups). Is this possible
to store 300 flags for each groups ina single field?
Thanks,
Igor
CREATE TABLE groups_bin
(
group_id SMALLINT ,
group_name VARCHAR(100),
group_bit_mask VARBINARY(2)
)
DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=30
0
WHILE @.intID<=@.intMaxID
BEGIN
INSERT INTO groups_bin(group_id,group_name,group_bit
_mask)
VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
ENDYou really should normalize your data. The SQL Engine is optimized for
working with sets of data, not for manual parsing and manipulation of
individual bits in columns.
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7,
> etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different
> groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying
> to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
> Thanks,
> Igor
> CREATE TABLE groups_bin
> (
> group_id SMALLINT ,
> group_name VARCHAR(100),
> group_bit_mask VARBINARY(2)
> )
> DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
> SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=30
0
> WHILE @.intID<=@.intMaxID
> BEGIN
> INSERT INTO groups_bin(group_id,group_name,group_bit
_mask)
> VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
> SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
> PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
> END
>
>|||"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7,
> etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different
> groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying
> to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
>
Is it really appropriate (or necessary) to enshrine the 300 groups in the
metadata?
The more usual model would be
create table Users(id int primary key, name varchar(50) not null)
create table Groups(id int primary key, name varchar(50) not null)
create table UserGroup
(
UserIDint not null references Users on delete cascade,
GroupID id not null references Groups on delete cascade,
constraint pk_UserGroup primary key (UserID,GroupID)
)
create index ix_UserGroup on UserGroup(GroupID)
If you do need to encode the 300 group memberships in a single row, either
because it's appropriate because the 300 groups are as unchanging as the
database schema, or necessary because of some crazy performance requirement,
why not store the 300 memberships in 300 columns?
create table Users
(
id int primary key,
name varchar(50) not null,
InGroup1 bit not null default 0,
InGroup2 bit not null default 0,
. . .
)
David|||Replied in microsoft.public.sqlserver.programming.
Please do not post the same question indepenedently in multiple groups.
David Portas
SQL Server MVP
--|||David,
I do not want to have 300 columns because of maintenance reason. Bitmask
approach will allow me to have certain degree of flexibility without need to
change schema (in case I will need to add new groups). For example, I can
create VARBINARY field with extra empty bits to reserve space for future
groups. My main objective is to save space. We will have millions of users.
UserGroup table will be huge if we use traditional relational approach.
Groups are very much static.
Any comments are welcome.
Igor
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:ef%23EjoBMFHA.3184@.TK2MSFTNGP09.phx.gbl...
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Is it really appropriate (or necessary) to enshrine the 300 groups in the
> metadata?
> The more usual model would be
> create table Users(id int primary key, name varchar(50) not null)
> create table Groups(id int primary key, name varchar(50) not null)
> create table UserGroup
> (
> UserIDint not null references Users on delete cascade,
> GroupID id not null references Groups on delete cascade,
> constraint pk_UserGroup primary key (UserID,GroupID)
> )
> create index ix_UserGroup on UserGroup(GroupID)
>
> If you do need to encode the 300 group memberships in a single row, either
> because it's appropriate because the 300 groups are as unchanging as the
> database schema, or necessary because of some crazy performance
> requirement, why not store the 300 memberships in 300 columns?
> create table Users
> (
> id int primary key,
> name varchar(50) not null,
> InGroup1 bit not null default 0,
> InGroup2 bit not null default 0,
> . . .
> )
> David
>

bit mask with 300 flags

Hello,
I have 300 groups. Each user can or can not be associated with certain
group. I would like to represent group membership using corresponding flag
(on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
4....
If user is member of group of 1,2 and 3 it will be represented by number 7,
etc.
It is power of 2.With BIGINT data type I can only store up to 63 different
groups. I need to store up to 300 groups.
I could split groups into several integer fields but I was hoping to use
single VARBINARY field.
I am trying to store all flags in VARBINARY(300) field. The problem is
that when populating groups table (see below), SQL server can only perform
bitwise operations when one of the agruments is of integer type. I am trying
to loop through all 300 groups and populate them with power of 2.
I run into the same limitation (no more than 63 groups). Is this possible
to store 300 flags for each groups ina single field?
Thanks,
Igor
CREATE TABLE groups_bin
(
group_id SMALLINT ,
group_name VARCHAR(100),
group_bit_mask VARBINARY(2)
)
DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=300
WHILE @.intID<=@.intMaxID
BEGIN
INSERT INTO groups_bin(group_id,group_name,group_bit_mask)
VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
END
You really should normalize your data. The SQL Engine is optimized for
working with sets of data, not for manual parsing and manipulation of
individual bits in columns.
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7,
> etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different
> groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying
> to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
> Thanks,
> Igor
> CREATE TABLE groups_bin
> (
> group_id SMALLINT ,
> group_name VARCHAR(100),
> group_bit_mask VARBINARY(2)
> )
> DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
> SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=300
> WHILE @.intID<=@.intMaxID
> BEGIN
> INSERT INTO groups_bin(group_id,group_name,group_bit_mask)
> VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
> SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
> PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
> END
>
>
|||"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7,
> etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different
> groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying
> to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
>
Is it really appropriate (or necessary) to enshrine the 300 groups in the
metadata?
The more usual model would be
create table Users(id int primary key, name varchar(50) not null)
create table Groups(id int primary key, name varchar(50) not null)
create table UserGroup
(
UserIDint not null references Users on delete cascade,
GroupID id not null references Groups on delete cascade,
constraint pk_UserGroup primary key (UserID,GroupID)
)
create index ix_UserGroup on UserGroup(GroupID)
If you do need to encode the 300 group memberships in a single row, either
because it's appropriate because the 300 groups are as unchanging as the
database schema, or necessary because of some crazy performance requirement,
why not store the 300 memberships in 300 columns?
create table Users
(
id int primary key,
name varchar(50) not null,
InGroup1 bit not null default 0,
InGroup2 bit not null default 0,
. . .
)
David
|||Replied in microsoft.public.sqlserver.programming.
Please do not post the same question indepenedently in multiple groups.
David Portas
SQL Server MVP
|||David,
I do not want to have 300 columns because of maintenance reason. Bitmask
approach will allow me to have certain degree of flexibility without need to
change schema (in case I will need to add new groups). For example, I can
create VARBINARY field with extra empty bits to reserve space for future
groups. My main objective is to save space. We will have millions of users.
UserGroup table will be huge if we use traditional relational approach.
Groups are very much static.
Any comments are welcome.
Igor
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:ef%23EjoBMFHA.3184@.TK2MSFTNGP09.phx.gbl...
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23x%23fgOBMFHA.3340@.TK2MSFTNGP14.phx.gbl...
> Is it really appropriate (or necessary) to enshrine the 300 groups in the
> metadata?
> The more usual model would be
> create table Users(id int primary key, name varchar(50) not null)
> create table Groups(id int primary key, name varchar(50) not null)
> create table UserGroup
> (
> UserIDint not null references Users on delete cascade,
> GroupID id not null references Groups on delete cascade,
> constraint pk_UserGroup primary key (UserID,GroupID)
> )
> create index ix_UserGroup on UserGroup(GroupID)
>
> If you do need to encode the 300 group memberships in a single row, either
> because it's appropriate because the 300 groups are as unchanging as the
> database schema, or necessary because of some crazy performance
> requirement, why not store the 300 memberships in 300 columns?
> create table Users
> (
> id int primary key,
> name varchar(50) not null,
> InGroup1 bit not null default 0,
> InGroup2 bit not null default 0,
> . . .
> )
> David
>

bit mask

Hello,
I have 300 groups. Each user can or can not be associated with certain
group. I would like to represent group membership using corresponding flag
(on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
4....
If user is member of group of 1,2 and 3 it will be represented by number 7,
etc.
It is power of 2.With BIGINT data type I can only store up to 63 different
groups. I need to store up to 300 groups.
I could split groups into several integer fields but I was hoping to use
single VARBINARY field.
I am trying to store all flags in VARBINARY(300) field. The problem is
that when populating groups table (see below), SQL server can only perform
bitwise operations when one of the agruments is of integer type. I am trying
to loop through all 300 groups and populate them with power of 2.
I run into the same limitation (no more than 63 groups). Is this possible
to store 300 flags for each groups ina single field?
Thanks,
Igor
CREATE TABLE groups_bin
(
group_id SMALLINT ,
group_name VARCHAR(100),
group_bit_mask VARBINARY(2)
)
DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=30
0
WHILE @.intID<=@.intMaxID
BEGIN
INSERT INTO groups_bin(group_id,group_name,group_bit
_mask)
VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
ENDArgh you are seriously, seriously trying to completely denormalize your
data? Have you considered the problem of all the logic involved with
updating a single flag for an individual user in the future? Or even all
the logic involved in just extracting a single flag setting for an
individual user? You've already run into one of the limitations of the
software - the physical limitation of the number of bits a certain field
type can hold. You're relying on what you perceive to be the observed
physical design of the system - nothing says that this won't change down the
road...
This was a very poor design decision...
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23Mmhe%23AMFHA.2384@.tk2msftngp13.phx.gbl...
> Hello,
> I have 300 groups. Each user can or can not be associated with certain
> group. I would like to represent group membership using corresponding flag
> (on/off). Using integer dattatype it will look like : G1- 1, G2 - 2, G3 -
> 4....
> If user is member of group of 1,2 and 3 it will be represented by number
> 7, etc.
> It is power of 2.With BIGINT data type I can only store up to 63
> different groups. I need to store up to 300 groups.
> I could split groups into several integer fields but I was hoping to use
> single VARBINARY field.
> I am trying to store all flags in VARBINARY(300) field. The problem is
> that when populating groups table (see below), SQL server can only perform
> bitwise operations when one of the agruments is of integer type. I am
> trying to loop through all 300 groups and populate them with power of 2.
> I run into the same limitation (no more than 63 groups). Is this possible
> to store 300 flags for each groups ina single field?
> Thanks,
> Igor
> CREATE TABLE groups_bin
> (
> group_id SMALLINT ,
> group_name VARCHAR(100),
> group_bit_mask VARBINARY(2)
> )
> DECLARE @.binBitMask VARBINARY(255),@.intID INT,@.intMaxID INT
> SELECT @.binBitMask=0x0001,@.intID=1,@.intMaxID=30
0
> WHILE @.intID<=@.intMaxID
> BEGIN
> INSERT INTO groups_bin(group_id,group_name,group_bit
_mask)
> VALUES(@.intID,'G'+CAST(@.intID AS VARCHAR(30)),@.binBitMask)
> SELECT @.binBitMask=CAST(@.binBitMask*2 AS VARBINARY(255)),@.intID=@.intID+1
> PRINT CAST(@.binBitMask*2 AS VARBINARY(255))
> END
>|||Sorry, but you have a lousy design and the best thing you can do is get rid
of it. The standard way to represent a many-to-many relationship is called a
Junction Table or Joining Table. Something like this:
CREATE TABLE UserGroups (user_id INTEGER REFERENCES Users (user_id),
group_id INTEGER REFERENCES Groups (group_id), PRIMARY KEY (user_id,
group_id))
I recommend you study a book on data modelling if you aren't already
familiar with relational design principles.
David Portas
SQL Server MVP
--|||A design change would be best, however if you insist on doing something like
this you can take a look at SQL Server's own sprocs for handling roles and
users.
Check out the code in sp_addrole, sp_addrolemember etc. In the SQL Server
version it uses a varbinary(2048) field to store up to 16k of options.
HTH
Rick Sawtell
MCT, MCSD, MCDBA|||No problem, David. The criticism is greately appreciated :) I am
familiar with relational design principals. The reason why I opted for
bitmask approach is because I am trying to save space. Every user can be
member of any/all of 300 groups (number of groups fixed). We can save a lot
of space if we can represent user's group membership with a single, for
example VARBINARY field. With traditional relational approach space
consumption would be higher.
We will have millions of users and I was hoping bimask approach will save
us a lot of space.
Please let me know if my thinking is incorrect.
Thanks,
Igor
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:zbidnU2EcNHB8t_fRVn-ow@.giganews.com...
> Sorry, but you have a lousy design and the best thing you can do is get
> rid of it. The standard way to represent a many-to-many relationship is
> called a Junction Table or Joining Table. Something like this:
> CREATE TABLE UserGroups (user_id INTEGER REFERENCES Users (user_id),
> group_id INTEGER REFERENCES Groups (group_id), PRIMARY KEY (user_id,
> group_id))
> I recommend you study a book on data modelling if you aren't already
> familiar with relational design principles.
> --
> David Portas
> SQL Server MVP
> --
>|||Rick, I am not 'insisting' on this design. My goal is to save space. We
will have millions of users. Number of groups is predefined and fixed. I see
no difference between representing several flags as Microsoft is doing in
system tables and 300 using bitmask. I think I might be able to save a lot
of space with this design. I can use bitwise logic to set/reset/verify
flags.
Is my thhinking incorrect?
Thanks,
Igor
"Rick Sawtell" <r_sawtell@.hotmail.com> wrote in message
news:uIIqJIIMFHA.1300@.TK2MSFTNGP10.phx.gbl...
>A design change would be best, however if you insist on doing something
>like
> this you can take a look at SQL Server's own sprocs for handling roles and
> users.
> Check out the code in sp_addrole, sp_addrolemember etc. In the SQL
> Server
> version it uses a varbinary(2048) field to store up to 16k of options.
> HTH
> Rick Sawtell
> MCT, MCSD, MCDBA
>
>|||Michael,
I am trying to conserver space. I have no problems updating flags using
bitwise logic. In fact, I already have prototype working using BIGINT field.
Unfortunately, it only allows me to store 62 flags. I can easily
set/rest/extract users flags.
I was hoping to use VARBINARY to store 300 flags in a single field. We
have predefined/fixed set of groups.
Any comments are welcome.
Thanks,
Igor
"Michael C#" <xyz@.abcdef.com> wrote in message
news:jAp0e.7247$AF3.6586@.fe10.lga...
> Argh you are seriously, seriously trying to completely denormalize your
> data? Have you considered the problem of all the logic involved with
> updating a single flag for an individual user in the future? Or even all
> the logic involved in just extracting a single flag setting for an
> individual user? You've already run into one of the limitations of the
> software - the physical limitation of the number of bits a certain field
> type can hold. You're relying on what you perceive to be the observed
> physical design of the system - nothing says that this won't change down
> the road...
> This was a very poor design decision...
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23Mmhe%23AMFHA.2384@.tk2msftngp13.phx.gbl...
>|||Is every user a member of all 300 groups? On average, how many groups would
you say that your users belong to?
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:eiqaZ6JMFHA.2464@.TK2MSFTNGP10.phx.gbl...
> Michael,
> I am trying to conserver space. I have no problems updating flags using
> bitwise logic. In fact, I already have prototype working using BIGINT
> field. Unfortunately, it only allows me to store 62 flags. I can easily
> set/rest/extract users flags.
> I was hoping to use VARBINARY to store 300 flags in a single field. We
> have predefined/fixed set of groups.
> Any comments are welcome.
> Thanks,
> Igor
>
> "Michael C#" <xyz@.abcdef.com> wrote in message
> news:jAp0e.7247$AF3.6586@.fe10.lga...
>|||P.S. - While you could kludge this design and probably force it to work,
you're trading off maintainability, scalability and speed to save a little
space. Hard drive costs are pretty darn low :)
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:eiqaZ6JMFHA.2464@.TK2MSFTNGP10.phx.gbl...
> Michael,
> I am trying to conserver space. I have no problems updating flags using
> bitwise logic. In fact, I already have prototype working using BIGINT
> field. Unfortunately, it only allows me to store 62 flags. I can easily
> set/rest/extract users flags.
> I was hoping to use VARBINARY to store 300 flags in a single field. We
> have predefined/fixed set of groups.
> Any comments are welcome.
> Thanks,
> Igor
>
> "Michael C#" <xyz@.abcdef.com> wrote in message
> news:jAp0e.7247$AF3.6586@.fe10.lga...
>|||A couple of notes..
1. Having a join table and storage table is not *that* much bigger storage
wise.
2. Yes, you can use the bitwise logic to set/reset/verify the values
however it is not particularly fast in SQL Server.
On a table with 5 million rows (and a varbinary(2048)) I did an update that
set the bitflag for 100,000 rows or so. It took much more time than doing
an update across the joined tables.
So you might save some space, but you will lose out on speed.
Rick
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:O90hw4JMFHA.3080@.TK2MSFTNGP10.phx.gbl...
> Rick, I am not 'insisting' on this design. My goal is to save space. We
> will have millions of users. Number of groups is predefined and fixed. I
see
> no difference between representing several flags as Microsoft is doing in
> system tables and 300 using bitmask. I think I might be able to save a lot
> of space with this design. I can use bitwise logic to set/reset/verify
> flags.
> Is my thhinking incorrect?
> Thanks,
> Igor
>
> "Rick Sawtell" <r_sawtell@.hotmail.com> wrote in message
> news:uIIqJIIMFHA.1300@.TK2MSFTNGP10.phx.gbl...
and
>

Bit fields in T-SQL Queries?

I'm creating a query that filters on a bit field that I'm using to represent a boolean value. My query looks something like this:

SELECT * FROM MYTABLE WHERE ACTIVEORDER = 1

And that query works okay--I get all records where 'active order' is true.

Here's my problem--I'd like to make this query more readable by using TRUE or FALSE, instead of 1 or 0. I thought that I had read in BOL that T-SQL can convert the string values TRUE and FALSE to 1 and 0. But I can't get that to work in my query.

Is there a way to use TRUE and FALSE as the filter value for a bit field in a T-SQL query, rather than 1 and o? If so, what does the query look like? Thanks.

AFAIR no, it's not possible. Moreover, I highly recommend you not to mix different languages - it is only confusing. In other words, your query may look more readable for you, but it may look completely enigmatic for another one at the same time.|||Thanks--that makes sense. Just for my own curiousity, any idea why T-SQL doesn't have a boolean type?|||Why, it has - a bit type is perfectly suitable for such a role
For example, there may be some strictly defined things in ANSI SQL which MSFT has to follow. Also, the SEQUEL language (ancestor for current SQL) was developed in the past when every byte in the listing was thrice-counted, so no wonder they decided to use short 1/0 instead of literal constants.|||

--Sure there's a way to make it more readable.

--Just put this at the top of the query:

declare @.True bit,@.False bit

select @.True = 0, @.False = 0

--then use:

select * from MyTable where ActiveOrder = @.True

--Sorry, I fat-fingered this query. The below is corrected:

declare @.True bit,@.False bit

select @.True = 1, @.False = 0

--then use:

select * from MyTable where ActiveOrder = @.True

|||

So, this works when BOTH True and False are equal to zero?

select @.True = 0, @.False = 0

--then use:

select * from MyTable where ActiveOrder = @.True

|||

Ennor wrote:

there may be some strictly defined things in ANSI SQL which MSFT has to follow.

None of the SQL Standard says BOOLEAN is one of the datatype in your database.

It says,

1. The value True/False are reserved words as per ANSI Standard

2. Boolean is not a valid datatype for your Table/SQL quries

3. Boolean/Truth values should be TRUE | FALSE | UNKNOWN

4. You can't use the TRUE/FALSE on your query.

|||

You can try

select case when activeorder=1 then 'TRUE'

case when activeorder=0 then FALSE'

end

from mytable

|||

Sorry, I fat fingered that one:

select @.True = 1, @.False = 0