用内存优化表替换了一个大表
对于某些东西,我对一些它死掉的东西有很好的响应时间
这是一个复合主键
我可以让它使用主键的唯一方法是搜索特定的行(整个 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 秒内运行
我并不是说每个
都在 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 )
最后注意我回到旧的基于磁盘的表
在应用程序使用的实际查询内存优化较慢优化
的内存加载速度更快,但该表是一次写入和多次读取