Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite: Should I use a text column as primary key, or convert it by a hash function?

Tags:

sqlite

I need to use a text column as primary key for my sqlite database. I also have the possibility to use a hash function to get an 32bit/64bit int from my text values (I want to use as primary key). So I could use these int hash values instead my text value as primary keys. But then I need to store the raw text value additionally in the table.

But I read that sqlite is using a rowid internally, So I don't know if using a hash function will help anyway.

Will sqlite store the rowid or the text value (when I use it instead of the hash value) in their pages? When it stores the text value then I think it could blow off the pages and using a hash value would be better.

But I have a lack of knowledge here. I hope you can help me.

like image 768
Chris Avatar asked Aug 25 '11 10:08

Chris


People also ask

Should you hash primary key?

Using a Hash or GUID as Primary Key is also bad idea because it causes Index Fragmentation and frequent Page Splits.

Do you need a primary key in SQLite?

SQLite lets you define a table with no primary key therefore there can be no insertion anomaly. Actually, SQLite silently creates a primary key column for your called rowid .

Can a text be a primary key?

You can not set primary key to text and ntext columns. But you can achiveve the same functionality by setting datatype as a varchar(8000). The difference is this column can now contain at most 8000 chars. Ideally you should not use this datatype column as a primary key.

Can you have two primary keys in SQLite?

Primary keys must contain unique values. A primary key column cannot have NULL values. A table can have only one primary key, which may consist of single or multiple fields.


2 Answers

If the values in the text column are supposed to be unique, you're going to have to put a unique index on that column anyway.

The real question doesn't have to do with primary keys, it has to do with foreign key references. If a table has a column of unique integers and a column of unique text, you can use either one as the target for a foreign key reference. All foreign key references to the integer will require a join; some foreign key references to the text will not require a join. (Because the crucial information is carried in the key itself. Only those queries that need more columns besides the text key will need a join.)

like image 197
Mike Sherrill 'Cat Recall' Avatar answered Oct 08 '22 10:10

Mike Sherrill 'Cat Recall'


Unfortunately yes, any indexes on strings (including usage as a primary key) take double space. Once for the value in the row, second time for the value in the index.

So if hashing meets your needs (you don't need sorting, lookup by ranges), using it will be a good idea.

like image 22
hamstergene Avatar answered Oct 08 '22 10:10

hamstergene