473,660 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance -- count(*)

I need to update the columns to blanks if it has Nulls; Most of the
time we do not have Nulls and to check that in a 3.2 million table it
takes 2 minutes and 45 seconds. (To do select count(*) from <table>
where <columnname> is Null) .

I just cut and pasted a part from my stored procedure:

EXEC ('IF (SELECT COUNT (*) FROM ' + @tblname + ' WHERE '
+@columnname + ' is Null ) >0
BEGIN
PRINT ''' + @columnName + ' has Null ''' +
' UPDATE ' + @tblname +
' SET ' + @columnName + ' = ''''
WHERE ' + @columnName + ' is Null
PRINT ''Updated ''
END'

Please let me know if there is a better way to do instead of doing
count (*).

Thanks in advance!

Jul 23 '05 #1
5 3144

<ge******@hotma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I need to update the columns to blanks if it has Nulls; Most of the
time we do not have Nulls and to check that in a 3.2 million table it
takes 2 minutes and 45 seconds. (To do select count(*) from <table>
where <columnname> is Null) .

I just cut and pasted a part from my stored procedure:

EXEC ('IF (SELECT COUNT (*) FROM ' + @tblname + ' WHERE '
+@columnname + ' is Null ) >0
BEGIN
PRINT ''' + @columnName + ' has Null ''' +
' UPDATE ' + @tblname +
' SET ' + @columnName + ' = ''''
WHERE ' + @columnName + ' is Null
PRINT ''Updated ''
END'

Please let me know if there is a better way to do instead of doing
count (*).

Thanks in advance!


If you only want to know that at least one row has NULL, then EXISTS should
be much faster:

if exists (
select *
from dbo.MyTable
where MyColumn is null
)
begin
-- do UPDATE here
end

But if you're updating all your NULLs to empty strings, you might want to
consider a default on the column:

create table dbo.MyTable (
....
MyColumn varchar(100) not null default '',
....
)

Or if NULL is correct in your data model, and you only want empty strings
for output purposes, then you could use COALESCE() to provide the empty
string when you query the data.

Simon
Jul 23 '05 #2
> Please let me know if there is a better way to do instead of doing
count (*).
can you not use defaults or an insert/update trigger? This is doing an awful
lot of work on the server.

Even with exists, if it fails you will have scanned the entire table. and if
it suceeds quickly youll scan it anyway with the update gaining no more than
a 0-50% reduction in runtime only if there are nulls found. You may be able
to speed up a specific example with an index but there is probably an
'elegant' solution rather than relying on brute force or dedicated indexes.

Mr Tea

<ge******@hotma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .I need to update the columns to blanks if it has Nulls; Most of the
time we do not have Nulls and to check that in a 3.2 million table it
takes 2 minutes and 45 seconds. (To do select count(*) from <table>
where <columnname> is Null) .

I just cut and pasted a part from my stored procedure:

EXEC ('IF (SELECT COUNT (*) FROM ' + @tblname + ' WHERE '
+@columnname + ' is Null ) >0
BEGIN
PRINT ''' + @columnName + ' has Null ''' +
' UPDATE ' + @tblname +
' SET ' + @columnName + ' = ''''
WHERE ' + @columnName + ' is Null
PRINT ''Updated ''
END'

Please let me know if there is a better way to do instead of doing
count (*).

Thanks in advance!

Jul 23 '05 #3
(ge******@hotma il.com) writes:
I need to update the columns to blanks if it has Nulls; Most of the
time we do not have Nulls and to check that in a 3.2 million table it
takes 2 minutes and 45 seconds. (To do select count(*) from <table>
where <columnname> is Null) .

I just cut and pasted a part from my stored procedure:

EXEC ('IF (SELECT COUNT (*) FROM ' + @tblname + ' WHERE '
+@columnname + ' is Null ) >0
BEGIN
PRINT ''' + @columnName + ' has Null ''' +
' UPDATE ' + @tblname +
' SET ' + @columnName + ' = ''''
WHERE ' + @columnName + ' is Null
PRINT ''Updated ''
END'

Please let me know if there is a better way to do instead of doing
count (*).


Since this is likely to cause a table scan, the best is just to submit
the UPDATE statement. If you want to know whether the table was updated
or not, you can catch @@rowcount when you are done. (And this can be
done outside the dynamic SQL.)

Then again, why not just say:

ALTER TABLE tbl ALTER COLUMN col <datatype> NOT NULL DEFAULT ''


--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Thank you everyone for all your replies.
Is there a performance difference for inserts when we make it Not Null
default ''VS when it is Null? We will be inserting a lot of data

Thanks
Erland Sommarskog wrote:
(ge******@hotma il.com) writes:
I need to update the columns to blanks if it has Nulls; Most of the
time we do not have Nulls and to check that in a 3.2 million table it takes 2 minutes and 45 seconds. (To do select count(*) from <table>
where <columnname> is Null) .

I just cut and pasted a part from my stored procedure:

EXEC ('IF (SELECT COUNT (*) FROM ' + @tblname + ' WHERE '
+@columnname + ' is Null ) >0
BEGIN
PRINT ''' + @columnName + ' has Null ''' +
' UPDATE ' + @tblname +
' SET ' + @columnName + ' = ''''
WHERE ' + @columnName + ' is Null
PRINT ''Updated ''
END'

Please let me know if there is a better way to do instead of doing
count (*).
Since this is likely to cause a table scan, the best is just to

submit the UPDATE statement. If you want to know whether the table was updated or not, you can catch @@rowcount when you are done. (And this can be
done outside the dynamic SQL.)

Then again, why not just say:

ALTER TABLE tbl ALTER COLUMN col <datatype> NOT NULL DEFAULT ''


--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

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


Jul 23 '05 #5
(ge******@hotma il.com) writes:
Thank you everyone for all your replies.
Is there a performance difference for inserts when we make it Not Null
default ''VS when it is Null? We will be inserting a lot of data


I don't remember exactly how NULL varchar values are stored. Possibly the ''
take up two bytes more than the NULL.

In any case, my opinion for all considerations like these is that you
should foremost look at this from a functional and logical point of view.
If all your values are known, but can be known to be empty use ''. If you
can have unknown values, permit NULL.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2200
by: Dennis Roberts | last post by:
I have a script to parse a dns querylog and generate some statistics. For a 750MB file a perl script using the same methods (splits) can parse the file in 3 minutes. My python script takes 25 minutes. It is enough of a difference that unless I can figure out what I did wrong or a better way of doing it I might not be able to use python (since most of what I do is parsing various logs). The main reason to try python is I had to look at...
3
949
by: Andy Tran | last post by:
I built a system using mysql innodb to archive SMS messages but the innodb databases are not keeping up with the number of SMS messages coming in. I'm looking for performance of 200 msgs/sec where 1 msg is 1 database row. I'm running on Red Linux: 2.4.20-8bigmem #1 SMP Thu Mar 13 17:32:29 EST 2003 i686 i686 i386 GNU/Linux The machine has dual CPU and 2G of RAM.
2
4906
by: Jaidev Paruchuri | last post by:
I have a table called work_order which has over 1 million records and a contractor table which has over 3000 records. When i run this query ,it takes long time since its grouping by contractor and doing multiple sub SELECTs. is there any way to improve performance of this query ?? ------------------------------------------------- SELECT ckey,cnam,t1.contractor_id,count(*) as tcnt,
0
2142
by: jaggee | last post by:
Hello, This is regarding log analysis of a web system, I am finding my backend SQL programming has taken so much of time to process the application due to following quires for a log table having 500 million records, i have already done index tuning , but no use, so I would like to change the following quires into a single stored procedure in order to reduce Client server network traffic and to increase performance. Anybody can help me to...
11
6966
by: Dave [Hawk-Systems] | last post by:
have the table "numbercheck" Attribute | Type | Modifier -----------+------------+---------- svcnumber | integer | not null svcqual | varchar(9) | svcequip | char(1) | svctroub | varchar(6) | svcrate | varchar(4) | svcclass | char(1) | trailer | varchar(3) |
0
296
by: rohit mehta via DotNetMonster.com | last post by:
There is c# code written for Plumtree Portal. It has been noticed that the aspnet_wp Handle Count on the Portal server is gradually increasing and hence a gradual decrease in the performance. Can anybody provide more details about this aspnet_wp handle count and the performance counter and ways to keep the handle count within the prescribed limits? i read somewhere that Handle Count: Threshold is 10000 A handle count of 2000 in aspnet_wp is...
20
2106
by: John Mark Howell | last post by:
I had a customer call about some C# code they had put together that was handling some large arrays. The performance was rather poor. The C# code runs in about 22 seconds and the equivalent C++.Net code runs in 0.3 seconds. Can someone help me understand why the C# code performance is so poor? I rewote the C# code to use a single dimenional array and the time went down to about 3 seconds, but that's still no explaination as to why the...
48
4456
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
1
3888
by: Billy | last post by:
Hi All, I'm attempting to use the MapNetworkDrive <snippedbelow from entire code below with very poor performance results. Basically, I have very small 73kb text files that are rewritten daily to a network share using the MapNetworkDrive command to connect to the valid \\servername\share path. After successfully connecting the drive in the ASP code, the text file
2
2766
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known 1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver 2) In that programme I m using prepared statement instead of statement 3) I am...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.