Cursors on MySQL - Good or Bad

2020-05-29 06:10发布

I have always heard people saying bad about using cursors and this is especially in Microsoft SQL Server as they are very slow. Is this the case with Cursors on MySQL as well? Does cursors in MySQL reduce performance as well? Can someone please advice on the usage of cursors in MySQL?

3条回答
狗以群分
2楼-- · 2020-05-29 06:19

Just create and fill a temporary table. That is how most RDBMS's implement cursors anyway.

查看更多
对你真心纯属浪费
3楼-- · 2020-05-29 06:26

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.

查看更多
▲ chillily
4楼-- · 2020-05-29 06:32

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.

查看更多
登录 后发表回答