使用文本作为主键在SQLite表坏? [英] Using text as a primary key in SQLite table bad?

查看:893
本文介绍了使用文本作为主键在SQLite表坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQLite数据库中将文本作为主键是不好的吗?我听说这是坏的性能原因,这是真的吗?

解决方案


它是否会被用作实际的主键?不好在SQLite数据库中有文本作为主键?我听说
,这是坏的性能原因,这是真的吗?


我从来没有听说有人使用 string作为主键。对我来说(和我真诚地希望也为其他人)非常丑陋的练习性能非常低。



如果你将使用字符串主键你需要考虑一些少数的事情:




  • 3个符号的组合是否足够?
  • $ b $
  • 可读性问题)以及是独一无二的。哦!这是下一步 - > 您需要创建一些唯一字符串生成器将产生唯一 1 字符串标识符 2



    p>


    • 更长的字符串 = 自动更难,难以比较

    • 表的大小从根本上提高,因为很清楚
      字符串的大小与数字相差很多。

    • 行数 - 如果你使用字符串作为主键, b $ b表可以有1000行以上



    这是更复杂的主题,但我想说的是,对于非常小的表可以使用字符串作为主键(如果它使一个sence),但如果你看看缺点,更好的技术使用数字作为主键肯定!



    结果是什么?



    我不建议您使用字符串作为主键。它有更多的缺点作为优点(它有真正的一些优势吗?)。



    使用数字作为主键是更好的(我害怕说最好的)练习。



    < blockquote>

    在这种情况下,rowid会被用作实际的主键吗?


    将使用字符串作为主键。



    1 在实际字符串中很少是唯一的
    $ b

    2 当然,你可以说,你可以从行中的项目名称创建标识符,但它又是一个意大利面条代码)。


    Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true? And will the rowid be used as the actual primary key in such a case?

    解决方案

    Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true?

    I never heard that somebody used string as primary key in table. For me (and I honestly hope also for others) very "ugly" practise with very low performance.

    If you'll use string as primary key you needs to think about a "few" things:

    • Will be combination of 3 symbols enough?
    • Or should I use 5 symbols?

    Here, each row must have same format (readability issue of course) and also be unique. Oh! Here is next "piggy work" -> you'll need to create some "unique string generator" that will generate unique1 string identificator2.

    And also there are next issues is good to consider:

    • Longer strings = automatically harder and harder to compare
    • Size of table radically raises because it's pretty clear that string has much more size as number
    • Number of rows - it's madness to use string as primary key if you table can have 1000+ rows

    It's more complex theme but i would like to say that OK, for very small tables would be possible to use strings as primary key (if it makes a sence) but if you'll look at disadvantages it's much more better technique to use number as primary key for sure!

    And what is conclusion?

    I don't recommend you to use string as primary key. It has more disadvantages as advantages (it has really some advantage?).

    Usage of number as primary key is much more better (I'm scared to say the best) practise.

    And will the rowid be used as the actual primary key in such a case?

    If you will use string as primary not.

    1In real strings are rarely unique.

    2Of course, you could say that you can create identificator from name of item in row, but it's once again spaghetti code (items can have same name).

    这篇关于使用文本作为主键在SQLite表坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