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

 Optimize query needed Rate Topic Display Mode Topic Options
Author
 Message
 Posted Friday, September 28, 2012 3:03 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, July 9, 2015 12:38 PM Points: 160, Visits: 438
 Hello Expert.I need to convert data from an existing column from BigInt to VarBinary type. I wrote the function to do the conversion; the function is fairly fast, however, it is not fast enough to use on a table with more than 40 millions records (takes about 27minutes). Could someone help optimize it so that it runs a bit faster? Thanks in advance,-Hai `ALTER function [dbo].[ConvertBigIntToVarBinary]((c)param BigInt)returns varbinary(128) ASbegin declare (c)result varchar(128) set (c)result = ''while 1=1 begin select (c)result= convert(char(1),(c)param % 2)+(c)result, (c)param = convert(bigint, ((c)param / 2)) if (c)param = 0 return convert(varbinary(128), replace(replace(reverse((c)result),'0','00'),'1','01'),2) end return convert(varbinary(128),'00',2)end`
Post #1366123
 Posted Friday, September 28, 2012 8:40 PM
 SSC-Forever Group: General Forum Members Last Login: Today @ 3:23 PM Points: 42,036, Visits: 39,415
 Is there a reason why you couldn't just use CAST(SomeBigIntColumn AS VARBINARY(128))? --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." Helpful Links:How to post code problemsHow to post performance problems
Post #1366151
 Posted Friday, September 28, 2012 9:18 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, July 9, 2015 12:38 PM Points: 160, Visits: 438
 Yes there is, if you run the function and pass a parameter like 5, you will see the result. Just perform a normal conversion would not give the desire result
Post #1366157
 Posted Monday, October 1, 2012 8:31 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, July 9, 2015 12:38 PM Points: 160, Visits: 438
 Some one suggested this function works`ALTER function [dbo].[ConvertBigIntToVarBinary1](@param bigint)returns varbinary(1000) ASbegin declare @iPower int; declare @result varbinary(1000); declare @powerOf2 bigint; set @result = 0x; set @powerOf2 = 1; set @iPower = 0; while @powerOf2 <= @param begin set @result = @result + CAST(@param / @powerOf2 % 2 AS binary(1)); set @iPower = @iPower + 1; set @powerOf2 = power(2., @iPower); end return @resultend`Although I dont see much advance of it plus it fails to convert the max bigint value for some reasonsSELECT dbo.ConvertBigIntToVarBinary1(9223372036854775807)
Post #1366599
 Posted Tuesday, October 2, 2012 10:55 AM
 SSCertifiable Group: General Forum Members Last Login: Today @ 2:49 PM Points: 5,643, Visits: 8,166
 I am seriously missing something:SELECT CAST(5 AS VARBINARY(128)) --0x00000005SELECT CAST(0x00000005 AS bigint) --5SELECT dbo.ConvertBigIntToVarBinary1(5) --0x010001SELECT CAST(0x010001 AS bigint) --65537 Best,Kevin G. BolesSQL Server ConsultantSQL MVP 2007-2012TheSQLGuru at GMail
Post #1367180
 Posted Tuesday, October 2, 2012 11:13 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, July 9, 2015 12:38 PM Points: 160, Visits: 438
 TheSQLGuru (10/2/2012)I am seriously missing something:SELECT CAST(5 AS VARBINARY(128)) --0x00000005SELECT CAST(0x00000005 AS bigint) --5SELECT dbo.ConvertBigIntToVarBinary1(5) --0x010001SELECT CAST(0x010001 AS bigint) --65537NAH! you aren't missing anything. it was just our requirements written that way. In the nutshell, our existing application has capability of searching the varbinary field data bit-by-bit (or byte-by-byte whatever that is..) without first converingt the bit to character, that is from what I was told and understood. so this is the how conversion process works: -first convert the bigint to binary value, -reverse the entire string-foreach bit in the string, add a 0 infront of it.-then convert the value to varbinary field using this format: convert(varbinary(128),@result,2)
