Guid as PK of Sql Server Table - sql-server

By default, a SQLServer table clustered index is the PK. If I define that:
such key is a GUID and
I will generate at random a.k.a Guid.NewGuid() and
forget to set the clustered index to a more meaningful column
will SQLServer reorder the pages as a new record enters the table or will it just "ignore" the clustered part of the index?

I think you got it reversed: by default, the PK is also the clustered index. You must choose the data type and what columns to include in the PK. SQL Server won't set a default PK for you. Without a PK, a table is a heap.
Using GUID as a PK is bad practice. You will cause unnecessary page splits upon INSERT. If the data does not have a natural key, use an IDENTITY column instead.

So you create PK and it defaults to a clustered. You change it to a non clustered index. What makes you think there is a clustered index hanging around? There is no clustered part to the index if you changed it to a non clustered.

Related

Is there a difference if I don't define a Primary Key in a table

I've been using SQL Server for quite a while, I always create database with design view.
The steps I took to create the table is:
Right Click Table -> New Table
I always have the first column as SOMETHING_ID (int) -> I make SOMETHING_ID as Identity with auto increment of 1
-> Then I add other columns
-> Save and use
As you can see, I didn't define SOMETHING_ID by right clicking it and SET AS PRIMARY.
Will there be any performance impact in this case?
Yes, it can impact performance because creating the primary key essentially makes an index for it. So when you join tables on that key it will improve performance greatly if there are indexes.... particularly if you have lots of data.
What you really need to do is to create a clustered index. A primary index, by default is a clustered index (but you can create a primary index that is not a clustered index). A table without a clustered index is called a heap and except for very special occasions you should have a clustered index on every table. A primary index is a index that has only unique values and does not have any (not even one) null index value.
A query that uses a clustered index is usually a very effective one but if there is not clustered index (even if the table has indexes) it can end up with forwarding pointers all over the place and searching for all the rows for a given customer can require SQL Server to read many, many pages.
To create a clustered index on a table you can use syntax such as
create clustered index ix1_table1 on table1(id)
The column(s) used in a index of any kind can occur anywhere in a table and does not necessarily have to be identity columns.
By not creating Primary key you're breaking the rule of First Normal Form in Normalization.
Disadvantages of not having Primary Key
Chances of Duplicates
Your table won't be clustered with clustered index
You won't be able to do Primary Key-Foreign Key relationship with other table.

SQL Server : Identity column indexing [duplicate]

I believe when I create an identity column it gets indexed automatically, but I'm not 100% sure.
Should I create an index for an identity column, or is it created automatically?
create table test (Id int identity)
go
sp_help test
The object 'test' does not have any indexes, or you do not have permissions.
No constraints are defined on object 'test', or you do not have permissions.
As a general practice you would create a unique index on your identity column, this speeds up lookups.
Usually you would like your identity columns to be 'clustered indexes' as well (Id int identity primary key is the shortcut notation), meaning table is layed out on disk in the same order your identity column is. This optimizes for inserts, as the page being inserted into tends to be in memory. In some cases, when you are doing ranged lookups very frequently on other data in the table, you may consider clustering other columns instead, as SQL Server only allows you one clustered index per table.
If you create a column with a PRIMARY KEY constraint, a clustered index will be created by default (assuming this is a new table and no such index is already defined).
Being an IDENTITY field has nothing to do with it.
So, to answer you question - if you are going to query/sort with this field and it is not a primary key, you should define an index on it (rule of thumb, always test for your scenario).
By default, your primary key column(s) will be made into a clustered index; this is a special index that is not separate from the data - but instead, the data is ordered in the table by that indexes values.
I say 'by default', because you can change the clustered index to another field if you wanted. If you've chosen a good identity value for your primary key, you will almost never want to change this, though.

Primary Key without any index in a table?

