Showing posts with label storing. Show all posts
Showing posts with label storing. Show all posts

Tuesday, March 20, 2012

Number of columns and performance

Hi,

I'm designing a new database and I have a doubt in which surely you
can help me.
I'm storing in this database historical data of some measurements and
the system in constantly growing, new measurements are added every
day.
So, I have to set some extra columns in advance, so space is available
whenever is needed and the client doesn't have to modify the structure
in SQL server.
The question is: the more columns I add "just in case", the slower the
SQL reads the table?
Of course the "empty" columns are not included in any query until they
have some valid data inside.
Will I have better performance if I configure only the columns being
used at the moment, without any empty columns?

Thanks in advance.

Ignacio"Nacho" <nacho.jorge@.gmail.comwrote in message
news:1177509485.676313.103910@.s33g2000prh.googlegr oups.com...

Quote:

Originally Posted by

Hi,
>
I'm designing a new database and I have a doubt in which surely you
can help me.
I'm storing in this database historical data of some measurements and
the system in constantly growing, new measurements are added every
day.
So, I have to set some extra columns in advance, so space is available
whenever is needed and the client doesn't have to modify the structure
in SQL server.


Umm... I don't see why you have to do this now.

Do it later.

I mean will you even know the type or size of the columns now?

And how will you even give them meaningful names when they have none now.

Quote:

Originally Posted by

The question is: the more columns I add "just in case", the slower the
SQL reads the table?
Of course the "empty" columns are not included in any query until they
have some valid data inside.
Will I have better performance if I configure only the columns being
used at the moment, without any empty columns?


A tad (since there's a small metadate overhead on the leave nodes).

But more importantly, you'll have a messy schema on your hands. I'd wait to
add them.

Quote:

Originally Posted by

>
Thanks in advance.
>
Ignacio
>


--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||Nacho (nacho.jorge@.gmail.com) writes:

Quote:

Originally Posted by

I'm designing a new database and I have a doubt in which surely you
can help me.
I'm storing in this database historical data of some measurements and
the system in constantly growing, new measurements are added every
day.
So, I have to set some extra columns in advance, so space is available
whenever is needed and the client doesn't have to modify the structure
in SQL server.
The question is: the more columns I add "just in case", the slower the
SQL reads the table?
Of course the "empty" columns are not included in any query until they
have some valid data inside.
Will I have better performance if I configure only the columns being
used at the moment, without any empty columns?


As always with performance questions, there are several "it depends".

If these just-in-case columns are varchar (or nvarchar or varbinary),
they only take up two bytes each extra, which may not be cause for
alarm. On the other hand, if they are char(50) or some other fixed
length, they take up the full space, NULL or not.

Next thing that matters is how the access against the tables are. If
there plenty of table scans, or for that matter range seeks in the clustered
index, like OrderDate BETWEEN @.fromdate AND @.todate, there is certainly
a performance cost for adding more bytes to every rows. More bytes per
row, fewer rows per page, and more pages to read for the same number
of rows. On the other hand, if access mainly is by non-clustered index
and key lookup, then the extra bytes do not have the same cost.

But this latter pattern is rarely the only access pattern for a table,
so the conclusion is that, yes, there is a cost. But how big the cost
is, is more difficult to tell.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Monday, February 20, 2012

Null or empty string use more space?

Hi all,
I have some columns in my database which allows null. I want to know if leaving the field to be NULL or storing an empty string into the field, which will take up more space?? if the field type is varchar(100)
No doubt, fields with empty strings take up way more space than their counterparts with NULL values......... have a look at this link for an example >>http://www.sqljunkies.com/WebLog/amachanic/archive/2004/10/22/4743.aspx|||

Hi Narayanas,

Thanks for your reply and solution. Now that i know storing an empty string will take up more spaces then leaving it Null, but if i have a table which has 4-5 fields that allows Null. Then how can i leave those columns to Null if the user don't enter anything for those string fields? e.g

CREATE PROCEDURE add_row
@.field1 varchar(100),
:
@.field5 varchar(100)

AS
IF @.filed1 = ''
INSERT INTO some_table (field2, ..., field5) VALUES (@.field2, ..., @.field5)
IF @.field2 = ''
INSERT INTO some_table(field1, field3, ..., field5) VALUES (@.field1, @.field3,... , @.field5)

If i do it this way then there will be too many combinations of IF statements. Can you show me some better ways to do this?

|||Are you seriously worried, in this modern age, about the extra space taken up with an empty string vs a null? I'd be more concerned about the problems and general confusion nulls create, let alone some potential performance problems with the database has to look up the null flag. If you want to use NULLs then all well and good, but treat them as a feature not a space saving device.
|||

kakusei wrote:

Hi all,
I have some columns in my database which allows null. I want to know if leaving the field to be NULL or storing an empty string into the field, which will take up more space?? if the field type is varchar(100)


You cannot pass an empty string to a Database NULL because ANSI SQL NULL which is database NULL is in an UNKNOWN value not an empty string. It evaluates mathematically to a NULL but it is an UNKNOWN value determined by the creator of the database. NET value types don't accept NULL the reason for DBNULL but SQL Server value types have to accept NULL to be ANSI SQL compliant. If it is Varchar (100) you use DBNULL. Space is not important but passing a Varchar (100) column an empty string will create data consistency problems in your database. Hope this helps.|||you can set default values at the time of defining the parameters in the stored proc.

CREATE PROCEDURE add_row
@.field1 varchar(100) = NULL,
:
@.field5 varchar(100) = NULL

AS
-- so you would need one insert statement.If there is a value in the variable
-- that will be inserted else null.
INSERT INTO some_table (field2, ..., field5) VALUES (@.field2, ..., @.field5)