In SQL Server 2016,If we create Memory Optimized Table - sql-server-2016

In SQL Server 2016,If we create Memory Optimized Table(i.e create table on RAM ),will the table data persist if I restart my computer?

When you create a memory optimized table you set a table option: WITH (MEMORY_OPTIMIZED = ON). That table option has a second parameter DURABILITY which either can be: SCHEMA_AND_DATA in which case the data is persisted, or SCHEMA_ONLY in which case only the schema is persisted. Default is SCHEMA_AND_DATA.
You can read more about it here.
Niels

Related

SQL server table variables and table hash preferences and Performance

When should I prefer table variable over hash table and hash table over table variable ones. As I see both are almost same. Is there a performance difference?
As per your question When should i prefer Variable table over hash table and Hash table over variable ,I guess this has been already answered in When should I use a table variable vs temporary table in sql server?
Note : For your Information don't believe that Table Variables are stored in memory and Temp Tables are stored in TempDB . Both where created in TempDB and not in memory. This is proved by Pinal Dave in Temp table and Table Variable storage place

Alter Memory Optimized SQL Server 2014 Table

Can I alter my memory optimized table? Like adding column or changing data types etc. If yes, how to do it?
I am using SQL Server 2014
Thanks
According to Altering Memory-Optimized Tables (SQL Server 2014):
Performing ALTER operations on memory-optimized tables is not
supported. This includes such operations as changing the bucket_count,
adding or removing an index, and adding or removing a column. This
topic provides guidelines on how to update memory-optimized tables.
Updating the definition of a memory-optimized table requires you to create a new table with the updated table definition, copy the data to the new table, and start using the new table.
But it will be possible with SQL Server 2016:
In SQL Server 2016 Community Technology Preview 2 (CTP2) you can
perform ALTER operations on memory-optimized tables by using the ALTER
TABLE statement. The database application can continue to run, and any
operation that is accessing the table is blocked until the alteration
process is completed.
In the previous release of SQL Server, you had to manually complete
several steps to update memory-optimized tables.

Will SQL Server 2005 Database Engine Tuning Advisor "tune" Temporary Tables?

I'm attempting to use Database Engine Tuning Advisor to tune my database. From the comments it's logging (it's just 40% into the analysis, after running all weekend) it appears that DTA is not capable of tuning operations on Temporary tables. Is that in fact the case?
No. Because in order to apply an index to a transient temporary table you would have to add the index to the script that used the temporary table. It can't just be applied to a non-permanent table as a set-and-forget operation.
What you can do is create a real table with the same name in your database and remove the creation of the temp table from scripts and replace with TRUNCATE TABLE. If you create this table, and then perform actions against it, the DTA will recommend indexing for the permanent table. You then script these recommended indexes, and add them to the temp table in your script.
Also: be aware that while the DTA does a pretty good job in most cases, it doesn't always get it right...

SQL: What is INSERT INTO #table?

I wanted to know what does the # symbol mean? Why is it being used?
Example:
INSERT INTO #tmpContracts (sym, contractCount)
SELECT sym,
COUNT(DISTINCT(fulloptionsym)) AS contractCount
It means it is a temporary table. Take a look at the "Temporary Table" section of the CREATE TABLE documentation.
It is a temporary table, and is only available to the current session. It will be automatically dropped when it goes out of scope. If the same code is run by multiple people at the same time, each gets their own table, like local variables, and there is no need to worry about a collision.
'#' is used for temporary table e.g : '#tmpContracts'
'##' is used for global temporary table e.g : '##tmpContracts'
Read this temporary table
A #Temp is a temp table that is active for the duration of the connection. It resides in the TempDB.
A #Temp is local (only available to the current connection) and is dropped once the connection is terminated.
A ##Temp is global (available to all connections) and is dropped once all connections using it are terminated.
corrected owing to feedback and correction from Gail
There is also a ## table type in SQL Server which is a temp table that is accessible removed when the server or the SQL Server service is restarted.
Try using
'#temp' = temperory table

How tempDB works?

I am trying to understand the tempDB and following are the doubts popping in my mind.
What is the lifetime of data in tempDB? Say a query is doing some Order By and uses tempDB for performing that. After this query finishes, someone else also executes a query which utilizes the tempDB. Will the second query find records written by first query in the tempDB or will they be deleted?
Are there any visible tables created inside the tempDB by the Sql Engine? How can I know which temporary table is created because of this query? Is there any naming convention followed by the Sql engine for naming these temporary tables?
I am new to tempDB so please pardon me for asking such silly (if at all) questions :-)
It will be very nice if someone can point me to a good resource which can help me learn about tempDB.
Temp table is stored in tempdb until the connection is dropped (or in the case of a global temp tables when the last connection using it is dropped). You can also (and it is a good practice to do so) manually drop the table when you are finished using it with a drop table statement.
No, others cannot see your temp tables if they are local temp tables (They can see and use global temp tables) Multiple people can run commands which use the same temp table name but they will not be overlapping in a local temp table and so you can have a table named #test and so can 10,000 other users, but each one has its own structure and data.
You don't want to generally look up temp tables in tempdb. It is possible to check for existence, but that is the only time I have ever referenced tempdb directly. Simply use your temp table name. Example below of checking for existence
IF OBJECT_ID('TempDB.dbo.#DuplicateAssignments') IS NOT NULL
BEGIN
DROP TABLE #DuplicateAssignments
END
You name temp tables by prefacing the name with # (for local tables the ones you would use 999.9% of the time) and ## for global temp tables, then the rest of the name you want.
There's a few MSDN articles that are probably the best source of information on the tempDB database in SQL Server.
tempdb Database
The tempdb system database is a global
resource that is available to all
users connected to the instance of SQL
Server and is used to hold the
following:
Temporary user objects that are explicitly created, such as: global or
local temporary tables, temporary
stored procedures, table variables, or
cursors.
Internal objects that are created by the SQL Server Database Engine, for
example, work tables to store
intermediate results for spools or
sorting.
Row versions that are generated by data modification transactions in a
database that uses read-committed
using row versioning isolation or
snapshot isolation transactions.
Row versions that are generated by data modification transactions for
features, such as: online index
operations, Multiple Active Result
Sets (MARS), and AFTER triggers.
Operations within tempdb are minimally
logged. This enables transactions to
be rolled back. tempdb is re-created
every time SQL Server is started so
that the system always starts with a
clean copy of the database. Temporary
tables and stored procedures are
dropped automatically on disconnect,
and no connections are active when the
system is shut down. Therefore, there
is never anything in tempdb to be
saved from one session of SQL Server
to another. Backup and restore
operations are not allowed on tempdb.
There's also tempdb and Index Creation, this blog post along with Working with tempdb in SQL Server 2005 which states:
The SQL Server system database, tempdb, has undergone a number of changes in SQL Server 2005. There are new tempdb usages and internal optimizations in SQL Server 2005; tempdb architecture is mostly unchanged since SQL Server 2000.
The tempdb system database is very similar to a user database. The main difference is that data in tempdb does not persist after SQL Server shuts down.
The temporary tables created in TempDB are dropped when the query is completed.
I'm not sure on this (I would have to try it), but I think theoretically ALL tables created in TempDB are visible, although only the user that created the table has permission to access it.