日期时间 VS 时间戳性能
我有一个查询。这应该获取 2 个月前创建的记录。 mysql的表类型是Innodb。 我使用哪种类型的日期(时间)。 Datetime 或 Timestamp int(11) 或 Timestamp 以获得更好的性能。 记录约为50000-100000条。
....
$monthsback = 2;
$date = strtotime("-$monthsback months",time());
$date =date( "Y-m-d H:i:s", $date ); // if i use Datetime
while($result=mysql_fetch_array($r))
{
$recorddate=$result['date'];//fetched from mysql
if ($recorddate>$monthsback)
{
echo "....";
}
else
{
echo "....";
}
}
...
i have a query.this should fetch records created 2 months ago. mysql table type is Innodb.
which type do i use for date (time). Datetime or Timestamp int(11) or Timestamp for better performance.
records is about 50000-100000.
....
$monthsback = 2;
$date = strtotime("-$monthsback months",time());
$date =date( "Y-m-d H:i:s", $date ); // if i use Datetime
while($result=mysql_fetch_array($r))
{
$recorddate=$result['date'];//fetched from mysql
if ($recorddate>$monthsback)
{
echo "....";
}
else
{
echo "....";
}
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只处理十万条记录?
那么这就是一个微优化。
选择最适合数据的列类型。
DATETIME
最终将成为存储日期和时间信息的最佳、最灵活的列类型,因为这就是它的设计目的。仅当您可以证明另一种方法更快时才更改此设置。通过对代码进行基准测试和分析来找到真实的代码 瓶颈。
You're only dealing with a hundred thousand records?
Then this qualifies as a micro-optimization.
Pick the column type that best fits the data.
DATETIME
is going to end up being the best, most flexible column type for storing date and time information, because that's what it's designed to do.Change this only when you can prove that another method is faster. Do this by benchmarking and profiling your code to find real bottlenecks.