Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need help writing a C# console app.

Posted on 2011-03-10
5
Medium Priority
?
348 Views
Last Modified: 2013-12-17
Hi Experts,
I need help writing a C# console application with SQL Server 2005 DB.  The table I will be querying has thousands of records so It needs to be multi-threaded.  The program needs two functions.  
One that calculates the Product of all ItemCost values by ItemCategory.
The second function needs to calculate the Median value of ItemCost by ItemCategory.

The name of my SQL table is  INVENTORY with the fields ItemCost (float), ItemCategory (varchar).

How can I do this?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35102040
Why does this need to be multi-threaded? "Thousands" is not very large in terms of DB processing.
0
 

Author Comment

by:mainrotor
ID: 35102634
It has to be multi-threaded because it will eventually grow to millions of records.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1000 total points
ID: 35109713
I will offer some broad advice, if you're interested...

Collect all the unique ItemCategory values using SELECT DISTINCT ItemCategory FROM INVENTORY, use these results to populate a pair of List<string>'s (one for product, one for median).  Create two Dictionary<string, float>'s - one each for the product results, and the median results.

Spawn two threads, one to calculate the product and one to find the median.

In each of the threads, grab the next 5 ItemCategory values and remove them from the List (make sure to use "lock" keyword, or some other thread synchronization mechanism).  For each of those 5 ItemCategory values spawn another thread that does the actual work - perhaps passing this thread a Delegate to be executed on completion to provide the result.

Of course, I have no idea how efficient multi-threading like this will be since I'm not sure if opening too many simultaneous connections to the SQL server will hurt performance, and ultimately all the threads are gonna be hitting the same SQL server anyway.

You should also tweak the number of threads that are running simultaneously to match the number of processor cores in your system (i.e. on a single processor system only one thread will be running at any given time anyway, so creating additional threads doesn't do much in the way of a performance increase; however, a system with two quad-core processors can run 8 threads simultaneously).
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35109757
...and also, if it's a remote SQL server, all the connections will be using the same network connection.  So if it's a lot of data being transferred, that might present a bottleneck too.  
0
 
LVL 8

Assisted Solution

by:Volox
Volox earned 1000 total points
ID: 35118517
Is your SQL server decently powered?  And is there any reason to do this outside of SQL server?

The reason I ask the above questions is because if you are talking about wanting to scale to millions of rows, the absolutely least efficient thing you can do is pull millions of rows OUT of SQL to do a calculation on them that you could do inside of SQL server.  Just the network consumption alone of pulling that much data out of SQL server is a waste of resources and time.

I'm not sure I'm clear on what you meant by a 'product' by category...?  Are you saying you have quantity and a price per quantity and have to calc a total price and then sum it?  Which would look like =>
SELECT SUM(Quantity * Price), Category FROM Items GROUP BY Category
If it's something different you are after, then please describe and I'm sure someone can help you come up with a solution.

Here is an article on how one can calculate medians within SQL server
http://sqlblog.com/blogs/adam_machanic/archive/2006/12/18/medians-row-numbers-and-performance.aspx 

And I'd also mention that if you have the disk space on SQL and you query for the total price more often than you change the per item price or quantity, then you might give consideration to using a computed column so that the total price is calculated for you within the table.  But be sure you read about the performance impacts on both sides of the equation before you implement.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question