Post #1367190
 Posted Tuesday, October 2, 2012 1:26 PM
 SSCertifiable Group: General Forum Members Last Login: Today @ 2:49 PM Points: 5,643, Visits: 8,166
 haiao2000 (10/2/2012)TheSQLGuru (10/2/2012)I am seriously missing something:SELECT CAST(5 AS VARBINARY(128)) --0x00000005SELECT CAST(0x00000005 AS bigint) --5SELECT dbo.ConvertBigIntToVarBinary1(5) --0x010001SELECT CAST(0x010001 AS bigint) --65537NAH! you aren't missing anything. it was just our requirements written that way. In the nutshell, our existing application has capability of searching the varbinary field data bit-by-bit (or byte-by-byte whatever that is..) without first converingt the bit to character, that is from what I was told and understood. so this is the how conversion process works: -first convert the bigint to binary value, -reverse the entire string-foreach bit in the string, add a 0 infront of it.-then convert the value to varbinary field using this format: convert(varbinary(128),@result,2)Ahh - what I was missing was the actual requirements! This may get you part-way there: http://pratchev.blogspot.com/2008/04/convert-hex-string-to-binary-string.html. You will need to efficiently deal with leading zeros though before doing the reverse/replace thing. The key is to do this as a set-based operation and not the iterative way you have now. I am confident there is a solution, but it goes beyond what I am willing to invest for free to get you there. Best,Kevin G. BolesSQL Server ConsultantSQL MVP 2007-2012TheSQLGuru at GMail
Post #1367257
 Posted Tuesday, October 2, 2012 2:12 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, July 9, 2015 12:38 PM Points: 160, Visits: 438
 [b] Ahh - what I was missing was the actual requirements! This may get you part-way there: http://pratchev.blogspot.com/2008/04/convert-hex-string-to-binary-string.html. You will need to efficiently deal with leading zeros though before doing the reverse/replace thing. The key is to do this as a set-based operation and not the iterative way you have now. I am confident there is a solution, but it goes beyond what I am willing to invest for free to get you there. Not big of a deal. Believe it or not...I did this entire thing for free too . It wasn't my assignment. Anyway what I got so far should be good for them to start with, they should work on the fine tune it. Thanks for direction!-Hai
Post #1367281
 Posted Tuesday, October 2, 2012 3:49 PM
 Ten Centuries Group: General Forum Members Last Login: Saturday, November 19, 2016 9:42 PM Points: 1,097, Visits: 1,199
 I'm not sure how logical this project is, but it seemed interesting to do this without looping. It was fun.`declare @b bigint = 31;declare @out varbinary(128) = 0x;-- simple inline tally tablewith cteTen as ( select N from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) as t(N) ),cteNums as ( select N = 0 union all select top (63) -- the 64th bit would be bigger than a bigint max value N = row_number() over(order by (select null)) from cteTen as cte1 cross join cteTen as cte2 ),-- the meat of the querycteInit as ( select cte.N , ValThis = power(convert(decimal(20,0), 2), N) , ValNext = power(convert(decimal(20,0), 2), N + 1) from cteNums as cte ) select @out = @out + convert(varbinary(1), case when (@b - (floor(@b / cte.ValNext)) * cte.ValNext) >= cte.ValThis then 1 else 0 end)from cteInit as ctewhere cte.ValThis <= @border by cte.N desc;select @out;`This is verified to work with the largest possible bigint. I did not code it with negative numbers in mind.
Post #1367312
 Posted Thursday, October 11, 2012 9:56 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, July 9, 2015 12:38 PM Points: 160, Visits: 438
 bteraberry (10/2/2012)I'm not sure how logical this project is, but it seemed interesting to do this without looping. It was fun.`declare @b bigint = 31;declare @out varbinary(128) = 0x;-- simple inline tally tablewith cteTen as ( select N from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) as t(N) ),cteNums as ( select N = 0 union all select top (63) -- the 64th bit would be bigger than a bigint max value N = row_number() over(order by (select null)) from cteTen as cte1 cross join cteTen as cte2 ),-- the meat of the querycteInit as ( select cte.N , ValThis = power(convert(decimal(20,0), 2), N) , ValNext = power(convert(decimal(20,0), 2), N + 1) from cteNums as cte ) select @out = @out + convert(varbinary(1), case when (@b - (floor(@b / cte.ValNext)) * cte.ValNext) >= cte.ValThis then 1 else 0 end)from cteInit as ctewhere cte.ValThis <= @border by cte.N desc;select @out;`This is verified to work with the largest possible bigint. I did not code it with negative numbers in mind.Wow this is way over my head It works, I will runs it against the database and see how faster compare to the original code.Thanks for spending time on this. alot to learn from this code. btw, can you help me understand the logics behind this query?
Post #1371626

 Permissions