How can I speed up this query with mysqli? - mysql

I have this query and it take 117 seconds to get 100000 rows can me get all this rows on 5 seconds or 10 seconds?
SELECT
`nas`.`nasname`,
`nas`.`api_username`,
`nas`.`api_password`,
`nas`.`api_port`,
`nas`.`secret`,
`nas`.`up_by`,
COALESCE(`nas`.`api_is_enabled`,'0') as `api_is_en`,
`radacct`.`nasipaddress`,
`radcheck`.`address_list_name`,
`radcheck`.`address_val`,
`radacct`.`framedipaddress`,
`radacct`.`callingstationid`,
`radacct`.`radacctid` as `id_session`,
`radacct`.`framedprotocol`,
`radacct`.`username`,
`radacct`.`last_speed` ,
`radacct`.`acctsessionid` as `acctsessionid`,
`radip`.`value` as `ip_address`,
`radmac`.`value` as `mac_address`,
`radcount`.`value` as `simul_sess`,
COUNT(`radacct`.`radacctid`) as count_login ,
COALESCE(`card_users`.`id`,0) as `id_card`,
COALESCE(`card_users`.`download_qouta`,`userinfo`.`in_down`) *1024*1024 as `down_qouta`,
COALESCE(`card_users`.`upload_qouta`,`userinfo`.`in_up`) *1024*1024 as `up_qouta`,
COALESCE(`card_users`.`all_quota`,`userinfo`.`av_qouta`) *1024*1024 as `av_qouta`,
`card_users`.`date_end_card` as `date_end_card`,
`card_users`.`val_date` as `val_date`,
`card_users`.`per_second` as `per_second`,
`card_users`.`at_the_first_login` as `at_the_first_login`,
`card_users`.`exp_first_login` as `exp_first_login`,
`card_users`.`val_time_exp` as `val_time_exp`,
`card_users`.`time_cards_exp` as `time_cards_exp`,
`card_users`.`exp_quota` as `exp_quota`,
`card_users`.`exp_date` as `exp_date`,
`userinfo`.`updatedate` as `updatedate`,
`userinfo`.`divi_down_speed_slm` as `divi_down_speed_slm`,
`userinfo`.`divi_up_speed_slm` as `divi_up_speed_slm`,
`userinfo`.`arr_days` as `u_arr_days`,
`userinfo`.`value_choice` as `u_value_choice`,
`userinfo`.`last_end_day` as `last_end_day`,
`userinfo`.`macs` as `macs`,
`p`.`profile_name` as `profile_name`,
`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,
`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,
`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,
`p`.`online_time` as `online_time`,
`p`.`hours_min` as `hours_min`,
`p`.`daily_online_time` as `daily_online_time`,
`p`.`daily_hours_min` as `daily_hours_min`,
`p`.`bandwidth_time` as `bandwidth_time`,
`p`.`daily_expire_service` as `daily_expire_service`,
`p`.`qouta_expire_service` as `qouta_expire_service`,
`p`.`profile_expire_service` as `profile_expire_service`,
`p`.`sp_up` as `sp_up`,
`p`.`sp_down` as `sp_down`,
`p`.`ch_day_end` as `ch_day_end`,
`p`.`set_day_end` as `set_day_end`,
`p`.`percent` as `percent`,
`p`.`arr_days` as `p_arr_days`,
`p`.`value_choice` as `p_value_choice`,
`r1`.`value` as `value_exp`,
`p1`.`bandwidth_time` as `band_exp`,
`p1`.`percent` as `percent_exp`,
`r2`.`value` as `val_address_exp_list`,
`r3`.`value` as `value_exp_daily`,
`p3`.`bandwidth_time` as `band_daily_exp`,
`p3`.`percent` as `percent_daily_exp`,
`r4`.`value` as `val_address_exp_daily_list`,
`r5`.`value` as `value_exp_serv`,
`p3`.`bandwidth_time` as `band_serv_exp`,
`p3`.`percent` as `percent_serv_exp`,
`r6`.`value` as `val_address_exp_serv_list`,
`r7`.`value` as `value_now`,
`r8`.`value` as `val_address_list`,
COALESCE(ROUND(time_to_sec(`userinfo`.`online_time`)),0) as `t_online_time` ,
COALESCE(ROUND(time_to_sec(`userinfo`.`daily_online_time`)),0) as `d_online_time`,
UNIX_TIMESTAMP(STR_TO_DATE(`radexp`.`value`, '%d %b %Y %H:%i')) as `exp_user`,
`radacct`.`callingstationid` as `callingstationid`,
UNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) as `session_st_date`
FROM `radacct`
INNER JOIN `radcheck`
ON `radacct`.`username` = `radcheck`.`username`
AND `radcheck`.`attribute` = 'Cleartext-Password'
INNER JOIN `radusergroup` `r9`
ON `r9`.`username` = `radcheck`.`username`
LEFT JOIN `userinfo`
ON `userinfo`.`username` = `r9`.`username`
LEFT JOIN (
SELECT SUM(`radacct`.`acctinputoctets`) as `up_today`
, SUM(`radacct`.`acctoutputoctets`) as `down_today`
, SUM(`radacct`.`acctsessiontime`) as `daily_time`
, `radacct`.`username`
FROM `radacct`
WHERE DATE_FORMAT(STR_TO_DATE(`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
GROUP BY `username`
) as `rad2`
ON `rad2`.`username` = `r9`.`username`
INNER JOIN `nas`
ON `nas`.`nasname`=`radacct`.`nasipaddress`
LEFT JOIN `profiles` `p`
ON `p`.`profile_name` = `r9`.`groupname`
LEFT JOIN `profiles` `p1`
ON `p1`.`id` = `p`.`qouta_expire_service`
LEFT JOIN `profiles` `p2`
ON `p2`.`id` = `p`.`daily_expire_service`
LEFT JOIN `profiles` `p3`
ON `p3`.`id` = `p`.`profile_expire_service`
LEFT JOIN `radgroupreply` `r1`
ON `r1`.`groupname` = `p1`.`profile_name`
AND `r1`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r2`
ON `r2`.`groupname` = `p1`.`profile_name`
AND `r2`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r3`
ON `r3`.`groupname` = `p2`.`profile_name`
AND `r3`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r4`
ON `r4`.`groupname` = `p2`.`profile_name`
AND `r4`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r5`
ON `r5`.`groupname` = `p3`.`profile_name`
AND `r5`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r6`
ON `r6`.`groupname` = `p3`.`profile_name`
AND `r6`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r7`
ON `r7`.`groupname` = `p`.`profile_name`
AND `r7`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r8`
ON `r8`.`groupname` = `p`.`profile_name`
AND `r8`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `card_users`
ON `card_users`.`id` = `radcheck`.`id_card`
LEFT JOIN `radreply` as `radip`
ON `radip`.`username` = `r9`.`username`
AND `radip`.`attribute`='Framed-IP-Address'
LEFT JOIN `radcheck` as `radmac`
ON `radmac`.`username` = `r9`.`username`
AND `radmac`.`attribute`='Calling-Station-Id'
LEFT JOIN `radcheck` as `radcount`
ON `radcount`.`username` = `r9`.`username`
AND `radcount`.`attribute`='Simultaneous-Use'
LEFT JOIN `radcheck` as `radexp`
ON `radexp`.`username` = `r9`.`username`
AND `radexp`.`attribute`='Expiration'
WHERE ( radacct .AcctStopTime IS NULL
OR radacct.AcctStopTime = '0000-00-00 00:00:00')
GROUP BY `radacct`.`username`
When remove this from query it speed up 60%
LEFT JOIN (SELECT SUM(`radacct`.`acctinputoctets`) as `up_today`,
SUM(`radacct`.`acctoutputoctets`) as `down_today`,
SUM(`radacct`.`acctsessiontime`) as `daily_time`,
`radacct`.`username` FROM `radacct`
WHERE DATE_FORMAT(STR_TO_DATE(`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
GROUP BY `username`) as `rad2` ON (`rad2`.`username` = `r9`.`username`)
I have index for all this tables so what is my problem ?
Note : I have pc have
cpu core i 5
ram 4 giga
this image for explain my table

