Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize F# string concatenation

I'm building a MySql query that batch inserts 4096 records at once. The actual insert is quite fast but the bottleneck is generating the query. Any hints on optimizing this? The string generation is currently taking about 18 times longer than the query.

                    let builder = StringBuilder(524288)
                    Printf.bprintf builder
                        "
                         INSERT INTO %s
                             (`ID`,
                              `Tag`,
                              `Port`,
                              `Excess`,
                              `Return`,
                              `StartDate`,
                              `EndDate`
                              ) 
                          values "
                        x.evaluationstable

                    evaluations
                    |> Seq.iter(fun (e) ->
                        Printf.bprintf builder 
                            " (%d, '%s', '%s', %A, %A, %A, %A), "
                            e.ID
                            e.Tag
                            e.Port
                            e.Excess
                            e.Return
                            (e.StartDate.ToString(x.datetimeformat))
                            (e.EndDate.ToString(x.datetimeformat))
                    )
like image 462
MikeW Avatar asked Dec 14 '22 03:12

MikeW


1 Answers

Try using StringBuilder.AppendFormat instead of Printf.bprintf. When I made this change in my example of your question, I saw a huge performance increase (~80x).

evaluations
|> Seq.iter (fun (e) ->
    builder.AppendFormat(
        " ({0}, '{1}', '{2}', {3}, {4}, {5}, {6}), ",
        e.ID,
        e.Tag,
        e.Port,
        e.Excess,
        e.Return,
        (e.StartDate.ToString("MM/dd/yyyy")),
        (e.EndDate.ToString("MM/dd/yyyy"))
    ) |> ignore
)
like image 186
Ray Vernagus Avatar answered Dec 21 '22 08:12

Ray Vernagus