Are you sure that Seq 8 is 36000000000000000001 dec, not 36000000000000000443?
[edit] nevermind, my mistake ;)[/edit]
Leszek, Just to be sure, I ran some non-SQL code against that huge binary string and confirm the expected result.
My source:
IF OBJECT_ID('TC51','U') IS NOT NULL BEGIN DROP TABLE TC51 END CREATE TABLE TC51( Seq INT, BinaryValue VARCHAR(2048) ) INSERT INTO TC51(Seq,BinaryValue) SELECT 1,'11111111' UNION ALL SELECT 2,'1001' UNION ALL SELECT 3,'10001' UNION ALL SELECT 4,'10000001' UNION ALL SELECT 5,'100011010101001011' UNION ALL SELECT 6,'0' UNION ALL SELECT 7,'001001' UNION ALL SELECT 8,'11111001110011001101100010100001110001010000100000000000000000001' UNION ALL SELECT 9,REPLICATE('10010011',256) UNION ALL SELECT 10,REPLICATE('0',1024) + REPLICATE('00011011',128) SELECT * FROM TC51
Result:
Seq DecimalValue 6 0 2 9 7 9 3 17 4 129 1 255 5 144715 8 36000000000000000001 10 19034397898542168434780878490707320708896226835859716652480832122583459791170690214051485834137268519883306175515794590810930681403879407087477963244179613169410718127338698441020235016602527612647973489044294728465215954600966658038414329351381707464354759954838549225406178981721381726394331846666094320411 9 18629803499932227738059164208762678188961894480188926089110434422925977668288548867843092578407326584949966824531447093590727745863224178043050817258070186740128388129385280099950113997283446508623609697421403657006621396629274918258000783795121557094805889961762105260017543749241320289667883147730265751653809439248161750336630161935784281238211207361541056245244852674202591569914288886980737306474643279053779253908289776594380225958385654697832719800569777345060343041774472826782737623348466292353108806899437390574320633719403493332618361566506852111078076525132097066207301240607560650135108668552257884885907
And stats:
(10 row(s) affected) Table 'Worktable'. Scan count 2, logical reads 315853, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 10, logical reads 30, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC51'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 9688 ms, elapsed time = 13020 ms. SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms.
My stats for Leszeks data:
SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms. (10 row(s) affected) Table 'Worktable'. Scan count 2, logical reads 25250, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 4201, logical reads 16804, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC51'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 1279 ms, elapsed time = 1281 ms.
And my version 2 for this data:
SQL Server parse and compile time: CPU time = 47 ms, elapsed time = 75 ms. (10 row(s) affected) Table 'Worktable'. Scan count 4, logical reads 24206, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 4000, logical reads 16000, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC51'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 1279 ms, elapsed time = 1307 ms.
1307 ms is really impressive!
My stats for Leszek's data:
(10 row(s) affected) Table 'Worktable'. Scan count 2, logical reads 1603120, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC51'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 4555 ms, elapsed time = 4677 ms.
@bertdehaes - awesome stats
my v2:
(10 row(s) affected) Table 'Worktable'. Scan count 2, logical reads 289634, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 10, logical reads 30, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC51'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 5850 ms, elapsed time = 7537 ms.
A script to generate some random data :
truncate table TC51; with l as ( select T.N as N ,cast(rand(T.N) * 1000000 as int) % 2048 as len from tsqlc_Tally T where T.N between 1 and 100 ) insert into TC51 ( Seq,BinaryValue ) select l.N as Seq ,(select cast(cast(RAND(T2.N) * 1000000 as int) % 2 as VARCHAR(1)) + '' from tsqlc_Tally T2 where T2.N between l.N and l.N + l.len for xml path('')) as BinaryValue from l;
First 10 lines :
Seq DecimalValue ----------- ---------------------------------------------------------- 64 11666 65 3643202340 66 3820174537504329 67 822280047201658451657 68 243253103131836075155352978 69 255069365869568144342099404577572 70 48659215962789527394986960769224334116 71 6421395640336745194654410830253452358694601 72 3366660677480871464614971745371922030235275081417 73 3530199586550182276880108612875108514775983803772071314 74 487806473148463380077912584171804580945336005458517779864356
And my stats:
SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms.
(100 row(s) affected) Table 'Worktable'. Scan count 4, logical reads 606722, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 99524, logical reads 398096, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC51'. Scan count 1, logical reads 15, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 33618 ms, elapsed time = 34025 ms.42
My time is over 5 min for your data. BTW check code, Seq 74 is '487806473148463380077912584171804580945336005458517779864356'
Leszek,
I guess bertdehaes fixed his post showing the result for Seq=74. His decimal result and your decimal result and my decimal result are now equal.
I too am amazed at his performance.
Running the 100 rows through some quick and dirty vbscript takes 32 seconds.
And I'm still struggling with the sql version.
Managed Windows Shared Hosting by OrcsWeb