Related

How to use UNION with two selects and some JOINS

I am having trouble executing this sql. I need to get the result and send it via JSON to my web page.
SELECT `produto`.`nome` `nome`, `estoque_movimento`.`data`, `estoque_movimento`.`qtd`, `estoque_movimento`.`motivo`, `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `estoque_movimento`
LEFT JOIN `produto` ON `produto`.`id` = `estoque_movimento`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `estoque_movimento`.`data` >= '2018-03-26 00:00:00'
AND `estoque_movimento`.`data` <= '2018-10-03 23:59:59'
ORDER BY `estoque_movimento`.`data` DESC
UNION
SELECT `produto`.`nome` `nome`, `pedido_produto`.`criado_em`, `pedido_produto`.`qtde`, CONCAT("Alocado no pedido n°: ", pedido_produto.produto_id), `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `pedido_produto`
LEFT JOIN `pedido` ON `pedido`.`id` = `pedido_produto`.`pedido_id`
LEFT JOIN `produto` ON `produto`.`id` = `pedido_produto`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `pedido`.`is_finalizado` = 0
GROUP BY `produto`.`nome`, `pedido_produto`.`produto_id`
I am getting this error:
#1221 - Incorrect usage of UNION and ORDER BY
Use parantheses ( ) around different Select query groups.
Try:
(
SELECT `produto`.`nome` `nome`, `estoque_movimento`.`data`, `estoque_movimento`.`qtd`, `estoque_movimento`.`motivo`, `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `estoque_movimento`
LEFT JOIN `produto` ON `produto`.`id` = `estoque_movimento`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `estoque_movimento`.`data` >= '2018-03-26 00:00:00'
AND `estoque_movimento`.`data` <= '2018-10-03 23:59:59'
ORDER BY `estoque_movimento`.`data` DESC
)
UNION
(
SELECT `produto`.`nome` `nome`, `pedido_produto`.`criado_em`, `pedido_produto`.`qtde`, CONCAT("Alocado no pedido n°: ", pedido_produto.produto_id), `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `pedido_produto`
LEFT JOIN `pedido` ON `pedido`.`id` = `pedido_produto`.`pedido_id`
LEFT JOIN `produto` ON `produto`.`id` = `pedido_produto`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `pedido`.`is_finalizado` = 0
GROUP BY `produto`.`nome`, `pedido_produto`.`produto_id`
)

