Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Bad performance when inserting to memory optimized table

$
0
0
When using normal (not natively compiled) SQL to insert values into a memory-optimized table-valued variable the performance is lower than expected.There seems to also be some kind of concurrency problem meaning that two different sessions can not execute the code simultaneously even on a multi-core server.Run the following script to create a test database with some test procedures:[code="sql"]/*-- Use this code to remove the database when finished testinguse mastergoalter database test1654287 set single_user with rollback immediategodrop database test1654287 */-- Change the path names below to put the files in some safe locationuse mastergo-- Use a database name that is unlikely to clash with any existing databasecreate database test1654287 on primary( NAME = N'test1654287', FILENAME = N'D:\DATA\test1654287.mdf' , SIZE = 200MB , FILEGROWTH = 100MB) LOG ON ( NAME = N'test1654287_log', FILENAME = N'D:\DATA\test1654287_log.ldf' , SIZE = 200MB , FILEGROWTH = 100MB)COLLATE Finnish_Swedish_100_BIN2;ALTER DATABASE test1654287 ADD FILEGROUP [test1654287_inmem_data] CONTAINS MEMORY_OPTIMIZED_DATA ;ALTER DATABASE test1654287 ADD FILE (name='test1654287_inmem_data', filename='D:\DATA\test1654287_inmem') TO FILEGROUP test1654287_inmem_dataALTER DATABASE [test1654287] SET DELAYED_DURABILITY = ALLOWED WITH NO_WAITALTER DATABASE [test1654287] SET RECOVERY SIMPLE WITH NO_WAITgouse test1654287go-- create a disk-based table with testdataif object_id('dbo.table1') is not null drop table dbo.table1goselect top 1000000 row_number() over (order by (select null)) as rn, column_id = c1.column_idinto dbo.table1from sys.all_columns c1cross join sys.all_columns c2-- Create a table valued memory typeif type_id('dbo.InputTypeMem') is not null drop type dbo.InputTypeMemgoCREATE TYPE dbo.InputTypeMem AS TABLE ( rn int not null, column_id int not null, INDEX IX_rn HASH (rn) WITH ( BUCKET_COUNT = 10000000)) WITH (MEMORY_OPTIMIZED = ON)go-- Create a table valued diskbased typeif type_id('dbo.InputTypeDisk') is not null drop type dbo.InputTypeDiskgoCREATE TYPE dbo.InputTypeDisk AS TABLE ( rn int not null, column_id int not null)go-- Create a procedure that populates the memory based typeif object_id('dbo.TestMem') is not null drop procedure dbo.TestMemgocreate procedure dbo.TestMemasdeclare @input dbo.InputTypeMeminsert into @input (rn, column_id)select rn,column_id from dbo.table1go-- Create a procedure that populates the disk based typeif object_id('dbo.TestDisk') is not null drop procedure dbo.TestDiskgocreate procedure dbo.TestDiskasdeclare @input dbo.InputTypeDiskinsert into @input (rn, column_id)select rn,column_id from dbo.table1go[/code]Run Profiler and catch BatchCompleted eventsOn my machine running TestDisk gives a duration of 380ms and TestMem gives 1200msThis is bad enough, but it is even worse when you try to run several procedures like this at the same time.Create two cmd files like this:testdisk.cmd:[code="plain"]start /b sqlcmd -S . -E -d test1654287 -Q "exec dbo.TestDisk"start /b sqlcmd -S . -E -d test1654287 -Q "exec dbo.TestDisk"[/code]testmem.cmd:[code="plain"]start /b sqlcmd -S . -E -d test1654287 -Q "exec dbo.TestMem"start /b sqlcmd -S . -E -d test1654287 -Q "exec dbo.TestMem"[/code]When running TestDisk.cmd, two sessions run dbo.TestDisk at the same time in parallel - total duration 460msWhen running TestMem.cmd, two sessions run dbo.TestMem at the same time in parallel - total duration 2100msI have attached a screen dump from running profiler on my system.[img]http://ibin.co/2Mf3YUUAPNTS[/img]So, it seems that there is a serious concurrency problem when two sessions are writing to memory optimized tables at the same time.I have tested this with both memory optimized table variables, and memory optimized tables - the results are similar.This is very disappointing to me. I thought the point of memory optimized tables was to increase performance...Any comments?

Viewing all articles
Browse latest Browse all 3145

Trending Articles