I am just wondering can we create a Primary key on a table in sql server without any type of index on it?
No. As an implementation detail, SQL Server maintains the primary key using an index. You cannot prevent it from doing so. The primary key:
Ensures that no duplicate key values exist
Allows individual rows to be identified/accessed
SQL Server already has mechanisms that offer these features - unique indexes - so it uses those to enforce the constraint.
You can create a table with a primary key that is not a clustered index by adding the keyword NONCLUSTERED after the primary key word.
Actually indexing functions in the same way a book is traversed. One cannot go to a particular page or topic unless there is page number and their relation to the topics. This "Paging" (ordering) of rows is done physically by Clustered Index in SQL Server. If there is no PK in a table one can add clustered index on any unique-key qualifying column(s). As there cannot be more than one clustered index on a table, and all other non-clustered indices depend on clustered index for search/traversal, you cannot make a column PK without clustered index.

Perfomance Issuses on Tables With NonClustered Index

We have a table with about 100,000 record which is used frequently in our applications. We had an identity (ID) columns and had a clustered index on it and everything worked good. But for some reasons we had to use a Uniqueidentifier column as Primary key. So we add a non clustered index on it and removed the clustered index on ID column. But now, we have lots of performance degradation issuses from our customer in peak times. Is it because the table has no clustered index now?
The fact that you added a primary key by no means implies you had to drop the clustered index. The two concepts are distinct. You can have an uniqueidentifier PK implemented by a non clustered index and a separate clustered index of choice (eg. the old ID column).
But the real question is How did you change your application when you added the uniqueidentifier PK? Did you also modified the application code to retrieve the records by this new PK (by the uniqueidentifier)? Did you update all joins to reference the new PK? Did you modified all foreign key cosntraints that referenced the old ID column? Or does the application continue to retrieve the data using the old identity ID column? My expectation is that you changed both the application and the table, and the access is now prevalent on the form of SELECT ... FROM table WHERE pk=#uniqueidentifier. If only such access occurs, then the table should perform OK even with a non-clustered uniqueidentifier primary key and no clustered index. So there must be something else at play:
your application continues to access the table based on the old identity ID column
there are joins in your query based on the old identity ID column
there are foreign key constraints referencing the table on the old ID column
Ultimately you have a performance troubleshooting issue at hand and approach it as a performance troubleshooting problem. I have two great resources for you:the Waits and Queue methodology and the Performance Troubleshooting Flowchart
Hi I think you can make uniqueidentifier column as clustered index with NEWSEQUENTIALID() instead of NEWID(). As newsequentialid generates the sequential ids and for clustered index its the best.

In SQL Server, should I create an index for an identity column, or is it created automatically?

I believe when I create an identity column it gets indexed automatically, but I'm not 100% sure.
Should I create an index for an identity column, or is it created automatically?
create table test (Id int identity)
go
sp_help test
The object 'test' does not have any indexes, or you do not have permissions.
No constraints are defined on object 'test', or you do not have permissions.
As a general practice you would create a unique index on your identity column, this speeds up lookups.
Usually you would like your identity columns to be 'clustered indexes' as well (Id int identity primary key is the shortcut notation), meaning table is layed out on disk in the same order your identity column is. This optimizes for inserts, as the page being inserted into tends to be in memory. In some cases, when you are doing ranged lookups very frequently on other data in the table, you may consider clustering other columns instead, as SQL Server only allows you one clustered index per table.
If you create a column with a PRIMARY KEY constraint, a clustered index will be created by default (assuming this is a new table and no such index is already defined).
Being an IDENTITY field has nothing to do with it.
So, to answer you question - if you are going to query/sort with this field and it is not a primary key, you should define an index on it (rule of thumb, always test for your scenario).
By default, your primary key column(s) will be made into a clustered index; this is a special index that is not separate from the data - but instead, the data is ordered in the table by that indexes values.
I say 'by default', because you can change the clustered index to another field if you wanted. If you've chosen a good identity value for your primary key, you will almost never want to change this, though.

Resources