Showing posts with label process. Show all posts
Showing posts with label process. Show all posts

Monday, March 19, 2012

How to dynamically process a Model in a Web App

Hi,

I am a novice at Data Mining realm on SQL Server.

Scenario:

I have created a Time Series model and deployed it into SQL Server. I hope users can see forecast based on the up-to-date data residing in data source rather than the old ones used to train the model. In a addition, the interface provided for users is a .aspx.

Problems:

What ADO APIs should I exploit to dynamically process the model, perform the forecast and retrieve the results.

Any help would be appreciated.

Best Wishes,

Ricky.

You would likely use ADOMD.NET, not ADO.NEt, but the results would essentially be the same. You would likely want to create models on the fly for this solution, much the same way we do for the Excel addins. You can download the addins and use the trace mechanism to see what commands we send to the server.

Essentially, you want to use CREATE SESSION MINING MODEL, INSERT INTO (you can use an input rowset, or an openquery if your data is in a database), and then SELECT PredictTimeSeries(...) to get the forecast.

Using a session model will cause the model to automatically be deleted on disconnection. Note that you will have to turn on the server property to allow session models.

|||

Thanks a lot, Jamie.

Could you give a tutorial or exmaple code to see the deatils?

Regards,

Ricky.

|||The sample here (http://www.sqlserverdatamining.com/DMCommunity/LiveSamples/1866.aspx) creates and trains models dynamically|||

Many Thanks, Jamie

Regards,

Ricky.

|||

mr jamie...

this link seems to be dead

please send in the link again

|||

http://www.sqlserverdatamining.com/DMCommunity/LiveSamples/1866.aspx

|||

hi jamie..

could u look into this thread plz...

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1449395&SiteID=1

;m stuck at thiz prb...

i've reinstalled the dm viewer controls.......

now here is wat i read in the readme file as to how to use the dm viewer controls:

In the Winform designer, right click on the Toolbox and select 'Choose Items...' menu item. Hit the Brows button and select file 'Microsoft.AnalysisServices.Viewers.dll'. Hit the OK button to add all the viewer controls to your toolbox.

the prob is tat i can't find this file:'Microsoft.AnalysisServices.Viewers.dll'

do u think i've gone wrong somewhr in the installation of the viewer controls?

or is there somethin else that i must do?

Sunday, February 19, 2012

How to do bulk Delete

I have a table that has a primary key made of 3 fields (we don't want to use a surrogate key in this situation). In a particular process there is a work table that contains these 3 PK fields and we want to bulk delete them from the base table. Without looping thru the work table, how can I write a Delete statement to delete records in the base table using the work table rows as the criteria?

This syntax illustrates what I want to do, but is not allowed by SQL.

DELETE bt.* FROM basetable bt
INNER JOIN #work wk On bt.fld1 = wk.fld1 And bt.fld2 = wk.fld2 And bt.fld3 = wk.fld3

The correct DELETE statement using TSQL extension is below:

DELETE basetable

FROM basetable bt
INNER JOIN #work wk

ON bt.fld1 = wk.fld1 And bt.fld2 = wk.fld2 And bt.fld3 = wk.fld3

But best is to use the ANSI SQL syntax which doesn't have any ambiguity:

DELETE FROM basetable

WHERE EXISTS(SELECT * FROM #work as wk

WHERE wk.fld1 = basetable.fld1

AND wk.fld2 = basetable.fld2

AND wk.fld3 = basetable.fld3)

|||Ahhh!!! I tried so many variations .... except that one. Thanks.Big Smile

How to do a SELECT ROW LOCK or READPAST

I have an application which use a table to figure out the next work it need to process. Application is running from multiple machines & multi-threaded also. I am using a database table, let say WorkQueue to track all works.

WorkID, WorkName, Status are the column name of the table.

Status='NEW' is a new work

Status='INPROCESS' is in process

Status='COMPLETE' is complete

I am trying to write a stored procedure, which will update single record status from NEW to INPROCESS and return WorkID. I'm trying to prevent more than one instances of the process grabbing the same record where the status = 'NEW'

Help....

I have 2 suggestions:

First Suggestion:

Create a stored procedure that will return the next work item to be processed. In the case that there has been a conflict, the stored procedure will return a certain value (I've assumed this to be -2). From the code itself you will manage that if a -2 value returns, you need to re-execute the stored procedure. If a -1 value is retuned then there are no new items. Here is the procedure I wrote:

CREATE PROCEDURE GetNextWorkItem
AS
BEGIN

-- @.WorkID will contain either the next work item to be processed
-- or -1 if there is no available 'New' work items
-- or -2 if there was a conflict and it needs to be executed again
DECLARE @.WorkID AS INT

-- Get the next 'New' work item and put the ID in @.WorkID
SELECT TOP 1 @.WorkID = WorkID
FROM WorkQueue
WHERE Status = 'New'

-- If @.@.RowCount is 0 then there are no 'New' work items
-- Return -1
IF @.@.RowCount = 0
BEGIN
SELECT -1 AS WorkID
RETURN
END

-- Update the status of the work item you've selected
-- making sure that its status is still 'New'
UPDATE WorkQueue
SET Status = 'In Progress'
WHERE WorkID = @.WorkID
AND Status = 'New'

-- If @.@.RowCount is 0 then the status of this work item was changed before you could update it
-- A -2 value will be returned and the code should execute this stored procedure again
IF @.@.RowCount = 0
SELECT -2 AS WorkID
ELSE
SELECT @.WorkID AS WorkID

END
GO

Second Suggestion:

Add a new column called WorkKey (in my example its datatype is UniqueIdenitfier). You will randomly generate a value in the stored procedure and update the WorkKey field of the first 'New' work item with this value. A certain value will indicate that there are no new work items (-1 in my procedure). Here is the procedure I wrote:

CREATE PROCEDURE GetNextWorkItem
AS
BEGIN

-- @.WorkKey is a temporary variable that you will use to select the work item that you will process next
DECLARE @.WorkKey AS UniqueIdentifier
SET @.WorkKey = NewID()

-- Update the first 'New' available work item with the key you have just generated
UPDATE WorkQueue
SET WorkKey = @.WorkKey, Status = 'In Progress'
WHERE WorkID IN
(SELECT TOP 1 WorkID
FROM WorkQueue
WHERE Status = 'New')

-- If @.@.RowCount is 0 then there are no 'New' work items. Re-execute this stored procedure later
IF @.@.RowCount = 0
BEGIN
SELECT -1 AS WorkID
RETURN
END

-- Get the WorkID that you have just updated
SELECT WorkID
FROM WorkQueue
WHERE WorkKey = @.WorkKey

END
GO

Please tell me if this answers your question.

Best regards,
Sami Samir

|||

You could make use of the OUTPUT clause that's available in SQL Server 2005, see the example below. This would avoid you having to use table hints.

Chris

SET NOCOUNT ON
DECLARE @.WorkQueue TABLE (WorkID INT, WorkName VARCHAR(100), Status VARCHAR(10))
INSERT INTO @.WorkQueue VALUES (1, 'Answer a forum question.', 'NEW')
INSERT INTO @.WorkQueue VALUES (2, 'Wash the car.', 'NEW')
INSERT INTO @.WorkQueue VALUES (3, 'Have a beer.', 'LATER')
INSERT INTO @.WorkQueue VALUES (4, 'Feed the cat.', 'COMPLETE')

DECLARE @.Output TABLE (WorkID INT)

UPDATE w
SET w.Status = 'INPROCESS'
OUTPUT inserted.WorkID INTO @.Output
FROM @.WorkQueue w
WHERE w.WorkID = (SELECT TOP 1 w2.WorkID FROM @.WorkQueue w2 WHERE Status = 'NEW' ORDER BY w2.WorkID)

DECLARE @.WorkID INT
SELECT @.WorkID = WorkID FROM @.Output

SELECT *
FROM @.WorkQueue
WHERE WorkID = @.WorkID

|||

Below is a code sample that you could put in a procedure. The sample starts a transaction a does a select to get the workid and workstatus. I used 3 lock hints readpast, rowlock, holdlock and updlock. The readpast will allow better concurrency in a multiple user system, however it is possible that a row may not be returned in all pages or rows are locked at the time the procedure is invoked. The rowlock hint makes sure that a pagelock isn't used so more users can access the data. The holdlock and updlock will lock the rows for the duration of the transaction and make sure that no other user can acquire the data. I have used this technique before for "work queue" and it works well. HTH.

DECLARE @.WorkID INT

,@.WorkName VARCHAR(20)

BEGIN TRAN

SELECT @.WorkID = WorkID, @.WorkName = WorkName

FROM WorkQueue(HOLDLOCK,UPDLOCK,READPAST,ROWLOCK)

UPDATE WorkQueue SET status = 'In Process'

WHERE

WorkID = @.WorkID;

COMMIT TRAN

|||

we are planning to sql server 2000. Sorry I forget to mention it.

I see different way do it. any suggestion which one I should use

|||

I think your answer is correct one. does this Guarantee uniqueness between concurrent & multiple user system. Do anyone see a problem in using it

|||

I AM GETTING FOLLOWING ERROR WITH YOUR SQL

You can only specify the READPAST lock in the READ COMMITTED or REPEATABLE READ isolation levels

|||I added WITH before the locking hints and it worked fine. Here is the updated query:

SELECT @.WorkID = WorkID, @.WorkName = WorkName
FROM WorkQueue WITH (HOLDLOCK, UPDLOCK, READPAST, ROWLOCK)

Best regards,
Sami Samir

|||

Sorry about forgetting the with clause. Yes it will guarantee concurrency and uniqueness. The readpast technique is great for a work queue. Haven't found it to be helpful in many other situations. Since this is a work queue make sure you do not put any uncessary indexes on it and/or have connections access it via different paths (i.e. indexes). Since work queues by nature are "busy" tables you do not want to introduce deadlocking by having multiple procedures access the data down different retrieval paths. HTH.

|||

Look at the below sql and tell me what is wrong with it. it doesn't have any transaction before it. does this works

CREATE PROC dbo.GetNextWork @.WorkItem BIGINT=0 OUTPUT as

UPDATE dbo.WorkQueue

SET Status='InProcess',@.WorkItem=WorkItemID

WHERE WorkItemID = ( SELECT TOP 1 WorkItemID

FROM dbo.WorkQueue WITH (ROWLOCK,UPDLOCK,READPAST)
WHERE Status = 'NEW' ORDER BY WorkItemID )

Variable @.WorkItem will be returned from stored procedure

|||

That would work, I created some sample code below for the forum. Learned something new today, never tried variable assignment in an update statement.

create table #workqueue (

workitemid int,

status varchar(30)

);

insert into #workqueue (workitemid,status) values (1,'new')

insert into #workqueue (workitemid,status) values (2,'new')

Declare @.WorkItem int;

UPDATE #WorkQueue

SET Status='InProcess',@.WorkItem=WorkItemID

WHERE WorkItemID = ( SELECT TOP 1 WorkItemID

FROM #WorkQueue WITH (ROWLOCK,UPDLOCK,READPAST)

WHERE Status = 'NEW' ORDER BY WorkItemID )

select @.workitem