Database Cursors: Row-by-Row Result Processing
A database cursor is an iterator for a query's results, letting you process a large dataset one row at a time. It's for batch jobs on huge record sets that would otherwise crash your app.
Why it exists
Databases excel at set-based operations that modify many rows at once. However, some application logic requires processing records individually. Loading a multi-million row result set into application memory is often impossible. Cursors solve this by creating a server-side pointer to the result set, allowing an application to fetch rows one by one or in small batches.
The mental model
Think of a cursor as a bookmark in a giant book (the query result set). Instead of carrying the whole book, you just keep track of your page. You can read the current line (fetch a row), then move the bookmark to the next line (advance the cursor). This lets you work through the entire result set without holding it all in memory at once.
How it works
An application declares a cursor for a specific SELECT query. The database engine executes the query but instead of sending all the data, it holds the result set on the server and returns a handle to the cursor. The application then uses commands like FETCH to retrieve one or more rows from the cursor's current position. The database manages the state, tracking which row is next. When processing is complete, the application must explicitly CLOSE the cursor to release server resources like locks and memory.
When to use it
Use cursors for procedural, row-by-row processing that cannot be expressed in a single, set-based SQL statement. They are a fit for complex data migration scripts, large batch updates where each row's logic is unique, or when serializing a massive result set to a file or stream.
When not to use it
Avoid cursors for any task that can be accomplished with a standard set-based SQL statement like UPDATE, INSERT, or DELETE with a WHERE clause. A single SQL statement is almost always more efficient. Cursors introduce overhead from network round-trips and can hold locks for a long time, which can severely degrade database performance and concurrency for other users.
One canonical example
A script needs to deactivate millions of user accounts based on a complex set of rules involving API calls to external systems, which can't be done in SQL. The script declares a cursor for SELECT user_id FROM users WHERE last_login < 'some_date'. It then fetches 1,000 users at a time, processes them in the application code, and issues individual UPDATE statements. This avoids loading millions of IDs into memory.
Interview question
For which task would a database cursor typically be the most suitable approach?
- a.Retrieving a small number of rows for display in a user interface.
- b.Processing a massive dataset where each record requires individual application-level business logic.Correct
- c.Performing a bulk update on millions of records using a single SQL statement.
- d.Optimizing the performance of a frequently executed query by pre-fetching results.
Why? this is the answer
Cursors are best suited for processing massive datasets row-by-row when each record needs individual application-level logic or external system interaction. For simple bulk updates, a single set-based SQL statement is significantly more efficient and should be preferred.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on databases — each one lists the topics its interview covers.
See open roles