Get number products on stock - mysql

I have very difficult query and I need obtain parameters (parameterValue_id) and number products, which has such parameter, on stock. I would like count only that products which are comply with other criterias.
SELECT `parameters`.`parameter_from`, `parametervalues`.*, (SELECT IF (sumOfProductIN - sumOfProductOUT > 0, COUNT(productparametervalues.f_product_id), 0) FROM productparametervalues`
LEFT JOIN `products` ON products.product_id = productparametervalues.f_product_id
LEFT JOIN `categories` ON categories.category_id = products.f_category_id
LEFT JOIN (SELECT oi1.f_product_id, SUM(oi1.orderItem_numberUnits) AS sumOfProductIN
FROM orderitems oi1 WHERE oi1.orderItem_type = 'IN' GROUP BY oi1.f_product_id) AS `t` ON t.f_product_id = products.product_id
LEFT JOIN (SELECT oi2.f_product_id, SUM(oi2.orderItem_numberUnits) AS sumOfProductOUT
FROM orderitems oi2 WHERE oi2.orderItem_type = 'OUT' GROUP BY oi2.f_product_id) AS `t_2` ON t_2.f_product_id = products.product_id
LEFT JOIN `parametervalues` ON parametervalues.f_parameter_id = parameters.parameter_id
WHERE (category_lft >= '8') AND (category_rgt <= '19') AND (products.product_price >= '1') AND (products.product_price <= '2500') AND (products.product_enabled = 1)
WHERE (parameter_id = '4') AND (parameters.parameter_enabled = 1) AND (parametervalues.parameterValue_enabled = 1)
GROUP BY `parametervalues`.`parameterValue_id`) AS countProducts FROM `parameters`
Edit
table categories
CREATE TABLE IF NOT EXISTS `categories` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_title` varchar(255) DEFAULT NULL,
`category_description` text,
`f_parent_id` int(11) DEFAULT NULL,
`category_lft` smallint(6) DEFAULT NULL,
`category_rgt` smallint(6) DEFAULT NULL,
`category_depth` smallint(6) DEFAULT NULL,
`category_enabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
table orderitems
CREATE TABLE IF NOT EXISTS `orderitems` (
`orderItem_id` int(11) NOT NULL AUTO_INCREMENT,
`f_order_id` int(11) DEFAULT NULL,
`f_product_id` int(11) DEFAULT NULL,
`orderItem_title` varchar(50) DEFAULT NULL,
`orderItem_numberUnits` smallint(6) NOT NULL DEFAULT '1',
`orderItem_pricePerUnit` decimal(18,5) NOT NULL DEFAULT '0.00000',
`orderItem_price` float(18,5) NOT NULL DEFAULT '0.00000',
`orderItem_type` varchar(10) DEFAULT NULL,
PRIMARY KEY (`orderItem_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=95 ;
table parameters
CREATE TABLE IF NOT EXISTS `parameters` (
`parameter_id` int(11) NOT NULL AUTO_INCREMENT,
`f_parameterGroup_id` int(11) DEFAULT NULL,
`parameter_title` varchar(100) DEFAULT NULL,
`parameter_enabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`parameter_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
table parametervalues
CREATE TABLE IF NOT EXISTS `parametervalues` (
`parameterValue_id` int(11) NOT NULL AUTO_INCREMENT,
`f_parameter_id` int(11) NOT NULL,
`parameterValue_title` varchar(100) NOT NULL,
`parameterValue_enabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`parameterValue_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;
table productparametervalues
CREATE TABLE IF NOT EXISTS `productparametervalues` (
`f_product_id` int(11) NOT NULL AUTO_INCREMENT,
`f_parameter_id` int(11) NOT NULL DEFAULT '0',
`f_parameterValue_id` int(11) NOT NULL DEFAULT '0',
`productParameter_value` varchar(100) DEFAULT NULL,
PRIMARY KEY (`f_product_id`,`f_parameter_id`,`f_parameterValue_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=41 ;
table products
CREATE TABLE IF NOT EXISTS `products` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`f_category_id` int(11) DEFAULT NULL,
`f_brand_id` int(11) DEFAULT NULL,
`product_title` varchar(100) DEFAULT NULL,
`product_description` text,
`product_price` float(10,2) DEFAULT NULL,
`product_enabled` tinyint(1) DEFAULT '0',
PRIMARY KEY (`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=43 ;

Related

Combining Multiple SELECT and CREATE queries in MySQL

In a PHP file I have multiple queries because I work with multiple tables, and with the data I get a and display such data. These queries could be merged into one? If anyone knows how I want to tell me what the code is like:
SELECT * FROM compras ;
SELECT * FROM clientes WHERE idcliente = '$row[idcliente]'
SELECT * FROM pedidos_ventas WHERE idcompra = '$row[idcompra]'
SELECT * FROM mercaderias WHERE idmerc = '$rowped[idmercaderia]' LIMIT 1
CREATE TABLE IF NOT EXISTS `clientes` (
`idcliente` INT(10) NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(50) NOT NULL,
`apellido` VARCHAR(50) NOT NULL,
`domicilio` VARCHAR(50) NOT NULL,
`telefono` VARCHAR(50) DEFAULT NULL,
`movil` VARCHAR(50) DEFAULT NULL,
`dni` VARCHAR(10) NOT NULL,
`familiar` VARCHAR(50) NOT NULL,
PRIMARY KEY (`idcliente`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `compras` (
`idcompra` INT(7) NOT NULL AUTO_INCREMENT,
`idcliente` INT(7) NOT NULL,
`observacion` text NOT NULL,
`fecha_ingreso` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`idcompra`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `entregas_ventas` (
`identrega` INT(11) NOT NULL AUTO_INCREMENT,
`idcompra` INT(11) NOT NULL,
`entrega` DOUBLE(7,2) NOT NULL,
`fecha` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`identrega`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `mercaderias` (
`idmerc` INT(7) NOT NULL AUTO_INCREMENT,
`cantidad` INT(5) NOT NULL,
`codigo` VARCHAR(20) NOT NULL,
`producto` VARCHAR(100) NOT NULL,
`preciofinal` DOUBLE(7,2) NOT NULL,
PRIMARY KEY (`idmerc`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `pedidos_ventas` (
`idpedido` INT(7) NOT NULL AUTO_INCREMENT,
`idcompra` INT(7) NOT NULL,
`idmercaderia` INT(7) NOT NULL,
`kilo` DOUBLE(7,2) NOT NULL,
`precio` DOUBLE(7,2) NOT NULL,
PRIMARY KEY (`idpedido`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
select compras.idcompra, case when clientes.idcliente is null
then 'ninguno' else CONCAT(clientes.nombre, ',', clientes.apellido)
end as nombre, case when pedidos_ventas.idmercaderia = '-1' then 'Vianda saludable' when pedidos_ventas.idmercaderia = '-2' then 'Comida Sana' else mercaderias.producto end as mercaderia, pedidos_ventas.kilo, pedidos_ventas.precio from compras
left join clientes on (compras.idcliente=clientes.idcliente)
left join pedidos_ventas on (pedidos_ventas.idcompra=compras.idcompra)
left join mercaderias on (pedidos_ventas.idmercaderia=mercaderias.idmerc)
order by compras.idcompra asc
If now I would like to use the LIKE to see if it matches a value that I enter in an input $ _POST ['search'], either by customer name, vianda(lunch-box), food, or any merchandise ?? Try to do this and it does not work
SELECT compras.idcompra, CASE WHEN clientes.idcliente IS NULL
THEN 'ninguno' ELSE CONCAT(clientes.nombre, ',', clientes.apellido)
END AS nombre, CASE WHEN pedidos_ventas.idmercaderia = '-1' THEN 'Vianda saludable' WHEN pedidos_ventas.idmercaderia = '-2' THEN 'Comida Sana' ELSE mercaderias.producto END AS mercaderia, pedidos_ventas.kilo, pedidos_ventas.precio FROM compras
LEFT JOIN clientes ON (compras.idcliente=clientes.idcliente)
LEFT JOIN pedidos_ventas ON (pedidos_ventas.idcompra=compras.idcompra)
LEFT JOIN mercaderias ON (pedidos_ventas.idmercaderia=mercaderias.idmerc) WHERE mercaderias.producto LIKE '%comida%' OR..... LIKE clientes... or LIKE ..
ORDER BY compras.idcompra ASC
For a query with your current schema, try the following:
SELECT * FROM compras
INNER JOIN clientes ON (compras.idcliente = clientes.idcliente)
INNER JOIN pedidos_ventas ON (compras.id = pedidos_venta.id)
INNER JOIN mercaderias ON (pedidos_ventas.idmercaderia = mercaderias.idmerc)
WHERE clientes.idcliente = :clienteid
AND compras.idcompra = :compraid
AND mercaderias.idmerc = :mercaderiaid
IDEAL DATABASE DESIGN
Try adding foreign keys to your schema:
CREATE TABLE IF NOT EXISTS `cliente` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(50) NOT NULL,
`apellido` VARCHAR(50) NOT NULL,
`domicilio` VARCHAR(50) NOT NULL,
`telefono` VARCHAR(50) DEFAULT NULL,
`movil` VARCHAR(50) DEFAULT NULL,
`dni` VARCHAR(10) NOT NULL,
`familiar` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `mercaderia` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`cantidad` INT(5) NOT NULL,
`codigo` VARCHAR(20) NOT NULL,
`producto` VARCHAR(100) NOT NULL,
`preciofinal` DOUBLE(7,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `compra` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`cliente_id` INT(10) NOT NULL,
`observacion` text NOT NULL,
`fecha_ingreso` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
CONSTRAINT `fk_compra_cliente`
FOREIGN KEY (`cliente_id`)
REFERENCES `cliente` (`id`)
ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `entregas_venta` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`compra_id` INT(10) NOT NULL,
`entrega` DOUBLE(7,2) NOT NULL,
`fecha` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
CONSTRAINT `fk_entregas_venta_compra`
FOREIGN KEY (`compra_id`)
REFERENCES `compra` (`id`)
ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `pedidos_venta` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`compra_id` INT(10) NOT NULL,
`mercaderia_id` INT(10) NOT NULL,
`kilo` DOUBLE(7,2) NOT NULL,
`precio` DOUBLE(7,2) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_pedidos_venta_compra`
FOREIGN KEY (`compra_id`)
REFERENCES `compra` (`id`)
ON DELETE NO ACTION,
CONSTRAINT `fk_pedidos_venta_mercaderia`
FOREIGN KEY (`mercaderia_id`)
REFERENCES `mercaderia` (`id`)
ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And then use the query:
$stmt = $pdo->prepare(<<<SQL
SELECT * FROM compra
INNER JOIN cliente ON (compra.cliente_id = cliente.id)
INNER JOIN pedidos_venta ON (compra.id = pedidos_venta.id)
INNER JOIN mercaderia ON (pedidos_venta.mercaderia_id = mercaderia.id)
WHERE cliente.id = :clienteid
AND compra.id = :compraid
AND mercaderia.id = :mercaderiaid
SQL
);
$stmt->execute(':clienteid' => $clienteid, ':compraid' => $compraid, ':mercaderiaid' => $mercaderiaid);

How to optimize this heavy MySQL query?

I need to optimize a MySQL query which takes a lot of time to load.
Here it is :
SELECT
p.id,
UNIX_TIMESTAMP(p.last_answer_date) AS last_answer_date_timestamp,
p.sender_id,
p.recipient_id,
p.is_read_sender,
p.last_answer_user_id,
p.is_read_recipient,
(SELECT m.read FROM pm_message m WHERE m.conv_id = p.id AND m.user_id != $user_id ORDER BY m.date DESC LIMIT 1) AS read_status,
(SELECT m.content FROM pm_message m WHERE m.conv_id = p.id ORDER BY m.date DESC LIMIT 1) AS last_message,
(SELECT u.username FROM user u WHERE (u.id = p.sender_id OR u.id = p.recipient_id) AND u.id != $user_id LIMIT 1) AS from_username,
(SELECT u.id FROM user u WHERE (u.id = p.sender_id OR u.id = p.recipient_id) AND u.id != $user_id LIMIT 1) AS from_userid,
(SELECT ui.gender FROM user_info ui WHERE (ui.user_id = p.sender_id OR ui.user_id = p.recipient_id) AND ui.user_id != $user_id LIMIT 1) AS from_gender,
(SELECT ph.thumb_url FROM photo ph, user_info ui WHERE ui.main_photo = ph.id AND (ph.user_id = p.sender_id OR ph.user_id = p.recipient_id) AND ph.user_id != $user_id LIMIT 1) AS from_thumb_url
FROM pm_conv p
WHERE p.sender_id = $user_id OR p.recipient_id = $user_id
ORDER BY p.last_answer_date DESC LIMIT 25;
This query gets me the result I want but it's really slow... And I think that the nested selects is the reason why this query is so slow.
Here are the tables structures for this query :
CREATE TABLE IF NOT EXISTS `photo` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(255) DEFAULT NULL,
`thumb_url` varchar(255) DEFAULT NULL,
`user_id` int(11) NOT NULL,
`date` datetime NOT NULL,
`status` int(11) NOT NULL,
`votes` int(11) DEFAULT '0',
`comments` int(11) DEFAULT '0',
`views` int(11) DEFAULT '0',
`text` text,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `pm_conv` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`sender_id` int(11) NOT NULL,
`recipient_id` int(11) NOT NULL,
`last_answer_date` datetime NOT NULL,
`nb_messages` int(11) NOT NULL,
`is_read_sender` int(11) NOT NULL,
`is_read_recipient` int(11) NOT NULL DEFAULT '0',
`last_answer_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `recipient_id` (`recipient_id`),
KEY `sender_id` (`sender_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `pm_message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`content` text NOT NULL,
`user_id` int(11) NOT NULL,
`conv_id` int(11) NOT NULL,
`read` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `conv_id` (`conv_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`encrypt_id` varchar(255) DEFAULT NULL,
`register_date` datetime DEFAULT NULL,
`last_login_date` datetime DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`banned` int(11) DEFAULT NULL,
`banned_reason` text,
`first_step_form` int(11) DEFAULT '0',
`status` int(11) DEFAULT NULL,
`valid_snapchat` int(11) DEFAULT '0',
`introduced_forum` int(11) DEFAULT '0',
`referer` varchar(255) DEFAULT NULL,
`allow_social_featuring` int(11) DEFAULT NULL,
`rank` int(11) DEFAULT NULL,
`fb_id` bigint(20) DEFAULT NULL,
`rate_app_status` int(11) DEFAULT NULL,
`last_activity_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `user_info` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`gender` int(11) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`about` text,
`main_photo` int(11) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`relation_type` varchar(30) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`fb_link` varchar(255) DEFAULT NULL,
`twitter_link` varchar(255) DEFAULT NULL,
`youtube_link` varchar(255) DEFAULT NULL,
`instagram_link` varchar(255) DEFAULT NULL,
`app_pref_forum` int(11) DEFAULT NULL,
`app_pref_pm` int(11) DEFAULT NULL,
`app_pref_snapchat_request` int(11) DEFAULT NULL,
`browse_invisibly` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `main_photo` (`main_photo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Can someone help me to optimize this heavy query?
Thanks!
you can see in the explain plan some tables are being accessed by inefficient indexes. try to calculate statistics on all the tables to see if it changes something (using analyze table).
You can join user table in order to get username and id at once instead of having two subqueries, probably you can do the same with pm_message, but it's a little trickier since subqueries have different conditions.
I would also combine user and user_info tables into, as I can see they have one-to-one relation, so it doesn't makes sense to store this data in different tables. This would allow you to get rid off 4th subquery and simplify the 5th one.
In some cases it is better to perform several queries instead of one with subqueries.

Spped up MySQL query

I need help speeding up a MySQL query that's running extremely slowly. It's taking over 35 seconds to return 900 rows.
Does anyone have ideas how I can speed things up on this query?
Many thanks in advance
select products.*,
p.price as lowest_price,
products_images.thumbnail
from products
inner join products_categories on products_categories.product_id = products.id
inner join products_colours on products_colours.product_id = products.id
inner join products_quantity_pricing on products_quantity_pricing.product_id = products.id
left join ( select min(price) as price, product_id from products_quantity_pricing group by products_quantity_pricing.product_id ) as p on p.product_id = products.id
inner join products_images on products_images.product_id = products.id
where products.id > 0 group by products.id
order by products.product_name
Here is the setup of the tables involved:
CREATE TABLE IF NOT EXISTS `products_categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` smallint(5) unsigned zerofill NOT NULL,
`category` int(11) NOT NULL,
`sub_category` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1016 ;
CREATE TABLE IF NOT EXISTS `products` (
`product_prefix` varchar(10) NOT NULL,
`id` smallint(5) unsigned zerofill NOT NULL AUTO_INCREMENT,
`supplier_id` int(11) NOT NULL,
`product_code` varchar(50) NOT NULL,
`supplier_product_code` mediumtext NOT NULL,
`product_name` varchar(200) NOT NULL,
`product_description` text NOT NULL,
`print_area` varchar(100) DEFAULT NULL,
`print_position` varchar(100) DEFAULT NULL,
`dimensions` varchar(100) DEFAULT NULL,
`origination` tinytext,
`unit_cost` decimal(9,2) NOT NULL,
`updated` datetime NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `product_code` (`product_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=901 ;
CREATE TABLE IF NOT EXISTS `products_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` smallint(5) unsigned zerofill NOT NULL,
`fullsize` varchar(255) NOT NULL,
`midsize` varchar(255) NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`updated` datetime NOT NULL,
`colour_tag` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2402 ;
CREATE TABLE IF NOT EXISTS `products_colours` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` smallint(5) unsigned zerofill NOT NULL,
`colour` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2546 ;
CREATE TABLE IF NOT EXISTS `products_quantity_pricing` (
`product_id` smallint(5) unsigned zerofill NOT NULL,
`quantity` smallint(6) NOT NULL,
`price` decimal(9,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Add indexes on the product_id columns in all the tables.
In the products_quantity_pricing, a composite index on (product_id, price) will also speed up finding the minimum price for each product. If you create this composite index, you don't need to create a separate index just on product_id; the prefix of a composite index also serves as an index of its own.

Updating table A.type = B.type WHERE A.id = B.id

I have two tables in different databases:
In the database named CRMALPHA:
CREATE TABLE IF NOT EXISTS `contacts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`accountId` int(11) NOT NULL,
`Type` int(11) NOT NULL,
`fName` varchar(255) NOT NULL,
`lName` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`workPhone` int(11) NOT NULL,
`workPhoneExt` int(11) NOT NULL,
`cellPhone` int(11) NOT NULL,
`altPhone` int(11) NOT NULL,
`altPhoneDescription` varchar(255) NOT NULL,
`dob` date NOT NULL,
`createdDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`createdById` int(11) NOT NULL,
`notes` varchar(255) NOT NULL,
`isDeleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `accountId` (`accountId`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12006 ;
In the DB named scottse1_lifestyle_test
CREATE TABLE IF NOT EXISTS `tbl_customers_contact_types` (
`ContactId` int(4) NOT NULL DEFAULT '0',
`TypeId` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`ContactId`,`TypeId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I need to:
UPDATE crmalpha.contacts
SET type = scottse1_lifestyle_test.tbl_customers_contact_types.TypeID
WHERE scottse1_lifestyle_test.tbl_customers_contact_types.ContactId = crmalpha.contacts.id
This causes the following error:
#1054 - Unknown column 'scottse1_lifestyle_test.tbl_customers_contact_types.ContactId' in 'where clause'
What am I doing wrong?
EDIT
Solution was:
UPDATE crmalpha.contacts c
JOIN scottse1_lifestyle_test.tbl_customers_contact_types t
ON t.ContactId = c.id
SET c.type = t.TypeId
You have to select the table in commaseperated way:
UPDATE CRMALPHA.contacts, scottse1_lifestyle_test.tbl_customers_contact_types
SET Type = scottse1_lifestyle_test.tbl_customers_contact_types.TypeID
WHERE scottse1_lifestyle_test.tbl_customers_contact_types.ContactId = CRMALPHA.contacts.id

MySQL - Slow Multiple subquery & GROUP BY

When running the following query using GROUP BY it takes way too much time.
SELECT specialities.id AS ID_DEPARTMENT,
specialities.name AS ID_DEPARTMENT_NAME,
agenda.idagenda AS ID_SERVICE,
agenda.name AS ID_SERVICE_NAME,
supervisor.clients_waiting AS CWaiting,
IFNULL(supervisor.clients_resent_waiting_area, 0) AS CWaiting_Resent_Area,
supervisor.clients_attending AS CAttending,
supervisor.clients_attended AS CAttended,
(SELECT SUM(TIME_TO_SEC(TIMEDIFF(NOW(), time_waiting)) / CWaiting)
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS TME,
(SELECT SUM(TIME_TO_SEC(TIMEDIFF(NOW(), time_attending)) / CAttending)
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS TMA,
(SELECT TIME_TO_SEC(MAX(TIMEDIFF(NOW(), time_waiting)))
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS MTE,
(SELECT TIME_TO_SEC(MAX(TIMEDIFF(NOW(), time_attending)))
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS MTA,
supervisor.tme_accumulated AS TME_ACCUMULATED,
supervisor.tma_accumulated AS TMA_ACCUMULATED
FROM supervisor, supervisor_time_data, agenda, specialities
WHERE supervisor.id_service = agenda.id
AND supervisor_time_data.id_service = supervisor.id_service
AND agenda.idspeciality = specialities.id
AND supervisor.booked_or_sequential = 0
AND supervisor.id_service IN (1,2,3)
GROUP BY supervisor.id_service
ORDER BY agenda.name ASC;
It takes over 3 seconds, when commenting the GROUP BY line it takes 8 ms.
Any ideas on how I can optimise this query?
Thanks
EDIT:
CREATE TABLE `supervisor` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`id_department` int(6) DEFAULT NULL,
`id_service` int(9) DEFAULT NULL,
`clients_waiting` int(6) DEFAULT '0',
`clients_attending` int(6) DEFAULT '0',
`clients_attended` int(6) DEFAULT '0',
`tma_accumulated` int(9) DEFAULT '0',
`tme_accumulated` int(9) DEFAULT '0',
`clients_resent_waiting_area` int(6) DEFAULT NULL,
`booked_or_sequential` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_service` (`id_service`),
KEY `booked_or_sequential` (`booked_or_sequential`),
KEY `clients_waiting` (`clients_waiting`)
) ENGINE=MyISAM AUTO_INCREMENT=172 DEFAULT CHARSET=latin1;
.
CREATE TABLE `supervisor_time_data` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`id_ogs` int(32) DEFAULT NULL,
`booked_or_sequential` tinyint(1) DEFAULT NULL,
`time_waiting` datetime DEFAULT NULL,
`time_attending` datetime DEFAULT NULL,
`status` int(2) DEFAULT '0',
`id_service` int(6) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_service` (`id_service`),
KEY `time_waiting` (`time_waiting`),
KEY `time_attending` (`time_attending`),
KEY `booked_or_sequential` (`booked_or_sequential`)
) ENGINE=MyISAM AUTO_INCREMENT=2281 DEFAULT CHARSET=latin1;
.
CREATE TABLE `agenda` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`idagenda` varchar(255) DEFAULT NULL,
`iduser` int(3) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`agendatype` tinyint(4) DEFAULT NULL,
`idspeciality` int(6) DEFAULT NULL,
`denomination` varchar(255) DEFAULT NULL,
`ticket_count` int(3) DEFAULT NULL,
`waiting` int(3) DEFAULT NULL,
`ticket_start` int(3) DEFAULT NULL,
`ticket_end` int(3) DEFAULT NULL,
`ticket_letter` varchar(12) DEFAULT NULL,
`idcenter` int(9) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idagenda` (`idagenda`),
KEY `idspeciality` (`idspeciality`)
) ENGINE=MyISAM AUTO_INCREMENT=2228 DEFAULT CHARSET=latin1;
.
CREATE TABLE `specialities` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`idwaitingarea` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=149 DEFAULT CHARSET=latin1;
Here is your queries with joins. Post your schema if this does not work.
SELECT
specialities.id AS ID_DEPARTMENT,
specialities.name AS ID_DEPARTMENT_NAME,
agenda.idagenda AS ID_SERVICE,
agenda.name AS ID_SERVICE_NAME,
supervisor.clients_waiting AS CWaiting,
IFNULL(supervisor.clients_resent_waiting_area, 0) AS CWaiting_Resent_Area,
supervisor.clients_attending AS CAttending,
supervisor.clients_attended AS CAttended,
SUM(TIME_TO_SEC(TIMEDIFF(NOW(), supervisor_time_data.time_waiting)) / supervisor.clients_waiting) AS TME,
SUM(TIME_TO_SEC(TIMEDIFF(NOW(), supervisor_time_data.time_attending)) / supervisor.clients_attending) AS TMA,
TIME_TO_SEC(MAX(TIMEDIFF(NOW(), supervisor_time_data.time_waiting))) AS MTE,
TIME_TO_SEC(MAX(TIMEDIFF(NOW(), supervisor_time_data.time_attending))) AS MTA,
supervisor.tme_accumulated AS TME_ACCUMULATED,
supervisor.tma_accumulated AS TMA_ACCUMULATED
FROM supervisor
LEFT JOIN supervisor_time_data
ON supervisor_time_data.id_service = supervisor.id_service
LEFT JOIN agenda
ON supervisor.id_service = agenda.id
LEFT JOIN specialities
ON agenda.idspeciality = specialities.id
WHERE supervisor.booked_or_sequential = 0
AND supervisor.id_service IN(1,2,3)
GROUP BY supervisor.id_service
ORDER BY agenda.name ASC;