Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2012-03-30 : 17:08:44
|
[code]I wonder if there is a way to combine into 1 or 2 statements instead of branch to multiple IF..ELSE statement.Any help is greatly appreciate. I am using SQL 2008.Thanks.------------------------------------------------------------------IF OBJECT_ID('dbo.spGetMetrics1', 'p') IS NOT NULL DROP PROCEDURE [dbo].[spGetMetrics1]GOCREATE PROCEDURE [dbo].[spGetMetrics1]( @OperationId INT = NULL, @FromDateTime DATETIME = NULL, @ToDateTime DATETIME = NULL, @Stores NVARCHAR(2000) = NULL, @Workstations NVARCHAR(2000) = NULL, @SelectMode TINYINT = 0)ASSET NOCOUNT ON;SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; IF ( (@SelectMode = 0 ) AND (@Stores = '' OR @Stores IS NULL) AND (@Workstations = '' OR @Workstations IS NULL) ) BEGIN PRINT 'Go to the first 1.' SELECT (b.MSecs/1000.0) AS MSecs, b.Operation, b.WSKey, b.ServTime, a.Store_No FROM WSCONFIGHT AS a JOIN MetricsHt AS b ON (a.P_Key = b.WSKey) WHERE ( b.Operation = @OperationId ) AND ( b.ServTime >= @FromDateTime ) AND ( b.ServTime < @ToDateTime ) END ELSE IF ( @SelectMode = 1 ) AND (@Stores > '') AND ( @Workstations = '' OR @Workstations IS NULL) BEGIN PRINT 'Go to #2.' SELECT (b.MSecs/1000.0) AS MSecs, b.Operation, b.WSKey, b.ServTime, a.Store_No FROM WSCONFIGHT AS a JOIN MetricsHt AS b ON (a.P_Key = b.WSKey) WHERE ( b.Operation = @OperationId ) AND ( b.ServTime >= @FromDateTime ) AND ( b.ServTime < @ToDateTime ) AND ( a.Store_No IN ( SELECT s FROM SPLITSTRING(@Stores, ',') ) ) END ELSE BEGIN PRINT 'Go to #3' SELECT (b.MSecs/1000.0) AS MSecs, b.Operation, b.WSKey, b.ServTime, a.Store_No FROM WSCONFIGHT AS a JOIN MetricsHt AS b ON (a.P_Key = b.WSKey) WHERE ( b.Operation = @OperationId ) AND ( b.ServTime >= @FromDateTime ) AND ( b.ServTime < @ToDateTime ) AND ( b.WSKey IN (SELECT s FROM SPLITSTRING(@Workstations, ',') ) ) END;GO---------------------------------------------------------------Testing...--1EXECute dbo.spGetMetrics1 @SelectMode = 0, @OperationId = 10400, @FromDateTime = '2012-01-30 10:05:04.553', @ToDateTime = '2012-03-30 10:05:04.553', @Stores = '', @Workstations = '' --#2 Call with storesEXECute dbo.spGetMetrics1 @SelectMode = 1, @OperationId= 10400, @FromDateTime= '2012-01-30 10:05:04.553', @ToDateTime = '2012-03-30 10:05:04.553', @Stores = N'100,230,316,327', @Workstations= N'';-- 3 call with workstationsEXECute dbo.spGetMetrics1 @SelectMode = 2, @OperationId = 10400, @FromDateTime = '2012-01-30 10:05:04.553', @ToDateTime = '2012-03-30 10:05:04.553', @Stores = N'100,230,316,327', @Workstations = N'733,852,80000087,80000107';go[/code] |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2012-03-30 : 18:06:19
|
[code]Here is the statistic.SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.MSecs Operation WSKey ServTime Store_No--------------------------------------- --------- ----------- ----------------------- --------1.625000 10400 712 2012-02-14 16:42:59.000 3431.796000 10400 80000009 2012-02-14 16:42:24.000 882.172000 10400 746 2012-02-14 16:42:22.000 322Table 'WSCONFIGHT'. Scan count 9, logical reads 52, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table 'METRICSHT'. Scan count 9, logical reads 347041, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 19440 ms, elapsed time = 5442 ms. SQL Server Execution Times: CPU time = 19440 ms, elapsed time = 5443 ms. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.Query time (minutes:seconds) 0 :5 [/code]quote: Originally posted by tkizer Read uncommitted!?Show us the statistics io and time plus the execution plan.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
|
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2012-03-30 : 18:53:25
|
[code]Here is the indexes on MetricHT table. Thanks.EXECute sp_helpindex MetricsHt;goindex_name index_description index_keys------------------------ ---------------------------------------------------- ----------------------nci_OperationServTime nonclustered located on PRIMARY Operation, ServTimenci_WSKey nonclustered located on PRIMARY WSKeyXPKPKey clustered, unique, primary key located on PRIMARY P_KEY[/code]quote: Originally posted by tkizer You are clearly missing an index on the METRICSHT table as per the logical reads shown in that output. Could you show us your indexes on that table?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
|
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
|
NguyenL71
Posting Yak Master
228 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|