I have a many to many link table CategoryProduct with 2 columns, which will have multi-million records:
CREATE TABLE [dbo].[CategoryProduct](
[Category_ID] [int] NOT NULL,
[Product_ID] [int] NOT NULL,
CONSTRAINT [PK_dbo.CategoryProduct] PRIMARY KEY CLUSTERED
(
[Category_ID] ASC,
[Product_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
Based on the clustered index, I expected to see the physical records to be stored in the following structure:
CategoryID ProductID
1 2
1 3
2 1
2 3
However, the result with Select is
CategoryID ProductID
2 1
1 2
1 3
2 3
Why is data stored in group of ProductID? Does this reflect the actual order of data? How can I save data in group of CategoryID so that a query like below can be optimised with a consecutive read when a matched CategoryID is hit.
select ProductID from CategoryProduct where CategoryID = value
CREATE TABLEincluding clustered index definition and add it to your question. Also show the execution plan you get forselect ProductID from CategoryProduct where CategoryID = value