Solved

Azure SQL

Posted on 2014-11-25
9
232 Views
Last Modified: 2014-12-01
I am converting some TSQL Code and I have a number of table variable declaration.  They are not performing as well as when they as on a physical device.  I have read some unofficial sites that say that a temp table is the way to go.  Do I have other options?
0
Comment
Question by:Alyanto
  • 5
  • 3
9 Comments
 
LVL 1

Expert Comment

by:Andre Batista
ID: 40465917
Hello Alyanto.

Temp tables is always a great way to speed up query's. Do you have a example of a query you want to improve ?
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40466318
If you share your code with us we would help you better. Maybe the issue isn't only on table variables.
0
 
LVL 1

Author Comment

by:Alyanto
ID: 40466546
I cannot share the code however I can describe it.

There is a table variable declared which takes an initial field of the type UNIQUEIDENTIFIER.  Thereafter a lot of updates are run to fully populate the table.  The reason for the many is that there is a lot of outer joins which use COALESCE to determine which is the active link within the ON part of the join.  Breaking it down A little has improved the performace by 70%.  

However having read around there is still some concern that DECLARE %TableName TABLE(...) is considered slower than CREATE TABLE  @TABLENAME (...) which traditionally it has not been when using any CRUD activity.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:Alyanto
ID: 40466549
Ignore previous slight changes where wrong symbols used.

I cannot share the code however I can describe it.

There is a table variable declared which takes an initial field of the type UNIQUEIDENTIFIER.  Thereafter a lot of updates are run to fully populate the table.  The reason for the many is that there is a lot of outer joins which use COALESCE to determine which is the active link within the ON part of the join.  Breaking it down A little has improved the performace by 70%.  

However having read around there is still some concern that DECLARE @TableName TABLE(...) is considered slower than CREATE TABLE  #TABLENAME (...) which traditionally it has not been when using any CRUD activity.
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40466759
A temporary table allows you to do the same operations you do with regular tables (index, truncate, ...). These kind of operations are not allowed in table variables and perhaps it's because of this that it's common to say that table variables are slower than temporary tables.

My recommendation is to use temporary tables everywhere you can (for example Functions only allows table variables) and create indexes if you are working with large data so you can increase the performance.

If you are working with a small set of data and you won't need to do more operations than a simple select then there's no performance difference between those two options.
0
 
LVL 1

Author Comment

by:Alyanto
ID: 40471157
I am not sure this answers my question.  I realise that adding an index is an obvious performance hike but the problem is simply why is there a performance difference and what can I do to resolve it?
0
 
LVL 47

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40471517
Better to send you to MSDN article about table variables. These are the interesting parts in the article:
table  variables are not supported in the SQL Server optimizer's cost-based reasoning model. Therefore, they should not be used when cost-based choices are required to achieve an efficient query plan. Temporary tables are preferred when cost-based choices are required. This typically includes queries with joins, parallelism decisions, and index selection choices.
Indexes cannot be created explicitly on table variables, and no statistics are kept on table variables. In some cases, performance may improve by using temporary tables instead, which support indexes and statistics
0
 
LVL 1

Author Comment

by:Alyanto
ID: 40473811
Hmmm, I see.  Not terribly impressed with MIcrosoft for producing a limited option with no obvious advantages to its alternative.
0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 40473814
While not an answer I had hoped for it is the right answer.  Thanks for your help
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 35
Webservices in T-SQL 3 30
SQL Server - Set Value of Multiple Fields in One Query 10 22
Find SQL query used by application 3 16
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now