views:

39

answers:

3

I have something like this:

    create table account
    (
       id int identity(1,1) primary key,
       usertype char(1) check(usertype in ('a', 'b')) not null, 
       unique(id, usertype)
    )

    create table auser
    (
       id int primary key,
       usertype char(1) check(usertype = 'a') not null, 
       foreign key (id, usertype) references account(id, usertype)
    )

    create table buser
    (
    ... same just with b
    )

the question is: if I'm going to use int instead of char(1), does it going to work faster/better ?

+2  A: 

it doesn't matter on most modern databases. int is fine.

char as well.

(when the database fetch data from a table, it's not in byte size....)

Dani
if you get into many rows of data, you will waste memory on your index, and get fewer rows per page using INT if CHAR(1) was enough, which will have an effect. It is ALWAYS best to pick the best size for your columns. This is the type of issue where just being you're lazy for 5 minutes can cause a lifetime of wasted computer resources.
KM
+1  A: 

why would you need IDENTITY columns: "auser.id" and "buser.id" that foreign key back to the "account.id" identity column?? seems hard to make sure everything could ever be in sync? When you insert into account you get an ID (say 1) and type "a", when you insert into "auser" you get an id (1) and FK to "account" how would you insert into "buser" (and get id 1) and fk back to account with 1,b??

Onto the real question. Size UserType to how many values you will have, if you will only have a few CHAR(1) is best, your index will take up less memory. if you will have more than a char(1) can hold, go tiny int (0-255, 1 byte), if you need more, go smallint (32k, 2 byte), if you need more go int (2,147,483,647, 4 byte)

KM
here is the answer to your question: http://consultingblogs.emc.com/davidportas/archive/2007/01/08/Distributed-Keys-and-Disjoint-Subtypes.aspx
Omu
in the example in your link there is no mention of IDENTITY/auto increment columns. Your example will not work unless you drop the identity from "usera" and "userb" tables. Generate the identity value ONLY in account, and use just it in "usera" and "userb", do not generate it there ("usera" and "userb").
KM
yes, sorry for that, I don't have the identity in the usera/b tables, I've edited my question
Omu
btw, I think it's bigint that you are showing here, int is much smaller :)
Omu
@Omu, yea, cut and paste error, I'll fix...
KM
+1  A: 

A char(1) is 1 byte, whereas an int is 4 bytes. Even a small int is 2 bytes.

ck