MySQL Limites

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Octobre 21st, 2009, 2:38 am

J'ai finalement eu le temps de la part de mon projet qui doit faire quelque chose comme ça hier. Je voulais juste faire une note de deux façons, j'ai essayé.

Heres la première tentative. Il est en moyenne 1,6 secondes pour générer la page sur le serveur de test.

PHP Code: [ Select ]
   $result = $db->query('SELECT category_id, label, description FROM ' . CATEGORIES_TABLE . ' WHERE soi = TRUE ORDER BY label ASC', MYSQLI_USE_RESULT);
   while($row = $result->fetch_object())
   {
      $c->categories[]  = $row;
   }
   $result->close();
 
   $sql = $db->prepare('
      SELECT wp.label, wp.path, wp.wallpaper_id
         FROM ' . CAT_WP_RELATIONS_TABLE . ' rel
            LEFT JOIN ' . WALLPAPERS_TABLE . ' wp
               ON rel.wallpaper_id = wp.wallpaper_id
         WHERE rel.category_id IN ((SELECT category_id FROM ' . CATEGORIES_TABLE . " WHERE ? IN(category_id, parent_id)))
      ORDER BY rel.insert_order DESC LIMIT {$config->options->index->new_wallpapers->count}"
   );
   if(!$sql){$page->messages .= new message($db->error);}
   else{$sql->bind_param('s', $c->category_id);}
   
   foreach($c->categories as &$c->category)
   {if(!$sql){continue;}
      $c->category->url = sprintf($config->options->url->rewrite_mode ? $config->options->url->category_mask : 'index.php?category_id=%1$s&page=%3$s',
         $c->category->category_id,
         string_utils::htmlentities($c->category->label),
         1
      );
      $c->category_id   = $c->category->category_id;
      $c->wallpapers = '';
      if($sql->execute())
      {
         $sql->bind_result($c->wallpaper->label, $c->wallpaper->path, $c->wallpaper->wallpaper_id);
         while($sql->fetch())
         {
  1.    $result = $db->query('SELECT category_id, label, description FROM ' . CATEGORIES_TABLE . ' WHERE soi = TRUE ORDER BY label ASC', MYSQLI_USE_RESULT);
  2.    while($row = $result->fetch_object())
  3.    {
  4.       $c->categories[]  = $row;
  5.    }
  6.    $result->close();
  7.  
  8.    $sql = $db->prepare('
  9.       SELECT wp.label, wp.path, wp.wallpaper_id
  10.          FROM ' . CAT_WP_RELATIONS_TABLE . ' rel
  11.             LEFT JOIN ' . WALLPAPERS_TABLE . ' wp
  12.                ON rel.wallpaper_id = wp.wallpaper_id
  13.          WHERE rel.category_id IN ((SELECT category_id FROM ' . CATEGORIES_TABLE . " WHERE ? IN(category_id, parent_id)))
  14.       ORDER BY rel.insert_order DESC LIMIT {$config->options->index->new_wallpapers->count}"
  15.    );
  16.    if(!$sql){$page->messages .= new message($db->error);}
  17.    else{$sql->bind_param('s', $c->category_id);}
  18.    
  19.    foreach($c->categories as &$c->category)
  20.    {if(!$sql){continue;}
  21.       $c->category->url = sprintf($config->options->url->rewrite_mode ? $config->options->url->category_mask : 'index.php?category_id=%1$s&page=%3$s',
  22.          $c->category->category_id,
  23.          string_utils::htmlentities($c->category->label),
  24.          1
  25.       );
  26.       $c->category_id   = $c->category->category_id;
  27.       $c->wallpapers = '';
  28.       if($sql->execute())
  29.       {
  30.          $sql->bind_result($c->wallpaper->label, $c->wallpaper->path, $c->wallpaper->wallpaper_id);
  31.          while($sql->fetch())
  32.          {


Puis theres la deuxième tentative, prend 0.19 secondes en moyenne pour générer la page.

PHP Code: [ Select ]
   $c->categories = array();
   
   $result = $db->query('SELECT category_id, label, description FROM ' . CATEGORIES_TABLE . ' WHERE soi = TRUE', MYSQLI_USE_RESULT);
   while($row = $result->fetch_object())
   {
      $row->sub_categories          = array($row->category_id => $row->category_id);
      $c->categories[$row->category_id]   = $row;
   }
   $result->close();
   
   $result = $db->query('
      SELECT category_id, parent_id
         FROM ' . CATEGORIES_TABLE . '
         WHERE parent_id IN(' . implode(',', array_keys($c->categories)) . ')'
   );
   while($row = $result->fetch_object())
   {
      $c->categories[$row->parent_id]->sub_categories[$row->category_id] = $row->category_id;
   }
   $result->close();
   
   $c->max_subs = 0;
   foreach($c->categories as &$c->category)
   {
      $c->max_subs = max($c->max_subs, count($c->category->sub_categories));
   }
   foreach($c->categories as &$c->category)
   {
      $c->category->sub_categories = array_pad($c->category->sub_categories, $c->max_subs, current($c->category->sub_categories));
   }
   
   function __sort_cats_label($a, $b){return mb_strtolower($a->label) > mb_strtolower($b->label) ? 1 : -1;}
   usort($c->categories, '__sort_cats_label');
 
   $sql = $db->prepare('
      SELECT wp.label, wp.path, wp.wallpaper_id
         FROM ' . CAT_WP_RELATIONS_TABLE . ' rel
            LEFT JOIN ' . WALLPAPERS_TABLE . ' wp
               ON rel.wallpaper_id = wp.wallpaper_id
         WHERE rel.category_id IN (' . implode(',', array_fill(0, $c->max_subs, '?')) . ")
      ORDER BY rel.insert_order DESC LIMIT {$config->options->index->new_wallpapers->count}"
   );
   if(!$sql){$page->messages .= new message($db->error);}
   
   foreach($c->categories as &$c->category)
   {if(!$sql){continue;}
      $c->category->url = sprintf($config->options->url->rewrite_mode ? $config->options->url->category_mask : 'index.php?category_id=%1$s&page=%3$s',
         $c->category->category_id,
         string_utils::htmlentities($c->category->label),
         1
      );
      array_unshift($c->category->sub_categories, str_repeat('i', $c->max_subs));
      call_user_func_array(array($sql, 'bind_param'), $c->category->sub_categories);
      $c->wallpapers = '';
      if($sql->execute())
      {
         $sql->bind_result($c->wallpaper->label, $c->wallpaper->path, $c->wallpaper->wallpaper_id);
         while($sql->fetch())
         {
  1.    $c->categories = array();
  2.    
  3.    $result = $db->query('SELECT category_id, label, description FROM ' . CATEGORIES_TABLE . ' WHERE soi = TRUE', MYSQLI_USE_RESULT);
  4.    while($row = $result->fetch_object())
  5.    {
  6.       $row->sub_categories          = array($row->category_id => $row->category_id);
  7.       $c->categories[$row->category_id]   = $row;
  8.    }
  9.    $result->close();
  10.    
  11.    $result = $db->query('
  12.       SELECT category_id, parent_id
  13.          FROM ' . CATEGORIES_TABLE . '
  14.          WHERE parent_id IN(' . implode(',', array_keys($c->categories)) . ')'
  15.    );
  16.    while($row = $result->fetch_object())
  17.    {
  18.       $c->categories[$row->parent_id]->sub_categories[$row->category_id] = $row->category_id;
  19.    }
  20.    $result->close();
  21.    
  22.    $c->max_subs = 0;
  23.    foreach($c->categories as &$c->category)
  24.    {
  25.       $c->max_subs = max($c->max_subs, count($c->category->sub_categories));
  26.    }
  27.    foreach($c->categories as &$c->category)
  28.    {
  29.       $c->category->sub_categories = array_pad($c->category->sub_categories, $c->max_subs, current($c->category->sub_categories));
  30.    }
  31.    
  32.    function __sort_cats_label($a, $b){return mb_strtolower($a->label) > mb_strtolower($b->label) ? 1 : -1;}
  33.    usort($c->categories, '__sort_cats_label');
  34.  
  35.    $sql = $db->prepare('
  36.       SELECT wp.label, wp.path, wp.wallpaper_id
  37.          FROM ' . CAT_WP_RELATIONS_TABLE . ' rel
  38.             LEFT JOIN ' . WALLPAPERS_TABLE . ' wp
  39.                ON rel.wallpaper_id = wp.wallpaper_id
  40.          WHERE rel.category_id IN (' . implode(',', array_fill(0, $c->max_subs, '?')) . ")
  41.       ORDER BY rel.insert_order DESC LIMIT {$config->options->index->new_wallpapers->count}"
  42.    );
  43.    if(!$sql){$page->messages .= new message($db->error);}
  44.    
  45.    foreach($c->categories as &$c->category)
  46.    {if(!$sql){continue;}
  47.       $c->category->url = sprintf($config->options->url->rewrite_mode ? $config->options->url->category_mask : 'index.php?category_id=%1$s&page=%3$s',
  48.          $c->category->category_id,
  49.          string_utils::htmlentities($c->category->label),
  50.          1
  51.       );
  52.       array_unshift($c->category->sub_categories, str_repeat('i', $c->max_subs));
  53.       call_user_func_array(array($sql, 'bind_param'), $c->category->sub_categories);
  54.       $c->wallpapers = '';
  55.       if($sql->execute())
  56.       {
  57.          $sql->bind_result($c->wallpaper->label, $c->wallpaper->path, $c->wallpaper->wallpaper_id);
  58.          while($sql->fetch())
  59.          {


Dans les deux situations sa génération d'une page pour les 15 catégories au total, un couple sont des pare pas de catégories qui comprennent des sous catégories 1-10.
Im pensée sur l'ajout de deux colonnes à la table des catégories appelées "min_id" et "Max" id "qui s'alignent avec la colonne insert_id sur la table de mes relations que je peux utiliser avec une clause ENTRE à réduire le nombre de lignes nécessaires pour numériser. Je dois travailler sur la façon de gérer la mise à jour ces index though.
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Octobre 21st, 2009, 2:38 am

Afficher de l'information

  • Total des messages de ce sujet: 16 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 207 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC