<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[php生成UUID唯一序列的代码示例]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 30 Mar 2016 06:50:26 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：生成一个唯一性的uuid有一个好处，那就是可以用它标识一个唯一的事物，或一次接口请求只能一次，统一了格式。<br/>用redis队列吧<br/>每次要唯一值都从redis里面拿<br/>uniqid（） 性能狠low<br/>你这么做可以满足自己的需求<br/>当前 机器IP+ pid+microtime+rand<br/>pid是当前进程号<br/><br/><br/><textarea name="code" class="php" rows="15" cols="100">
/** 
&nbsp;&nbsp;* Generates an UUID 
&nbsp;&nbsp;* 
&nbsp;&nbsp;* @author&nbsp;&nbsp;&nbsp;&nbsp; Anis uddin Ahmad&nbsp;&nbsp;
&nbsp;&nbsp;* @param&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;&nbsp;an optional prefix 
&nbsp;&nbsp;* @return&nbsp;&nbsp;&nbsp;&nbsp; string&nbsp;&nbsp;the formatted uuid 
&nbsp;&nbsp;*/&nbsp;&nbsp;
&nbsp;&nbsp;function uuid($prefix = &#039;&#039;)&nbsp;&nbsp;
&nbsp;&nbsp;&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$chars = md5(uniqid(mt_rand(), true));&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$uuid&nbsp;&nbsp;= substr($chars,0,8) . &#039;-&#039;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$uuid .= substr($chars,8,4) . &#039;-&#039;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$uuid .= substr($chars,12,4) . &#039;-&#039;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$uuid .= substr($chars,16,4) . &#039;-&#039;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$uuid .= substr($chars,20,12);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;return $prefix . $uuid;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; 
//Example of using the function -&nbsp;&nbsp;
//Using without prefix.&nbsp;&nbsp;
echo uuid(); //Returns like ‘1225c695-cfb8-4ebb-aaaa-80da344e8352′&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp; 
//Using with prefix&nbsp;&nbsp;
echo uuid(‘urn:uuid:’);//Returns like ‘urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344e8352′
</textarea><br/><br/><br/>*************************************************************************************<br/><br/>也可以直接用sql语句生成如：<br/>直接在insert语句中插入UUID作主键的用法(简便)：<br/>insert into Price( Name, UUID, Price, BID) values(&#039;FEIFEI_TEST&#039;, uuid(), 32, 3);<br/><br/>************************************************************************************<br/><br/>php生成uuid 来表示唯一码<br/>uuid是什么？guid是什么？php如何生成uuid?(google 搜索相关内容时使用uuid + mysql得到的结果较uuid+php要多些)<br/><br/>这里有一篇关于UUID的说明 蛮详细<br/><br/>http://mlxia.javaeye.com/blog/279059<br/><br/>以下部分内容为转载：<br/><br/>我唯一还算熟悉的数据库就 算是MySQL了，大概使用MySQL的人，百分之九九以上的人会使用Autoincrement ID做主键，这是可以理解的，因为MySQL的自增ID效率很高，使用也很方便。那么剩下的百分之一的人使用什么做主键呢？可能是自己做的 KeyGenerator，也可能是我们下面要说的UUID。<br/><br/>据说在Oracle的圈子里，如果谁用自增ID做主键是要被鄙视的，主键最自然的选择就是UUID。我不了解Oracle，这些道听途说的结论是否正确不做承诺。<br/><br/>那么我们先看看什么是UUID？简单的说，UUID是指在一台机器上生成的数字，它保证对在同一时空中的所有机器都是唯一的。在UUID的算法中，可能会用到诸如网卡MAC地址，IP，主机名，进程ID等信息以保证其独立性。<br/><br/>如果你的MySQL版本不太老的话，键入 SELECT UUID(); 输出的就是UUID，如下：<br/><br/>mysql&gt; select uuid();<br/>+--------------------------------------+<br/>&#124; uuid()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+--------------------------------------+<br/>&#124; 54b4c01f-dce0-102a-a4e0-462c07a00c5e &#124;<br/>+--------------------------------------+<br/><br/><br/>现在大家应该对UUID有一个比较直观的认识了，我们来看看UUID的优缺点分别是什么。<br/><br/>优点：<br/><br/>能够保证独立性，程序可以在不同的数据库间迁移，效果不受影响。<br/>保证生成的ID不仅是表独立的，而且是库独立的，这点在你想切分数据库的时候尤为重要。<br/><br/>缺点：<br/><br/>比较占地方，和INT类型相比，存储一个UUID要花费更多的空间。<br/>使用UUID后，URL显得冗长，不够友好。<br/><br/>下面针对上述UUID的缺点说说我的看法，比较占地方这个缺点我不是很在乎，现在最不值钱的就是硬盘了，略过此条缺点无妨。至于说使用UUID后，URL 显得不友好，我觉得这多少是你的INT情结造成的惯性思维，其实，和INT类型相比，UUID才是最自然的主键选择，注意，我这里用的是自然这个形容词， 仔细体会一下你能理解我的意思。另外，很多时候，URL本身就不需要友好，比如，一个电子商务网站，按照INT友好的URL说法，她的订单URL大概是下 面这个形式的：/order.php/id/123，我要说明的是，这样是很友好，但是有些太友好了，友好的甚至不安全，比如说，我早晨下一个订单，发现 URL是/order.php/id/1000，晚上再下一个订单发现URL是/order.php/id/2000，那么我就可以估计出此网站一天的订 单数大致是1000左右，甚至能大体估计出它的销售额，而这些数据往往都是重要的商业秘密。使用UUID就没有这个顾虑。<br/><br/>效率？<br/><br/>如果上面说的UUID的所谓缺点都不成立的话，那么是否使用UUID做主键，唯一的问题就是效率了。据说在PostgreSQL等数据库里，都有专门的 UUID类型，在这样的数据库里，使用UUID做主键，效率没有任何问题，可惜在MySQL里没有这样的字段，如果想在MySQL里保存UUID做主键， 一般是使用CHAR(36)来模拟，因为不是一个原生的UUID类型，所以主键的效率到底如何有待测试，另外，UUID做主键的效率和UUID本身的算法 实现也有很大关系。<br/><br/>我本来想在我自己的电脑上插入1000000条数据测试一下看看来着，可惜一测试，硬盘灯就一直亮，让我很担心它会挂，虽然硬盘不值钱，但是我重要的数据都在上面，一旦坏了，损失就大了，所以，测试只好作罢。<br/><br/>至于在MySQL上使用UUID（用char(36)存储）做主键，效率到底如何，我也不知道，抱歉 -_-!!!<br/><br/>如何生成UUID？下面这种方法生成的貌似不是UUID，因为MD5实际上是可能存在重复值的(参考http://www.phpx.com/happy/archiver/tid-56636.html)，况且使用随机更不能避免存在重复.所以应直接使用mysql中的uuid函数生成<br/><br/>de&gt; function uuid($prefix = &#039;&#039;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; &#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; $chars = md5(uniqid(mt_rand(), true));<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; $uuid&nbsp;&nbsp;&nbsp;&nbsp; = substr($chars,0,8) . &#039;-&#039;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; $uuid .= substr($chars,8,4) . &#039;-&#039;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; $uuid .= substr($chars,12,4) . &#039;-&#039;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; $uuid .= substr($chars,16,4) . &#039;-&#039;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; $uuid .= substr($chars,20,12);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; return $prefix . $uuid;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125; de&gt;<br/><br/>在mysql中插入uuid使用mysql的uuid()函数<br/><br/>INSERT INTO Table(id,..) VALUES( UUID(), ...)<br/><br/>当然也可以用 SELECT UUID() 先得到一个uuid值再插入进去<br/><br/>题外：<br/><br/>可能相比较使用整型做主键，效率稍差，<br/>另外一个问题是可能导致URL太长,比如显示某个id下的分类时<br/>通常这样category.php?cid=2 但是现在可能是category.php?uuid=a93f16c5-9634-102c-824f-3ea0651c5b77<br/>是否能更改为整型做主键<br/><br/>摘自 ：http://blog.csdn.net/china_skag/article/details/7297957
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] php生成UUID唯一序列的代码示例]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>