Multiple CPUs - SQL2005 - Windows 2003 -

We are developing a application against the SQL2005 / Windows 2003 server (4 CPUs), when we run the application which has heavily sql queries, the CPU performance of the SQL server only see one CPU at a time being pegged at 100%, while others are pretty much quiet, but the 100% peak takes turns from one CPU to the other. My question is that:
1) how is it possible to have the CPU usage be evenly spread between the 4 CPUs?
2) why I see 8 CPU instants in Windows Performance task manager other than 4?
3) Is the OS, SQL or App. itself controlling the CPU ultilization?

Who is Participating?
mastooConnect With a Mentor Commented:
Multi-threaded programming is complicated and I can't do the subject justice here.  Can I suggest a couple other things before you go that route?  They all have potential for boosting performance enough that you might not feel the need to thread your app.

#1.  If you're going to have several people running your app at the same time, in effect this is the same as threading since it means your database server will be running several queries at the same time.  In this case, threading would help a single person but when several people run the performance would be unchanged.

#2. Use a code profiler on your app to be sure it spends most of its time waiting on the database.  If it only spends 20% of its time waiting on the db, and you thread it to cut that in half, you've only boosted your performance by 10%.

#3. If your app is waiting on the db for most of the time, use sql profiler to identify the queries that take the longest and then optimize those queries.  A simple index change can have a dramatic effect on performance.

#4. If none of those help, then look at the SqlCommand methods like BeginExecuteReader and BeginExecuteNonQuery.  Those are the easiest ways to thread things when db performance is the bottleneck.  They limit the scope of the threading.  They make coding a little more difficult because code needs to start several queries (loading up multiple cpus) and then react when they queries finish.  If you're using databound controls, threading isn't an option.
DarrenDConnect With a Mentor Commented:

You can tell SQL Server how many CPU's to use so it could be just set to one.

You have to buy a licence per processor though under certain licence agreements.

If you right click on the SQL Server in Microsoft SQL Server Management Server and choose properties you can then select processors and see how many are being used

mastooConnect With a Mentor Commented:
After checking what DarrenD describes...

If you are running a single, single threaded app against Sql Server most activity will take place on a single processor.  In isolated cases, sql server might optimize a query by spreading it across more than one processor but that would be the exception rather than the general rule.  If your app is threaded or multiple instances run, they can be submitting multiple concurrent queries which will then cause more than one cpu to be used.

You'll see 8 if you have 4 processors that either are dual core or support hyperthreading.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

fshguoAuthor Commented:
I checked the SQL server can see 8 processors. and yes, it is dual core.

Now my question to Mastoo is that how can we make the change so that the App is multiple threaded?
fshguoAuthor Commented:
Thanks Wastoo, how code profiler knows how long it is spending waiting for database? simply a URL link will be helpful.  

I use ants:

Basically you run your program under the profiler.  When done, it will show you what percent of the total time was spent in what functions.  This lets you quickly identify where your code spends most of its time so you can focus on fixing the right chunk of code.  For example I might see that a button click takes 5 seconds, and of that time 25% is in a couple of database calls and the remaining time is spent populating a listview control.  In your case, if it leads to a couple stored procedure calls you'd know which procs you should try to optimize.

They let you download an eval copy for a trial period so you can give it a shot.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.