r/SQLServer • u/rip55jcp • Sep 04 '25
Question In memory heap tables - Is it possible
I have a database that is used to import data, compare it to data/ update data in a different database and then the data is deleted. This happens hundreds of times per day. For various reasons, I want to us in-memory tables for the tables used for the import. These tables do not need indexes or primary keys. Can I create in-memory heap tables? I hate to add constraints to these tables, as it could slow down the import process. I'm using MSSQL 2019, but I am porting it to MSSQL 2022 shortly.
2
Upvotes
3
u/jshine13371 6 Sep 04 '25
Nahhh, you don't.
In-memory tables are really only good for high concurrency writes, like if you had 100s of concurrent
INSERTqueries per second. Not 100 inserts that happen over the entire day. Otherwise they're not worth the trouble and not superior to a regular table which will automatically be brought into Memory when you query from it anyway.2
u/rip55jcp Sep 04 '25
Well, I might be dead in the water on this. I found this blurb: "memory-optimized tables do not support cross-database transactions". So, even if i can load the data, i can't use it to update the tables in the database that holds the permanent data. Other option to consider is moving those in-memory tables to my real database.
6
u/dbrownems Microsoft Employee Sep 04 '25 edited Sep 07 '25
You misunderstand the role and structure of indexes on in-memory tables. There's no such thing as a heap or btree with in-memory tables. They are stored as structs in memory, tracked by a hashtable or special range index.
SQL_Server_2016_In_Memory_OLTP_White_Paper.pdf
If you want an in-memory heap, just use a heap, and SQL Server will cache it in memory. If you want more aggressive caching, possibly avoiding ever storing on disk, Temp tables (or permanent tables in TempDb) will be in-memory heap tables if there is sufficient memory.
1
u/watchoutfor2nd Sep 04 '25
I don't think you'll know until you test it. I would make sure you understand any limitation around in memory OLTP and then just set this up in a test environment to do comparisons.
I just finished moving our databases out of in memory OLTP as we found it too limiting in many areas. It also came with some drawbacks. When starting up the databases would go to in recovery while the dlls were compiled and data was all loaded into memory. When more data you have the longer this takes. If you plan to put stored procedures in memory, they are slower on first execution as the dll needs to be compiled. When it came to shutting down our servers we found that XTP files were not getting cleaned up and if the number of files grew too large everything slowed down. We designed a shut down process that would kick everyone out of the database to work around this problem. Again, I think it could work for your smaller use case of loading/updating/deleting data. TRUNCATE TABLE is not supported with in memory so you'll have to just use DELETE.
1
u/Informal_Pace9237 Sep 05 '25
Just wondering if you can change your process to use TEMP tables. I see from your explanations you do not need the data for anything else but your current process. If you need others in the system to acccess the data you can go for Global TEMP Tables..
1
u/No_Resolution_9252 Sep 06 '25
Why?
If you don't want to physically write it, put them in a temp table, its already memory optimized if you have enough memory.
In memory is really only for extreme high concurrency and a staging table doesn't match that. It will give you more problems that whatever you are trying to solve and no performance benefit.
2
u/Dry_Author8849 Sep 04 '25
Sounds a bit over engineering to me. You answered yourself. Add the db to your OA group to keep the schema in sync and go with physical tables.
Cheers!
1
u/thatOMoment Sep 04 '25
Even if they don't have an index you're still going to have to rebuild the heap to avoid forward fetches from bogging down performance.
Have you confirmed that the time saved from insertion outweights time to compare after a few iterations?