Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2015
    Messaggi
    81

    Velocizzare accesso dati mysql

    salve, avreste qualche consiglio per velocizzare l'accesso di dati da due tabelle una con poche righe e l' altra con circa 200 righe. Il codice è questo, il problema è che per caricare la lista in una pagina ci mette 5 secondi anche di piu, non mi sembra normale visto che alla fine sono solo circa 210 righe in totale.

    Codice PHP:
    <?php
    include('config.php'); 
    /* CONTO QUANTE RIGHE STAMPARE */
    $sqlcatC "SELECT id FROM categorie ORDER BY id DESC LIMIT 0, 1;";
    $requestcatC mysqli_query($con$sqlcatC);
    $rowcatC mysqli_fetch_row($requestcatC);

    $numcat $rowcatC[0];
    $i 1;

    while (
    $i <= $numcat) {
        
    /* STAMPO UNA CATEGORIA */
        
    $sqlcat "SELECT categoria FROM categorie WHERE id = '$i';";
        
    $requestcat mysqli_query($con$sqlcat);
        
    $rowcat mysqli_fetch_row($requestcat);
        
    $categoria $rowcat[0];
        echo(
    '<option class=opt-categoria>'.$categoria.'</option>');
        
    /* CONTO QUANTE SOTTOCATEGORIE PESENTI IN CATEGORIA APPENA STAMPATA*/
        
    $sqlsotC "SELECT id FROM sottocategorie ORDER BY id DESC LIMIT 0, 1;";
        
    $requestsotC mysqli_query($con$sqlsotC);
        
    $rowsotC mysqli_fetch_row($requestsotC);
        
        
    $numsot $rowsotC[0];
        
    $y 1;
        
        while (
    $y <= $numsot) {
            
    /* ISOLO LE SOTTOCATEGORIE DI ALTRE CATEGORIE E STAMPO SOLO SOOTTOCATEGORIE DELLA CATEGORIA APPENA STAMPATA */
            
    $sqlsot "SELECT sottocategoria FROM sottocategorie WHERE id = $y AND idcategoria = $i;";
            
    $requestsot mysqli_query($con$sqlsot);
            
    $rowsot mysqli_fetch_row($requestsot);
            
    $sottocategoria $rowsot[0];
            if(
    $sottocategoria != NULL){
                echo(
    '<option>'.$sottocategoria.'</option>');$y++;}
            else{
    $y++;}
        }
        
    $i++;
    }
    ?>

  2. #2
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Alla fin fine il tuo codice dovrebbe assomigliare a questo.
    Ovviamente non sono stato in grado di testare quindi potrebbe non funzionare. Comunque l'idea c'è.

    Codice PHP:
    <?php
    include('config.php'); 
    $sqlcatC "SELECT id,categoria FROM categorie ORDER BY id";
    $requestcatC mysqli_query($con$sqlcatC);
    while (list(
    $i,$categoria) = mysqli_fetch_array($requestcatC)) {
        
    /* STAMPO UNA CATEGORIA */
        
    echo('<option class=opt-categoria>'.$categoria.'</option>');
        
    /* ISOLO LE SOTTOCATEGORIE DI ALTRE CATEGORIE E STAMPO SOLO SOOTTOCATEGORIE DELLA CATEGORIA APPENA STAMPATA */
        
    $sqlsot "SELECT sottocategoria FROM sottocategorie WHERE idcategoria = $i;";
        
    $requestsot mysqli_query($con$sqlsot);
        while (list(
    $sottocategoria) = mysqli_fetch_array($requestsot)) {
          echo(
    '<option>'.$sottocategoria.'</option>');
        }
    }
    ?>
    Ridatemi i miei 1000 posts persi !!!!
    Non serve a nulla ottimizzare qualcosa che non funziona.
    Cerco il manuale dell'Olivetti LOGOS 80B - www.emmella.fr

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2015
    Messaggi
    81
    Grazie mille sei riuscito a migliorare di moltissimo il caricamento della lista (ora ci mette solo 1,5 secondi);
    avrei ancora una domanda? ma questo è il metodo più veloce possibile o si può ancora migliorare? per esempio migliorando i valori della tabella

  4. #4
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Volendo si puo' sempre.

    Codice PHP:
    $sqlcatC     "SELECT id,categoria,sottocategoria FROM categorie INNER JOIN sottocategorie ON idcategoria=id ORDER BY id,sottocategoria ";
    $requestcatC mysqli_query($con$sqlcatC);
    $sav_id      = -9;
    while (list(
    $i,$categoria,$sottocategoria) = mysqli_fetch_array($requestcatC)) {
     if (
    $sav_id != $i) {
       
    $sav_id $i;
       echo(
    '<option class=opt-categoria>'.$categoria.'</option>');
     }
     echo(
    '<option>'.$sottocategoria.'</option>');

    Ridatemi i miei 1000 posts persi !!!!
    Non serve a nulla ottimizzare qualcosa che non funziona.
    Cerco il manuale dell'Olivetti LOGOS 80B - www.emmella.fr

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.