码迷,mamicode.com
首页 > 其他好文 > 详细

内存优化表支持的collation

时间:2016-08-14 19:03:19      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

在SQL Server中,Collation 决定varchar 或 char字段的Byte representation和Code Page。在内存优化表中,字符类型(varchar,char,nvarchar,nchar)的column,必须使用Code Page=1252的Collation;如果 memory-optimized index 创建在字符类型的Column上,那么该column只能使用以 BIN2 结尾的collation。

限制1,在字符列(unicode和 non-nicode)上创建Memory-optimized index,那么该字符列必须使用BIN2 collation。

Collation :Bosnian_Latin_100_BIN2 的Code Page 是1520,用于 LastName varchar(20) 时报错:The data types char(n) and varchar(n) using a collation that has a code page other than 1252 are not supported with memory optimized tables.

-- create a table with collation
CREATE TABLE dbo.Employees 
(
  EmployeeID int NOT NULL , 
  LastName varchar(20) COLLATE Bosnian_Latin_100_BIN2 NOT NULL INDEX IX_LastName NONCLUSTERED, 
  FirstName nvarchar(10) NOT NULL ,
  CONSTRAINT PK__Employees PRIMARY KEY NONCLUSTERED HASH(EmployeeID)  WITH (BUCKET_COUNT=1024)
) 
WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_AND_DATA)
GO

在内存优化表中,要在non-unicode字符列上创建index,必须满足两个条件:Collation Name 以 BIN2 结尾,Code Page是1252;要在unicode 字符列上创建Index,只需要满足一个条件:Collation Name 以BIN2 结尾。

Indexes on (n)(var)char columns can only be specified with BIN2 collations

-- all supported collations for indexes on memory-optimized tables and 
-- comparison/sorting in natively compiled stored procedures
select * 
from sys.fn_helpcollations() 
where name like %BIN2 
    and collationproperty(name, codepage) = 1252;

限制2 ,在内存优化表中,non-unicode 字符列的Code Page必须是1252。如果需要存储Code Page不是1252的字符,必须使用 unicode 数据类型。

(var)char columns in memory-optimized tables must use code page 1252 collation. This restriction does not apply to n(var)char columns. If you need to store non-Latin characters, use n(var)char columns.

-- all supported collations for (var)char columns in memory-optimized tables
select * 
from sys.fn_helpcollations() where collationproperty(name, codepage) = 1252;

 

引用《Collations and Code Pages》:

In-Memory OLTP has restrictions on supported code pages for (var)char columns in memory-optimized tables and supported collations used in indexes and natively compiled stored procedures.

The code page for a (var)char value determines the mapping between characters and the byte representation that is stored in the table. For example, with the Windows Latin 1 code page (1252; the SQL Server default), the character ‘a‘ corresponds to the byte 0x61.

The code page of a (var)char value is determined by the collation associated with the value. For example, the collation SQL_Latin1_General_CP1_CI_AS has the associated code page 1252.

The collation of a value is either inherited from the database collation, or can be specified explicitly using the COLLATE keyword. Database collation cannot be changed if the database contains memory-optimized tables or natively compiled stored procedures. The following example sets the database collation and creates a table, which has a column with a different collation. The database uses Latin case-insensitive collation.

Indexes can only be created on string columns if they use a BIN2 collation. The LastName variable uses BIN2 collation. FirstName uses the database default, which is CI_AS (case-insensitive, accent-sensitive).

示例:创建一个内存优化表,使用collate 子句显式指定字符列的Collation,然后在字符列上创建memory-optimized index。

use master 
go 

--create database
create database TestMemoryDB;

--add filegroup
alter database TestMemoryDB
add FileGroup FG_TestMemoryDB
contains MEMORY_OPTIMIZED_DATA;

--add file
alter database TestMemoryDB
add file
(
    name=TestMemoryDBDirectory,
    filename=D:\MSSQLServerData\MSSQL12.MSSQLSERVER\MSSQL\DATA\TestMemoryDBDirectory
)
to FileGroup FG_TestMemoryDB;

--  set the database collations
ALTER DATABASE TestMemoryDB COLLATE Latin1_General_100_CI_AS
GO

USE TestMemoryDB 
GO

-- create a table with collation
CREATE TABLE dbo.Employees 
(
  EmployeeID int NOT NULL , 
  LastName varchar(20) COLLATE Latin1_General_100_BIN2 NOT NULL INDEX IX_LastName NONCLUSTERED, 
  FirstName nvarchar(10) NOT NULL ,
  CONSTRAINT PK__Employees PRIMARY KEY NONCLUSTERED HASH(EmployeeID)  WITH (BUCKET_COUNT=1024)
) 
WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_AND_DATA)
GO

 

参考doc:

Collations and Code Pages

内存优化表支持的collation

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5770575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!