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
181 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
- SQL schema for a fill-in-the-blank exercise
- Hibernate: JOIN inheritance question - why the need for two left joins
- What's supposed to be the problem in this query?
- Compare fields in two tables
- How to change woocomerce or full wordpress currency with value from USD to AUD
- Dynamic query creation with Array like implementation
- SQL query to get student enrolled in this month in a course - Moodle
- SQL LAG() function returning 0 for every row despite available previous rows
- Convert C# DateTime.Ticks to Bigquery DateTime Format
- Use row values from another table to select them as columns and establish relations between them (pivot table)
- SQL: Generate combination table based on source and destination column from same table
- how to use system's environnement variables in sql script
- PHP fetchAll on JOIN
- Multitable joining in Sql
- How to display name starting from 'z' by using BETWEEN cmd only?
Related Questions in PRIMARY-KEY
- Claves combinadas
- Postgresql Primary Key Index Maintenance
- "Number of referencing columns in foreign key differs from number of referenced columns" inline error
- Entity Framework Core 7 - configuring two keys
- Create a flexible key for merge in R
- Record versioning and approving changes TypeOrm postgres
- Primary key issue of a table imported from CSV, using MySQL Workbench (Linux Mint 21 Vanessa)
- Why Hibernate now uses TABLE strategy as default for MySQL?
- how to make a django model with no id column and primary key?
- CockroachDB: The best way to turn a UNIQUE CONSTRAINT into a PRIMARY KEY?
- Alter entries of Primary key in table to match number of rows from COUNT Postgresql
- How to update a part of primary key if it is a combination of three columns
- Is it wise to construct a custom string (combination of entity, year and alphanumeric) to have as a primary key for a postgres database in Django?
- Django filtering with Custom primary key
- TypeORM joins by multiple PrimaryColumns ignoring defined JoinColumn
Related Questions in GUID
- Argument to method seems to lose scope when method is invoked
- Approach to generate Short Unique key like (AboU8N) in distributed environment?
- An Error On the Served GUID shoudl contain 32... Post Request to retrieve Image not showing in PowerApps
- ldap_search() by GUID in AD with Php
- Azure Devops EF Core migration script GUID PK error
- Fail to convert base64 string when using TryFromBase64String
- UUID to GUID in Android
- Is there any way to create a column of GUID type in pyspark/sql databricks?
- Generating private key with guid in Open SSL
- How to make UWP can auto collect UUID or GUID?
- Query guids with different binary subTypes in the same collection
- Guid value doesn't get inserted properly into the cassandra table
- Razor modal component with GUID as ID
- Header preprocessor unique IDs generation C++
- Transparent Browser for our own web-app and PWA [Using static HTML,CSS and JavaScript][like user can see desktop wallpaper through our web-app/PWA]
Related Questions in PRIMARY-KEY-DESIGN
- Is it wise to construct a custom string (combination of entity, year and alphanumeric) to have as a primary key for a postgres database in Django?
- Selecting the right keys for a DDB table
- Are Primary Keys Recommended on a Timescale Hypertable?
- ETL process where Primary Unique key changes after order status change
- How to create a Custom Unique Primary Key using prisma?
- SQL Server use same Guid as primary key in 2 tables
- 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?
- JSON object with id as primary key schema design
- Extension of an ID based off the ID of another table
- SQL Server Removing Duplicate Rows & Managing Keys
- Create a Primary Key on a large table (6Million records) without locking the table in PostgreSQL
- Can a CLR UDT be a PK on a table?
- Why laravel delete() function getting error?
- REST API Primary Key Design MySQL
- Why two primary keys in a table is not allowed?
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 # Hahtags
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.