Projekt

Obecné

Profil

« Předchozí | Další » 

Revize a0025b38

Přidáno uživatelem Filip Jani před téměř 6 roky(ů)

Optimalizace SQL dotazu

Zobrazit rozdíly:

app/model/repository/TransliterationRepository.php
36 36
     * @param ArrayHash $queryParams objekt s podmínkami pro hledaný text
37 37
     * @param $offset null|int
38 38
     * @param $limit null|int
39
     * @param bool $countOnly : flag pokud chceme vrátit pouze počet výsledků
39 40
     * @return \Nette\Database\ResultSet
40 41
     */
41
    public function transliterationsFulltextSearch(ArrayHash $queryParams, int $offset = null, int $limit = null)
42
    public function transliterationsFulltextSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, bool $countOnly = false)
42 43
    {
43 44
        $where = '';
44 45
        $whereArgs = [];
......
118 119
            $whereArgs[] = $this->prepareQueryArgByOperator($queryParams['date'], $queryParams['date_condition']);
119 120
        }
120 121

  
121
        $query = "SELECT 
122
                    t.id_transliteration as id,
123
                    s.id_surface as surface_id, 
124
                    b.book_abrev, 
125
                    t.chapter, 
126
                    l.transliteration, 
127
                    l.id_line as line_id,
128
                    l.line_number 
129
                  FROM transliteration t
122
        if ($countOnly)
123
        {
124
            $query = "SELECT COUNT(t.id_transliteration) as count ";
125
        } else
126
        {
127
            $query = "SELECT t.id_transliteration as id,
128
                             s.id_surface as surface_id, 
129
                             b.book_abrev, 
130
                             t.chapter, 
131
                             l.transliteration, 
132
                             l.id_line as line_id,
133
                             l.line_number ";
134
        }
135

  
136
        $query .= "FROM transliteration t
130 137
                  LEFT JOIN surface s ON s.id_transliteration = t.id_transliteration
131 138
                  LEFT JOIN line l ON l.id_surface = s.id_surface
132 139
                  LEFT JOIN book b ON t.id_book = b.id_book
133
                  WHERE " . $where .
134
                 " ORDER BY id DESC ";
140
                  WHERE " . $where;
141

  
142
        if (!$countOnly)
143
        {
144
            $query .= " ORDER BY id DESC ";
145
        }
135 146

  
136 147
        if($offset !== null && $limit !== null)
137 148
        {
......
145 156

  
146 157
    public function getTransliterationsFulltextSearchTotalCount($queryParams)
147 158
    {
148
        return $this->transliterationsFulltextSearch($queryParams, null, null)->getRowCount();
159
        return $this->transliterationsFulltextSearch($queryParams, null, null, true)->fetch()->count;
149 160
    }
150 161

  
151 162
    /**
......
225 236
     * @param ArrayHash $queryParams : parametry hledání
226 237
     * @param int|null $offset
227 238
     * @param int|null $limit
239
     * @param bool $countOnly : flag pokud chceme vrátit pouze počet výsledků
228 240
     * @return \Nette\Database\ResultSet
229 241
     */
230
    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null)
242
    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, bool $countOnly = false)
231 243
    {
232 244
        /**
233 245
         * @var $mc string : udává jestli je mezi vyhledávanými parametry logické AND nebo OR
......
303 315
            $where = substr($where, strlen($mc));
304 316
        }
305 317

  
306
        $query = "SELECT 
307
                    t.id_transliteration as id,
308
                    b.book_abrev, 
309
                    t.chapter, 
310
                    t.id_museum
311
                  FROM transliteration t
318
        if ($countOnly)
319
        {
320
            $query = "SELECT COUNT(t.id_transliteration) as count ";
321
        } else
322
        {
323
            $query = "SELECT t.id_transliteration as id,
324
                             b.book_abrev, 
325
                             t.chapter, 
326
                             t.id_museum ";
327
        }
328

  
329
        $query .= "FROM transliteration t
312 330
                  LEFT JOIN book b ON t.id_book = b.id_book ";
313 331

  
314 332
        if (!empty($where))
......
316 334
            $query .= "WHERE " . $where;
317 335
        }
318 336

  
319
        $query .= " GROUP BY b.book_abrev, t.chapter, t.id_transliteration ORDER BY t.chapter ";
337
        if(!$countOnly)
338
        {
339
            $query .= " GROUP BY b.book_abrev, t.chapter, t.id_transliteration ORDER BY t.chapter ";
320 340

  
321
        if($offset !== NULL && $limit !== NULL){
322
            $query .= ' LIMIT ?, ?';
323
            $whereArgs[] = (int) $offset;
324
            $whereArgs[] = (int) $limit;
341
            if ($offset !== NULL && $limit !== NULL)
342
            {
343
                $query .= ' LIMIT ?, ?';
344
                $whereArgs[] = (int)$offset;
345
                $whereArgs[] = (int)$limit;
346
            }
325 347
        }
326 348

  
327 349

  
......
340 362
     */
341 363
    public function getTransliterationCatalogueSearchTotalCount(ArrayHash $queryParams, int $offset = null, int $limit = null): int
342 364
    {
343
        return $this->transliterationCatalogueSearch($queryParams, $offset, $limit)->getRowCount();
365
        return $this->transliterationCatalogueSearch($queryParams, $offset, $limit, true)->fetch()->count;
344 366
    }
345 367
}

Také k dispozici: Unified diff