Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime VS timestamp performance

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 "....";
    }
    }
...
like image 348
user006779 Avatar asked Sep 27 '26 18:09

user006779


1 Answers

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.

like image 176
Charles Avatar answered Sep 29 '26 07:09

Charles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!