Question

Replaced a large table with a memory optimized table
For some stuff I am getting good response time for some stuff it dies
It is a composite primary key
The only way I can get it to use the Primary Key is to search on a specific row (the entire PK)
Will NOT use the PK for sort or only one component of composite key
Sized the hash buckets from existing data

used the syntax in this link for the composite primary key
Hekaton: Composite Primary Key in create table statement
CREATE TABLE (SQL Server)

CREATE TABLE [dbo].[FTSindex]
(
    [sID] [int] NOT NULL,
    [wordPos] [int] NOT NULL,
    [wordID] [int] NOT NULL,
    [charPos] [int] NOT NULL,

INDEX [ix_wordID_MO_2] NONCLUSTERED HASH 
(
    [wordID]
)WITH ( BUCKET_COUNT = 524288),
CONSTRAINT [pk_FTSindexMO_2] PRIMARY KEY NONCLUSTERED HASH 
(
    [sID],
    [wordPos]
)WITH ( BUCKET_COUNT = 268435456)
)WITH ( MEMORY_OPTIMIZED = ON , DURABILITY = SCHEMA_ONLY )

select top 10 * from [FTSindex] where [sID] = 100
-- runs in 0 seconds 
-- Index Seek on ix_wordID_MO_2 
-- it is NOT using the PRIMARY KEY pk_FTSindexMO_2

select top 10 * from [FTSindex] where [wordPos] = 100
-- never finishes (I only waited 10 minutes) 
-- will not even display an execution plan

select top 10 * from [FTSindex] where [sID] = 100 and [wordPos] < 1000
-- never finishes (I only waited 10 minutes) 
-- will not even display an execution plan


select top 10 * from [FTSindex] order by [sID]
-- never finishes (I only waited 10 minutes) 
-- query plan is Table Scan 

select top 10 * from [FTSindex] order by [sID], [wordPos]
-- never finishes (I only waited 10 minutes) 
-- will not even display an execution plan

select top 10 * from [FTSindex] where [wordID] = 100 and [sID] = 856515 
-- runs in 0 seconds
-- Index Seek on ix_wordID_MO_2

select top 10 * from [FTSindex] where [wordID] = 100 and [sID] = 856515  and [wordPos] < 1000
-- never finishes (I only waited 10 minutes) 
-- will not even display an execution plan

select * from [FTSindex] where [sID] = 100  
-- 45 seconds to return 1500 rows 
-- table scan 

select * from [FTSindex] where [sID] = 100  and [wordPos] = 1133 
-- runs in 0 seconds
-- this uses the pk_FTSindexMO_2 
-- this is the only way I could get it to use the primary key 

Note in the original (non memory optimized table)
All of these queries run in 0 seconds
I don't mean each
ALL run in 0 seconds

I think this summarizes my problem
Troubleshooting Common Performance Problems with Memory-Optimized Hash Indexes

Not using HASH for Primary Key seems to have fixed it

CREATE TABLE [dbo].[FTSindex]
(
    [sID] [int] NOT NULL,
    [wordPos] [int] NOT NULL,
    [wordID] [int] NOT NULL,
    [charPos] [int] NOT NULL,

INDEX [ix_wordID_MO_2] NONCLUSTERED HASH 
(
    [wordID]
)WITH ( BUCKET_COUNT = 524288),
CONSTRAINT [pk_FTSindexMO_2] PRIMARY KEY NONCLUSTERED 
(
    [sID] ASC,
    [wordPos] ASC
)
)WITH ( MEMORY_OPTIMIZED = ON , DURABILITY = SCHEMA_ONLY )

Note in the end I whet back to the old disk based tables
In the actual queries used by the application memory optimized was slower
The memory optimized did load faster but this table is write once and read many

Was it helpful?

Solution

Hash indexes are not usable for range scans. Range indexes are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top