New question

Question:

Date: 09-06-2015 10:56:34 (In Spanish)

¿Cómo saber si una tabla necesita optimizarse en MySQL?[Resolved]

hola amigos tal y como dice el titulo como puedo saber si una tabla necesita optimizarse.
Esq tengo este código que me optimiza todas las tablas
<?php
include "../sistem/acceso_db.php";
//EXRAEMOS LAS TABLAS DE LA base DE DATOS
$sql = "SHOW TABLES";
$tablas = mysqli_query($link, $sql) or die("No se puede ejecutar la consulta: ".mysqli_error());
while ($tabla = mysqli_fetch_assoc($tablas))  {
	foreach ($tabla as $item => $nombre_tabla) {
		echo '<script language="javascript">alert("';
		echo "Tabla: ".$nombre_tabla.": ";
		//echo $nombre_tabla.": ";
		//OPTIMIZAMOS LAS TABLAS
		mysqli_query($link, "OPTIMIZE TABLE ".$nombre_tabla) or die("No se puede ejecutar la consulta: ".mysqli_error());
		// MOSTRAMOS EL RESULTADO
		if (mysqli_error($link)){
			echo " No ha podido ser optimizada.";
		}else{
			echo "Optimizada.";
			echo '");</script>';
			echo '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://elcapa8.hol.es/admin/">';
		}
		echo "<br />";
	}
}
mysqli_close($link);
?>

Pero quiero saber como saber cuando la tabla necesita optimizarse y poder ejecutar este script n.n
Salu2
Tags: MySQL - MySQL Optimization - Question Votes: 1 - Answers: 2 - Views: 11 Share on: Google Facebook Twitter LinkedIn Link
 

Answers:

  • Date: 10-06-2015 04:33:02 Hola Edison, lo que puedes hacer es ver la fragmentación de las tablas y proceder a optimizar solo las que lo requieran.

    Un script para ver la fragmentación en % "frag_percent" en TODAS LAS TABLAS de TODAS LAS BASE DE DATOS (ojo porque puede tardar) es el siguiente:

    select
         table_schema
        ,table_name
        ,data_length
        ,data_free
        ,(data_free/data_length)*100 AS frag_percent
        ,engine
    from information_schema.tables
    where
        table_schema not in ('information_schema', 'mysql')
        and data_free > 0
    order by frag_percent desc;


    Obviamente que puedes restringir el script a un base de datos en particular con un simple cambio:

    table_schema = 'NOMBRE_BASE_DE_DATOS'


    Espero que mi respuesta te sea de ayuda.

    Saludos,
    Fernando
      Votes: 2 - Link answer
     
  • Date: 10-06-2015 15:14:21 Muy bueno el script de Fernando
    lo voy a probar

    saludos
      Votes: 0 - Link answer
     
To actively participate in the community first must authenticate, enter the system.Sign In
 
frjcbbae garagebible.com