内存优化表有一些可怕的响应时间

时间:2014-05-13 20:51:37

标签: tsql query-optimization sql-server-2014 memory-optimized-tables

用内存优化表替换了一个大表 对于某些东西,我对某些东西的反应时间很长 它是复合主键
我可以使用主键的唯一方法是搜索特定行(整个PK)
不会将PK用于排序或仅使用复合键的一个组件
从现有数据中调整哈希桶的大小

使用此链接中的语法作为复合主键
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 

注意原始(非内存优化表)
所有这些查询都在0秒内运行 我不是指每个人 ALL在0秒内运行

我认为这总结了我的问题 Troubleshooting Common Performance Problems with Memory-Optimized Hash Indexes

主键上没有使用HASH似乎修复了它

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 )

注意到最后我会回到旧的基于磁盘的表格 在应用程序使用的实际查询中,优化的内存较慢 优化的内存确实加载得更快但是这个表写了一次并且读了很多

1 个答案:

答案 0 :(得分:2)

散列索引不适用于范围扫描。范围索引是。