473,387 Members | 1,573 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,387 software developers and data experts.

Optimized query

20
Hello,

I am looking for some Query Optimization help,

We are running a web application that is growing experientially in traffic and we are not running into Problems with Deadlocks on the SQL database.

After analyzing the logs 90% of our deadlocks happen from the main login page(the page hit after login)

This page doubles as the first page you see and as the main work area.

The old way was every time someone did something the page would refresh, the values displayed would be updated.

To help alleviate the stress onto the sql we instead set it on a timer, so page will auto refresh every 600 seconds or when the client hits “Refresh” on the browser.

This page basically displays the number of documents that had errors, the number that had no errors, ones that where so bad we could not display properly and ones that arrived with out any identifier.

It also shows documents archived or rejected by users after review, and documents they exported out of the system as complete.

I am looking for the most efficient sql query to run to get all this data. The data is in multiple tables.

Right now my most efficient attempt is in a stored procedure and looks like this:

Expand|Select|Wrap|Line Numbers
  1. create proccedure
  2. @value char(50)
  3. AS
  4.  
  5. DECLARE @AWAITINGREVIEW int
  6. DECLARE @REJECTED int
  7. DECLARE @RELEASED int
  8. DECLARE @ARCHIVED int
  9. DECLARE @NOPRVDR int
  10. DECLARE @TRANSERR int
  11.  
  12.  
  13. SELECT 
  14. @TRANSERR = (SELECT count(Col1) FROM table1 DI
  15. WHERE DI.col2 = 'TransErr' AND DI.col3 = @value ),
  16. @NOPRVDR = (SELECT count(Col1) FROM table1 DI
  17. WHERE  DI.col2 = 'NoPrvdr' and DI.col3 = @value )
  18.  
  19.  
  20. SELECT 
  21. @AWAITINGREVIEW = (SELECT count(Col1) FROM table1 DI
  22. WHERE  DI.Col4 = 'Pending' AND DI.col2 = 'VERIFY' AND DI.Col5 = 'Data Validation' AND DI.col3 = @value),
  23. @REJECTED = (SELECT count(Col1) FROM table1 DI
  24. WHERE  DI.Col4 = 'Pending' AND DI.col2 = 'Cancelled' AND DI.Col5 = 'Archived' AND DI.col3 = @value),
  25. @RELEASED = (SELECT count(Col1) FROM table1 DI
  26. WHERE  DI.col2 = 'Release' AND DI.Col5 = 'Export' AND DI.col3 = @value),
  27. @ARCHIVED = (SELECT countCol1) FROM table1 DI
  28. WHERE  DI.Col4 = 'Complete'  AND DI.Col5 = 'Archived' AND DI.col3 = @value)
  29.  
  30.  
  31. SELECT @TRANSERR as TRANSERR, @NOPRVDR as NOPRVDR, @AWAITINGREVIEW as AWAITINGREVIEW, @REJECTED as REJECTED, @RELEASED as RELEASED, @ARCHIVED as ARCHIVED
  32.  
  33. GO
Let me know what you think.

Using SQL Server 2000
Nov 7 '07 #1
3 1465
Jim Doherty
897 Expert 512MB
Hello,

I am looking for some Query Optimization help,

We are running a web application that is growing experientially in traffic and we are not running into Problems with Deadlocks on the SQL database.

After analyzing the logs 90% of our deadlocks happen from the main login page(the page hit after login)

This page doubles as the first page you see and as the main work area.

The old way was every time someone did something the page would refresh, the values displayed would be updated.

To help alleviate the stress onto the sql we instead set it on a timer, so page will auto refresh every 600 seconds or when the client hits “Refresh” on the browser.

This page basically displays the number of documents that had errors, the number that had no errors, ones that where so bad we could not display properly and ones that arrived with out any identifier.

It also shows documents archived or rejected by users after review, and documents they exported out of the system as complete.

I am looking for the most efficient sql query to run to get all this data. The data is in multiple tables.

Right now my most efficient attempt is in a stored procedure and looks like this:

Expand|Select|Wrap|Line Numbers
  1. create proccedure
  2. @value char(50)
  3. AS
  4.  
  5. DECLARE @AWAITINGREVIEW int
  6. DECLARE @REJECTED int
  7. DECLARE @RELEASED int
  8. DECLARE @ARCHIVED int
  9. DECLARE @NOPRVDR int
  10. DECLARE @TRANSERR int
  11.  
  12.  
  13. SELECT 
  14. @TRANSERR = (SELECT count(Col1) FROM table1 DI
  15. WHERE DI.col2 = 'TransErr' AND DI.col3 = @value ),
  16. @NOPRVDR = (SELECT count(Col1) FROM table1 DI
  17. WHERE DI.col2 = 'NoPrvdr' and DI.col3 = @value )
  18.  
  19.  
  20. SELECT 
  21. @AWAITINGREVIEW = (SELECT count(Col1) FROM table1 DI
  22. WHERE DI.Col4 = 'Pending' AND DI.col2 = 'VERIFY' AND DI.Col5 = 'Data Validation' AND DI.col3 = @value),
  23. @REJECTED = (SELECT count(Col1) FROM table1 DI
  24. WHERE DI.Col4 = 'Pending' AND DI.col2 = 'Cancelled' AND DI.Col5 = 'Archived' AND DI.col3 = @value),
  25. @RELEASED = (SELECT count(Col1) FROM table1 DI
  26. WHERE DI.col2 = 'Release' AND DI.Col5 = 'Export' AND DI.col3 = @value),
  27. @ARCHIVED = (SELECT countCol1) FROM table1 DI
  28. WHERE DI.Col4 = 'Complete' AND DI.Col5 = 'Archived' AND DI.col3 = @value)
  29.  
  30.  
  31. SELECT @TRANSERR as TRANSERR, @NOPRVDR as NOPRVDR, @AWAITINGREVIEW as AWAITINGREVIEW, @REJECTED as REJECTED, @RELEASED as RELEASED, @ARCHIVED as ARCHIVED
  32.  
  33. GO
Let me know what you think.

Using SQL Server 2000

Seems pretty much standard to me? make sure indexes are on the correct columns in any WHERE clauses. You might also want to consider the WITH (NOLOCK) table hint in the FROM clause of a SELECT. (if you can afford your table not to be locked during a select that is..performance is improved if you can.... less overhead for the server)

Lookup the hint in SQL (Books online) for a full description of its useage)

Regards

Jim :)
Nov 8 '07 #2
Javilen
20
Hey,

That is exactly what we needed, I implemented it and found immediately a decrease in the Deadlocking.

I also think that just the changes this made for the main page will have a residual effect through out the rest of the site which is nice.

We will be running it like this for a week to monitor the actual improvements but I wanted to thank you for the answer you gave as it seems to have worked wonderfully.

Thanks
Nov 8 '07 #3
Jim Doherty
897 Expert 512MB
Hey,

That is exactly what we needed, I implemented it and found immediately a decrease in the Deadlocking.

I also think that just the changes this made for the main page will have a residual effect through out the rest of the site which is nice.

We will be running it like this for a week to monitor the actual improvements but I wanted to thank you for the answer you gave as it seems to have worked wonderfully.

Thanks
You're very welcome I am pleased it worked for you

Regards

Jim :)
Nov 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: StinkFinger | last post by:
All, Been reading other posts on other forums, i.e. Nukecops. My original code is this: function is_active($module) { global $prefix, $dbi; $result = sql_query("select active from...
3
by: Glen Low | last post by:
I have written a new implemention of the std::valarray library that is optimized to use Altivec (Apple's "Velocity Engine", part of the PowerPC G4's in most Macintoshes and the announced IBM PPC...
1
by: Peder Bacher | last post by:
Hello :-) My question is: If I query a partitioned view, but don't know the values in the "where x in(<expression>)" clause, i.e.: select * from viewA where intVal in(select intVal from tbl1) ....
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
10
by: Mike | last post by:
Is it still true that the managed C++ compiler will produce much better opimizations than the C# compiler, or have some of the more global/aggressive opimizations been rolled into the 2005...
4
by: Michel Esber | last post by:
Hello, DB2 LUW V8 FixPack 13. create table Table (ID varchar(20), USED char) I need to find out the total row count per ID, as well as the row count where USED=Y. I could do this with a...
17
by: Grizlyk | last post by:
Hello. What can be optimised in C++ code and how i can garantee stable behaviour below 1. Are expression "auto volatile" can deny removing as "unused temporary" like this: auto volatile...
7
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression...
0
by: Doranj00 | last post by:
Hello Everyone, Its been awhile since developing SQL code and I have a dilemma. Now, I can think of several ways to get what I need, BUT the important thing is that I need the optimized way to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.