What is the best practice to insert data into table with the clustered index ?? - sql azure required it. I mean programically by scharp
?? I´m friend with GUID but they aren´t Int :-(. I Usually create some GUID, check that exist in table - if no: insert - if yes: create new and check again...
EDIT: Concerning an INSERT: i don't think it's always the same whether you have an (clustered) index or not. A Clustered index creates an index in which the logical order of the key values determines the physical order of the corresponding rows in a table.
Depending on your choice of clustered index this may affect insert performance.
Like Olaf said, it doesn´t really matter if a clustered index exists or not. If your primary key is a GUID (uniqueidentifier) you can use
CREATE TABLE Customers
CustomerID uniqueidentifier NOT NULL
ALTER TABLE Customers ADD CONSTRAINT Ct_ID PRIMARY KEY (CustomerID)
Therefore, everytime you perform an INSERT in that table it will automatically generate a unique (random) GUID for each record. You no longer need to perform that check you mentioned. And in your csharp code, you no longer need to provide
the ID parameter anymore, as well.
Hope this helps!
Cheers, Carlos Sardo
Proposed As Answer byCarlos SardoMonday, August 06, 2012 11:35 AM
A clustered indexed is actually a constrain imposed on writes and forces all rows (at database page level) to be in physical sorted order.
Where a clustered index really shines, is on reads which retrieve a set of rows. For example, getting all orders in a given time frame. Assume the clustered index is on date
(or on Date + Order Id). Once the query locates the first order via the index, subsequent orders can be cheaply retrieved, because they are physically near on the same database page and on subsequent database pages and can be easily traversed to, without an
That being said, I would create clustered indexes for read efficiency, if that is possible; and worry about write efficiency only when it is more important than read efficiency.