I have a query that makes the MariaDB server occupy 100% CPU for more than 10 minutes and finally the page gives timeout.
The query in question is the following:
SELECT
formasdepago.nombre as 'Forma de pago',
SUM(transaccion) AS Total
FROM contabilidad
INNER JOIN formasdepago ON formasdepago.id = contabilidad.formadepago
WHERE contabilidad.anulado = 0
Group By formadepago
The corresponding tables are the following:
Accounting :
CREATE TABLE 'contabilidad' (
'anulado' TINYINT(1) NOT NULL DEFAULT '0',
'cuentacorriente' INT(11) NOT NULL DEFAULT '0',
'cuotas' DECIMAL(10,2) NOT NULL DEFAULT '0',
'empleado' INT(11) NOT NULL DEFAULT '0',
'fecha' DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
'formadepago' INT(11) NOT NULL DEFAULT '1',
'id' INT(11) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
'idgasto' INT(11) NOT NULL DEFAULT '0',
'interesfinanciero' DECIMAL(10,2) NOT NULL DEFAULT '0',
'interesformadepago' DECIMAL(10,2) NOT NULL DEFAULT '0',
'ticketid' INT(11) NOT NULL DEFAULT '0',
'tipodetransaccion' VARCHAR(50) NOT NULL,
'transaccion' DECIMAL(10,2) NOT NULL DEFAULT '0',
'valordecuota' DECIMAL(10,2) NOT NULL DEFAULT '0',
'valorfinal' DECIMAL(10,2) NOT NULL DEFAULT '0',
PRIMARY KEY ('id'),
INDEX 'fecha' ('fecha'),
INDEX 'ticketid' ('ticketid'),
INDEX 'idgasto' ('idgasto'),
INDEX 'formadepago' ('formadepago'),
FULLTEXT INDEX 'tipodetransaccion' ('tipodetransaccion')
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=13975947
;
Payment method :
CREATE TABLE 'formasdepago' (
'id' INT(11) NOT NULL AUTO_INCREMENT,
'habilitado' INT(1) NOT NULL DEFAULT '1',
'eliminado' INT(1) NOT NULL DEFAULT '0',
'simbolo' VARCHAR(5) NOT NULL,
'diferencia' DECIMAL(11,2) NOT NULL DEFAULT '0',
'imagen' MEDIUMBLOB NULL DEFAULT NULL,
'nombre' VARCHAR(50) NOT NULL,
'preciodelista' INT(1) NOT NULL DEFAULT '0',
'numeropreciodelista' INT(5) NOT NULL DEFAULT '1',
'financiacion' INT(5) NOT NULL DEFAULT '0',
'imagenmini' BLOB NULL DEFAULT NULL,
PRIMARY KEY ('id'),
INDEX 'id' ('id'),
INDEX 'eliminado' ('eliminado'),
INDEX 'habilitado' ('habilitado'),
FULLTEXT INDEX 'nombre' ('nombre')
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=8
;
Accounting has 1.5 GB of information, while payment only takes 3 records.
With such a large amount of data, I separate them by date to make a smaller query, but I do not understand why the delay is.
Could someone give me some indication of what the server is doing so that it takes so long? I do not understand, really. I hope someone helps me, thank you very much.