Improving the Performance of this Stored Procedure in MySQL

I am attempting to optimize this Stored Procedure. Currently, this iteration when done from the very top of the chain, can take about 2.4-2.7 seconds. I would love to get this down to below 1 second if possible.
I have done research on what I can do to optimize this and most of MySQL's own tips do not work. You cannot LOCK tables or use the LOAD DATA statements inside Stored Procedures. When running tests, it appears that the INSERT INTO SELECT statements are taking the most time inside this query.
When I ran one CALL, I got 2.6 seconds with around 13 INSERT INTO statements firing off. At least 3 took 0.43-0.45 seconds to complete. All other statements took very little time to complete.
Here is my current settings inside my 'my.cnf' file:
default-storage-engine = MyISAM
key_buffer_size = 256M
innodb_buffer_pool_size = 3G
innodb_buffer_pool_load_at_startup = ON
innodb_sort_buffer_size = 16M
innodb_flush_method = O_DIRECT
read_buffer_size = 4M
thread_stack = 256K
bulk_insert_buffer_size = 64MB
Here is my Stored Procedure:
BEGIN
DECLARE lastLayer INT;
DECLARE lastInsertCount INT;
DECLARE tempTableRowCount INT;
DROP TABLE IF EXISTS `temp`;
DROP TABLE IF EXISTS `pendingBusiness`;
CREATE TABLE `temp`
(
`layer` INT(5),
`title_c` VARCHAR(255),
`name` VARCHAR(255),
`rep_first_name` VARCHAR(255),
`rep_last_name` VARCHAR(255),
`id_c` CHAR(36),
`contact_status_reps_c` VARCHAR(255),
`phone_mobile` VARCHAR(255),
`contract_type_c` VARCHAR(255),
`email_address` VARCHAR(255)
);
CREATE TABLE `pendingBusiness`
(
`layer` INT(5),
`title_c` VARCHAR(255),
`name` VARCHAR(255),
`rep_first_name` VARCHAR(255),
`rep_last_name` VARCHAR(255),
`id_c` CHAR(36),
`contact_status_reps_c` VARCHAR(255),
`investor_first_name` VARCHAR(255),
`investor_last_name` VARCHAR(255),
`product_detail` VARCHAR(255),
`paperwork_status_c` VARCHAR(255),
`next_step` VARCHAR(255),
`outstanding_requirements_c` VARCHAR(255),
`funding_status_c` VARCHAR(255),
`funding_next_step_c` VARCHAR(255),
`fulfillment_status_c` VARCHAR(255),
`fulfillment_next_step_c` VARCHAR(255),
`amount` VARCHAR(255),
`stage` VARCHAR(255),
`date_entered` DATETIME,
`case_manager` VARCHAR(255),
`lost_sale_date_c` DATETIME,
`compensation_run_c` DATETIME,
`product_id` CHAR(36),
`rep_state` VARCHAR(255),
`investor_state` VARCHAR(255)
);
INSERT INTO `temp`(`layer`, `title_c`, `name`, `rep_first_name`, `rep_last_name`, `id_c`, `contact_status_reps_c`, `phone_mobile`,`contract_type_c`,`email_address`)
SELECT 0,
wn_writing_number_cstm.title_c,
wn_writing_number.`name`,
preps_contracted_reps.first_name,
preps_contracted_reps.last_name,
wn_writing_number_cstm.id_c,
preps_contracted_reps_cstm.contact_status_reps_c,
preps_contracted_reps.phone_mobile,
preps_contracted_reps_cstm.contract_type_c,
email_addresses.email_address
FROM wn_writing_number LEFT OUTER JOIN wn_writing_number_cac_customize_agent_comp_1_c ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing946b_number_ida = wn_writing_number.id
LEFT OUTER JOIN cac_customize_agent_comp ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing3148nt_comp_idb = cac_customize_agent_comp.id
LEFT OUTER JOIN cac_customize_agent_comp_cstm ON cac_customize_agent_comp.id = cac_customize_agent_comp_cstm.id_c
LEFT OUTER JOIN aos_products_cac_customize_agent_comp_1_c ON cac_customize_agent_comp_cstm.id_c = aos_products_cac_customize_agent_comp_1_c.aos_produca2b8nt_comp_idb
LEFT OUTER JOIN preps_contracted_reps_wn_writing_number_1_c ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id
LEFT OUTER JOIN preps_contracted_reps ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
LEFT OUTER JOIN preps_contracted_reps_cstm ON preps_contracted_reps.id = preps_contracted_reps_cstm.id_c
LEFT OUTER JOIN email_addr_bean_rel ON email_addr_bean_rel.bean_id = preps_contracted_reps_cstm.id_c
LEFT OUTER JOIN email_addresses ON email_addr_bean_rel.email_address_id = email_addresses.id
LEFT OUTER JOIN wn_writing_number_cstm ON wn_writing_number_cstm.id_c = wn_writing_number.id
WHERE wn_writing_number_cstm.wn_writing_number_id_c = passId AND wn_writing_number.deleted = '0' AND preps_contracted_reps.deleted = '0' AND wn_writing_number.deleted = '0' AND email_addr_bean_rel.deleted = '0'
GROUP BY wn_writing_number.`name`;
INSERT INTO `pendingBusiness`(`title_c`, `name`, `rep_first_name`, `rep_last_name`, `id_c`, `contact_status_reps_c`, `investor_first_name`, `investor_last_name`, `product_detail`, `paperwork_status_c`, `next_step`, `outstanding_requirements_c`, `funding_status_c`, `funding_next_step_c`, `fulfillment_status_c`, `fulfillment_next_step_c`, `amount`, `stage`, `date_entered`, `case_manager`, `lost_sale_date_c`, `compensation_run_c`, `product_id`, `rep_state`, `investor_state`)
SELECT
wn_writing_number_cstm.title_c,
wn_writing_number.`name`,
preps_contracted_reps.first_name,
preps_contracted_reps.last_name,
wn_writing_number_cstm.id_c,
preps_contracted_reps_cstm.contact_status_reps_c,
contacts.first_name,
contacts.last_name,
aos_products.`name`,
opportunities_cstm.paperwork_status_c,
opportunities.next_step,
opportunities_cstm.outstanding_requirements_c,
opportunities_cstm.funding_status_c,
opportunities_cstm.funding_next_step_c,
opportunities_cstm.fulfillment_status_c,
opportunities_cstm.fulfillment_next_step_c,
opportunities.amount,
opportunities_cstm.stage_c,
opportunities.date_entered,
CONCAT(users.first_name, " ", users.last_name) AS case_manager,
opportunities_cstm.lost_sale_date_c,
opportunities_cstm.compensation_run_c,
aos_products.id,
preps_contracted_reps.primary_address_state,
opportunities_cstm.state_of_residence_c
FROM wn_writing_number
LEFT OUTER JOIN preps_contracted_reps_wn_writing_number_1_c ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id
LEFT OUTER JOIN preps_contracted_reps ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
LEFT OUTER JOIN preps_contracted_reps_cstm ON preps_contracted_reps.id = preps_contracted_reps_cstm.id_c
LEFT OUTER JOIN wn_writing_number_cstm ON wn_writing_number_cstm.id_c = wn_writing_number.id
LEFT OUTER JOIN opportunities_cstm ON opportunities_cstm.wn_writing_number_id_c = wn_writing_number.id
LEFT OUTER JOIN contacts ON opportunities_cstm.contact_id_c = contacts.id
LEFT OUTER JOIN opportunities ON opportunities_cstm.id_c = opportunities.id
LEFT OUTER JOIN aos_products ON opportunities_cstm.aos_products_id_c = aos_products.id
LEFT OUTER JOIN users ON opportunities_cstm.user_id_c = users.id
WHERE wn_writing_number.id = passId AND opportunities.deleted = '0' AND opportunities.date_entered >= DATE_SUB(NOW(),INTERVAL 1.5 YEAR)
GROUP BY opportunities_cstm.id_c;
SET lastLayer := 0;
SET lastInsertCount := 1;
LayerLoop: WHILE lastInsertCount > 0 DO
INSERT INTO `temp`(`layer`, `title_c`, `name`, `rep_first_name`, `rep_last_name`, `id_c`, `contact_status_reps_c`, `phone_mobile`,`contract_type_c`,`email_address`)
SELECT lastLayer + 1,
wn_writing_number_cstm.title_c,
wn_writing_number.`name`,
preps_contracted_reps.first_name,
preps_contracted_reps.last_name,
wn_writing_number_cstm.id_c,
preps_contracted_reps_cstm.contact_status_reps_c,
preps_contracted_reps.phone_mobile,
preps_contracted_reps_cstm.contract_type_c,
email_addresses.email_address
FROM wn_writing_number LEFT OUTER JOIN wn_writing_number_cac_customize_agent_comp_1_c ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing946b_number_ida = wn_writing_number.id
LEFT OUTER JOIN cac_customize_agent_comp ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing3148nt_comp_idb = cac_customize_agent_comp.id
LEFT OUTER JOIN cac_customize_agent_comp_cstm ON cac_customize_agent_comp.id = cac_customize_agent_comp_cstm.id_c
LEFT OUTER JOIN aos_products_cac_customize_agent_comp_1_c ON cac_customize_agent_comp_cstm.id_c = aos_products_cac_customize_agent_comp_1_c.aos_produca2b8nt_comp_idb
LEFT OUTER JOIN preps_contracted_reps_wn_writing_number_1_c ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id
LEFT OUTER JOIN preps_contracted_reps ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
LEFT OUTER JOIN preps_contracted_reps_cstm ON preps_contracted_reps.id = preps_contracted_reps_cstm.id_c
LEFT OUTER JOIN email_addr_bean_rel ON email_addr_bean_rel.bean_id = preps_contracted_reps_cstm.id_c
LEFT OUTER JOIN email_addresses ON email_addr_bean_rel.email_address_id = email_addresses.id
LEFT OUTER JOIN wn_writing_number_cstm ON wn_writing_number_cstm.id_c = wn_writing_number.id
WHERE wn_writing_number_cstm.wn_writing_number_id_c IN (SELECT id_c FROM `temp` WHERE layer = lastLayer) AND wn_writing_number.deleted = '0' AND preps_contracted_reps.deleted = '0' AND wn_writing_number.deleted = '0' AND email_addr_bean_rel.deleted = '0'
GROUP BY wn_writing_number.`name`;
SET lastInsertCount := ROW_COUNT();
INSERT INTO `pendingBusiness`(`layer`, `title_c`, `name`, `rep_first_name`, `rep_last_name`, `id_c`, `contact_status_reps_c`, `investor_first_name`, `investor_last_name`, `product_detail`, `paperwork_status_c`, `next_step`, `outstanding_requirements_c`, `funding_status_c`, `funding_next_step_c`, `fulfillment_status_c`, `fulfillment_next_step_c`, `amount`, `stage`, `date_entered`, `case_manager`, `lost_sale_date_c`, `compensation_run_c`, `product_id`, `rep_state`, `investor_state`)
SELECT lastLayer + 1,
wn_writing_number_cstm.title_c,
wn_writing_number.`name`,
preps_contracted_reps.first_name,
preps_contracted_reps.last_name,
wn_writing_number_cstm.id_c,
preps_contracted_reps_cstm.contact_status_reps_c,
contacts.first_name,
contacts.last_name,
aos_products.`name`,
opportunities_cstm.paperwork_status_c,
opportunities.next_step,
opportunities_cstm.outstanding_requirements_c,
opportunities_cstm.funding_status_c,
opportunities_cstm.funding_next_step_c,
opportunities_cstm.fulfillment_status_c,
opportunities_cstm.fulfillment_next_step_c,
opportunities.amount,
opportunities_cstm.stage_c,
opportunities.date_entered,
CONCAT(users.first_name, " ", users.last_name) AS case_manager,
opportunities_cstm.lost_sale_date_c,
opportunities_cstm.compensation_run_c,
aos_products.id,
preps_contracted_reps.primary_address_state,
opportunities_cstm.state_of_residence_c
FROM wn_writing_number
LEFT OUTER JOIN preps_contracted_reps_wn_writing_number_1_c ON preps_contracted_reps_wn_writing_number_1_c.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = wn_writing_number.id
LEFT OUTER JOIN preps_contracted_reps ON preps_contracted_reps_wn_writing_number_1_c.preps_cont9effed_reps_ida = preps_contracted_reps.id
LEFT OUTER JOIN preps_contracted_reps_cstm ON preps_contracted_reps.id = preps_contracted_reps_cstm.id_c
LEFT OUTER JOIN wn_writing_number_cstm ON wn_writing_number_cstm.id_c = wn_writing_number.id
LEFT OUTER JOIN opportunities_cstm ON opportunities_cstm.wn_writing_number_id_c = wn_writing_number.id
LEFT OUTER JOIN contacts ON opportunities_cstm.contact_id_c = contacts.id
LEFT OUTER JOIN opportunities ON opportunities_cstm.id_c = opportunities.id
LEFT OUTER JOIN aos_products ON opportunities_cstm.aos_products_id_c = aos_products.id
LEFT OUTER JOIN users ON opportunities_cstm.user_id_c = users.id
WHERE opportunities.deleted = '0' AND wn_writing_number_cstm.wn_writing_number_id_c IN (SELECT id_c FROM `temp` WHERE layer = lastLayer) AND opportunities.date_entered >= DATE_SUB(NOW(),INTERVAL 1.5 YEAR)
GROUP BY opportunities_cstm.id_c;
SET lastLayer := lastLayer + 1;
END WHILE LayerLoop;
SELECT `title_c`, `name`, `rep_first_name`, `rep_last_name`, `id_c`, `contact_status_reps_c`, `investor_first_name`, `investor_last_name`, `product_detail`, `paperwork_status_c`, `next_step`, `outstanding_requirements_c`, `funding_status_c`, `funding_next_step_c`, `fulfillment_status_c`, `fulfillment_next_step_c`, `amount`, `stage`, `date_entered`, `case_manager`, `lost_sale_date_c`, `compensation_run_c`, `product_id`, `rep_state`, `investor_state`
FROM `pendingBusiness`
ORDER BY rep_first_name ASC;
DROP TABLE IF EXISTS `pendingBusiness`;
END
FWIW, your query is functionally identical to the following. However, the following has the advantage of being easier to read. That said, your GROUP BY clause will return (potentially) indeterminate results for all but name.
SELECT 0
, k.title_c
, a.name
, g.first_name
, g.last_name
, k.id_c
, h.contact_status_reps_c
, g.phone_mobile
, h.contract_type_c
, j.email_address
FROM wn_writing_number a
JOIN wn_writing_number_cac_customize_agent_comp_1_c b
ON b.wn_writing946b_number_ida = a.id
JOIN cac_customize_agent_comp c
ON c.id = b.wn_writing3148nt_comp_idb
LEFT
JOIN cac_customize_agent_comp_cstm d
ON d.id_c = c.id
LEFT
JOIN aos_products_cac_customize_agent_comp_1_c e
ON e.aos_produca2b8nt_comp_idb = d.id_c
LEFT
JOIN preps_contracted_reps_wn_writing_number_1_c f
ON f.preps_contracted_reps_wn_writing_number_1wn_writing_number_idb = a.id
JOIN preps_contracted_reps g
ON g.id = f.preps_cont9effed_reps_ida
LEFT
JOIN preps_contracted_reps_cstm h
ON h.id_c = g.id
JOIN email_addr_bean_rel i
ON i.bean_id = h.id_c
LEFT
JOIN email_addresses j
ON j.id = i.email_address_id
JOIN wn_writing_number_cstm k
ON k.id_c = a.id
WHERE k.wn_writing_number_id_c = passId
AND a.deleted = 0
AND g.deleted = 0
AND i.deleted = 0
GROUP
BY a.name;

