Cursor for prepared statements?
See original GitHub issueI 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:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
Ah, well. Thank you for pointing me in the right direction!
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;
)