title: large SQL query getting real slow when filtering date, embedded in PHP
body: <p>I am no <strong>SQL</strong> pro at all and this query took a lot from me. It all does work fine until it comes to <strong>date-filtering</strong>.
  This query  is embedded in PHP Code. 
  Firstly the user is able to set filters, like the user-filter area-filter. After pressing the filter button the defined SQL statement is loaded. Everything works pretty nicely when filtering users, status, and area. It is a bit slow, but bearable. <strong>But when I use the date filter the whole statements takes nearly one minute to load, which is not cool at all.</strong> I read a lot about SQL date filtering but nothing really helped (or I didn't understand). Is there a noob mistake I made? Like I said, I write SQL statements once in a while, but I have no profound knowledge of it. </p>
  
  <pre><code>$userstatement = "AND BenutzerId ='".$benutzerfilter."'"; // asd1134
  $statusstatement = "AND s.Name ='".$statusfilter."'"; // ie "deleted"
  $areastatement = "AND b.Name ='".$areafilter."'"; // ie "Video"
  $selected_date = $datefilter_from; // ie '2018-01-01'
  $monthfilter = 2; //possible values 1-24
  
  
  $bigQuery = "SELECT * FROM (  SELECT r.id, r.BenutzerId as BenutzerId,  equi.Bezeichnung, equi.VerleihprioritaetId, r.EquipmentId as eId, r.res_von, r.res_bis, equi.TypId, t.Name as TypName, r.Kommentar_user, s.Name as Status, b.Name as Bereich, b.Id as BereichId, btzr.Vorname, btzr.Nachname, IF(pe.Id,true,false) AS inPaket,
                   (  SELECT  max(gr.verleihstufe) as Verleihstufe
                      FROM benutzeringruppe as bengr
                      left join benutzergruppen as gr
                       on bengr.gruppenid = gr.Id
                      left join benutzer as ben
                       on bengr.benutzerid = ben.Id
                      where ben.id = r.BenutzerId 
                      AND gr.bereichid = b.Id
                      group by ben.id  ) as Verleihstufe,
                   (  SELECT true 
                      FROM benutzeringruppe as bg 
                      LEFT JOIN direktberechtigung as d2 
                       ON bg.gruppenid = d2.gruppenid 
                      WHERE bg.benutzerid = r.BenutzerId 
                      AND d2.equipmentid = r.EquipmentId LIMIT 1) 
                      as EquipmentDirektberechtigt
                     FROM reservierung as r
                     left join equipment as equi
                      on r.EquipmentId = equi.Id
                     left join typ as t 
                      on equi.TypId = t.Id
                     left join status as s
                      on r.StatusId = s.Id
                     left join bereich as b 
                      on equi.BereichId = b.Id
                     left join direktberechtigung as d
                      on r.EquipmentId = d.equipmentid
                     left join paketEq as pe
                      on r.EquipmentId = pe.EquipmentId 
                     left join benutzer as btzr
                      on r.BenutzerId = btzr.Id
                     Where r.res_von &gt; '$selected_date' 
                     AND r.res_von &lt; DATE_ADD
                     ('" .$selected_date. "', INTERVAL ".$monatsfilter." MONTH)
                     ".$statusstatement." ".$userstatement." ".$areastatement.")
              AS Big
              Order by Big.res_von DESC";
  </code></pre>