subquery doesn't seem to work

mysql query:
`SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`,` `a.`lastreplied`,`
a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id
and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON
`a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c`
ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN
(select cm.`conversation_id`, max(cm.`message_id`) as `message_id` from
`jos_social_conversations_message_maps` as cm
inner join `jos_social_conversations_message` as bm
on cm.`message_id` = bm.`id`
LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id`
AND `bus`.`target_id` = '602'
WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg
FROM jos_social_conversations_message_maps as maps WHERE a.id = maps.conversation_id AND
maps.isread = 0 AND maps.user_id = '602') AND `cm`.`state` = '1' and `bus`.`id` IS NULL
group by cm.`conversation_id`) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users`
as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
AND bus.`id` IS NULL AND `c`.`state` = '1'
AND this gives error like:
1054 - Unknown column 'a.id' in 'where clause'
subquery from above query as below:
AND(SELECT count(isread) AS newMsg FROM jos_social_conversations_message_maps WHERE conversation_id = 3 AND isread = 0 AND user_id = '602')
Current output:
Expected output:
There should be another column called 'newMsg' displaying total count of 'isread' column which has '0' as value for each id..
Here's my formatted version:
SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON `a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c` ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN (select cm.`conversation_id`, max(cm.`message_id`) as `message_id`
from `jos_social_conversations_message_maps` as cm
inner join `jos_social_conversations_message` as bm on cm.`message_id` = bm.`id`
LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id` AND `bus`.`target_id` = '602'
WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg
FROM jos_social_conversations_message_maps
WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
)
AND `cm`.`state` = '1'
and `bus`.`id` IS NULL
group by cm.`conversation_id`
) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users` as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
AND bus.`id` IS NULL AND `c`.`state` = '1'
Your subquery is part of the WHERE clause, so it will not return another column. Perhaps you are lookig for something like this:
SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`,
(SELECT count(isread)
FROM jos_social_conversations_message_maps
WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
) AS newMsg
FROM `jos_social_conversations` AS `a`
{remainder removed for brevity}

