- RECORD the things, so we can "view offline" later on.

------------------------------------------------------------
0: Just a smell of what they are going to get...
------------------------------------------------------------
- Start with a small cache 20M: sp_cacheconfig "default data cache", "20M"
- Generate 'dest3' type....
- Start 1 worker...
Watch:
	- De-Queue size / speed
	- What table is this on... how many insert per second

------------------------------------------------------------
1: To small cache
------------------------------------------------------------
- Start with a small cache 20M
- Generate 'dest2' type....
- Start 1 worker
Watch:
	- CPU Usage
	- What table is this on
FIX:
	- sp_cacheconfig "default data cache", "90M"
Result:
	- all physical IO's goes to logical IO (in memory table scan)

------------------------------------------------------------
2: Fix in-memory table scan
------------------------------------------------------------
- Continue from the above... (dest2)
Watch:
	- Object tab:            ok Logical IO on the table
	- Active Statements Tab: I cant really se what statement is killing mre here (the in-mem-scan is to fast)
	- Tool; Capture SQL:     identify what proc/SQL statement that is responsible + SHOWPLAN
	- Object Tab + right click: sp_help / sp_helpindex
FIX:
	- create nonclustered index DummyLookupTab2_ix1 on DummyLookupTab2(intCol1)
Result:
	- Everyting now works much faster... 
		- NOT so much Logical IO
		- Dequeue per second & Trans per second is higher 


------------------------------------------------------------
3: Find long running SQL Statement
------------------------------------------------------------
- Generate 'dest1' type....
- Start 1-3 worker
Watch:
	- De-Queue per seconds (is not going up)
	- CPU usage?
	- Active Statements / processes
	- Proc Call Stack
FIX:
	- fix trigger to do waitfor delay "00:00:00"
	- create nonclustered index DummyLookupTab1_ix1 on DummyLookupTab1(intCol1)



------------------------------------------------------------
4: Find locking statements
------------------------------------------------------------
- Continue from 3, since this is a add-on to exercise 3
Watch:
	- Objects, Active statements
FIX:
	- alter table qStatInfo lock datarows
	- disable qStatInfo update...
	- alter table DestTab1     lock datarows
	- alter table DestTab1Hist lock datarows
	(maybe DestTab2 & DestTab3 as well)

dump tran perfdemo with truncate_only

------------------------------------------------------------
5: Time to add more engines
------------------------------------------------------------
- Continue from 4, since this is a add-on to privious exercise

Watch:
	- Spinlock contention
	- Processes, check Checkpoint... and HK...
	- Cached Procedure: EXEC COUNT
FIX:
	HK,Checkpoint: 
		truncate table DestTab1
		truncate table DestTab1Hist
		truncate table DestTab2
		truncate table DestTab3
		dump tran perfdemo with truncate_only
	- try add more engines
		sp_engine "online"
		select * from master..sysengines

------------------------------------------------------------
x: Create Used Defined Counter Set
------------------------------------------------------------

------------------------------------------------------------
x: Offline Capabilities
------------------------------------------------------------










/*---------------------------------------------------------------
** engines
*/
sp_engine "online"
sp_engine "offline"
select * from sysengines


/*---------------------------------------------------------------
** locks
*/
alter table qStatInfo lock allpages
alter table qStatInfo lock datarows


/*---------------------------------------------------------------
** indexes
*/
create unique clustered index XXX on XXX()
create        clustered index XXX on XXX() with allow_dup_row

create unique nonclustered index XXX on XXX()
create        nonclustered index XXX on XXX()


-- INDEX on: DummyLookupTab1
create nonclustered index DummyLookupTab1_ix1 on DummyLookupTab1(intCol1)
go

/*---------------------------------------------------------------
** XXX
*/
sp_dboption "perfdemo", "trunc log on chkpt", true
sp_dboption "perfdemo", "trunc log on chkpt", false
go
sp_helpdb "perfdemo"

/*---------------------------------------------------------------
** MEMORY
*/
sp_configure "max memory", 0, "300M"
sp_cacheconfig "default data cache", "90M"




/*---------------------------------------------------------------
** GRAPH
*/
select dbname='perfdemo', name, rowsInTable = row_count(db_id('perfdemo'), id)
from perfdemo..sysobjects 
where type = 'U' 
  and name like 'DestTab%'
order by name

