Question : i just need speed   (one procedure, five servers, real-time)

have two databases -- X and Y
- X takes a real-time feeds of live trades
- Y is redundant copy of the same feeds
- we have 3 instances of X, and 2 instances of Y

we monitor all instances of both datasets real-time, simply for volume, by exchange.  all trades go into an 'order' table, and we simply do a sum(quantity) for the current volume, at any time, for each exchange.  (ie CCC, DDD, etc.)

the code has been in place forever -- one proc on each X and Y instance, doing the same thing.  we've got three X's and two Y's, so the front-end executes the same proc five different times, to get volume from all 5 servers.  (all linked)

it MUST be re-written, i'm thinking synoyms, cte's, etc.  I put 2 'new' procs together (Xproc, Yproc) -- for Xproc, I create a temp table for the results, and walk through all 3 instances, writing serveralias,sum(quantity) to temp table, from each instance.  then i return from #results to the front end.  Yproc same thing, only targeting two Y servers.

- i gave it to them in haste, told them it wasn't ready, and now i gotta fix it.  :-(
- I know it is not optimal, I frequently see ROLLBACKS, and deadlocks
- we trade 24/7, they want to run it 24/7
- they want ONE procedure to return volumes from ALL five instances


unfortunatley, there are conditions for three exchanges -- if @exchange = 'CCC', use this WHERE.   if @exchange = 'DDD', use this, and if @exchange = 'EEE', do this...  say we trade on 15 different exchanges, only three have conditions.  so mostly static, but three do have conditions.


all five instances are linked production servers
I created five synonyms local on all of them, referencing the remote servers:
    srvX1, srvX2, srvX3, srvY1, xrvY2

see the Xproc below --- way bad, i know  -- the Yproc is pretty much the same
(first run was a table variable, but the #temp table performed better)
the code below is referencing the synonyms -- but i don't care if it's servername.database.dbo.ordes, or the synonym  -- i just need speed.

and i need to get X and Y counts from the execution of ONE proc


please advise
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
CREATE PROC Xproc
  @Start datetime,
  @End datetime,
  @exchange varchar(8)=NULL
AS
SET NOCOUNT ON
BEGIN
CREATE TABLE #results (ID INT NULL, srv1 VARCHAR(50) NULL, srv2 VARCHAR(50) NULL,srv3 VARCHAR(50) NULL)
CREATE TABLE #userIDs(userID varchar(16) PRIMARY KEY(userID))
INSERT #userIDs (userID)
SELECT DISTINCT userID FROM dbo.orders WITH (NOLOCK) WHERE userID LIKE '%FF%'
CREATE TABLE #Symbols (Symbol varchar(25) not null,PRIMARY KEY(Symbol))
INSERT #symbols (Symbol)
SELECT DISTINCT Symbol FROM dbo.orders WHERE Symbol LIKE 'xoxo' OR Symbol LIKE '%[:-]%'

	IF(@exchange = 'CCC')
	BEGIN
		INSERT #results (ID,srv1)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 
		FROM dbo.srv1X p WITH (NOLOCK) 
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange='CCC')
		AND NOT EXISTS(SELECT 1 FROM #userIDs t WHERE t.userID = p.userID)
		AND NOT EXISTS(SELECT 1 FROM #Symbols s WHERE s.Symbol = p.Symbol)
		
		INSERT #results (ID,srv2)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 
		FROM dbo.srv2X p WITH (NOLOCK) 
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange='CCC')
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		AND NOT EXISTS(SELECT 1 FROM #Symbols s WHERE s.Symbol = p.Symbol)

		INSERT #results (ID,srv3)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 
		FROM dbo.srv3X p WITH (NOLOCK) 
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange='CCC')
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		AND NOT EXISTS(SELECT 1 FROM #Symbols s WHERE s.Symbol = p.Symbol)
	END
	ELSE
	IF(@exchange = 'DDD')
	BEGIN
		INSERT #results (ID,srv1)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 	
		FROM dbo.srv1X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = 'DDD')
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		AND NOT EXISTS(SELECT 1 FROM #Symbols s WHERE s.Symbol = p.Symbol)

		INSERT #results (ID,srv2)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 	
		FROM dbo.srv2X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = 'DDD')
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		AND NOT EXISTS(SELECT 1 FROM #Symbols s WHERE s.Symbol = p.Symbol)		
		
		INSERT #results (ID,srv3)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 	
		FROM dbo.srv3X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = 'DDD')
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		AND NOT EXISTS(SELECT 1 FROM #Symbols s WHERE s.Symbol = p.Symbol)
	END
	ELSE
	IF(@exchange = 'EEE')
	BEGIN
		INSERT #results (ID,srv1)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 		
		FROM dbo.srv1X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = 'EEE')

		INSERT #results (ID,srv2)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 		
		FROM dbo.srv2X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = 'EEE')

		INSERT #results (ID,srv3)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 		
		FROM dbo.srv3X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = 'EEE')
	--	AND client <> 'TT'	REMOVED PER MR. COWARD, 10/22/09
	END
	ELSE
	BEGIN
		INSERT #results (ID,srv1)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 		
		FROM dbo.srv1X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = @exchange )
		AND Symbol NOT LIKE 'xoxo'
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		
		INSERT #results (ID,srv2)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 		
		FROM dbo.srv2X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = @exchange )
		AND Symbol NOT LIKE 'xoxo'
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)
		
		INSERT #results (ID,srv3)
		SELECT 1,CONVERT(VARCHAR,SUM(Quantity) ) 		
		FROM dbo.srv3X p WITH (NOLOCK)
		WHERE p.Timefield>=@Start
		AND p.Timefield<=@End
		AND (@exchange IS NULL OR p.exchange = @exchange )
		AND Symbol NOT LIKE 'xoxo'
		AND NOT EXISTS(SELECT userID FROM #userIDs t WHERE t.userID = p.userID)				
	END

	
	SELECT a.srv1,b.srv2,c.srv3 FROM #results a INNER JOIN #results b
		ON a.ID = b.ID AND a.srv1 is not null and b.srv2 is not null 
		INNER JOIN #results c ON a.ID = c.ID 
			and c.srv3 is not null

	IF OBJECT_ID('tempdb..#results') IS NOT NULL
	DROP TABLE #results;
	IF OBJECT_ID('tempdb..#symbols') IS NOT NULL
	DROP TABLE #symbols;
	IF OBJECT_ID('tempdb..#userIDs') IS NOT NULL
	DROP TABLE #userIDs;

END

SET NOCOUNT OFF

GO

Answer : i just need speed   (one procedure, five servers, real-time)

Instead of this:

CREATE TABLE #results
        (ID INT NULL,
         srv1 VARCHAR(50) NULL,
         srv2 VARCHAR(50) NULL,
         srv3 VARCHAR(50) NULL)
...

SELECT  1,
              CONVERT(VARCHAR, SUM(Quantity))...
...

SELECT  a.srv1,
            b.srv2,
            c.srv3
FROM    #results a

...

Use this:
CREATE TABLE #results
        (ID tinyint NOT NULL,
         srv1 integer NULL,
         srv2 integer NULL,
         srv3 integer NULL)
...

SELECT  1,
              SUM(Quantity)
...

SELECT  CONVERT(varchar(10), a.srv1) srv1,                -- Change length appropriately
            CONVERT(varchar(10), b.srv2) srv2,                  -- Change length appropriately
            CONVERT(varchar(10), c.srv3) srv3                    -- Change length appropriately
FROM    #results a
...
Random Solutions  
 
programming4us programming4us