English 中文(简体)
PK for web, autoincrement vs UUID作风更好,原因何在?
原标题:PK for web, autoincrement vs UUID style, which is better and why?

自动加固与统一私法之间的真正区别是什么?

我认为,汽车机车很容易降落。

在许多记录中,设计比自动加固要慢,但是在这项工作中却存在巨大差异?

问题回答

从关系模式的角度来看,“主要关键

  • uniqueness must be checked fast
  • used in other table as foreign key
  • used to join

PK越小越好。 因此,<>数字 PK是最好的。

如果你担心“hack”很容易,你可以补充UIDD,作为天然钥匙

  • used only for "direct access" to the row

这就是我在几个项目中所看到的,它像魔法一样起作用。

一个递增的ID本身并不“容易被黑客攻击”,它只是提供了入口点,当您使用一个大的随机ID时,这些入口点被隐藏起来(但并非完全隐藏)。仍然需要有糟糕的实现和可利用的软件才会有真正的危险。正如您在地址栏中看到的URL,这个网站使用递增的ID没有任何问题。

除了安全方面的考虑之外,当您不希望用户轻易猜测其他(虽然是公开的)内容的URL时,随机的唯一ID有时会很有帮助。例如,在房地产网站上,即使他们可以通过搜索找到所有竞争对手的条目,您也可能不希望提供“向上和向下”查看ID的可能性。有点阻碍可能是一件好事。

为什么不两个都用呢?一个数字自增键用于索引和关系的速度;一个随机UID用于外部访问。

一些想法:

  • auto-inc: DB assures uniq ID, but you ll have to retrieve it or lose contact to the data-record you just inserted.
  • UUID: must be created outside (not in db server, could be in app-server). ID is known and link to inserted record exists, but (very small, depends on uuid-ness of the uuid) collision risk on insert.

Take care of the LENGTH of your PK column... UUIDs and GUIDs are extremely long... strings. An INT or even BIGINT autoincement column can ensure uniqueness in a much smaller space.

BEWARE that autoincement columns have a few issues of their own around table management. If you truncate/drop-create tables then an auto-increment will be hard to maintain. ALSO, only 1 auto-increment column in MySQL is allowed for a single table.

如果您的数据允许,使用从数据派生的某种哈希来进行索引和性能优化。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签