Category Archives: SQL Server

Temp Table vs. Table Variable

This is a comparison summary that I’ve shared to describe the differences between Temp Tables vs. Table Variables.  A few people have asked that I post it so here you go:

Comparison Summary

This is a summary of the functionality and behavior of the different object types. Note that global temporary tables aren’t included, because typically you use those for different purposes than the other types of temporary objects. You might find this table handy as a reference when you need to choose the appropriate temporary object type for a given task.

Functionality/Object Type Local Temp Table Table Variable Table Expression
Scope/Visibility Current and inner levels Local Batch Derived Table/CTE: Current statement View/Inline UDF: Global
Physical representation in tempdb Yes Yes No
Part of outer transaction/Affected by outer transaction rollback Yes No Yes
Logging To support transaction rollback To support statement rollback Yes
Locking Yes No Yes
Statistics/recompilations/efficient plans Yes No Yes
Table size Any Typically recommended for small tables Any

Temp tables can be altered with DDL statements but table variables can’t (so you cannot create a non-clustered index on a table variable for example). That makes every table variable a heap, or at best a table with a single, clustered index and every table variable access a table scan (or clustered index scan, which is essentially the same thing).

SQL Server creates statistics for temp tables, so the query optimizer can choose different plans for data involving temp tables. However, SQL Server does not create statistics on columns in table variables (So, the only way to access data in a table variable is through a table scan).

Now go read Paul White’s (b | t) post on Temporary Tables in Stored Procedures

Table Partitioning vs. Partitioned Views

Recently I was doing some work trying to figure out whether to use Table Partitioning or Partitioned views in a data warehouse to solve a problem.  In this particular data warehouse the data cannot be partitioned by date, it’s actually partitioned by a value in a table.  I was leaning towards Table Partitioning and talking with Brent (b | t) we put together this quick comparison up on a whiteboard:

Table Partitioning Partitioned Views
Parallel Loads Y Y
Query Partition Elimination Y Y
Divide Filegroups Y Y
Index Tuning EASY (single table means single set of indexes) POWERFUL (different indexes on different tables)
Schema changes doable N (single table means adding a column is time consuming) Y (different tables can be different schemas and then managed through views)
Enforced Relational Integrity Y N
Freeze Data 10% R/O, Perfect Stat/Indexes Y Y

As I said I was leaning towards table partitioning while Brent was leaning towards partitioned views.  My argument was for referential integrity while Brent’s was ability of schema changes.  Well turns out that Brent won (big surprise huh?) however not for any of the reasons listed.  The reason we went Partitioned Views is that once that is in place we can then implement table partitioning on the individual tables.  Combining these two techniques can be very powerful (if done correctly that is).  If we went straight to table partitioning, we would be limited in our future potential in this regards.

So lesson of the day is when looking and planning architecture – think, whiteboard and then think some more – do not just open up SSMS and start typing code.

SQLSaturday Indianapolis

This past weekend I had the pleasure of presenting at SQLSaturday #126 Indianapolis. You can read Hope Foley’s write-up here

A few highlights:

  • As someone who has organized and attended multiple SQLSaturday’s it’s still awesome to see so many first timers at every event.
  • Speaker dinner, now with bowling shirt and…bowling! I hadn’t bowled in probably 7 years, was a lot of fun!
  • The venue was great, university classrooms work very well.
  • I got to hang out with other speakers and the organizers, had many great conversations
  • My session on Writing Professional Database Code was standing room only (course I’m sure it’s only because we were in the smallest room, but it is cool nonetheless)
  • I was pulled into a Professional Development session – Consulting – The Good, The Bad, and The Ugly with Ted (blog | @onpnt), Hope (blog | @hope_foley) and Josh (blog | @joshuafennessy) – was a lot of fun!
  • My wife made the trip – every SQLSaturday I attend either my wife or my son comes with, which is realy cool

Overall I enjoyed the energy event and was glad to meet up with some existing friends and make new ones. Thanks to the great organizer, vendors, other speakers and most importantly the attendees.

My presentation slides as promised:

Also during my session we discussed baselines, I brought up Erin Stellato (blog | @erinstellato), here’s the link.