Linked Questions

-1 votes
2 answers
2k views

What are the differences between a table type and a table variable in MS SQL [duplicate]

Are there differences between a table variable and a table type in Microsoft SQL Server? Or is a table type just a table variable that is defined once for broad usage?
GettingItDone's user avatar
1 vote
1 answer
1k views

Table variable for large amount of data [duplicate]

One of our stored procedure was using Cursor to update rows. Total rows to be updated varies from 1000 to 2000. In our scenario, this stored procedure was taking 50 minutes to complete the update. I ...
RPK's user avatar
  • 1,425
0 votes
0 answers
60 views

Do Primary Keys On Table Variables Have No Statistics At All? [duplicate]

My confusion is down to an apparent contradiction in principles. Primary keys are indexes and those always have statistics, but table variables notoriously always don't have statistics. What follows ...
J. Mini's user avatar
  • 139
190 votes
7 answers
214k views

What's the difference between a CTE and a Temp Table?

What is the difference between a Common Table Expression (CTE) and a temp table? And when should I use one over the other? CTE WITH cte (Column1, Column2, Column3) AS ( SELECT Column1, Column2, ...
Rachel's user avatar
  • 8,457
17 votes
5 answers
4k views

Inclusion of ORDER BY on query that returns no rows drastically affects performance

Given a simple three table join, query performance changes drastically when ORDER BY is included even with no rows returned. Actual problem scenario take 30 seconds to return zero rows but is instant ...
Hafthor's user avatar
  • 273
38 votes
2 answers
16k views

Why is using a table variable more than twice as fast as a #temp table in this specific case?

I was looking at the article here Temporary Tables vs. Table Variables and Their Effect on SQL Server Performance and on SQL Server 2008 was able to reproduce similar results to those shown there for ...
Martin Smith's user avatar
  • 84.4k
11 votes
2 answers
3k views

Dodgy T-SQL Query Execution driving me crazy

SQL Server 2019. Here's a link to a gist with the xml plan. Hello, I am having a heck of a time understanding why this query is taking 0.02 seconds to execute when it can't find a record with one of ...
ZCT's user avatar
  • 111
8 votes
3 answers
16k views

Using a CTE in IF EXISTS Query

Is it possible to do something similar to the following in SQL Server 2012? IF EXISTS ( WITH DATA AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY column ORDER BY Column) AS rn ...
Weston Sankey's user avatar
18 votes
2 answers
6k views

Why is table variable forcing an index scan while temp table uses seek and bookmark lookup?

I am trying to understand why using a table variable is preventing the optimizer from using an index seek and then bookmark lookup versus an index scan. Populating the table: CREATE TABLE dbo.Test ...
8kb's user avatar
  • 2,619
4 votes
4 answers
2k views

Effect on execution plans when a table variable has a primary key

Having read a great deal about the differences between temporary tables and table variables in SQL Server, I am experimenting with switching from mostly using temporary tables to mostly using table ...
sam.bishop's user avatar
12 votes
2 answers
606 views

@table_variable or #temp_table

I have a big user defined table type variable that has 129 columns. I will store around 2000-3000 records in this table variable at a time and pass it to various stored procedures and functions to get ...
Sreekumar P's user avatar
3 votes
2 answers
8k views

Why is using a CTE so much faster than using a #Temp Table in this query?

I have 2 different queries for identifying dups (below). The only difference between the 2 queries is that one uses a CTE and the other uses a #Temp table. Does anyone know why the CTE is so much ...
Juan Velez's user avatar
  • 3,245
2 votes
1 answer
9k views

Massive joins Vs Updating temp table

I'm working on a query that essentially aggregates a slew of tables together. The first method I tried was to inner join all of the tables into one nice big query that pretty much looked like this: ...
Tsaukpaetra's user avatar
4 votes
1 answer
3k views

Parallelism with temp table but not table variable?

The first query (inserts into table variable) takes twice as long as the second one. It does not use parallelism in the execution plan. The second query (inserts into temp table) uses parallelism in ...
K. R. 's user avatar
  • 143
4 votes
1 answer
5k views

Efficiently bulk upsert unrelated rows

As part of a manual replication process between databases with different but related schemas, for each table, we identify new and modified records in the source database and feed them via a table ...
Edward Brey's user avatar
8 votes
2 answers
362 views

Most efficient way to generate a diff

I have a table in SQL server that looks like this: Id |Version |Name |date |fieldA |fieldB ..|fieldZ 1 |1 |Foo |20120101|23 | ..|25334123 2 |2 |Foo ...
ame's user avatar
  • 83
5 votes
1 answer
2k views

varchar and nvarchar in tuning a stored procedure - how to improve performance in this scenario?

I have the following procedure that is called over a million times a day, and I think it can be tuned for better resources usage. ALTER PROCEDURE [DenormV2].[udpProductTaxRateGet] ( @itemNo ...
Marcello Miorelli's user avatar
0 votes
1 answer
2k views

Optimizing performance of INSERT INTO table from INNER JOIN between two large tables

I have the following query: DECLARE @accountCard TABLE ( numUsu VARCHAR(10) ,quantity INT ) /*Takes less than a minute*/ INSERT INTO @accountCard SELECT a.numUsu ,count(*) FROM ...
Heathcliff's user avatar
2 votes
1 answer
337 views

Understanding execution plans using TVPs

I have some slow running queries (8 seconds across 1M records grouped by months) and have been trying to make sense of the execution plans. We use around 10 TVPs to send in a set of filters that the ...
SteveB's user avatar
  • 131
0 votes
1 answer
5k views

TempLog.ldf Grows Too Large [duplicate]

We have a stored procedure that we run regularly. It blows up the size of TempLog.ldf from nothing to 150 GB in the course of 3-4 hours. What's going on and why is it so big? TempDB.mdf is a constant ...
Robbie's user avatar
  • 61
3 votes
1 answer
3k views

Cannot access a temporary table?

I am using ADO/Visual C++ to access SQL Server database. I find it is OK to create only one temp table in the database. But if I create two temp tables and open recordset of one table, and access the ...
user2347976's user avatar
6 votes
1 answer
263 views

MSSQL table variable identification

Is there any way to identify which stored proc is creating a table variable in tempDB? I am looking at the forward_fetch_count in sys.dm_db_index_operational_stats and we have a few tables that have a ...
Evan King's user avatar
1 vote
1 answer
2k views

Another Dynamically alias column question

I might be asking the same question as this, but perhaps if I ask it my way, a different solution may become obvious. I have this: DECLARE @Table1 AS TABLE( Table1ID Int Primary Key ,ColumnName ...
Phillip Senn's user avatar
1 vote
1 answer
3k views

Different execution plans during INSERT at @table variable and #temporary table

I faced with strange situation: inserting of ~800 000 rows into table variable is much more faster than inserting (with the same select) into temporary table. INSERT is under stored procedure. SELECT ...
whitebeast's user avatar
3 votes
1 answer
2k views

Should I use tempdb or memory optimized table variable?

I would like to change some of my stored procs to use memory optimized tables. Before I do this, could you help me to clarify some things? parallel execution Here I can read, the tempdb doesn’t ...
deemon's user avatar
  • 33
3 votes
1 answer
2k views

PAGELATCH_EX on 2:1:128

We are having problems on SQL Server 2012 with contention on tempdb page 2:1:128, which seems to be related with system table sysobjvalues. We have about 400 sessions waiting on this resourse 2:1:...
Renato Ferreira's user avatar
1 vote
1 answer
1k views

Cursors and concurrent users

I typically avoid cursor operations like the plaque, however, I have come across my first problem where using a cursor outperforms a query. So I am forced to use it. I have created an elaborate ...
Ross Bush's user avatar
  • 505