### ================================================================================
### BEGIN: UDC(User Defined Counter) - perfDemo1
### --------------------------------------------------------------------------------
udc.perfDemo1.name              = perfDemo1
udc.perfDemo1.displayName       = perf Demo 1
udc.perfDemo1.description       = number of rows in some of the tables
udc.perfDemo1.sql               = select dbname='perfdemo', name, rowsInTable = row_count(db_id('perfdemo'), id) \
from perfdemo..sysobjects  \
where type = 'U'  \
  and name like 'DestTab%' \
order by name \

udc.perfDemo1.needVersion       = 0
udc.perfDemo1.pk                = dbname, name
udc.perfDemo1.diff              = rowsInTable
udc.perfDemo1.negativeDiffCountersToZero = true
udc.perfDemo1.graph              = true
udc.perfDemo1.graph.type         = byRow
udc.perfDemo1.graph.name         = perfDemo1Graph
udc.perfDemo1.graph.label        = Perf Demo 1 Graph
udc.perfDemo1.graph.menuLabel    = perf Demo 1 Graph
udc.perfDemo1.graph.data.cols    = rowsInTable
udc.perfDemo1.graph.data.methods = rateVal
udc.perfDemo1.graph.data.labels  = -pk-
### ================================================================================
### END: UDC(User Defined Counter) - perfDemo1
### --------------------------------------------------------------------------------

select name, rowsInTab=row_count(db_id('perfdemo'), id)
from perfdemo..sysobjects 
where type = 'U' 
  and name = 'TestQueue'
order by name
### ================================================================================
### BEGIN: UDC(User Defined Counter) - QueueSize
### --------------------------------------------------------------------------------
udc.QueueSize.name              = QueueSize
udc.QueueSize.displayName       = QueueSize
udc.QueueSize.description       = QueueSize
udc.QueueSize.sql               = select name, rowsInTab=row_count(db_id('perfdemo'), id) \
from perfdemo..sysobjects  \
where type = 'U'  \
  and name = 'TestQueue' \
order by name \

udc.QueueSize.needVersion       = 0
udc.QueueSize.pk                = name
udc.QueueSize.diff              = rowsInTab
udc.QueueSize.negativeDiffCountersToZero = false
udc.QueueSize.graph              = true
udc.QueueSize.graph.type         = byRow
udc.QueueSize.graph.name         = QueueSizeGraph
udc.QueueSize.graph.label        = QueueSize Graph
udc.QueueSize.graph.menuLabel    = QueueSize Graph
udc.QueueSize.graph.data.cols    = rowsInTab
udc.QueueSize.graph.data.methods = rateVal
udc.QueueSize.graph.data.labels  = -pk-
### ================================================================================
### END: UDC(User Defined Counter) - QueueSize
### --------------------------------------------------------------------------------



sp_who
sp_configure "number of user connections", 50

	set clientapplname "qConsumer",
	    clienthostname "sdfgsdfgsdg sd fgsdfg s sdkljfh lsdkfj hldskh fldksf ",
	    clientname     "sdfgsdfgsdfgsdfg sdfg sdlfhj gsdfh g"

	set clientapplname "xxxxx",
	    clienthostname "sdfgsdfgsdg sd fgsdfg s",
	    clientname     "sdfgsdfgsdfgsdfg"

select spid, clientapplname, clienthostname, clientname
from sysprocesses where spid = @@spid

select * from master..sysprocesses

qConsumer

create database perfdemo
use perfdemo

create table qStatInfo
(
	operation	varchar(30)	not null,
	execCounter	int		not null,
	lastExecTime	datetime	not null,
	
	avgExecTimeMs	int		not null,
	minExecTimeMs	int		not null,
	maxExecTimeMs	int		not null,
	
	primary key(operation)
)
go
insert into qStatInfo values('dummy', 0, getdate(), 0,0,0,0)
if ((select object_id('qStatInfo')) is not null)
begin
	print "  drop table: qStatInfo"
	drop table qStatInfo
end
go
print "create table: qStatInfo"
go
create table qStatInfo
(
	operation	varchar(30)	not null,
	execCounter	int		not null,
	lastExecTime	datetime	not null,
	
	lastExecTimeMs	int		not null,
	avgExecTimeMs	int		not null,
	minExecTimeMs	int		not null,
	maxExecTimeMs	int		not null,
	
	primary key(operation)
)
go
select max(1,1)


drop proc destProc1
go
drop proc destProc2
go
drop proc destProc3
go

select row_count(db_id(), object_id('TestQueue'))

dump tran perfdemo with truncate_only

sp_helpdb
sp_helpdevice

use perfdemo
sp_configure "locks", 100000
sp_configure "max memory", 0, "200M"





qGenerate 
exec qGenerate null, null

sp_configure "max online engines", 2



