468,133 Members | 1,228 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Whats better?

<usualDisclaimer>Please forgive me if this is in the wrong group, and if so,
what is the right group.</usualDisclaimer>

Let me start off by first saying im a newb. Ok, with that out of the way I
am trying really hard and boy have I learned a lot in the last little while
but I have a question i just can't seem to find a good answer to.

Lets say i have a table that simply stores how many times someone has logged
into a webpage. Is it better to store each login as a new record then count
the records or is it better to simply have one record that updates the total
value by incrementing that one field. I have read all manner of articles
and some say one way is better vs. the other but what I can't find is why?
If I knew why one way was better than another then I could make and educated
decision and choose the best way that is right for me. Is updating more or
less expensive then inserting? Does it matter and is a relevant question?

And before anyone comments on my use of all uppercase letters for my table
name I do this so that my table names stand out within the sql server
enterprise manager. In other words system tables are lowercase and my
tables are uppercase. People always seem to give me crap for this but never
back it up with a good explanation so far as I can tell its personal
preference? Am I wrong?

CREATE TABLE USER_METRICS
(
-- here i can select all the records and count them up.
ID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(250)
);

-- Or this?

CREATE TABLE USER_METRICS
(
-- and of course here can i simply retrieve the value of Total_Logins
ID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(250) NOT NULL,
Total_Logins INT
);
Regards,
Muhd
Jul 20 '05 #1
3 1822
Muhd (mu**@binarydemon.com) writes:
Lets say i have a table that simply stores how many times someone has
logged into a webpage. Is it better to store each login as a new record
then count the records or is it better to simply have one record that
updates the total value by incrementing that one field. I have read all
manner of articles and some say one way is better vs. the other but what
I can't find is why? If I knew why one way was better than another then
I could make and educated decision and choose the best way that is right
for me. Is updating more or less expensive then inserting? Does it
matter and is a relevant question?
There is no clear-cut answer to this question, because it depends on
what you want. But that said, this table:
CREATE TABLE USER_METRICS
(
-- here i can select all the records and count them up.
ID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(250)
);
does not really make any sense. If this is all you want, then the other
table is much better. It will be faster to use, and take up less disk
space.

But let's say one day, you starting thinking "̀ wonder how often people
log in? Do they make many logins in cluster, or do they log in once an
hour?" Well, this when you want a table like the above, but you add a
login_time column. But even then you may want to keep the other table,
because since if there are many logins, you may prefer to delete old
rows, but still keep the total number of logins since the start of time.
Yet then again, if you only want to know the number of logins the last
two mobths, a table with a row for each login and a timestamp can easily
be emptied at the end of the month, whereas one with only number cannot.

So the bottom line is that to get what you want, you have to know what
you want.
And before anyone comments on my use of all uppercase letters for my
table name I do this so that my table names stand out within the sql
server enterprise manager. In other words system tables are lowercase
and my tables are uppercase. People always seem to give me crap for
this but never back it up with a good explanation so far as I can tell
its personal preference? Am I wrong?


That is of course personal preference. But you may be interested to know
that if you right-click a server in EM, and select Edit SQL Server
Registration Properties, there is a checkbox with which you can hide
system objects.

Personally, I never or very rarely use EM to look at objects, so this
is not an issue for me. My preference is all lowercase for all identifiers,
since I use all uppercase for keywords.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Erland,
Ok one question in response to your message. If your not using EM (and
i don't mean to imply that you should, its certainly helpful for me as a
relative newb) what do you use? Your hiding system objects point was
helpful btw.
So, with that said, thanks for your answer, it did indeed help out alot.
Between the time I wrote my message and the time you answered i did some
thinking about exactly what i was trying to collect, how i was going to use
the data i collected, and was it even important to be collecting in the
first place.
So i decided that yes i did want to know the number of times someone
logged in, but as you pointed out, i also wanted to know when they logged in
and when they logged out. Thus i can track total logins, login time, logout
time, and total time.
Hence i believe this is the best solution for.

