question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cursor for prepared statements?

See original GitHub issue

I was wondering if this library supported cursors with prepared statements.

Something like,

await stmt.execute();
const row0 = await stmt.next();
const row1 = await stmt.next();
const row2 = await stmt.next();
//etc.
await stmt.close();

I couldn’t find anything like that in the tests.

My rationale for asking is that the executed query may fetch a lot of rows, and we might only want to load one (or a small handful) of those rows at a given point in time in memory, so we don’t get an OOM exception.

Something like,

//The statement may have a result set containing 100M rows
await stmt.execute();
let row = await stmt.next();
while (row != undefined) {
  //Do complicated processing or something
  row = await stmt.next();
}
await stmt.close();

Without a cursor, one can easily use the LIMIT and ORDER BY clauses in a transaction to emulate such behaviour but I was wondering if there was a way to do it already in the library.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AnyhowStepcommented, Sep 18, 2019

Ah. I see. I always assumed it existed on the protocol level because I remember using cursors with PHP a long time ago. I forgot that it’s just an abstraction!

I decided to go back to the PHP documentation and found this,

https://www.php.net/manual/en/pdostatement.fetch.php#114458

Because MySQL does not currently support the use of cursors…

Ah, well. Thank you for pointing me in the right direction!

1reaction
sidorarescommented, Sep 18, 2019

I don’t think this functionality exist at protocol level ( all rows are sent from server as a response and there is no way to cancel that mid way )

One thing that I do think might be useful is if you on receiving side decided that you don’t need more data we could save some CPU by just throwing away rest of incoming packets ( no parsing / saving to results array ) See https://github.com/sidorares/node-mysql2/pull/822#issuecomment-409415308 ( and comment next to break; )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using prepared statements with cursor - mysql
I put the cursor declaration in the prepared statement and then executed it, then returns an error #1324 - Undefined CURSOR: getid.
Read more >
Associating a Cursor with a Prepared Statement
The PREPARE statement lets you assemble the text of an SQL statement at runtime and pass the statement text to the database server...
Read more >
10.6.8 cursor.MySQLCursorPrepared Class
In Connector/Python, there are two ways to create a cursor that enables execution of prepared statements using the binary protocol.
Read more >
Cursors and prepared statements - Db2 for i SQL
Cursors and prepared statements are scoped to the compilation unit and also to the connection. Scoping to the compilation unit means that a...
Read more >
Associating a Cursor with a Prepared Statement
The DECLARE statement allows you to declare a cursor for the collection variable. Such a cursor is called a Collection cursor. You use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found