Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 4 Ways to Increase Your Salary (Using UPDATE) Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, November 25, 2009 11:43 PM
 SSC-Addicted Group: General Forum Members Last Login: Tuesday, July 3, 2012 5:49 AM Points: 418, Visits: 365
 Comments posted to this topic are about the item 4 Ways to Increase Your Salary (Using UPDATE)
Post #825066
 Posted Thursday, November 26, 2009 2:39 AM
 SSC-Addicted Group: General Forum Members Last Login: Thursday, November 3, 2016 9:04 AM Points: 447, Visits: 69
 Hi Arup,I'm fairly new to SQL so maybe it's a stupid question, but why do you use a temp-table in the last example? I think there's no use for that since both the emp-table and the temp-table share the same identity. The way the query is built I think it wouldn't even work if the id's in the emp-table would be different (for example 1,2,5,6,7 (after delete)).Jochen
Post #825107
 Posted Thursday, November 26, 2009 2:51 AM
 Ten Centuries Group: General Forum Members Last Login: Tuesday, November 22, 2016 11:46 AM Points: 1,121, Visits: 356
 You say that salary * 115 / 100 and (salary * 115) / 100 return different results due to operator precedence - I can't see how operator precedence makes any difference in this scenario. Could you elaborate or provide an example?Basically :salary x (115/100) = (salary x 115)/100 so operator precedence should make no difference...Actually - thinking about it, is it the integer division that's the issue? What I say above is true in a pure mathematical sense, but if you divide 115/100 as integers in SQLServer, you get 1. If however you divide 115 as a decimal /100, then you get 1.150000, and the calculation will work in any layout. So technically it's the operator precedence causing the integer divide to happen first which is the issue I suppose... though it's the integer divide in its own right that means there is any confusion to be had in the first place.declare @percentage decimal(3,0)set @percentage = 115 -- Even works with an 'integer' decimal.select 3000 * @percentage / 100select (3000 * @percentage) / 100select 3000 * (@percentage / 100.0)Cheers-- Kev -------------------------------Oh no!
Post #825111
 Posted Thursday, November 26, 2009 3:45 AM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, December 29, 2009 2:39 AM Points: 9, Visits: 40
 I have to agree, there is no real difference with the calculations except in the interpretation by the system. Int divide by Int = Int which causes incorrect values. Simpler would be to change one of the Int's or even remove one altogether.What is wrong with using Salary * 1.15? After all Salary is a float.Remember the key to performance is simplicity. Every level of complexity will exponentially affect your query performance.
Post #825131
 Posted Thursday, November 26, 2009 4:50 AM
 SSC Rookie Group: General Forum Members Last Login: Tuesday, November 13, 2012 3:23 AM Points: 32, Visits: 65
 There is actually another way to archive a row by row processing. I am using it in different scenarios since quite a few years and it turned out to be very efficient.`BEGINDeclare @ID intSelect @ID = Min(ID) from empWHILE @ID Is Not Null BEGIN UPDATE emp SET salary = (salary * 115) /100 WHERE emp.id = @ID Select @ID = Min(ID) from emp where ID > @ID END END`In relation to the actual performance of this method I listed the times/cost on my server below. Please note that I also included the cost calculated by the execution plan:` READ ESTIMATED EXECUTION COST Direct SQL 5 0.0132935 100%Cursor 69 0.1462060 1099%Temp table with TOP 254 0.2531007 1904%Temp table with IDENTITY column 121 0.0994881 748%Min Loop 39 0.0861955 648%`
Post #825165
 Posted Thursday, November 26, 2009 5:14 AM
 Ten Centuries Group: General Forum Members Last Login: Monday, October 31, 2016 11:11 AM Points: 1,076, Visits: 3,229
 Martins answer is a lot sweeter and more robust if you need to loop through a data set, as there is no guarantee that the numbers are consecutive eg record number 4 is deleted for some reason (yes it can happen), so the last record will never get updated, assuming a 1% row deletion count, then in an organisation with 1000 members, 10 wont get pay rises. As always the advice is use set based queries rather than Loops and Cursors, though in some cases you have to revert to them they should be rare.As an aside, one of my colleagues ran some internal tests on a somewhat larger data set (1000 rows, same number of columns) and found that the Cursor ran significantly faster than the last option. _________________________________________________________________________SSC Guide to Posting and Best Practices
Post #825173
 Posted Thursday, November 26, 2009 5:14 AM
 SSC-Addicted Group: General Forum Members Last Login: Thursday, November 3, 2016 9:04 AM Points: 447, Visits: 69
 nice one Martin,That's a nice and easy way to do the updates. ...And independent of identity values in the table.
Post #825174
 Posted Thursday, November 26, 2009 6:12 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, October 5, 2016 3:46 AM Points: 1,699, Visits: 1,323
 The last example uses assumption on the identity column values in the main table.You can just add these values to the temp table.But Martin's solution is better See, understand, learn, try, use efficient© Dr.Plch
Post #825191
 Posted Thursday, November 26, 2009 6:22 AM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, December 1, 2009 7:03 PM Points: 4, Visits: 14
 Jason Lees-299789 (11/26/2009)As an aside, one of my colleagues ran some internal tests on a somewhat larger data set (1000 rows, same number of columns) and found that the Cursor ran significantly faster than the last option.Cursors and temp tables are very similar, since a cursor creates a temp table in tempdb (spooling all data to disk before being read back)...just like a temp table does. A cursor has an upfront hit when allocating itself, but that overhead doesn't occur with each row fetch and each row fetch is quite speedy.The CPU cycles is much greater on each row's operation in the identity temp table method (it is doing a COUNT(*) on the temp table each iteration). With a large data set, a cursor will (always) startup slower but finish faster and with less CPU. On a small data set, a cursor will still startup slower but finish slower as well.Bottom line is, the last method makes certain assumptions about the identity column that can't always be made and thus isn't a universal solution. And of course, 99% of the time there is a set-based solution that will run circles around any procedural solution. I once tuned a stored procedure that took over 2 hours to complete by replacing a cursor with a set-based solution and brought the execution time down to just around 15 seconds. If you are forced into a procedural situation, first post your situation onto a forum and let someone find a set-based solution to the problem (there's a great chance there is one), and if none is found, use a method that suits the size of the data set. Martin's solution is the appropriate one here, no matter the size of the data set - it doesn't use a temp table.
Post #825198
 Posted Thursday, November 26, 2009 6:37 AM
 Forum Newbie Group: General Forum Members Last Login: Friday, July 30, 2010 7:17 AM Points: 1, Visits: 9
 Hi All,I´m test for 24200 rows, the cursor done in 35 seconds and the tabla with identity in 84 seconds.But when i modify the WHILE control for an constant the final time is 35 seconds, equal to cursor.BEGIN CREATE TABLE #temp(id INT IDENTITY(1,1), name VARCHAR(32), salary float) INSERT INTO #temp SELECT name, salary FROM #emp DECLARE @i INT, @last INT SELECT @last = COUNT(id) FROM #temp SET @i = 1 WHILE (@i <= @last ) BEGIN UPDATE #emp SET salary = (salary * 115)/100 WHERE #emp.id = @i SET @i = @i + 1 END END
Post #825206

 Permissions