PHP变量名和SQL表列名长度

时间:2015-07-02 20:15:58

标签: php mysql variables

我有这个真正的新手问题:)

尽管事实是

$lastInvoiceNumber 

$lastInvNum

或:

last_invoice_number(int 10)
last_inv_num(int 10)

节省一点时间来写。他们有什么好处(即使是最轻微的)
性能明智?

长与短?

有没有机会php和MySQL更重要的是会消耗
如果查询具有较短的表列名,则内存较少?
例如,如果我必须在单个查询中获取500行,我想是 查询将运行500次并运行
last_invoice_number 500次
vs跑步 last_inv_num可以节省一些记忆或使事情稍快一点

感谢。

1 个答案:

答案 0 :(得分:5)

不,无论如何都没有明显的性能差异,并且您通过使用描述性变量名称可以大大提高可读性。在内部,这些变量由内存地址引用(简单地说),而不是它们的ASCII / Unicode名称。它几乎可以用任何语言对性能产生的影响是如此微不足道,以至于它永远不会被注意到。

修改

我添加了一个基准。它表明,使用单个字母作为变量名称和使用17个字符的变量名称之间确实没有任何区别。单个字母甚至可能会慢一点。但是,我注意到在使用90个字符的变量名时,轻微的时间一致增加,但同样地,差异太小,不能用于实际目的。这是基准和输出:

<?php
# To prevent any startup-costs from skewing results of the first test.
$start = microtime(true);
for ($i = 0; $i<1000; $i++)
{
    $noop = null;
}
$end = microtime(true);

# Let's benchmark!
$start = microtime(true);
for ($i = 0; $i<1000000; $i++)
{
    $thisIsAReallyLongAndReallyDescriptiveVariableNameInFactItIsJustWayTooLongHonestlyWtf = mt_rand(0, 1000);
}
$end = microtime(true);

printf("Using a long name took %f seconds.\n", ($end - $start));


$start = microtime(true);
for ($i = 0; $i<1000000; $i++)
{
    $thisIsABitTooLong = mt_rand(0, 1000);
}
$end = microtime(true);

printf("Using a medium name took %f seconds.\n", ($end - $start));

$start = microtime(true);
for ($i = 0; $i<1000000; $i++)
{
    $t = mt_rand(0, 1000);
}
$end = microtime(true);

printf("Using a short name took %f seconds.\n", ($end - $start));

输出:

$ php so-test.php
Using a long name took 0.148200 seconds.
Using a medium name took 0.142286 seconds.
Using a short name took 0.145952 seconds.

同样适用于MySQL;我几乎可以保证,但它并不像基准那么容易。使用MySQL,您将从网络和IO获得比代码中符号命名更多的开销。就像PHP一样,在内部,列名不仅仅是迭代的字符串;数据以内存有效的格式存储。