Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Advantages and Disadvantages of Cursor?

Answer Posted / nikunj padaliya

Advantage of Cursor:

Row-by-row operations: Cursors are best used when performing
row-by-row operations that can't be accomplished with
set-based operations (i.e, when you need to fire a stored
procedure once per row in a table).

Efficiency: The query optimizer automatically selects the
appropriate query plan, so the developer does not need to
design a complex algorithm to access the required data.
Adaptability: As data changes or indexes are added or
dropped, the query optimizer automatically adapts its
behavior by using alternative plans.

Fewer errors: Instead of the developer handling data and
algorithms in the application, the SQL Server Compact 3.5
Database Engine natively supports the required operations.
--------------------------------------------------
Disadvantage of Cursor:

Resources consumed by cursors: it occupies memory from your
system that may be available for other processes. Poorly
written cursors can completely deplete available memory.

Speed and performance issues: Cursors can be faster than a
while loop but they do have more overhead. If your cursor
will not be updating the base tables, use a FAST_FORWARD
cursor for optimal performance. The problem with cursor
speed is that, in many cases, the operation can be more
efficiently written as a set operation or perhaps in a while
loop. It's these cursor rewrites that lead to the impression
that cursors are evil – or cursed.
Another factor affecting cursor speed is the number of rows
and columns brought into the cursor. Time how long it takes
to open your cursor and fetch statements. If it's lengthy,
look carefully at your cursor logic; see if you can remove
columns from the declare statement, and change your where
clause in the declare statement to only return rows the
cursor needs. If the fetch statements themselves are lengthy
or consuming too much IO or CPU, look at the cursor declare
statement and ensure you have optimal indexes in place on
your base tables or temporary tables.

Wrong tool for the wrong task: Cursors are frequently the
wrong tool for the wrong task. They're used for
quick-and-dirty programming when a developer does not have a
good understanding of set operations or they're used for the
wrong task entirely.
Subtle errors:
Cursors sometimes introduce subtle errors. We already
looked at a few:
1.Failing to check the value of @@Fetch_Status
2.Improper indexes on the base tables in your results set or
FETCH statement
3.Too many columns being dragged around in memory, which are
never referenced in the subsequent cursor operations
(probably the result of legacy code)
4.WHERE clause that brings too many rows into the cursor,
which are subsequently filtered out by cursor logic.

Is This Answer Correct ?    44 Yes 13 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are unicode character string data types in ms sql server?

1268


How to download microsoft sql server 2005 express edition?

1056


What is always encrypted?

1012


How to optimize stored procedure optimization?

904


Will sql server 2005 allow you to reduce the size of a column?

1007


What is impersonation? What are the different impersonation options available in ssas? : sql server analysis services, ssas

987


Does windows server 2016 come with sql server?

939


Using the customer, and order table in northwind database, please write a query to produce xml?

1085


How to create stored procedures with parameters in ms sql server?

1095


Does sql server 2016 have ssms?

969


What do you understand by replication in sql server?

1071


What is filestream?

1008


How data can be copied from one table to another table?

959


Is it possible for a stored procedure to call itself or recursive stored procedure?

957


Explain the truncate command? : SQL Server Architecture

1187