# vs @ - it’s not about social media it’s about temporary tables and data =tg= Thomas Grohser, NTT Data SQL Server MVP SQL Server Performance Engineering Pittsburgh, PA 9/30/2017
select * from =tg= where topic = @@Version Remark SQL 4.21 First SQL Server ever used (1994) SQL 6.0 First Log Shipping with failover SQL 6.5 First SQL Server Cluster (NT4.0 + Wolfpack) SQL 7.0 2+ billion rows / month in a single Table SQL 2000 938 days with 100% availability SQL 2000 IA64 First SQL Server on Itanium IA64 SQL 2005 IA64 First OLTP long distance database mirroring SQL 2008 IA64 First Replication into mirrored databases SQL 2008R2 IA64 SQL 2008R2 x64 First 256 CPUs & >500.000 STMT/sec First Scale out > 1.000.000 STMT/sec First time 1.2+ trillion rows in a table SQL 2012 > 220.000 Transactions per second > 1.3 Trillion Rows in a table SQL 2014 > 400.000 Transactions per second Fully automated deploy and management SQL 2016 AlwaysOn Automatic HA and DR, crossed the PB in storage SQL vNext Can’t wait to push the limits even further =tg= Thomas Grohser, NTT DATA Senior Director Technical Solutions Architecture email: Thomas.grohser@nttdata.com / tg@grohser.com Focus on SQL Server Security, Performance Engineering, Infrastructure and Architecture Wrote some of Close Relationship with SQLCAT (SQL Server Customer Advisory Team) SCAN (SQL Server Customer Advisory Network) TAP (Technology Adoption Program) Product Teams in Redmond Active PASS member and PASS Summit Speaker 23 Years with SQL Server
What is the difference between @ and #? 29 SELECT ASCII('@') - ASCII('#')
Source Temporary Target Repeatable
How to measure performance Time to read source(s) n x sources Time to create temp table 1 x Time to insert into temp table n x sources Time to modify temp table 1 x Time to read temp table n x targets Time to insert into target(s) n x targets
Options for Temporary Tables DECLARE @TableName AS TABLE (…); @Table CREATE #TableName (…); #Table CREATE ##TableName (…); ##Table Use TempDB; CREATE TableName (…); TinTDB CREATE TABLE TableName ( … ) WITH ( MO MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_ONLY); CREATE TABLE TableName ( … ) WITH ( MOD DURABILITY=SCHEMA_AND_DATA); Use MyDatabase; CREATE TABLE … UT
Indexes @Table Yes during declaration only DEMO @Table Yes during declaration only #Table Yes (Clustered, Non Clustered, Filtered) ##Table Yes (Clustered, Non Clustered, Filtered) TinTDB Yes (Clustered, Non Clustered, Filtered) MO Yes (Hash and BTree) MOD Yes (Hash and BTree) UT Yes (Clustered, Non Clustered, Filtered, Special)
Scope / Isolation / Durability @Table Current Batch Yes None #Table Current Session Yes None ##Table Global No None TinTDB Global No None MO Global No None MOD Global No Yes UT Global No Yes DEMO
Create Table Triggers Recompilation @Table No #Table Yes ##Table Yes TinTDB Yes MO Yes MOD Yes UT Yes
Statistics @Table No #Table Yes ##Table Yes TinTDB Yes MO Yes MOD Yes UT Yes Cardinality is assumed to be 30% of row count Same problems as with normal tables that update statistics might not be triggered depending on number of rows added/changed Manual Update Statistics can happen
Commit / Rollback supported @Table Yes No #Table Yes Yes ##Table Yes Yes TinTDB Yes Yes MO Yes Yes MOD Yes Yes UT Yes Yes DEMO
Truncate table @Table No #Table Yes ##Table Yes TinTDB Yes MO No (but DELETE is super fast) MOD No (but DELETE is super fast) UT Yes
Transaction Log IO @Table None #Table Reduced (no recovery needed) TinTDB Reduced (no recovery needed) MO None MOD Optimized UT Yes TempDB Your DB
Locking and Blocking @Table No #Table No ##Table Yes TinTDB Yes MO No (retry logic for updates/deletes) MOD No (retry logic for updates/deletes) UT Yes
Performance Read Write @Table Fast Fast #Table Fast OK ##Table Fast OK TinTDB Fast OK MO Fastest Fastest MOD Fastest Very Fast UT Fast Slowest
Create Table Time @Table µs #Table ms ##Table ms TinTDB ms MO seconds MOD seconds UT ms
Alternative Option
CTE – Common Table Expression DEMO WITH tempName AS ( SELECT … ) Do Something with tempName
Summary @Table small datasets in procedures, logging of transaction steps #Table all other cases ##Table sharing temp data between processes TinTDB just don’t do/use it MO staging if source grab is repeatable high frequency MOD staging if source grab is not repeatable UT only is in memory is not available
THANK YOU! and may the force be with you… Questions? thomas.grohser@nttdata.com (9 to 5 5 days a week :-) tg@grohser.com (24x7)