I am facing here a problem with a SQL statement, which fetches the data开发者_运维技巧 from table called News according to category : News : Id(PK) int, Title string, Subject string, Uid int, Cid int.
SELECT Id, Subject, Uid, Title FROM News WHERE Uid = @Uid
This statement operates slowly comparing to statement without WHERE since it should go and check every single row to ensure if it is accomplish the condition.
So imagine with me the table News with 10000000 article. What should I do about such a thing?
If the Id column is a primary key that might already be clustered (they are by default), you just need to create a non-clustered index on the Uid column - especially of the Uid column is a uniqueidentifier (GUID) data type.
This can be created by running the following SQL:
CREATE NONCLUSTERED INDEX IX_News_Uid ON News ( Uid )
You should create an index for the column Uid
, with Id
, Subject
and Title
as included columns.
That way the database can run the query using only the index, and doesn't have to touch the table at all.
I would create an unique index on Uid.
You should could create a clustered index on the Uid column. This should improve performance. Note: You can only have one clustered index column per table.
精彩评论