无法理解数据库游标
对于大多数关系数据库的大多数驱动程序,访问结果的默认且首选方法是使用游标或迭代器。
我猜测数据库会执行以下操作:
- 运行查询。
- 准备结果,将其存储在 RAM 中?
- 将结果的游标返回给客户端。
每当数据库驱动程序收到获取下一个结果的调用时,它都会将该游标传递给数据库,从而给出下一个结果。
不过,我不确定这是否真的正确。让我困惑的一件事是,如果数据库客户端和数据库服务器位于不同的节点上并通过网络进行通信,这不是很慢吗?难道真的用了这么偷懒的做法吗?不返回所有数据是有道理的,但是是否需要一些中间路径?
With most drivers for most relational databases, the default and preferred way to access results is to use a cursor or iterator.
What I'm guessing is that the database does something like:
- Runs the query.
- Prepares the result, stores it in RAM?
- Returns the cursor for the result to the client.
Whenever the database driver gets a call to fetch the next result, it passes that cursor to the database, which gives the next result.
However, I'm not sure if that's really correct. One thing that stumps me is that if the database client and database server are on different nodes and communicating via the network, isn't this slow? Does it really use such a lazy approach? It makes sense not to return all the data, but is there some middle path it takes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该在文本编辑器中召唤出光标的图像。它(在某些情况下)是指针(光标)在给定数据集中的位置的占位符。使用cursor.fetchone() 返回一行(即一行),并且光标前进到下一行/下一行的开头。
游标抽象了数据库客户端当前缓冲的行数。当光标接近缓冲区末尾时,底层框架将获取更多内容。默认值通常是内存分配、网络延迟和其他因素之间的良好权衡的一个很好的猜测。
这就变成了一个优化问题。谷歌地图提供了很好的比较。用户可以平移,看起来整个国家地图都已下载到客户端,但实际上它是在您需要它们之前下载相邻的面板。
让数据库客户端执行此缓冲可以使程序员不必在需要优化之前考虑它。
It is supposed to conjure up the image of a cursor in a text editor. It is (in some contexts) a place holder for where the pointer (cursor) is in a given dataset. A row (i.e. a line) is returned with cursor.fetchone() and the cursor is advanced to the beginning of the next row/line.
The cursor abstracts how many rows are currently buffered at a database client. As the cursor nears the end of a buffer, the underlying framework will fetch more content. The defaults are usually a good guess at a good tradeoff between memory allocation, network latency and other factors.
It becomes a question of optimization. Google maps provides a good comparison. The user can pan around and it seems like the whole country map was downloaded to the client, but in reality it is downloading adjacent panels right before you need them.
Having the database client perform this buffering relieves the programmer from having to think about it before optimization is required.
数据库一次性发送完整的结果集。游标/迭代器位于客户端的驱动程序中。
The database sends the complete result set in one go. The cursor/iterator is in the driver on the client side.