我目前在这个设计中有一个表
CREATE TABLE [dbo].[test_all_DIVISION](
[all_id] [int] NOT NULL,
[division_id] [int] NOT NULL,
[some_value] [tinyint] NULL,
CONSTRAINT [PK_test_all_DIVISION] PRIMARY KEY CLUSTERED
(
[all_id] ASC,
[division_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY]
其中all_id和division_id是引用其他表的外键 因此,按照
的顺序插入数据时all_id division_id some_value
94619 1153 24
94619 1133 25
94619 1002 22
94619 1156 23
根据division_id对其进行排序,并按以下方式插入
all_id division_id some_value
94619 1002 22
94619 1133 25
94619 1153 24
94619 1156 23
现在我已将该约束更改为非群集约束。 像这样
ALTER TABLE [dbo].[test_all_DIVISION]
DROP CONSTRAINT [PK_test_all_DIVISION]
GO
ALTER TABLE [dbo].[test_all_DIVISION]
ADD CONSTRAINT [PK_test_all_DIVISION] PRIMARY KEY NONCLUSTERED
(
[all_id] ASC,
[division_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]
GO
仍然行为没有改变,并且表在进行表插入时仍然在排序,并且表现得像聚簇索引。 所以我做错了什么。请帮忙。
答案 0 :(得分:2)
插入堆时数据未排序。您能否澄清为什么需要按特定顺序插入数据?您可以使用订单来选择数据时控制订单。
如果必须控制插入顺序,则可以添加IDENTITY列和群集。
- 编辑 -
保证结果顺序的唯一方法是在select语句中。聚集索引只会影响数据的存储顺序。
CREATE TABLE [dbo].[test_all_DIVISION](
[id] [int] IDENTITY(1, 1) NOT NULL,
[all_id] [int] NOT NULL,
[division_id] [int] NOT NULL,
[some_value] [tinyint] NULL) ON [PRIMARY];
然后在select语句中控制顺序。
SELECT
[all_id],
[division_id],
[some_value]
FROM
[dbo].[test_all_DIVISION]
ORDER BY
[id];