I have 3 tables and I want to retrieve from the history table the last event, so I wrote this request but the result is not good. or am i mistaken? thank you
CREATE TABLE `equipment` (
`id_equipment` int(10) UNSIGNED NOT NULL,
`reference` varchar(32) DEFAULT NULL COMMENT 'clé AIRO2',
`buying_date` date DEFAULT NULL,
`first_use` date DEFAULT NULL,
`checking_delai` varchar(45) DEFAULT NULL,
`serial_number` varchar(256) DEFAULT NULL,
`lot_number` varchar(256) DEFAULT NULL,
`lapsing_date` date NOT NULL,
`status` varchar(32) NOT NULL,
`inspection` date NOT NULL,
`compteur` int(11) DEFAULT NULL,
`id_model` int(10) UNSIGNED DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`detruit` int(1) DEFAULT NULL,
`date_REEPREUVE` date DEFAULT NULL,
`validite_apragaz` date DEFAULT NULL,
`restituee_AJR` int(11) NOT NULL,
`perdu` int(11) DEFAULT NULL,
`maintenance_catt_o2` date DEFAULT NULL,
`vendu` int(11) DEFAULT NULL,
`colonne_inogen` date DEFAULT NULL COMMENT 'date changement colonne inogen',
`rappel_525KS_dec19` int(1) DEFAULT NULL COMMENT 'rappel_cable_DEVILBISS_525_KS'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='matériel';
CREATE TABLE `equipment_history` (
`id_equipment_history` int(10) UNSIGNED NOT NULL,
`id_user` int(10) UNSIGNED NOT NULL,
`id_equipment` int(10) UNSIGNED DEFAULT NULL,
`id_patient` int(11) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`status_date` datetime DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`tuyau` tinyint(1) DEFAULT NULL,
`cable_alim` tinyint(1) DEFAULT NULL,
`compteur` int(11) DEFAULT NULL,
`verif_alarme` tinyint(1) DEFAULT NULL,
`verif_volume` tinyint(1) DEFAULT NULL,
`verif_pression` tinyint(1) DEFAULT NULL,
`verif_o2` tinyint(1) DEFAULT NULL,
`verif_debit` tinyint(1) DEFAULT NULL,
`remplacer_consommable` tinyint(1) DEFAULT NULL,
`complet` tinyint(1) DEFAULT NULL,
`comment_panne` text,
`ras` tinyint(1) DEFAULT NULL,
`panne` tinyint(1) DEFAULT NULL,
`id_piece_jointe` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='historique de l''utilisation du matériel';
CREATE TABLE `patient_has_equipment` (
`id_patient_has_equipment` int(10) UNSIGNED NOT NULL,
`id_intervention` int(10) UNSIGNED DEFAULT NULL,
`id_equipment` int(10) UNSIGNED NOT NULL,
`id_patient` int(10) UNSIGNED DEFAULT NULL,
`date_attribution` date DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
`version_appli` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
request :
SELECT
equipment.id_equipment,
equipment.reference,
history.updated,
history.`status`
FROM
equipment
LEFT JOIN
(
SELECT
*
FROM
`equipment_history`
GROUP BY
id_equipment
ORDER BY
`equipment_history`.`updated`
DESC
) AS history
ON
history.id_equipment = equipment.id_equipment
WHERE
history.`status` = 3 AND equipment.id_equipment NOT IN(
SELECT
id_equipment
FROM
patient_has_equipment
)
thanks
The group by subquery is the problem. While this is a syntax error in most databases, MySQL - unfortunately - tolerates this syntax (when sql mode ONLY_FULL_GROUP_BY is disabled), but does not do what you think: you actually an arbitary record from the table for each equipment_id (the order by clause has no effect on this behavior).
You need to filter for the last history record rather than aggregate. I think that this should do what you want:
select
e.id_equipment,
e.reference,
eh.updated,
eh.status
from equipment e
inner join equipment_history eh on eh.id_equipment = e.id_equipment
where
eh.status = 3
and eh.updated = (
select max(eh1.updated)
from equipment_history eh1
where eh1.id_equipment = e.id_equipment
)
and not exists (
select 1
from patient_has_equipment phe
where phe.id_equipment = e.id_equipment
)
my query is
select cp_emp_personal_detail.id
,cp_emp_personal_detail.emp_code
,cp_emp_personal_detail.emp_name
,cp_department.department
,GROUP_CONCAT(TIME(devicelogs_3_2018.LogDate) ORDER BY devicelogs_3_2018.LogDate) present
from cp_emp_personal_detail
left join cp_emp_official_detail on cp_emp_official_detail.emp_id = cp_emp_personal_detail.id
left join devicelogs_3_2018 on devicelogs_3_2018.DeviceId = cp_emp_personal_detail.org_id and cp_emp_personal_detail.id AND date(devicelogs_3_2018.LogDate) = CURRENT_DATE()
left join cp_department on cp_emp_official_detail.department = cp_department.id
where cp_emp_personal_detail.org_id = 1
group by cp_emp_personal_detail.id
and database design is:
CREATE TABLE `cp_emp_personal_detail` (
`id` int(11) NOT NULL,
`org_id` int(11) NOT NULL,
`emp_code` varchar(100) DEFAULT NULL,
`emp_name` varchar(100) NOT NULL,
`emp_guardian_name` varchar(100) DEFAULT NULL,
`emp_dob` date NOT NULL,
`emp_email` varchar(100) DEFAULT NULL,
`emp_password` varchar(100) NOT NULL,
`emp_phone` bigint(15) DEFAULT NULL,
`emp_mobile` bigint(15) NOT NULL,
`emp_emergency_no` int(11) DEFAULT NULL,
`emp_address1` longtext NOT NULL,
`emp_address2` longtext,
`emp_city` int(11) NOT NULL,
`emp_pan` varchar(30) NOT NULL,
`emp_country` int(11) NOT NULL,
`emp_state` int(11) NOT NULL,
`emp_aadhar` varchar(30) DEFAULT NULL,
`emp_tax_status` varchar(100) NOT NULL,
`emp_pic` varchar(255) DEFAULT NULL,
`emp_gender` varchar(50) NOT NULL,
`emp_status` varchar(100) NOT NULL DEFAULT 'activate',
`crm_status` varchar(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY LIST (org_id)
(
PARTITION P1 VALUES IN (1) ENGINE=InnoDB,
PARTITION P2 VALUES IN (2) ENGINE=InnoDB
);
CREATE TABLE `devicelogs_3_2018` (
`DeviceLogId` bigint(20) NOT NULL,
`DownloadDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`DeviceId` bigint(20) NOT NULL,
`UserId` varchar(50) NOT NULL,
`LogDate` timestamp NOT NULL DEFAULT '1970-12-31 18:30:01',
`Direction` varchar(255) DEFAULT NULL,
`AttDirection` varchar(255) DEFAULT NULL,
`C1` varchar(255) DEFAULT NULL,
`C2` varchar(255) DEFAULT NULL,
`C3` varchar(255) DEFAULT NULL,
`C4` varchar(255) DEFAULT NULL,
`C5` varchar(255) DEFAULT NULL,
`C6` varchar(255) DEFAULT NULL,
`C7` varchar(255) DEFAULT NULL,
`WorkCode` varchar(255) DEFAULT NULL,
`hrapp_syncstatus` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `cp_department` (
`id` int(11) NOT NULL,
`org_id` int(11) NOT NULL,
`department` varchar(20) NOT NULL,
`depart_abb` varchar(10) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY LIST (org_id)
(
PARTITION P1 VALUES IN (1) ENGINE=InnoDB,
PARTITION P2 VALUES IN (2) ENGINE=InnoDB
);
CREATE TABLE `cp_emp_official_detail` (
`id` int(11) NOT NULL,
`emp_id` int(11) NOT NULL,
`org_id` int(11) NOT NULL,
`joining_date` date DEFAULT NULL,
`confirmation_periode` int(11) DEFAULT NULL,
`job_type` varchar(100) DEFAULT NULL,
`payment_mode` varchar(100) DEFAULT NULL,
`department` int(11) DEFAULT NULL,
`acc_no` int(11) DEFAULT NULL,
`designation` int(11) DEFAULT NULL,
`ess_status` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY LIST (org_id)
(
PARTITION P1 VALUES IN (1) ENGINE=InnoDB,
PARTITION P2 VALUES IN (2) ENGINE=InnoDB
);
can someone explain me how to optimize th query currently it is taking 3 to 4 min to execute with more than 8k records
Load take 60 sec, how can I optimize?
Explain struct:
explain tablename;
IMG : http://i.stack.imgur.com/TfbY3.png
Explain SQL
explain select * from tablename;
IMG : http://i.stack.imgur.com/jHI47.png
REAL DB
DROP TABLE IF EXISTS `afiliados`;
CREATE TABLE `afiliados` (
`id_afiliado` int(10) NOT NULL AUTO_INCREMENT,
`clave_panel` char(15) COLLATE latin1_spanish_ci DEFAULT NULL,
`clave_panel_ask` datetime DEFAULT NULL,
`id_trabajador` char(20) COLLATE latin1_spanish_ci DEFAULT NULL,
`persona` char(1) COLLATE latin1_spanish_ci DEFAULT 'H',
`Estado` int(10) NOT NULL DEFAULT '0',
`doc_fiscal` int(10) DEFAULT NULL,
`id_fiscal` char(20) COLLATE latin1_spanish_ci DEFAULT NULL,
`Nombre` char(25) COLLATE latin1_spanish_ci DEFAULT NULL,
`Apellidos` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`fecha_nacimiento` datetime DEFAULT NULL,
`Telefono Principal` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Telefono Secundario` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fecha_Alta` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Fecha_Aprobacion` datetime DEFAULT NULL,
`id_cuenta_aprobacion` int(11) DEFAULT NULL,
`Fecha_Documentacion` datetime DEFAULT NULL,
`Fecha_Alta_Sindicato` datetime DEFAULT NULL,
`Fecha_Baja_Sindicato` datetime DEFAULT NULL,
`Motivo_Baja_Sindicato` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fecha_Desafiliacion` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fax` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Direccion` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`CP` char(5) COLLATE latin1_spanish_ci DEFAULT NULL,
`Ciudad` char(35) COLLATE latin1_spanish_ci DEFAULT NULL,
`Provincia` char(25) COLLATE latin1_spanish_ci DEFAULT NULL,
`Pais` char(2) COLLATE latin1_spanish_ci DEFAULT NULL,
`Nacionalidad` char(2) COLLATE latin1_spanish_ci NOT NULL DEFAULT '',
`Email` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`Email_secundario` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`Id_delegado` int(10) DEFAULT NULL,
`Comentario` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Interno` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Eliminado` tinyint(1) NOT NULL DEFAULT '0',
`Es_Delegado` tinyint(1) NOT NULL DEFAULT '0',
`id_cuenta` int(10) DEFAULT NULL,
`Fecha_mod` datetime DEFAULT NULL,
`mod_id_cuenta` int(10) DEFAULT NULL,
`noemail` tinyint(1) NOT NULL DEFAULT '0',
`noemail_public` tinyint(1) NOT NULL DEFAULT '0',
`noemail_fact` tinyint(1) NOT NULL DEFAULT '0',
`noti_alta` datetime DEFAULT NULL,
`noti_fact` datetime DEFAULT NULL,
`noti_baja` datetime DEFAULT NULL,
`fecha_star_facturacion` datetime DEFAULT NULL,
`fecha_proxima_facturacion` datetime DEFAULT NULL,
`ciclo_mensual_facturacion` int(10) unsigned NOT NULL DEFAULT '3',
`fecha_sync` datetime DEFAULT NULL,
`fecha_chk` datetime DEFAULT NULL,
`LOPD` tinyint(1) NOT NULL DEFAULT '0',
`ncarnet` int(10) DEFAULT NULL,
`id_caja` int(10) unsigned NOT NULL DEFAULT '0',
`caja_fecha_add` datetime DEFAULT NULL,
`id_cuenta_caja_add` int(10) DEFAULT '0',
`caja_fecha_del` datetime DEFAULT NULL,
`id_cuenta_caja_del` int(10) DEFAULT '0',
PRIMARY KEY (`id_afiliado`),
KEY `apellidos` (`Apellidos`),
KEY `Email` (`Email`),
KEY `es_delegado` (`Es_Delegado`),
KEY `id_caja` (`id_caja`),
KEY `id_cuenta` (`id_cuenta`),
KEY `id_delegado` (`Id_delegado`),
KEY `id_fiscal` (`id_fiscal`),
KEY `id_trabajador` (`id_trabajador`),
KEY `nombre` (`Nombre`)
) ENGINE=MyISAM AUTO_INCREMENT=4467 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=DYNAMIC;
INSERT INTO `afiliados` VALUES (194,NULL,NULL,'XXXXX','M',1,1,'XXXXX','Frank','Poland','1967-11-13 00:00:00','0000000','','2012-08-08 10:35:31','2012-08-08 21:05:59',0,'2012-08-09 00:00:00','2005-04-04 00:00:00',NULL,NULL,NULL,'','Adress','00000','Sevilla','Sevilla','es','es',NULL,NULL,NULL,NULL,NULL,0,0,NULL,NULL,NULL,0,0,0,NULL,NULL,NULL,NULL,NULL,3,NULL,NULL,0,NULL,0,NULL,NULL,NULL,NULL);
DROP TABLE IF EXISTS `contratos`;
CREATE TABLE `contratos` (
`Id_contrato` int(10) NOT NULL AUTO_INCREMENT,
`id_empresa` int(10) DEFAULT NULL,
`Marca` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Departamento` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Localizacion` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`fecha_baja` datetime DEFAULT NULL,
`id_cuenta_add` int(10) DEFAULT NULL,
PRIMARY KEY (`Id_contrato`),
KEY `id_empresa` (`id_empresa`)
) ENGINE=MyISAM AUTO_INCREMENT=1013 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `contratos` VALUES (38,7,'Telefonica','CAOL','2º Planta',NULL,NULL),(193,11,'Vodafone','Atencion al Cliente','Planta Baja / 1ª Planta',NULL,3);
DROP TABLE IF EXISTS `empresa`;
CREATE TABLE `empresa` (
`Id_empresa` int(10) NOT NULL AUTO_INCREMENT,
`cif` char(10) COLLATE latin1_spanish_ci DEFAULT NULL,
`Razon_Social` char(60) COLLATE latin1_spanish_ci DEFAULT NULL,
`empresa` char(40) COLLATE latin1_spanish_ci DEFAULT NULL,
`Centro` char(40) COLLATE latin1_spanish_ci DEFAULT NULL,
`Provincia` char(30) COLLATE latin1_spanish_ci DEFAULT NULL,
`localizacion_sindicato` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_setting` int(10) DEFAULT NULL,
`direccion` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`cp` char(5) COLLATE latin1_spanish_ci DEFAULT NULL,
`ciudad` char(30) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_cuenta_add` int(10) unsigned DEFAULT '0',
`fecha_baja` datetime DEFAULT NULL,
`id_cuenta_baja` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`Id_empresa`),
KEY `cif` (`cif`),
KEY `empresa` (`empresa`),
KEY `id_setting` (`id_setting`)
) ENGINE=MyISAM AUTO_INCREMENT=871 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `empresa` VALUES (7,'A78751997','ATENTO TELESERVICIOS, S.A.','Atento','Indotorre','Sevilla','2º Planta',13,NULL,NULL,NULL,0,NULL,NULL),(11,'B62916077','KONECTA BTO, S.L.','Konecta BTO','Prado de la Torre','Sevilla','1ª Planta (Pasillo Baño)',19,'Prado de la Torre s/n','41110','Bollulos',0,NULL,NULL);
DROP TABLE IF EXISTS `historico`;
CREATE TABLE `historico` (
`Id_historico` int(10) NOT NULL AUTO_INCREMENT,
`id_afiliado` int(10) DEFAULT NULL,
`fecha_alta` datetime DEFAULT NULL,
`tipo_contrato` smallint(5) DEFAULT NULL COMMENT 'Obra y Servicio ,etc ....',
`fecha_baja` datetime DEFAULT NULL,
`motivo_baja` smallint(5) DEFAULT NULL,
`fecha_provision` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`id_contrato` int(10) DEFAULT NULL,
`comentario` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_cuenta` int(10) DEFAULT NULL,
`Fecha_mod` datetime DEFAULT NULL,
`mod_id_cuenta` int(10) DEFAULT NULL,
PRIMARY KEY (`Id_historico`),
KEY `id_afiliado` (`id_afiliado`),
KEY `id_contrato` (`id_contrato`),
KEY `id_cuenta` (`id_cuenta`),
KEY `tipo_contrato` (`tipo_contrato`)
) ENGINE=MyISAM AUTO_INCREMENT=4814 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `historico` VALUES (227,194,'2005-04-04 00:00:00',0,'2014-01-01 00:00:00',-1,'2012-08-08 10:35:47',38,'',16,NULL,NULL),(1586,194,'2014-01-01 00:00:00',0,NULL,NULL,'2015-08-24 21:14:41',193,'',115,NULL,NULL);
VIEWs:
DROP VIEW IF EXISTS `detalle_full_ko`;
CREATE VIEW `detalle_full_ko` AS
select `afiliados`.`id_afiliado` AS `Id_afiliado`,`afiliados`.`ncarnet` AS `ncarnet`,
`afiliados`.`Id_delegado` AS `Id_delegado`,`afiliados`.`Fecha_Alta` AS `Fecha_Alta_Afiliado`,
`afiliados`.`id_trabajador` AS `id_trabajador`,`afiliados`.`persona` AS `persona`,
`afiliados`.`Nombre` AS `Nombre`,`afiliados`.`Apellidos` AS `Apellidos`,
`afiliados`.`id_fiscal` AS `id_fiscal`,`afiliados`.`Email` AS `Email`,
`afiliados`.`Email_secundario` AS `Email_secundario`,
`afiliados`.`noemail` AS `noemail`,`afiliados`.`noemail_public` AS `noemail_public`,
`afiliados`.`Telefono Principal` AS `Telefono Principal`,
`afiliados`.`Telefono Secundario` AS `Telefono Secundario`,
`afiliados`.`Fax` AS `Fax`,`afiliados`.`Ciudad` AS `Ciudad`,
`afiliados`.`CP` AS `CP`,`afiliados`.`Provincia` AS `Provincia_Afiliado`,
`afiliados`.`fecha_nacimiento` AS `fecha_nacimiento`,
`afiliados`.`Estado` AS `Estado`,`afiliados`.`Es_Delegado` AS `Es_Delegado`,
`afiliados`.`Eliminado` AS `Eliminado`,`detalle_historico_ko`.`fecha_alta` AS `Fecha_Alta_Empresa`,
`empresa`.`Razon_Social` AS `Razon_Social`,`empresa`.`empresa` AS `nombre_empresa`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Departamento` AS `Departamento`,
`contratos`.`Marca` AS `Marca`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`localizacion_sindicato` AS `localizacion_sindicato`,
`detalle_historico_ko`.`Id_contrato` AS `id_contrato`,
`contratos`.`id_empresa` AS `id_empresa`,`detalle_historico_ko`.`tipo_contrato` AS `tipo_contrato`,
`afiliados`.`Fecha_Alta_Sindicato` AS `Fecha_Alta_Sindicato`,
`afiliados`.`Fecha_Baja_Sindicato` AS `Fecha_Baja_Sindicato`,
`afiliados`.`fecha_sync` AS `fecha_sync`,`afiliados`.`noti_alta` AS `noti_alta`,
`afiliados`.`noti_fact` AS `noti_fact`,`afiliados`.`noti_baja` AS `noti_baja`,
`afiliados`.`Fecha_mod` AS `Fecha_mod`,`afiliados`.`LOPD` AS `LOPD`
from (((`contratos`
join `empresa` on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`)))
join `detalle_historico_ko` on(((`empresa`.`Id_empresa` = `detalle_historico_ko`.`Id_empresa`)
and (`contratos`.`Id_contrato` = `detalle_historico_ko`.`Id_contrato`))))
join `afiliados` on((`detalle_historico_ko`.`id_afiliado` = `afiliados`.`id_afiliado`))
)
where (`afiliados`.`LOPD` = 0)
group by `afiliados`.`id_afiliado`;
DROP VIEW IF EXISTS `detalle_full_ok`;
CREATE VIEW `detalle_full_ok` AS
select `afiliados`.`id_afiliado` AS `Id_afiliado`,`afiliados`.`ncarnet` AS `ncarnet`,
`afiliados`.`Id_delegado` AS `Id_delegado`,`afiliados`.`Fecha_Alta` AS `Fecha_Alta_Afiliado`,
`afiliados`.`id_trabajador` AS `id_trabajador`,`afiliados`.`persona` AS `persona`,
`afiliados`.`Nombre` AS `Nombre`,`afiliados`.`Apellidos` AS `Apellidos`,
`afiliados`.`id_fiscal` AS `id_fiscal`,`afiliados`.`Email` AS `Email`,
`afiliados`.`Email_secundario` AS `Email_secundario`,
`afiliados`.`noemail` AS `noemail`,`afiliados`.`noemail_public` AS `noemail_public`,
`afiliados`.`Telefono Principal` AS `Telefono Principal`,
`afiliados`.`Telefono Secundario` AS `Telefono Secundario`,
`afiliados`.`Fax` AS `Fax`,`afiliados`.`Ciudad` AS `Ciudad`,
`afiliados`.`CP` AS `CP`,`afiliados`.`Provincia` AS `Provincia_Afiliado`,
`afiliados`.`fecha_nacimiento` AS `fecha_nacimiento`,
`afiliados`.`Estado` AS `Estado`,`afiliados`.`Es_Delegado` AS `Es_Delegado`,
`afiliados`.`Eliminado` AS `Eliminado`,`detalle_historico_ok`.`fecha_alta` AS `Fecha_Alta_Empresa`,
`empresa`.`Razon_Social` AS `Razon_Social`,`empresa`.`empresa` AS `nombre_empresa`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Departamento` AS `Departamento`,
`contratos`.`Marca` AS `Marca`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`localizacion_sindicato` AS `localizacion_sindicato`,
`detalle_historico_ok`.`Id_contrato` AS `id_contrato`,
`contratos`.`id_empresa` AS `id_empresa`,`detalle_historico_ok`.`tipo_contrato` AS `tipo_contrato`,
`afiliados`.`Fecha_Alta_Sindicato` AS `Fecha_Alta_Sindicato`,
`afiliados`.`Fecha_Baja_Sindicato` AS `Fecha_Baja_Sindicato`,
`afiliados`.`fecha_sync` AS `fecha_sync`,`afiliados`.`noti_alta` AS `noti_alta`,
`afiliados`.`noti_fact` AS `noti_fact`,`afiliados`.`noti_baja` AS `noti_baja`,
`afiliados`.`Fecha_mod` AS `Fecha_mod`,`afiliados`.`LOPD` AS `LOPD`
from (((`contratos`
join `empresa` on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`)))
join `detalle_historico_ok` on(((`empresa`.`Id_empresa` = `detalle_historico_ok`.`Id_empresa`)
and (`contratos`.`Id_contrato` = `detalle_historico_ok`.`Id_contrato`))))
join `afiliados` on((`detalle_historico_ok`.`id_afiliado` = `afiliados`.`id_afiliado`))
)
where (`afiliados`.`LOPD` = 0)
group by `afiliados`.`id_afiliado`;
DROP VIEW IF EXISTS `detalle_historico_ko`;
CREATE VIEW `detalle_historico_ko` AS
select high_priority `historico`.`Id_historico` AS `Id_historico`,
`historico`.`id_afiliado` AS `id_afiliado`,`contratos`.`Id_contrato` AS `Id_contrato`,
`historico`.`fecha_provision` AS `fecha_provision`,`empresa`.`Id_empresa` AS `Id_empresa`,
`empresa`.`empresa` AS `Empresa`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Marca` AS `Marca`,
`contratos`.`Departamento` AS `Departamento`,`historico`.`fecha_alta` AS `fecha_alta`,
`historico`.`fecha_baja` AS `fecha_baja`,`historico`.`comentario` AS `comentario`,
`historico`.`tipo_contrato` AS `tipo_contrato`
from (`empresa`
join (`contratos`
join `historico` on((`contratos`.`Id_contrato` = `historico`.`id_contrato`))) on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`))
)
group by `historico`.`id_afiliado`
order by isnull(`historico`.`fecha_baja`) desc,`historico`.`fecha_baja` desc;
DROP VIEW IF EXISTS `detalle_historico_ok`;
CREATE VIEW `detalle_historico_ok` AS
select high_priority `historico`.`Id_historico` AS `Id_historico`,
`historico`.`id_afiliado` AS `id_afiliado`,`contratos`.`Id_contrato` AS `Id_contrato`,
`historico`.`fecha_provision` AS `fecha_provision`,`empresa`.`Id_empresa` AS `Id_empresa`,
`empresa`.`empresa` AS `Empresa`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Marca` AS `Marca`,
`contratos`.`Departamento` AS `Departamento`,`historico`.`fecha_alta` AS `fecha_alta`,
`historico`.`fecha_baja` AS `fecha_baja`,`historico`.`comentario` AS `comentario`,
`historico`.`tipo_contrato` AS `tipo_contrato`
from (`empresa`
join (`contratos`
join `historico` on((`contratos`.`Id_contrato` = `historico`.`id_contrato`))) on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`))
)
group by `historico`.`Id_historico`,`historico`.`id_afiliado`
order by isnull(`historico`.`fecha_baja`) desc,`historico`.`fecha_baja` desc;
When SELECT the view detalle_full_ok all go right, but if SELECT the view detalle_full_ko go wrong.
View view detalle_full_ok is slow and view detalle_full_ko is fast. The problem is view detalle_full_ko retrieve wrong nombre_empresa field.
Rows:
afiliados(4700)
empresa(800)
contratos(4700)
First pare down the query to get only historico.Id_historico and historico.id_afiliado. That is, avoid any columns and tables you don't need in getting just the desired id pair:
SELECT DISTINCT Id_historico, id_afiliado
FROM ...
-- no GROUP BY or ORDER BY
Then use that as subquery to get the rest of the data:
SELECT lots-of-stuff
FROM ( the above select )
JOIN back to all the tables
WHERE ...
GROUP BY -- probably needed again?
ORDER BY ...
However, the GROUP BY does not include all the non-aggregate values in the SELECT, so it is probably 'improper' and will probably be flagged as an error with ONLY_FULL_GROUP_BY.
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
I am taking over a project. It is very old and the original programmer is long gone. No one has any idea why certain decisions were made.
The following query runs (on my Mac) in 9.5 seconds but if I remove the last JOIN then it drops to 2.5 seconds. What is wrong with that last JOIN?
select `ttl`.`id` AS `id`,
`ttl`.`name` AS `name`,
`ttl`.`updated_at` AS `last_update_on`,
`ttl`.`user_id` AS `list_creator`,
`ttl`.`retailer_nomination_list` AS `nomination_list`,
`ttl`.`created_at` AS `created_on`,
count(distinct `tlb`.`title_id`) AS `title_count`
from `haha_title_lists` `ttl`
left join `haha_title_list_to_users` `tltu` on((`ttl`.`id` = `tltu`.`title_list_id`))
left join `users` `u` on((`tltu`.`user_id` = `u`.`id`))
left join `users` `u2` on((`tltu`.`user_id` = `u2`.`id`))
left join `haha_title_list_to_venues` `tlv` on((`ttl`.`id` = `tlv`.`title_list`))
left join `haha_venue_properties` `tvp` on((`tlv`.`venue_id` = `tvp`.`id`))
join `haha_title_list_to_books` `tlb` on((`ttl`.`id` = `tlb`.`title_list_id`))
join `wawa_title` `ot` on((`tlb`.`title_id` = `ot`.`title_id`))
group by `ttl`.`id`;
The tables:
CREATE TABLE `haha_title_list_to_books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title_id` int(11) NOT NULL,
`title_list_id` int(11) NOT NULL,
`sdk` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`created_at` datetime NOT NULL,
`promo_start_date` date DEFAULT NULL,
`promo_end_date` date DEFAULT NULL,
`promo_price` float DEFAULT NULL,
`confirmations` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nominations` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`title_note` text COLLATE utf8_unicode_ci,
`executed` int(11) DEFAULT NULL,
`event_created` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_promo_start_date` (`promo_start_date`),
KEY `idx_promo_end_date` (`promo_end_date`),
KEY `idx_title_list_to_books_title_id` (`title_id`),
KEY `idx_title_list_to_books_title_list_id` (`title_list_id`)
) ENGINE=MyISAM AUTO_INCREMENT=21847 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
and:
CREATE TABLE `wawa_title` (
`title_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
`title_alpha` varchar(25) NOT NULL,
`display_title` varchar(200) NOT NULL,
`subtitle` text NOT NULL,
`sdk10` varchar(13) DEFAULT '',
`sdk13` varchar(15) DEFAULT NULL,
`primary_sdk13` varchar(15) DEFAULT NULL,
`asin` varchar(10) DEFAULT NULL,
`pub_season` varchar(15) NOT NULL,
`pub_year` varchar(15) NOT NULL,
`bisac1` varchar(15) NOT NULL,
`bisac2` varchar(15) NOT NULL,
`bisac3` varchar(15) NOT NULL,
`barcode` varchar(30) DEFAULT NULL,
`dewey_decimal` varchar(15) NOT NULL,
`lib_of_congress` varchar(15) NOT NULL,
`spanish_language` tinyint(4) NOT NULL,
`target_audience` tinyint(3) unsigned DEFAULT NULL,
`language` varchar(20) DEFAULT NULL,
`edition` varchar(45) DEFAULT NULL,
`pages` int(11) DEFAULT NULL,
`number_in_series` int(11) DEFAULT NULL,
`trimsize` varchar(10) DEFAULT NULL,
`filesize` varchar(10) DEFAULT NULL,
`duration_hours` int(11) DEFAULT NULL,
`duration_minutes` int(11) DEFAULT NULL,
`discs` int(11) DEFAULT NULL,
`download` date DEFAULT NULL,
`size_unit` varchar(15) NOT NULL DEFAULT '',
`digitization_date` date NOT NULL,
`us_on_sale_date` date NOT NULL,
`aus_on_sale_date` date NOT NULL,
`can_on_sale_date` date NOT NULL,
`uk_on_sale_date` date NOT NULL,
`us_list_price` varchar(10) NOT NULL,
`aus_list_price` varchar(10) NOT NULL,
`can_list_price` varchar(10) NOT NULL,
`uk_list_price` varchar(10) NOT NULL,
`isPrimary` varchar(1) DEFAULT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modifier` int(11) NOT NULL,
`activated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`active` varchar(3) NOT NULL DEFAULT 'N',
`flagged_string` text,
`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`assets_id` varchar(20) DEFAULT NULL,
`book_details` text,
`book_keynote` text,
`exclude_goodreads` char(1) NOT NULL DEFAULT 'N',
`series_description` text,
`review_quote1` text,
`territory_id` int(11) DEFAULT '27',
`featured_newsletter_id` tinyint(3) unsigned DEFAULT '0',
`retailer_discovery_check` datetime DEFAULT NULL,
`suppress_retailer_approval` tinyint(1) DEFAULT '0',
`suppress_retailer_approval_reason` varchar(255) DEFAULT NULL,
`ebb_description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`slug` varchar(150) DEFAULT NULL,
`legacy_slug` varchar(150) DEFAULT NULL,
`us_agency_price` varchar(10) DEFAULT NULL,
`firebrand_title_id` int(11) DEFAULT NULL,
`ebb_label` varchar(200) DEFAULT NULL,
`ebb_end_sale_date` date DEFAULT NULL,
`ebb_downprice` decimal(10,2) DEFAULT NULL,
`book_club` varchar(1) DEFAULT NULL,
`best_seller` varchar(1) DEFAULT NULL,
`award_winner` varchar(1) DEFAULT NULL,
`discovery` char(1) NOT NULL DEFAULT 'Y',
`narrator_id` int(11) DEFAULT NULL,
`suppress_series_data` varchar(255) DEFAULT NULL,
PRIMARY KEY (`title_id`),
KEY `active_index` (`active`),
KEY `fk_title_series_id_idx` (`series_id`),
KEY `series_id` (`series_id`),
KEY `idx_title_sdk13` (`sdk13`),
KEY `idx_title_active_isprimary` (`active`,`isPrimary`),
KEY `bisac1` (`bisac1`),
KEY `bisac2` (`bisac2`),
KEY `bisac3` (`bisac3`),
KEY `idx_primary_sdk13` (`primary_sdk13`),
KEY `idx_territory_id` (`territory_id`),
CONSTRAINT `fk_title_series_id` FOREIGN KEY (`series_id`) REFERENCES `wawa_series` (`series_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=19700 DEFAULT CHARSET=utf8 |
If I remove this line:
join `wawa_title` `ot` on((`tlb`.`title_id` = `ot`.`title_id`))
The query speed drops from 9.5 seconds to 2.5 seconds. Not great, but a huge improvement.
And yet, both tables have indexes on table_id, so why would that line be a problem?
I notice that one table is InnoDB and the other is MyISAM. Would that have an effect?
Do not JOIN to tables that you don't use.
A JOIN often "explodes" the number of rows, then a GROUP BY like you have reels in the number of rows. To see this, leave all the JOINs there, but remove the GROUP BY. See how many rows you get.
To avoid part of that explosion, change
count(distinct `tlb`.`title_id`) AS `title_count`
to
( SELECT count(distinct `title_id`)
FROM `haha_title_list_to_books`
WHERE `ttl`.`id` = `title_list_id`
) AS `title_count`
and remove the current JOIN to tlb.
Mixing MyISAM and InnoDB should not have any direct impact on this SELECT. However, you should consider moving all of your tables to InnoDB.
I have this query that doesn't run under odd circumstances. It's doing two joins, I have two indexes of the same name on two different tables (but this shouldn't matter). When I remove one of the indexes from either tables, the query runs fine. Something else that makes it run is removing the order by clause, but I definitely need that. When I run show processlist the state is stuck on "Copying to tmp table". The indexes I'm talking about are channelID.
the query
SELECT twitterTweets.*,
COUNT(twitterRetweets.id) AS retweets,
sTwitter.followers,
sTwitter.date
FROM twitterTweets
LEFT JOIN twitterRetweets
ON twitterTweets.id = twitterTweetsID
JOIN sTwitter
ON DATE(twitterTweets.dateCreated) = sTwitter.date
AND twitterTweets.channelID = sTwitter.channelID
WHERE twitterTweets.channelID = 32
AND type = 'tweet'
AND DATE(dateCreated) >= '2013-12-05'
AND DATE(dateCreated) <= '2014-01-05'
GROUP BY twitterTweets.id
ORDER BY dateCreated
explain results
1 SIMPLE sTwitter ref channelID channelID 5 const 1162 Using where; Using temporary; Using filesort
1 SIMPLE twitterTweets ref channelID channelID 5 const 17456 Using where
1 SIMPLE twitterRetweets ref twitterTweetsID twitterTweetsID 5 social.twitterTweets.id 3
create for the three tables
CREATE TABLE `twitterTweets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`type` enum('tweet','reply','direct in','direct out') COLLATE utf8_unicode_ci DEFAULT 'tweet',
`subType` enum('reply beginning','retweet beginning','reply middle','retweet middle') COLLATE utf8_unicode_ci DEFAULT NULL,
`tweetID` bigint(20) DEFAULT NULL,
`tweet` text COLLATE utf8_unicode_ci,
`replies` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`hash` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`retweet` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`media` text COLLATE utf8_unicode_ci,
`tweetUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`expandedUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`replyToStatus` bigint(20) DEFAULT NULL,
`user` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`followers` int(11) DEFAULT NULL,
`following` int(11) DEFAULT NULL,
`updates` int(11) DEFAULT NULL,
`group1` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`campaign` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`segment` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`destination` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`retweets` smallint(6) DEFAULT '0',
`favorites` smallint(6) DEFAULT NULL,
`dateCreated` datetime DEFAULT NULL,
`dateAdded` datetime DEFAULT NULL,
`dateUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=296264 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `twitterRetweets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`twitterTweetsID` int(11) DEFAULT NULL,
`tweetID` bigint(20) DEFAULT NULL,
`screenName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`followers` int(11) DEFAULT NULL,
`following` int(11) DEFAULT NULL,
`updates` int(11) DEFAULT NULL,
`favorites` smallint(6) DEFAULT NULL,
`dateAdded` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `twitterTweetsID` (`twitterTweetsID`)
) ENGINE=MyISAM AUTO_INCREMENT=93821 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `sTwitter` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`following` int(11) DEFAULT '0',
`followers` int(11) DEFAULT '0',
`updates` int(11) DEFAULT '0',
`date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=35615 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
So how do I make this thing run with both indexes in place? Do I have to ignore one of them?