什么是更快/更好的使用:MySQL或PHP md5函数? [英] What's faster/better to use: the MySQL or PHP md5 function?

查看:127
本文介绍了什么是更快/更好的使用:MySQL或PHP md5函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了用户对数据库的密码。



速度更快, MySQL MD5函数

  ... pwd = MD5('。$ pwd。')

PHP MD5函数

  ... pwd ='.md5($ pwd)。'

这两个选项之间的正确方法是什么?如果您的应用程序仅在您的网站上注册或正在登录时计算md5,则拥有多个对md5的调用将会你每小时做什么?几百人?如果是这样,我不认为PHP和MySQL之间的真的很小差异将是显着的。



这个问题应该更像我在哪里将密码存储为使用md5而不是什么让我几乎赢不了。

并且,作为旁注,另一个问题可能是:你可以在哪里花费资源进行这种计算?如果你有10台PHP服务器和一台数据库服务器已经处于高负载状态,你会得到你的答案; - )

但是,只是为了好玩:

  mysql>选择基准(1000000,md5('test')); 
+ --------------------------------- +
|基准(1000000,md5('test'))|
+ --------------------------------- +
| 0 |
+ --------------------------------- +
1行(2.24秒) )

在PHP中:

  $ before = microtime(true); 
for($ i = 0; $ i <1000000; $ i ++){
$ a = md5('test');
}
$ after = microtime(true);
echo($ after- $ before)。 \\\
;

给出:

  $ php〜/ developpement / tests / temp / temp.php 
3.3341760635376



<但是你可能不会计算一百万这样的md5,你会吗?

$ b $(这与防止SQL注入无关:只是逃避/引用您的数据!永远!或使用预先准备的语句)


I checked the passwords for the users against the DB.

What is faster, the MySQL MD5 function

... pwd = MD5('.$pwd.')

OR the PHP MD5 function

... pwd = '.md5($pwd).'

What is the right way between the two options?

解决方案

If your application is only calcullating md5 when someone registers on your site, or is logging in, own many calls to md5 will you do per hour ? Couple of hundreds ? If so, I don't think the really small difference between PHP and MySQL will be significant at all.

The question should be more like "where do I put the fact that password are stored using md5" than "what makes me win almost nothing".

And, as a sidenote, another question could be : where can you afford to spend resources for that kind of calculations ? If you have 10 PHP servers and one DB server already under heavy load, you get your answer ;-)

But, just for fun :

mysql> select benchmark(1000000, md5('test'));
+---------------------------------+
| benchmark(1000000, md5('test')) |
+---------------------------------+
|                               0 |
+---------------------------------+
1 row in set (2.24 sec)

And in PHP :

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    $a = md5('test');
}
$after = microtime(true);
echo ($after-$before) . "\n";

gives :

$ php ~/developpement/tests/temp/temp.php
3.3341760635376

But you probably won't be calculating a million md5 like this, will you ?

(And this has nothing to do with preventing SQL injections : just escape/quote your data ! always ! or use prepared statements)

这篇关于什么是更快/更好的使用:MySQL或PHP md5函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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