Why MYSQL doesn't use my indexes?

I have table karton00 which holds thousands of records. This makes the queries slow. I would like to use indexes, but even though I set them, they are not being used.
Here is my table. and here is the result of using EXPLAIN.
This is my query:
SELECT '' checkbox,
k00_eszkozid,
k00_eszkoznev,
k01_gyariszam,
k01_leltszam,
prsn_name,
k00_status,
tstikon,
k00_startflow,
tstname,
k00_eszkozid,
k01_gyariszam,
k01_leltszam
FROM karton00
LEFT JOIN karton01
ON k00_eszkozid = k01_eszkozid
AND k01_status = 'A'
LEFT JOIN karton02
ON k00_eszkozid = k02_eszkozid
AND k02_status = 'A'
LEFT JOIN karton04
ON k00_eszkozid = k04_eszkozid
AND k04_status = 'A'
LEFT JOIN karton05
ON k00_eszkozid = k05_eszkozid
AND k05_status = 'A'
LEFT JOIN karton06
ON k00_eszkozid = k06_eszkozid
AND k06_status = 'A'
LEFT JOIN karton08
ON k00_eszkozid = k08_eszkozid
AND k08_status = 'A'
LEFT JOIN karton09
ON k00_eszkozid = k09_eszkozid
AND k09_status = 'A'
LEFT JOIN telephely et
ON k06_telephelyid = et.telepid
LEFT JOIN ktghely00 ek
ON k02_ktghid = ek.ktghid
LEFT JOIN person00 us
ON k06_userid = prsn_id
LEFT JOIN ktghely00 fk
ON prsn_ktgh = fk.ktghid
LEFT JOIN telephely ft
ON prsn_telep = ft.telepid
LEFT JOIN tamstatus
ON k02_tamstatusid = tstid
LEFT JOIN szakleltar
ON k02_szakleltarid = szleltid
LEFT JOIN tamszerv
ON k02_tamszervid = tszid
LEFT JOIN ktghely01
ON k02_vgazdaid = vgid
AND vgstatus = 'A'
LEFT JOIN ktghely00 vk
ON vgktghid = vk.ktghid
LEFT JOIN szallitok m
ON k05_mincegid = m.szallid
LEFT JOIN szallitok s
ON k04_szallitoid = s.szallid
LEFT JOIN dctnry00 ym
ON k05_minosit = ym.dctnryname
AND ym.dctnrygrp = 'YESNO'
LEFT JOIN dctnry00 yd
ON k05_check = yd.dctnryname
AND yd.dctnrygrp = 'YESNO'
WHERE k00_status IN ( 'A', 'H' )
AND ( k02_vgazdaid IN ( 7 ) )
AND NOT EXISTS (SELECT *
FROM eszkozkizar
WHERE ez_flid = 0
AND ez_emintaid = k00_eszkozmintaid
AND k00_uzembedatum < ( Date_add(Curdate(),
INTERVAL ez_honap month) )
AND k00_uzembedatum != ''
AND k02_vgazdaid NOT IN ( 7 ))
AND NOT EXISTS(SELECT *
FROM eszkozkizar
WHERE ez_flid = 0
AND ez_emintaid = k00_eszkozmintaid
AND k02_tamszervid = ez_tamszerv
AND k02_tamstatusid = ez_tamsts
AND k00_uzembedatum < ( Date_add(Curdate(),
INTERVAL ez_honap month) )
AND k00_uzembedatum != ''
AND k02_vgazdaid NOT IN ( 7 ))
ORDER BY k00_eszkoznev ASC;
Also you can use USE INDEX (index_list) statement: http://dev.mysql.com/doc/refman/5.5/en/index-hints.html

