which is better to be used Guid Or Int Primary Key? some search result favors Guid and other favor Int. so what are really the pros and cons of each.
SQL tables primary key
180 Views Asked by SShebly At
2
There are 2 best solutions below
1
spender
On
Because integer primary keys are sequential (assuming auto-increment), it's going to be easier to write rows to the DB. GUIDs are essentially random, so inserting a row can cause page splits and will lower insertion speed. If you really want to use GUIDs as PK, consider the newsequentialid function which ensures that GUIDs are produced in order.
Related Questions in SQL
- Can MVC.NET prevent SQL-injection at razor or controller level?
- SQL server not returning all rows
- When dealing with databases, does adding a different table when we can use a simple hash a good thing?
- Creating a parametrized field name for a SELECT clause
- Combine two rows based on common ID
- Column displays each count
- Slick query for one to optional one (zero or one) relationship
- Aggregate and count in PostgreSQL
- MAX and GROUP BY - SQL
- SQL statement for a tricky 2 table query
- How to create nested selects with sql?
- Pull and push data from and into sql databases using Excel VBA without pasting the data in Excel sheets
- Best Practice for adding columns to a Table in Oracle database
- SQL FIFO STACK using two tables
- SQL Query - Order by String (which contains number and chars)
Related Questions in PRIMARY-KEY
- How to get primary key value with Entity Framework Core
- Showing MYSQL table columns with key types and reference
- MySQL: Using natural primary index or adding surrogate when tables are given
- Is it necessary to use an integer ID for the Country table when the name is already univocal?
- Entity Framework Code First from database not adding Key attribute
- Adding primary key to some exising tables in a database
- Finding primary key given relation and functional dependencies
- How to set "auto insert" foreign key value by using SQL Server?
- Python Pandas to_sql, how to create a table with a primary key?
- How to add a (primary) key to an existing table using SAP HANA
- creating a unique column in Linux (more like an sql primary key?)
- MySQL: is primary key unique by default?
- Is it possible to set an EF6 model's primary key value?
- php to return id using Scope_identity
- Include foreign key in composite primary key in Code-First Entity Framework
Related Questions in GUID
- Is there a way to generate a Guid from a list of Guids?
- Guid property regenerating in WebApi POST call
- Call a msbuild task that receives a required Guid argument
- Unique Identifier for Android Device
- deserialize a list of Guid c#
- Will new Guid(byte[]) always produce a valid guid if the array is exactly 16 bytes and taken from a successfully received UDP socket?
- Can a case insensitive comparison be made on html side in AngularJS
- Can't output Guid Hashcode
- What is the default value for Guid?
- Java GUI Swing programming
- Is this a reliable way of generating a GUID?
- Cannot implicitly convert to BaseEntity<System.Guid>
- Encode and Decode Mac address from UUID using C#
- Can I use messaging system's CorrelationId as a primary key and can it be used for further communcation
- Ue4 and visual studio unrecognized guid format
Related Questions in PRIMARY-KEY-DESIGN
- Allocate ranges of primary key integers
- Primary Key as domain username AND userID?
- MySQL - Using foreign key as primary key too
- Primary Key and Composite Key
- Are Primary Keys Recommended on a Timescale Hypertable?
- Why does an SQL table have multiple primary keys?
- SQL tables primary key
- Database design: Primary key from multiple data sources
- JSON object with id as primary key schema design
- ASP MVC code first create computed primary key
- In SQL is it possible to create a serial primary key that begins with a specific letter and is followed by a specific amount of numbers?
- Output Inserted or deleted in SQL Server
- Use of natural key in date dimension
- MySQL moving primary key from varchar to int
- REST API Primary Key Design MySQL
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
For SQL Server, I personally use
INT IDENTITYfor most of my primary and clustering keys.You need to keep apart the primary key which is a logical construct - it uniquely identifies your rows, it has to be unique and stable and
NOT NULL. AGUIDworks well for a primary key, too - since it's guaranteed to be unique. AGUIDas your primary key is a good choice if you use SQL Server replication, since in that case, you need an uniquely identifying GUID column anyway.The clustering key in SQL Server is a physical construct is used for the physical ordering of the data, and is a lot more difficult to get right. Typically, the Queen of Indexing on SQL Server, Kimberly Tripp, also requires a good clustering key to be unique, stable, as narrow as possible, and ideally ever-increasing (which a
INT IDENTITYis).See her articles on indexing here:
and also see Jimmy Nilsson's The Cost of GUIDs as Primary Key
A GUID is a really bad choice for a clustering key, since it's wide, totally random, and thus leads to bad index fragmentation and poor performance. Also, the clustering key row(s) is also stored in each and every entry of each and every non-clustered (additional) index, so you really want to keep it small -
GUIDis 16 byte vs.INTis 4 byte, and with several non-clustered indices and several million rows, this makes a HUGE difference.In SQL Server, your primary key is by default your clustering key - but it doesn't have to be. You can easily use a GUID as your NON-Clustered primary key, and an
INT IDENTITYas your clustering key - it just takes a bit of being aware of it.