<p>When writing database queries in something like TSQL or PLSQL, we often have a choice of iterating over rows with a cursor to accomplish the task, or crafting a single SQL statement that does the same job all at once.</p>

<p>Also, we have the choice of simply pulling a large set of data back into our application and then processing it row by row, with C# or Java or PHP or whatever.</p>

<p>Why is it better to use set-based queries?  What is the theory behind this choice?  What is a good example of a cursor-based solution and its relational equivalent?</p>

[{'answer_id': 24170, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 4, 'selected': False, 'text': '<p>The main reason that I\'m aware of is that set-based operations can be optimised by the engine by running them across multiple threads. For example, think of a quicksort - you can separate the list you\'re sorting into multiple "chunks" and sort each separately in their own thread. SQL engines can do similar things with huge amounts of data in one set-based query.</p>\n\n<p>When you perform cursor-based operations, the engine can only run sequentially and the operation has to be single threaded.</p>\n'}, {'answer_id': 24176, 'author': 'Anders Eurenius', 'author_id': 1421, 'author_profile': 'https://Stackoverflow.com/users/1421', 'pm_score': 0, 'selected': False, 'text': "<p>The idea behind preferring to do the work in queries is that the database engine can optimize by reformulating it. That's also why you'd want to run EXPLAIN on your query, to see what the db is <em>actually</em> doing. (e.g. taking advantage of indices, table sizes and sometimes even knowledge about the distributions of values in columns.)</p>\n\n<p>That said, to get good performance in your actual concrete case, you may have to bend or break rules.</p>\n\n<p>Oh, another reason might be constraints: Incrementing a unique column by one might be okay if constraints are checked after <em>all</em> the updates, but generates a collision if done one-by-one.</p>\n"}, {'answer_id': 24192, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 0, 'selected': False, 'text': '<p>set based is done in one operation\ncursor as many operations as the rowset of the cursor</p>\n'}, {'answer_id': 24211, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 4, 'selected': False, 'text': "<p>Set based queries are (usually) faster because:</p>\n\n<ol>\n<li>They have more information for the query optimizer to optimize</li>\n<li>They can batch reads from disk</li>\n<li>There's less logging involved for rollbacks, transaction logs, etc.</li>\n<li>Less locks are taken, which decreases overhead</li>\n<li>Set based logic is the focus of RDBMSs, so they've been heavily optimized for it (often, at the expense of procedural performance)</li>\n</ol>\n\n<p>Pulling data out to the middle tier to process it can be useful, though, because it removes the processing overhead off the DB server (which is the hardest thing to scale, and is normally doing other things as well). Also, you normally don't have the same overheads (or benefits) in the middle tier. Things like transactional logging, built-in locking and blocking, etc. - sometimes these are necessary and useful, other times they're just a waste of resources.</p>\n\n<p>A simple cursor with procedural logic vs. set based example (T-SQL) that will assign an area code based on the telephone exchange:</p>\n\n<pre><code>--Cursor\nDECLARE @phoneNumber char(7)\nDECLARE c CURSOR LOCAL FAST_FORWARD FOR\n   SELECT PhoneNumber FROM Customer WHERE AreaCode IS NULL\nOPEN c\nFETCH NEXT FROM c INTO @phoneNumber\nWHILE @@FETCH_STATUS = 0 BEGIN\n   DECLARE @exchange char(3), @areaCode char(3)\n   SELECT @exchange = LEFT(@phoneNumber, 3)\n\n   SELECT @areaCode = AreaCode \n   FROM AreaCode_Exchange \n   WHERE Exchange = @exchange\n\n   IF @areaCode IS NOT NULL BEGIN\n       UPDATE Customer SET AreaCode = @areaCode\n       WHERE CURRENT OF c\n   END\n   FETCH NEXT FROM c INTO @phoneNumber\nEND\nCLOSE c\nDEALLOCATE c\nEND\n\n--Set\nUPDATE Customer SET\n    AreaCode = AreaCode_Exchange.AreaCode\nFROM Customer\nJOIN AreaCode_Exchange ON\n    LEFT(Customer.PhoneNumber, 3) = AreaCode_Exchange.Exchange\nWHERE\n    Customer.AreaCode IS NULL\n</code></pre>\n"}, {'answer_id': 24226, 'author': 'kemiller2002', 'author_id': 1942, 'author_profile': 'https://Stackoverflow.com/users/1942', 'pm_score': 2, 'selected': False, 'text': "<p>I think the real answer is, like all approaches in programming, that it depends on which one is better.  Generally, a set based language is going to be more efficient, because that is what it was designed to do. There are two places where a cursor is at an advantage:</p>\n\n<ol>\n<li><p>You are updating a large data set in a database where locking rows is not acceptable (during production hours maybe). A set based update has a possibility of locking a table for several seconds (or minutes), where a cursor (if written correctly) does not. The cursor can meander through the rows updating one at a time and you don't have to worry about affecting anything else. </p></li>\n<li><p>The advantage to using SQL is that the bulk of the work for optimization is handled by the database engine in most circumstances.  With the enterprise class db engines the designers have gone to painstaking lengths to make sure the system is efficient at handling data.  The drawback is that SQL is a set based language.  You have to be able to define a set of data to use it.  Although this sounds easy, in some circumstances it is not. A query can be so complex that the internal optimizers in the engine can't effectively create an execution path, and guess what happens... your super powerful box with 32 processors uses a single thread to execute the query because it doesn't know how to do anything else, so you waste processor time on the database server which generally there is only one of as opposed to multiple application servers  (so back to reason 1, you run into resource contentions with other things needing to run on the database server).  With a row based language (C#, PHP, JAVA etc.), you have more control as to what happens.  You can retrieve a data set and force it to execute the way you want it to. (Separate the data set out to run on multiple threads etc).  Most of the time, it still isn't going to be efficient as running it on the database engine, because it will still have to access the engine to update the row, but when you have to do 1000+ calculations to update a row (and lets say you have a million rows), a database server can start to have problems.</p></li>\n</ol>\n"}, {'answer_id': 24234, 'author': 'Matt Rogish', 'author_id': 2590, 'author_profile': 'https://Stackoverflow.com/users/2590', 'pm_score': 4, 'selected': False, 'text': '<p>In addition to the above "let the DBMS do the work" (which is a great solution), there are a couple other good reasons to leave the query in the DBMS:</p>\n\n<ul>\n<li><strong>It\'s (subjectively) easier to read.</strong> When looking at the code later, would you rather try and parse a complex stored procedure (or client-side code) with loops and things, or would you rather look at a concise SQL statement?</li>\n<li><strong>It avoids network round trips.</strong> Why shove all that data to the client and then shove more back? Why thrash the network if you don\'t need to?</li>\n<li><strong>It\'s wasteful.</strong> Your DBMS and app server(s) will need to buffer some/all of that data to work on it. If you don\'t have infinite memory you\'ll likely page out other data; why kick out possibly important things from memory to buffer a result set that is mostly useless?</li>\n<li><strong>Why wouldn\'t you?</strong> You bought (or are otherwise using) a highly reliable, very fast DBMS. Why wouldn\'t you use it?</li>\n</ul>\n'}, {'answer_id': 331613, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 3, 'selected': False, 'text': "<p>You wanted some real-life examples. My company had a cursor that took over 40 minutes to process 30,000 records (and there were times when I needed to update over 200,000 records). It took 45 second to do the same task without the cursor. In another case I removed a cursor and sent the processing time from over 24 hours to less than a minute. One was an insert using the values clause instead of a select and the other was an update that used variables instead of a join. A good rule of thumb is that if it is an insert, update, or delete, you should look for a set-based way to perform the task.</p>\n\n<p>Cursors have their uses (or the code wouldn't be their in the first place), but they should be extremely rare when querying a relational database (Except Oracle which is optimized to use them). One place where they can be faster is when doing calculations based on the value of the preceeding record (running totals). BUt even that should be tested.</p>\n\n<p>Another limited case of using a cursor is to do some batch processing. If you are trying to do too much at once in set-based fashion it can lock the table to other users. If you havea truly large set, it may be best to break it up into smaller set-based inserts, updates or deletes that will not hold the lock too long and then run through the sets using a cursor. </p>\n\n<p>A third use of a cursor is to run system stored procs through a group of input values. SInce this is limited to a generally small set and no one should mess with the system procs, this is an acceptable thing for an adminstrator to do. I do not recommend doing the same thing with a user created stored proc in order to process a large batch and to re-use code. It is better to write a set-based version that will be a better performer as performance should trump code reuse in most cases.</p>\n"}, {'answer_id': 331676, 'author': 'dkretz', 'author_id': 31641, 'author_profile': 'https://Stackoverflow.com/users/31641', 'pm_score': 1, 'selected': False, 'text': "<p>I think it comes down to using the database is was designed to be used. Relational database servers are specifically developed and optimized to respond best to questions expressed in set logic.</p>\n\n<p>Functionally, the penalty for cursors will vary hugely from product to product. Some (most?) rdbmss are built at least partially on top of isam engines. If the question is appropriate, and the veneer thin enough, it might in fact be as efficient to use a cursor. But that's one of the things you should become intimately familiar with, in terms of your brand of dbms, before trying it.</p>\n"}, {'answer_id': 331709, 'author': 'Cervo', 'author_id': 16219, 'author_profile': 'https://Stackoverflow.com/users/16219', 'pm_score': 1, 'selected': False, 'text': "<p>As has been said, the database is optimized for set operations.  Literally engineers sat down and debugged/tuned that database for long periods of time.  The chances of you out optimizing them are pretty slim.  There are all sorts of fun tricks you can play with if you have a set of data to work with like batching disk reads/writes together, caching, multi-threading.  Also some operations have a high overhead cost but if you do it to a bunch of data at once the cost per piece of data is low.  If you are only working one row at a time, a lot of these methods and operations just can't happen.</p>\n\n<p>For example, just look at the way the database joins.  By looking at explain plans you can see several ways of doing joins.  Most likely with a cursor you go row by row in one table and then select values you need from another table.  Basically it's like a nested loop only without the tightness of the loop (which is most likely compiled into machine language and super optimized).  SQL Server on its own has a whole bunch of ways of joining.  If the rows are sorted, it will use some type of merge algorithm, if one table is small, it may turn one table into a hash lookup table and do the join by performing O(1) lookups from one table into the lookup table.  There are a number of join strategies that many DBMS have that will beat you looking up values from one table in a cursor.  </p>\n\n<p>Just look at the example of creating a hash lookup table.  To build the table is probably m operations if you are joining two tables one of length n and one of length m where m is the smaller table.  Each lookup should be constant time, so that is n operations.  so basically the efficiency of a hash join is around m (setup) + n (lookups).  If you do it yourself and assuming no lookups/indexes, then for each of the n rows you will have to search m records (on average it equates to m/2 searches).  So basically the level of operations goes from m + n (joining a bunch of records at once) to m * n / 2 (doing lookups through a cursor).  Also the operations are simplifications.  Depending upon the cursor type, fetching each row of a cursor may be the same as doing another select from the first table.</p>\n\n<p>Locks also kill you.  If you have cursors on a table you are locking up rows (in SQL server this is less severe for static and forward_only cursors...but the majority of cursor code I see just opens a cursor without specifying any of these options).  If you do the operation in a set, the rows will still be locked up but for a lesser amount of time.  Also the optimizer can see what you are doing and it may decide it is more efficient to lock the whole table instead of a bunch of rows or pages.  But if you go line by line the optimizer has no idea.</p>\n\n<p>The other thing is I have heard that in Oracle's case it is super optimized to do cursor operations so it's nowhere near the same penalty for set based operations versus cursors in Oracle as it is in SQL Server.  I'm not an Oracle expert so I can't say for sure.  But more than one Oracle person has told me that cursors are way more efficient in Oracle.  So if you sacrificed your firstborn son for Oracle you may not have to worry about cursors, consult your local highly paid Oracle DBA :)</p>\n"}, {'answer_id': 860127, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>The REAL answer is go get one of <a href="http://en.wikipedia.org/wiki/Edgar_F._Codd" rel="nofollow noreferrer">E.F. Codd</a>\'s books and brush up on <a href="http://en.wikipedia.org/wiki/Relational_algebra" rel="nofollow noreferrer">relational algebra</a>. Then get a good book on <a href="http://en.wikipedia.org/wiki/Big_O_notation" rel="nofollow noreferrer">Big O notation</a>. After nearly two decades in IT this is, IMHO, one of the big tragedies of the modern MIS or CS degree: Very few actually study computation. You know...the "compute" part of "computer"? Structured Query Language (and all its supersets) is merely a practical application of relational algebra. Yes, the RDBMS have optimized memory management and read/write but the same could be said for procedural languages. As I read it, the original question is not about the IDE, the software, but rather about the efficiency of one method of computation vs. another.</p>\n\n<p>Even a quick familiarization with Big O notation will begin to shed light on why, when dealing with sets of data, iteration is more expensive than a declarative statement.</p>\n'}, {'answer_id': 1119577, 'author': 'J. Polfer', 'author_id': 40411, 'author_profile': 'https://Stackoverflow.com/users/40411', 'pm_score': 0, 'selected': False, 'text': "<p>Simply put, in most cases, it's faster/easier to let the database do it for you.  </p>\n\n<p>The database's purpose in life is to store/retrieve/manipulate data in set formats and to be really fast.  Your VB.NET/ASP.NET code is likely nowhere near as fast as a dedicated database engine. Leveraging this is a wise use of resources.</p>\n"}]
['https://Stackoverflow.com/questions/24168', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1219/']