CREATE TABLE USER_METRICS
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(250) NOT NULL
Login_Time DATETIME NOT NULL
Logout_Time DATETIME NOT NULL
)
"Erland Sommarskog" <so****@algonet.se> wrote in message
news:Xn**********************@127.0.0.1...
Muhd (mu**@binarydemon.com) writes:
Lets say i have a table that simply stores how many times someone has
logged into a webpage. Is it better to store each login as a new record
then count the records or is it better to simply have one record that
updates the total value by incrementing that one field. I have read all
manner of articles and some say one way is better vs. the other but what
I can't find is why? If I knew why one way was better than another then
I could make and educated decision and choose the best way that is right
for me. Is updating more or less expensive then inserting? Does it
matter and is a relevant question?
There is no clear-cut answer to this question, because it depends on
what you want. But that said, this table:
CREATE TABLE USER_METRICS
(
-- here i can select all the records and count them up.
ID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(250)
);


does not really make any sense. If this is all you want, then the other
table is much better. It will be faster to use, and take up less disk
space.

But let's say one day, you starting thinking "̀ wonder how often people
log in? Do they make many logins in cluster, or do they log in once an
hour?" Well, this when you want a table like the above, but you add a
login_time column. But even then you may want to keep the other table,
because since if there are many logins, you may prefer to delete old
rows, but still keep the total number of logins since the start of time.
Yet then again, if you only want to know the number of logins the last
two mobths, a table with a row for each login and a timestamp can easily
be emptied at the end of the month, whereas one with only number cannot.

So the bottom line is that to get what you want, you have to know what
you want.
And before anyone comments on my use of all uppercase letters for my
table name I do this so that my table names stand out within the sql
server enterprise manager. In other words system tables are lowercase
and my tables are uppercase. People always seem to give me crap for
this but never back it up with a good explanation so far as I can tell
its personal preference? Am I wrong?


That is of course personal preference. But you may be interested to know
that if you right-click a server in EM, and select Edit SQL Server
Registration Properties, there is a checkbox with which you can hide
system objects.

Personally, I never or very rarely use EM to look at objects, so this
is not an issue for me. My preference is all lowercase for all

identifiers, since I use all uppercase for keywords.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #3
Muhd (mu**@binarydemon.com) writes:
Ok one question in response to your message. If your not using EM (and
i don't mean to imply that you should, its certainly helpful for me as a
relative newb) what do you use?
To write ad hoc-queries I use Query Analyzer. For writing stored
procedures and other code that is part of our system I use a third-
party text editor, TextPad. TextPad has on particular SQL support, it
is just a better editor. To load the procedures to the database for
testing, TextPad permits me to invoke a command-line tool which connects
to the database. My load tool is a home-written one that has a lot of
bells and whistles to take care of whether I should use CREATE/ALTER
procedures, permissions and a lot more.

Using a third-party editor is probably a very common solution, but
there are too many persons out there that use the Stored Procedure
window in EM, which has really port editing capabilities. Query Analyzer
is a lot better.

For data-modelleling I use another third-party tool, PowerDesigner which
is a more powerful data-modelling tool than the diagrams you have in
Enterprise Manager.

And to look at the table data - again, I run queries in Query Analyzer.
Hence i believe this is the best solution for.

CREATE TABLE USER_METRICS
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(250) NOT NULL
Login_Time DATETIME NOT NULL
Logout_Time DATETIME NOT NULL
)


You should probably make Logout_Time nullable, and write a row when the
user logs in, and then update the row when he logs out. If there is a
crash, he may neevr log out.

Also, the id column may not be necessary, but (Email, Login_Time) is a
natural key here - as long as no evil user manages to login in the
same 3.33 millisecond interval.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David Lozzi | last post: by
4 posts views Thread by sophie | last post: by
17 posts views Thread by student | last post: by
5 posts views Thread by denis | last post: by
9 posts views Thread by =?Utf-8?B?cm9nZXJfMjc=?= | last post: by
26 posts views Thread by Muzammil | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.