0

In the following code, I would like to insert or update rows in an SQL table through F#. It takes a matrix of tuple representing users and an associated scores (usrID,score) which are the results of some F# calculations.
Now I want to update a SQL table called UserScoresTable.
The code I wrote is working but is very slow.

let cnn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings.Item("Database").ConnectionString)
cnn.Open()

// Definition d'une fonction qui execute une demande sous MySQL
let execNonQuery s =
    let comm = new System.Data.SqlClient.SqlCommand(s, cnn, CommandTimeout = 10)
    comm.ExecuteNonQuery() |> ignore

// Definition d'une fonction qui utilise les éléments d'une matrice pour updater une table dans MySQL
let updateMySQLTable (m : Matrix<float * float>) =
    for j in 0 .. (snd m.Dimensions) - 1 do
        for i in 1 .. (fst m.Dimensions) - 1 do
        // first check if the user and score and date do not exist
            sprintf "IF NOT EXISTS (SELECT * FROM ProductScores WHERE UserID = %f AND ProductID = %f) INSERT INTO ProductScores (UserID, Score, Date) VALUES (%f, %f,CURRENT_TIMESTAMP)"  (fst m.[i, j]) (snd m.[i, j])
            |>  execNonQuery
       // otherwise update the row
            sprintf "UPDATE ProductScores SET Score = %f, Date = CURRENT_TIMESTAMP WHERE UserID = %f " (snd m.[i, j]) (fst m.[i, j])
            |>  execNonQuery

I would like to avoid the two execNonQuery requests in the code by using a stored procedure such as

CREATE PROCEDURE updateScoreByUsers 
    -- Add the parameters for the stored procedure here
    @UserID int, 
    @Score  float

AS
BEGIN

    IF NOT EXISTS 
    (SELECT * FROM ProductScores WHERE UserID = @UserID ) 
        INSERT INTO 
        ProductScores (UserID, Score, Date) VALUES (@UserID,@Score,CURRENT_TIMESTAMP)
    ELSE

    UPDATE ProductScores 
        SET Score = @Score, Date = CURRENT_TIMESTAMP 
        WHERE UserID = @UserID 
END
GO

But I don't know how to call a SQL stored procedure in F#.
How can I call it with F#?
DO you see any other way of improvements?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
fabco63
  • 747
  • 1
  • 8
  • 17

2 Answers2

3

Set SqlCommand.CommandText to the name of the stored procedure and set CommandType to StoredProcedure. For example

use cmd = new SqlCommand("MyStoredProcedure", con)
cmd.CommandType <- CommandType.StoredProcedure
cmd.ExecuteNonQuery()

If you're on 2008 you may want to look into the MERGE statement.

To significantly improve performance you need to avoid making a database call for each item. Perhaps use SqlBulkCopy to load all the data to the server at once (use a temp table or designated staging table), then use set-based operations (UPDATE/INSERT/MERGE) to merge with your target table.

Daniel
  • 47,404
  • 11
  • 101
  • 179
3

As pointed out by others, just doing the two statements in a single SQL command isn't going to improve the performance much. To make it really efficient, you'll need to transfer the data from Matrix to the database and then update the table. To copy the data, you could use SqlBulkCopy (see MSDN for more info), which lets you upload a large amount of data efficiently. You can use this to insert data in a new table and then run a single SQL command to update the data in the actual table.


Aside - Daniel's answer is the most direct way to call a stored proecdure from F#. If you want to make it a bit nicer, you can also use the dynamic operator, which let's you write something like this:

db?MyStoredProcedure(userId, scores)

... instead of creating and executing SqlCommand by hand. This can be done using a DynamicDatabase type that is implemented in this MSDN article.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Thank you thomas and Daniel, based on your recommandation I am going to use SQLBulkCopy. I am sure you already know this but just in case, one of my friend just told me that the new F# update, which should come soon, will include some convient functions to communicate with SQL. http://msdn.microsoft.com/en-us/library/hh361033(v=vs.110).aspx – fabco63 Jan 18 '12 at 15:15