- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题
我需要读取和写入大量记录(大约 1000 条)。下面的例子写 1000 条记录需要 20 分钟,读取它们需要 12 秒(在进行“读取”测试时,我注释掉了 do create_notes()
行)。
来源
这是一个完整的示例(构建和运行)。它只将输出打印到控制台(而不是浏览器)。
type User.t =
{ id : int
; notes : list(int) // a list of note ids
}
type Note.t =
{ id : int
; uid : int // id of the user this note belongs to
; content : string
}
db /user : intmap(User.t)
db /note : intmap(Note.t)
get_notes(uid:int) : list(Note.t) =
noteids = /user[uid]/notes
List.fold(
(h,acc ->
match ?/note[h] with
| {none} -> acc
| {some = note} -> [note|acc]
), noteids, [])
create_user() =
match ?/user[0] with
| {none} -> /user[0] <- {id=0 notes=[]}
| _ -> void
create_note() =
key = Db.fresh_key(@/note)
do /note[key] <- {id = key uid = 0 content = "note"}
noteids = /user[0]/notes
/user[0]/notes <- [key|noteids]
create_notes() =
repeat(1000, create_note)
page() =
do create_user()
do create_notes()
do Debug.alert("{get_notes(0)}")
<>Notes</>
server = one_page_server("Notes", page)
get_notes_via_transaction
方法与
get_notes
完全一样慢.
get_notes_via_transaction(uid:int) : list(Note.t) =
result = Db.transaction( ->
noteids = /user[uid]/notes
List.fold(
(h,acc ->
match ?/note[h] with
| {none} -> acc
| {some = note} -> [note|acc]
), noteids, [])
)
match result with
| {none} -> []
|~{some} -> some
最佳答案
Nic,这可能不是您希望的答案,但它是:
create_node
的代码功能与此:counter = Reference.create(0)
create_note() =
key = Db.fresh_key(@/note)
do /note[key] <- {id = key uid = 0 content = "note"}
noteids = /user[0]/notes
do Reference.update(counter, _ + 1)
do /user[0]/notes <- [key|noteids]
cntr = Reference.get(counter)
do if mod(cntr, 100) == 0 then
Log.info("notes", "{cntr} notes created")
else
void
void
import stdlib.profiler
create_notes() =
repeat(1000, -> P.execute(create_note, "create_note"))
P = Server_profiler
_ =
do P.init()
do create_user()
do create_notes()
do P.execute(-> get_notes(0), "get_notes(0)")
P.summarize()
/user[0]/notes <- [key|noteids]
这显然会导致再次写入整个列表。 AFAIK 我们进行了优化来避免这种情况,但要么我错了,要么由于某些原因它们在这里不起作用 - 我会尝试调查这一点,一旦我知道更多,就会让你知道。 type Note.t =
{ id : int
; uid : int // id of the user this note belongs to
; content : string
}
db /user_notes[{user_id; note_id}] : { user_id : int; note_id : int }
db /note : intmap(Note.t)
get_notes(uid:int) : list(Note.t) =
add_note(acc : list(Note.t), user_note) =
note = /note[user_note.note_id]
[note | acc]
noteids = /user_notes[{user_id=uid}] : dbset({user_id:int; note_id:int})
DbSet.fold(noteids, [], add_note)
counter = Reference.create(0)
create_note() =
key = Db.fresh_key(@/note)
do /note[key] <- {id = key uid = 0 content = "note"}
do DbVirtual.write(@/user_notes[{user_id=0}], {note_id = key})
do Reference.update(counter, _ + 1)
cntr = Reference.get(counter)
do if mod(cntr, 100) == 0 then
Log.info("notes", "{cntr} notes created")
else
void
void
import stdlib.profiler
create_notes() =
repeat(1000, -> Server_profiler.execute(create_note, "create_note"))
_ =
do Server_profiler.init()
do create_notes()
do Server_profiler.execute(-> get_notes(0), "get_notes(0)")
Server_profiler.summarize()
关于Opa:如何高效读写大量记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7857357/
今天我需要在我的 OPA 应用程序中发送电子邮件。然后我正在尝试使用有关邮件的 opa 功能。 我正在使用模块:stdlib.web.mail . 和 OPA 版本: Opa compiler (c)
我试图更深入地了解 Opa 背后的概念.特别是,我对不提供像 ML 或 Haskell 那样的可区分联合的决定很好奇(即定义几个包装零个或多个值的构造函数),而是保留记录类型和它们的总和类型。这个决定
Opa 使用 AJAX 还是 Websockets?我猜应该是前者。如果我想使用 websockets 而不是 AJAX 怎么办?还是类似socket.io对于 Javascript - 如果浏览器支
我是 Opa(和兼容语言)的新手 - 通常使用 Python 开发网络应用程序。 我正在学习 http://doc.opalang.org/index.html#_introducing_opa 上的
在 MLState 的新编程语言 Opa 中,是否可以创建全局变量? 最佳答案 Opa 是一种函数式语言,因此该语言中没有全局变量。但是,可以使用 Mutable 实现类似的行为。 .在顶层,用以下方
寻找解释指令的文档,例如“Hello Chat”示例中提到的 @publish。 最佳答案 http://opalang.org/resources/book/index.html#directive
我只是在研究 OPA 并试图从传统的 LAMP 背景中实现飞跃,所以这是我的第一个新手问题: 我能否让两个 OPA 应用程序共享同一个数据库,比如一个写入数据库而另一个从数据库读取? 最佳答案 是的,
我认为新手会对“do”感到困惑,从语言设计的角度来看,我想知道它。您不想在新语言生命的这个阶段混淆新手,在这个阶段几乎每个人都是新手,您需要新手来建立社区和临界质量 ;-) “做”的文档(3.8.3。
我在 API 中找不到任何可以让我执行 shell 命令的东西,比如 system 行。或 exec做。使用外部函数接口(interface)是目前实现这一目标的唯一方法吗? 最佳答案 您可以在代码中
是否可以在 Opa 中访问 HTML5 Canvas 元素? 最佳答案 是的,可以在 Opa 中访问 HTML5 Canvas 元素! 请参阅在线 API 文档: http://opalang.org
我是 Dot Net 开发人员,想尝试使用 Opa,我的问题是: 什么是好的 IDE 可以使用? 如何托管 Opa 代码? 最佳答案 您可以使用 Sublime 文本。 插件是here . 关于ide
我已经切换到 opa 1.0.1,现在似乎 opa 在 ~/.opa/mongo 中安装了它自己的 mongodb。显然,这个 mongo 实例运行在默认的 27017 端口上。 我怎样才能阻止 op
问题 我需要读取和写入大量记录(大约 1000 条)。下面的例子写 1000 条记录需要 20 分钟,读取它们需要 12 秒(在进行“读取”测试时,我注释掉了 do create_notes() 行)
我正在编辑 Hello-wiki来自 Opa 文档的代码。我想要一个 wiki-topic 有一个数据库中现有主题的列表。我有一个在默认主题上调用的函数: /** * Collect all cr
在我的项目之一中,我必须编写HTML和JavaScript代码。我宁愿使用静态类型的语言来代替,因此我正在评估OPA。但是,我的目标是生成静态页面的集合,因此我不在乎OPA HTTP服务器和持久层。
我已经定义了一个名为 node 的类型以及一个节点列表。 type node = {name: string; description: string} nodes = [] : list(node)
在 Opa 中,如何防止提交表单时重新加载页面?当按下提交时,我想执行一些客户端验证,并且仅在验证通过时才提交表单。 我正在使用此表单: ready()} id=#form_id> 此函数在
我已声明一个存储客户数据的数据库: type Customer = { name : string email: string cp: int } db /customer : Custo
我正在使用函数 Float.of_string 来转换由公式获得的输入。 当然,用户可能是一个坏人,在输入中写一个句子而不是一个浮点数:) 有很多解决方案,可以测试字符串输入......但在我看来,函
目录 一.系统环境 二.前言 三.Open Policy Agent 简介 四.Rego 语言简介 五.配置基本环境 六.docker安装OPA插件
我是一名优秀的程序员,十分优秀!