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))
)
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With