Suggestions on complex sql statement

SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags_relations` AS `tr` ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
WHERE (v.video_id IN (
SELECT gc2.video_id
FROM video_categories gc2
WHERE gc2.genre_id IN(44)
GROUP BY gc2.video_id
HAVING COUNT(gc2.video_id)=1))
AND (v.video_id IN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1))
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
Query takes 0.4s~ to run. It's because of subquery in WHERE statement:
v.video_id IN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1
)
Without it, it runs 0.07s~
Maybe try something with advanced joins? shy
Thanks ;)
In MySQL, never use WHERE in (SELECT ... it is notoriously slow.
Use a join instead:
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM video AS v
INNER JOIN video_categories AS gc ON gc.video_id = v.video_id
AND gc2.genre_id = '44'
LEFT JOIN video_genres AS gg ON gg.genre_id = gc.genre_id
LEFT JOIN tags_relations AS tr ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN tags AS tg ON tg.tag_id = tr.tag_id
INNER JOIN tags_relations tr2 ON (tr2.parent_id = v.video_id
AND tr2.tag_id = 14 AND tr2.department=18
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags_relations` AS `tr` ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
JOIN (
SELECT gc2.video_id
FROM video_categories gc2
WHERE gc2.genre_id IN(44)
GROUP BY gc2.video_id
HAVING COUNT(gc2.video_id)=1
) vc ON v.video_id = vc.video_id
JOIN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1
) tr ON v.video_id = tr.parent_id
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
Edit:
I took a closer look at your query and think it can be reduced to the following:
SELECT `v1`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM (
SELECT v.*
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `tags_relations` AS `tr` ON tr.parent_id = v.video_id
WHERE gc.genre_id IN(44)
AND tr.tag_id IN(14)
AND tr.department=18
GROUP BY `v`.`video_id`
HAVING COUNT(*) = 1
) v1
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
GROUP BY v1.video_id
ORDER BY v1.video_id DESC