看一下这篇文章,它对这两者做了很好的研究。它是根据 SQL Server 编写的,但大多数概念都适用。
Most modern databases (including MySQL) are designed to perform set based operations. The problem with cursors is that they perform row based (or procedural) operations. Because of this you will almost always see a performance hits when you are using cursors to do a job that can be done without cursors on a modern DBMS.
Take a look at this article, which does a decent job going over the two. It is written with SQL Server in mind but most of the concepts apply.
Cursors by nature are Iterative - they are definitely going to be slower irrespective of any database type. You should therefore do whatever to avoid them and try to find solutions using SQL queries. They are however there for problems which cannot be solved with queries - so use them only when absolutely necessary.
发布评论
评论(3)
大多数现代数据库(包括 MySQL)都被设计为执行基于集合的操作。游标的问题在于它们执行基于行(或过程)的操作。因此,当您使用游标执行在现代 DBMS 上无需游标即可完成的工作时,您几乎总是会看到性能下降。
看一下这篇文章,它对这两者做了很好的研究。它是根据 SQL Server 编写的,但大多数概念都适用。
Most modern databases (including MySQL) are designed to perform set based operations. The problem with cursors is that they perform row based (or procedural) operations. Because of this you will almost always see a performance hits when you are using cursors to do a job that can be done without cursors on a modern DBMS.
Take a look at this article, which does a decent job going over the two. It is written with SQL Server in mind but most of the concepts apply.
只需创建并填充一个临时表。这就是大多数 RDBMS 实现游标的方式。
Just create and fill a temporary table. That is how most RDBMS's implement cursors anyway.
游标本质上是迭代的 - 无论任何数据库类型,它们肯定会变慢。因此,您应该尽一切努力避免它们并尝试使用 SQL 查询找到解决方案。然而,它们用于解决无法通过查询解决的问题 - 因此仅在绝对必要时才使用它们。
Cursors by nature are Iterative - they are definitely going to be slower irrespective of any database type. You should therefore do whatever to avoid them and try to find solutions using SQL queries. They are however there for problems which cannot be solved with queries - so use them only when absolutely necessary.