473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2017
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: RK | last post by:
Our Application solution consists Web Service and 3 other C# class libraries. We added Trace Listener to the application and writing log information in all our components. Since multiple...
4
by: David Lozzi | last post by:
OK simple question. Whats the default value for an string() array? sub LoadStuff(byval one as integer, byval two as string, optional byval three() as string = ??) Its driving me nuts! ...
5
by: | last post by:
Hi, In our system we get image buffers through a native dll as void*. We want to manage several of this blocks by C# objects an pass them wen needed to a native image processing library. What...
4
by: sophie | last post by:
Whats going on here: Read in a number as a string: scanf("%s", &number); number = 12345, for arguements sake Print it like this its fine:
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
17
by: student | last post by:
hi, C is an evergreen programming language developed by denis richie. C++ is only an extension to C with some extra added features and VC++ provides an IDE for programming. still C is not obselete....
5
by: denis | last post by:
Hi there, Whats more efficient and why .... int func(int x) { return x*2; } or
9
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have some preliminary code in my program (check for a certain file, define a like 15 variables) and I put it in the form load. I recently got the idea to make a method for it, and do it in...
26
by: Muzammil | last post by:
whats the beauty of "malloc" over "new" why its helpful for programmer.for its own memory area.??
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.