Skip to content

Commit

Permalink
floor dates to minutes for better performance with indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tastaturberuf committed Apr 30, 2024
1 parent 23eac10 commit 865b646
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions models/AnyStoresModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Tastaturberuf;


use Contao\Date;
use Contao\Model\Collection;

class AnyStoresModel extends \Model
Expand Down Expand Up @@ -73,7 +74,8 @@ public static function findPublishedByIdOrAlias($varId, array $arrOptions=array(
$arrColumns = is_numeric($varId) ? array("$t.id=?") : array("$t.alias=?");

if (!static::isPreviewMode($arrOptions)) {
$arrColumns[] = "($t.start='' OR $t.start<UNIX_TIMESTAMP()) AND ($t.stop='' OR $t.stop>UNIX_TIMESTAMP()) AND $t.published=1";
$time = Date::floorToMinute();
$arrColumns[] = "($t.start='' OR $t.start<=$time) AND ($t.stop='' OR $t.stop>$time) AND $t.published=1";
}

return static::findOneBy($arrColumns, $varId, $arrOptions);
Expand All @@ -93,7 +95,8 @@ public static function findPublishedByCategory(array $arrCategories, array $arrO
$arrColumns = array("$t.pid IN(".implode(',', array_map('intval', $arrCategories)).")");

if (!static::isPreviewMode($arrOptions)) {
$arrColumns[] = "($t.start='' OR $t.start<UNIX_TIMESTAMP()) AND ($t.stop='' OR $t.stop>UNIX_TIMESTAMP()) AND $t.published=1";
$time = Date::floorToMinute();
$arrColumns[] = "($t.start='' OR $t.start<=$time) AND ($t.stop='' OR $t.stop>$time) AND $t.published=1";
}

return static::findBy($arrColumns, null, $arrOptions);
Expand All @@ -114,8 +117,9 @@ public static function findPublishedByCategoryAndCountry(array $arrCategories, $
$arrColumns = array("$t.pid IN(".implode(',', array_map('intval', $arrCategories)).")");

if (!static::isPreviewMode($arrOptions)) {
$arrColumns[] = "($t.start='' OR $t.start<UNIX_TIMESTAMP())";
$arrColumns[] = "($t.stop='' OR $t.stop>UNIX_TIMESTAMP())";
$time = Date::floorToMinute();
$arrColumns[] = "($t.start='' OR $t.start<=$time)";
$arrColumns[] = "($t.stop='' OR $t.stop>$time)";
$arrColumns[] = "$t.published=1";
}

Expand Down Expand Up @@ -159,7 +163,8 @@ public static function findPublishedByAdressAndCountryAndCategory(
);

if (!static::isPreviewMode([])) {
$arrOptions['column'][] = "($t.start='' OR $t.start<UNIX_TIMESTAMP()) AND ($t.stop='' OR $t.stop>UNIX_TIMESTAMP()) AND $t.published=1";
$time = Date::floorToMinute();
$arrOptions['column'][] = "($t.start='' OR $t.start<=$time) AND ($t.stop='' OR $t.stop>$time) AND $t.published=1";
}

// Country
Expand Down Expand Up @@ -218,8 +223,9 @@ public static function findPublishedByAdressAndCountryAndCategory(
public static function countAllPublished()
{
if (!static::isPreviewMode([])) {
$arrColumns[] = "(start='' OR start<UNIX_TIMESTAMP())";
$arrColumns[] = "(stop='' OR stop>UNIX_TIMESTAMP())";
$time = Date::floorToMinute();
$arrColumns[] = "(start='' OR start<=$time)";
$arrColumns[] = "(stop='' OR stop>$time)";
$arrColumns[] = "published=1";
}

Expand All @@ -235,8 +241,9 @@ public static function countAllPublished()
public static function countPublishedByPid($intPid)
{
if (!static::isPreviewMode([])) {
$arrColumns[] = "(start='' OR start<UNIX_TIMESTAMP())";
$arrColumns[] = "(stop='' OR stop>UNIX_TIMESTAMP())";
$time = Date::floorToMinute();
$arrColumns[] = "(start='' OR start<=$time)";
$arrColumns[] = "(stop='' OR stop>$time)";
$arrColumns[] = "published=1";
$arrColumns[] = "pid=?";
}
Expand Down

0 comments on commit 865b646

Please sign in to comment.