gpt4 book ai didi

functional-programming - F# 查询表达式是否具有与 native SQL 相同的性能?

转载 作者:行者123 更新时间:2023-12-04 07:22:29 26 4
gpt4 key购买 nike

我注意到 F# 查询表达式示例看起来好像在进行计算之前获取了表的全部内容。这似乎比本地 SQL 效率低。或者性能应该是一样的。

// A query expression.
let query1 =
query {
for customer in db.Customers do
select customer
}

最佳答案

F# 查询表达式非常智能。除了使用引号(类似于 System.Linq.Expressions )将代码转换为 SQL 之外,我并不完全理解它们是如何工作的。 ORM 有 measurable overhead ,原始查询会更快,但它们能够生成优化的查询
鉴于这些模型和 ef-core 上下文

[<CLIMutable>] type Address = { Id: int; City: string; Country: string }
[<CLIMutable>] type Person = { Id: int; Name: string; Age: int; Address: Address }

type PeopleContext() =
inherit DbContext()

[<FSharp.Core.DefaultValue>]
val mutable _people : DbSet<Person>

member db.People
with get () = db._people
and set value = db._people <- value

override _.OnConfiguring options =
options.UseSqlite("Data source=db.db3") |> ignore
我创建了这个查询
query {
for person in ctx.People do
where (person.Age > 21)
select person.Address.City
take 1
} |> Seq.toArray |> printfn "%A"
这被翻译成这个 sql
SELECT "a"."City"
FROM (
SELECT "p"."AddressId"
FROM "People" AS "p"
WHERE "p"."Age" > 21
LIMIT @__p_0
) AS "t"

关于functional-programming - F# 查询表达式是否具有与 native SQL 相同的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68413673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com