MySQL Query Take much time for 2600 records - mysql

Table Structures:
CREATE TABLE `ebay_items` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`item_id` bigint(20) NOT NULL,
`item_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`main_image_low` text COLLATE utf8mb4_unicode_ci NOT NULL,
`main_image_mid` text COLLATE utf8mb4_unicode_ci NOT NULL,
`main_image_max` text COLLATE utf8mb4_unicode_ci NOT NULL,
`price` decimal(10,2) NOT NULL,
`price_with_sign` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`shipping_price` decimal(10,2) NOT NULL,
`shipping_with_sign` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`quantity` int(11) NOT NULL,
`brand` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`model` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`upc` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`variation_array` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`image_array` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`category_array` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`step_number` int(11) NOT NULL,
`archived` int(11) NOT NULL,
`archived_number` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5765 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `amazon_items` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`asin` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`related_ebay_item_id` bigint(20) NOT NULL,
`item_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`main_image_low` text COLLATE utf8mb4_unicode_ci NOT NULL,
`main_image_mid` text COLLATE utf8mb4_unicode_ci NOT NULL,
`main_image_max` text COLLATE utf8mb4_unicode_ci NOT NULL,
`buy_box_price` decimal(10,2) NOT NULL,
`buy_box_price_with_sign` text COLLATE utf8mb4_unicode_ci NOT NULL,
`lowest_other_sellers_price` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`other_number_of_seller` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`variation_array` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`category_array` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`step_number` int(11) NOT NULL,
`archived` int(11) NOT NULL,
`archived_number` int(11) NOT NULL,
`status` int(11) NOT NULL DEFAULT '0',
`user_id` int(11) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7398 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Create Table
CREATE TABLE `item_attributes` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`amazon_id` bigint(20) NOT NULL,
`sku` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`quantity` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`currency` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`list_price` decimal(10,2) DEFAULT NULL,
`min_price` decimal(10,2) DEFAULT NULL,
`max_price` decimal(10,2) DEFAULT NULL,
`estimated_profit` decimal(10,2) DEFAULT NULL,
`added_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`refresh_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
`status` smallint(6) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `import_items_logs` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`ebay_id` int(11) NOT NULL,
`amazon_id` int(11) NOT NULL,
`tag` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`status` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6002 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SELECT ei.id
, ei.item_id
, ei.item_url
, ei.title
, ei.main_image_low
, ei.price
, ei.price_with_sign
, ei.shipping_price
, ei.shipping_with_sign
, ei.quantity
, ei.brand
, ei.model
, ei.upc
, ei.step_number
, ei.archived
, ei.archived_number
, ei.user_id
, ei.created_at
, ei.updated_at
, ai.id amazon_id
, ai.asin
, ai.item_url amazon_item_url
, ai.title amazon_title
, ai.main_image_low amazon_low_image
, ai.buy_box_price_with_sign
, ai.lowest_other_sellers_price
, ai.other_number_of_seller
, ai.category_array
, ai.status amazon_status
, ia.sku
, ia.list_price
, ia.min_price
, ia.max_price
, ia.estimated_profit
, ia.status attr_status
, ia.added_time attr_added_time
, ia.refresh_time attr_refresh_time
, l.status import_status
, l.tag import_tag
FROM ebay_items ei
JOIN amazon_items ai
ON ai.related_ebay_item_id = ei.id
LEFT
JOIN item_attributes ia
ON ia.amazon_id = ai.id
LEFT
JOIN import_items_logs l
ON l.ebay_id = ei.id
WHERE ei.user_id = 1
AND ei.archived =2
GROUP
BY ei.id
I'm trying to get item and additional details and Those are in separated tables
There are 4 tables
ebay_items, amazon_items, item_attributes, import_items_logs
This what i want as result,
ebay_items and amazon_items must be matched (ebay_items.id =
amazon_items.related_ebay_item_id)
also item_attributes (amazon_items.id = item_attributes.amazon_id)
and import_items_logs (ebay_items.id = import_items_logs.ebay_id)
records need if any records exist
This is my query when i ran on local it's takes 50+sec or amazon server it's take 12sec to finish. What's wrong im doing here. Total records is 2600. i don't know why it's too slow
Query Explain
id select_type table type possible_keys key key_len ref rows Extra
------ ----------- ------ ------ ------------- ------- ------- --------------------------------------- ------ --------------------------------------------------------
1 SIMPLE ai ALL (NULL) (NULL) (NULL) (NULL) 4277 Using temporary; Using filesort
1 SIMPLE ei eq_ref PRIMARY PRIMARY 8 other_rp-tool-2.ai.related_ebay_item_id 1 Using where
1 SIMPLE ia ALL (NULL) (NULL) (NULL) (NULL) 3134 Using where; Using join buffer (flat, BNL join)
1 SIMPLE l ALL (NULL) (NULL) (NULL) (NULL) 6076 Using where; Using join buffer (incremental, BNL join) ```

Related

Mysql query is taking so much time when trying to fetch data based on institutes using subquery

I have a question listing page where I want to sort the data with the following for each question
Requirement 1
Total Question attempted
Total Passed students
Passed students percentage
Requirement 2
Total Question attempted (for particular institute, Institute id is 20 in our case which we get by logged in user's Institute)
Total Passed students (for particular institute, Institute id is 20 in our case which we get by logged in user's Institute)
Passed students percentage (for particular institute, Institute id is 20 in our case which we get by logged in user's Institute)
I have achieved "Requirement 1" but for the "Requirement 2" which is very similar to "Requirement 1" I forced to run 4 subqueries to find out below:
total_students_facility
passed_students_facility
percentage_facility
Which is making the query very slow for 9M records. I wonder if I can achieve "Requirement 2" also with join and correct indexing.
Also I run two subquery again to calculate percentage as we cannot use ALIAS in calculation it shows unknown column. As we can just use ALIAS in "group by", "having", and "order by" any solution will be helpful.
CREATE TABLE IF NOT EXISTS `mdl_question` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`category` bigint(20) NOT NULL DEFAULT 0,
`parent` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`name` text COLLATE utf8mb4_unicode_ci NOT NULL,
`questiontext` text COLLATE utf8mb4_unicode_ci NOT NULL,
`questiontextformat` tinyint(4) NOT NULL DEFAULT 0,
`generalfeedback` text COLLATE utf8mb4_unicode_ci NOT NULL,
`generalfeedbackformat` tinyint(4) NOT NULL DEFAULT 0,
`defaultmark` decimal(12,7) NOT NULL DEFAULT 1.0000000,
`penalty` decimal(12,7) NOT NULL DEFAULT 0.3333333,
`qtype` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '''1''',
`length` bigint(20) UNSIGNED NOT NULL DEFAULT 1,
`stamp` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`version` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`hidden` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
`timecreated` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`timemodified` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`createdby` bigint(20) UNSIGNED DEFAULT NULL,
`modifiedby` bigint(20) UNSIGNED DEFAULT NULL,
`type_data_id` bigint(20) NOT NULL,
`img_id` bigint(20) DEFAULT NULL,
`qimg_gallary_text` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`qrimg_gallary_text` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`qimg_gallary_ids` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`qrimg_gallary_ids` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`case_id` bigint(20) NOT NULL DEFAULT 0,
`ques_type_id` bigint(20) DEFAULT NULL,
`year` bigint(20) DEFAULT NULL,
`spec` bigint(20) DEFAULT NULL,
`sub_speciality_id` int(11) DEFAULT NULL,
`sub_sub_speciality_id` int(11) DEFAULT NULL,
`spec_level` bigint(20) DEFAULT 1,
`is_deleted` int(11) NOT NULL DEFAULT 0,
`sequence` int(11) NOT NULL DEFAULT 0,
`sort_order` bigint(20) NOT NULL DEFAULT 0 COMMENT 'Question order in list',
`idnumber` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`addendum` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`text_for_search` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'this is for the text based searching, this will store the text of the question without html tags',
`text_for_search_ans` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `type_data_id` (`type_data_id`),
UNIQUE KEY `mdl_ques_catidn_uix` (`category`,`idnumber`),
KEY `mdl_ques_cat_ix` (`category`),
KEY `mdl_ques_par_ix` (`parent`),
KEY `mdl_ques_cre_ix` (`createdby`),
KEY `mdl_ques_mod_ix` (`modifiedby`),
KEY `id` (`id`),
KEY `mq_spec_ix` (`spec`),
KEY `sort_order` (`sort_order`),
KEY `idx2` (`is_deleted`,`sort_order`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='The questions themselves';
CREATE TABLE IF NOT EXISTS `mdl_question_attempts` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`questionusageid` bigint(20) UNSIGNED NOT NULL,
`slot` bigint(20) UNSIGNED NOT NULL,
`behaviour` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`questionid` bigint(20) UNSIGNED NOT NULL,
`variant` bigint(20) UNSIGNED NOT NULL DEFAULT 1,
`maxmark` decimal(12,7) NOT NULL,
`minfraction` decimal(12,7) NOT NULL,
`flagged` tinyint(3) UNSIGNED NOT NULL DEFAULT 2,
`questionsummary` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`rightanswer` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`responsesummary` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`timemodified` bigint(20) UNSIGNED NOT NULL,
`maxfraction` decimal(12,7) DEFAULT 1.0000000,
`in_remind_state` int(11) NOT NULL DEFAULT 0,
`is_correct` tinyint(1) DEFAULT 1,
`institution_id` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `mdl_quesatte_queslo_uix` (`questionusageid`,`slot`),
KEY `mdl_quesatte_que2_ix` (`questionusageid`),
KEY `is_correct` (`is_correct`),
KEY `idx1` (`questionid`,`is_correct`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Each row here corresponds to an attempt at one question, as ';
COMMIT;
CREATE TABLE IF NOT EXISTS `mdl_quiz_attempts` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`uniqueid` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`quiz` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`userid` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`attempt` mediumint(9) NOT NULL DEFAULT 0,
`sumgrades` decimal(10,5) DEFAULT NULL,
`timestart` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`timefinish` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`timemodified` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`layout` text COLLATE utf8mb4_unicode_ci NOT NULL,
`preview` smallint(5) UNSIGNED NOT NULL DEFAULT 0,
`needsupgradetonewqe` smallint(5) UNSIGNED NOT NULL DEFAULT 0,
`comments` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`is_deleted` int(11) NOT NULL DEFAULT 0,
`groupname` text CHARACTER SET utf8mb3 DEFAULT NULL,
`pageid` int(11) DEFAULT 0,
`currentpage` bigint(20) DEFAULT 0,
`state` varchar(16) COLLATE utf8mb4_unicode_ci DEFAULT 'inprogress',
`timemodifiedoffline` bigint(20) NOT NULL DEFAULT 0,
`timecheckstate` bigint(20) DEFAULT 0,
`retake` int(11) NOT NULL DEFAULT 0,
`is_on_going` int(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `mdl_quizatte_uni_uix` (`uniqueid`),
KEY `mdl_quizatte_use_ix` (`userid`),
KEY `mdl_quizatte_qui_ix` (`quiz`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `mdl_user` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`auth` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'manual',
`confirmed` tinyint(1) NOT NULL DEFAULT 0,
`policyagreed` tinyint(1) NOT NULL DEFAULT 0,
`deleted` tinyint(1) NOT NULL DEFAULT 0,
`suspended` tinyint(1) NOT NULL DEFAULT 0,
`mnethostid` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`username` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`password` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`idnumber` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`firstname` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`lastname` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`recommend_email` tinyint(1) NOT NULL DEFAULT 0,
`emailstop` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
`icq` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`skype` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`yahoo` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`aim` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`msn` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`phone1` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`phone2` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`institution` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`department` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`address` varchar(70) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`city` varchar(120) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`country` varchar(2) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`lang` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'en',
`theme` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`timezone` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '99',
`firstaccess` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`lastaccess` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`lastlogin` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`currentlogin` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`lastip` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`secret` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`picture` tinyint(1) NOT NULL DEFAULT 0,
`url` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`description` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`descriptionformat` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
`mailformat` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
`maildigest` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
`maildisplay` tinyint(3) UNSIGNED NOT NULL DEFAULT 2,
`htmleditor` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
`ajax` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
`autosubscribe` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
`trackforums` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
`timecreated` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`timemodified` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`trustbitmask` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`imagealt` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`screenreader` tinyint(1) NOT NULL DEFAULT 0,
`furlastname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`furfirstname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`grade` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`group_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`jobtitle` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL,
`comment` text COLLATE utf8mb4_unicode_ci NOT NULL,
`actstatus` enum('1','0') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`slearncount` int(11) NOT NULL DEFAULT 0,
`agrrement_status` tinyint(1) NOT NULL DEFAULT 0,
`excludesure` int(11) DEFAULT 0,
`calendartype` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'gregorian',
`lastnamephonetic` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`firstnamephonetic` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`middlename` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`alternatename` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`version` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reviewertype` smallint(6) DEFAULT 0,
PRIMARY KEY (`id`),
KEY `mdl_user_del_ix` (`deleted`),
KEY `mdl_user_con_ix` (`confirmed`),
KEY `mdl_user_fir_ix` (`firstname`),
KEY `mdl_user_las_ix` (`lastname`),
KEY `mdl_user_cit_ix` (`city`),
KEY `mdl_user_cou_ix` (`country`),
KEY `mdl_user_las2_ix` (`lastaccess`),
KEY `mdl_user_ema_ix` (`email`),
KEY `mdl_user_aut_ix` (`auth`),
KEY `mdl_user_idn_ix` (`idnumber`),
KEY `mdl_user_fir2_ix` (`firstnamephonetic`),
KEY `mdl_user_las3_ix` (`lastnamephonetic`),
KEY `mdl_user_mid_ix` (`middlename`),
KEY `mdl_user_alt_ix` (`alternatename`),
KEY `mdl_user_institution_ix` (`institution`),
KEY `actstatus` (`actstatus`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='One record for each person';
CREATE TABLE IF NOT EXISTS `mdl_my_ques_type_data` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`question_id` bigint(20) DEFAULT NULL,
`ques_pref` int(11) DEFAULT NULL,
`ques_case` bigint(20) DEFAULT NULL,
`ques_type_id` bigint(20) DEFAULT NULL,
`year` bigint(20) DEFAULT NULL,
`spec` bigint(20) DEFAULT NULL,
`subspec` bigint(20) DEFAULT NULL,
`subsubspec` bigint(20) DEFAULT NULL,
`spec_level` bigint(20) NOT NULL,
`is_deleted` tinyint(4) NOT NULL DEFAULT 0,
`institute_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `question_id` (`question_id`),
KEY `id` (`id`),
KEY `ques_type_id` (`ques_type_id`),
KEY `ques_case` (`ques_case`),
KEY `spec` (`spec`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;
CREATE TABLE IF NOT EXISTS `mdl_my_ques_attributes` (
`attrib_id` int(11) NOT NULL AUTO_INCREMENT,
`question` bigint(20) NOT NULL,
`related_question` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`keywords` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`explanation` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ext_link` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ext_link_txt` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`link_type` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
`diff_id` int(11) DEFAULT NULL,
`shuffle` int(11) DEFAULT NULL,
`ownerid` int(11) DEFAULT NULL,
`creator_id` int(11) DEFAULT NULL,
`date_created` bigint(20) DEFAULT NULL,
`last_modified_id` int(11) DEFAULT NULL,
`last_modified_date` bigint(20) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`insti_id` bigint(20) NOT NULL DEFAULT 0,
`ques_type` int(11) NOT NULL,
`no_ques` bigint(20) NOT NULL DEFAULT 0,
`is_deleted` tinyint(4) NOT NULL DEFAULT 0,
`review_id` int(11) NOT NULL DEFAULT 1,
`land_id` int(11) NOT NULL DEFAULT 1,
`ques_label` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`exclude_self_learning` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
`text_for_search` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'this is for the text based searching, this will store the text of the question without html tags',
PRIMARY KEY (`attrib_id`),
KEY `attrib_id` (`attrib_id`),
KEY `question` (`question`),
KEY `diff_id` (`diff_id`),
KEY `mqa_ques_type_ix` (`ques_type`),
KEY `mqa_insti_id_ix` (`insti_id`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `specilities` (
`sp_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`percentage` float NOT NULL DEFAULT 0,
`color` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`order` bigint(20) NOT NULL DEFAULT 0,
`is_deleted` int(11) NOT NULL DEFAULT 0,
`institute_id` int(11) NOT NULL DEFAULT 0,
`sp_level` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`sp_id`),
KEY `sp_id` (`sp_id`),
KEY `mq_spec_parent_ix` (`parent_id`),
KEY `specilities_institute_id` (`institute_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
select
mq.id, mq.ques_type_id, mq.qtype, mq.name, mq.qimg_gallary_ids,
qtd.question_id, qtd.year, qtd.spec, qtd.subspec, qtd.subsubspec,
qa.status, qa.review_id, qa.diff_id, qa.land_id, qa.insti_id, qa.exclude_self_learning,
COUNT(mqa.questionid) AS total_students,
SUM(CASE WHEN is_correct = 1 then 1 else 0 end) passed_students,
round((SUM(CASE WHEN is_correct = 1 then 1 else 0 end) / COUNT(mqa.questionid) * 100 ),2) AS percentage,
(SELECT COUNT(mqas1.questionid)
FROM mdl_question_attempts mqas1
LEFT JOIN mdl_quiz_attempts mquizas1 ON mqas1.questionusageid=mquizas1.uniqueid
LEFT JOIN mdl_user us1 ON mquizas1.userid=us1.id
WHERE us1.institution=20 AND mqas1.questionid=mq.id) AS total_students_facility,
(SELECT COUNT(mqas1.questionid)
FROM mdl_question_attempts mqas1
LEFT JOIN mdl_quiz_attempts mquizas1 ON mqas1.questionusageid=mquizas1.uniqueid
LEFT JOIN mdl_user us1 ON mquizas1.userid=us1.id
WHERE us1.institution=20 AND mqas1.questionid=mq.id AND mqas1.is_correct=1) AS passed_students_facility,
round(((SELECT COUNT(mqas1.questionid)
FROM mdl_question_attempts mqas1
LEFT JOIN mdl_quiz_attempts mquizas1 ON mqas1.questionusageid=mquizas1.uniqueid
LEFT JOIN mdl_user us1 ON mquizas1.userid=us1.id
WHERE us1.institution=20 AND mqas1.questionid=mq.id AND mqas1.is_correct=1) / (SELECT COUNT(mqas1.questionid)
FROM mdl_question_attempts mqas1
LEFT JOIN mdl_quiz_attempts mquizas1 ON mqas1.questionusageid=mquizas1.uniqueid
LEFT JOIN mdl_user us1 ON mquizas1.userid=us1.id
WHERE us1.institution=20 AND mqas1.questionid=mq.id) * 100 ),2) AS percentage_facility
from mdl_question mq
LEFT JOIN mdl_question_attempts mqa ON mq.id = mqa.questionid
left join mdl_my_ques_type_data qtd on (qtd.question_id=mq.id)
left join mdl_my_ques_attributes qa on (qa.question=mq.id)
left join specilities as s on(s.sp_id=qtd.spec)
where NOT mq.is_deleted
and ((qa.status = 1) OR (qa.insti_id = 20 and qa.status = 0))
GROUP BY mq.id
ORDER by mq.sort_order desc, mq.id DESC
LIMIT 0,50;
Sorting (or ordering by) should not be the problem.
Can you take a look at this:
explain select
mq.id, mq.ques_type_id, mq.qtype, mq.name, mq.qimg_gallary_ids,
qtd.question_id, qtd.year, qtd.spec, qtd.subspec, qtd.subsubspec,
qa.status, qa.review_id, qa.diff_id, qa.land_id, qa.insti_id, qa.exclude_self_learning,
COUNT(mqa.questionid) AS total_students,
SUM(CASE WHEN is_correct = 1 then 1 else 0 end) passed_students,
round((SUM(CASE WHEN is_correct = 1 then 1 else 0 end) / COUNT(mqa.questionid) * 100 ),2) AS percentage,
sq.total_students_facility,
sq.passed_students_facility,
ROUND((sq.passed_students_facility / sq.total_students_facility * 100 ),2) AS percentage_facility
from mdl_question mq
LEFT JOIN mdl_question_attempts mqa ON mq.id = mqa.questionid
left join mdl_my_ques_type_data qtd on (qtd.question_id=mq.id)
left join mdl_my_ques_attributes qa on (qa.question=mq.id)
left join specilities as s on(s.sp_id=qtd.spec)
left join (
SELECT
COUNT(mqas1.questionid) total_students_facility,
SUM(CASE WHEN mqas1.is_correct=1 THEN 1 END) passed_students_facility
FROM mdl_question_attempts mqas1
LEFT JOIN mdl_quiz_attempts mquizas1 ON mqas1.questionusageid=mquizas1.uniqueid
LEFT JOIN mdl_user us1 ON mquizas1.userid=us1.id
WHERE us1.institution=20
GROUP BY mqas1.questionid
) as sq on questionid=mq.id
where NOT mq.is_deleted
and ((qa.status = 1) OR (qa.insti_id = 20 and qa.status = 0))
GROUP BY mq.id
ORDER by mq.sort_order desc, mq.id DESC
LIMIT 0,50;
The subqueries are moved to a LEFT JOIN, and the explain plan is looking quicker. (But I cannot test, because no data is available)
see: DBFIDDLE
Start by doing the percent in a single query.
( SELECT FORMAT(100 * SUM(mqas1.is_correct=1) / COUNT(*), 2)
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE us1.institution=20
AND mqas1.questionid=mq.id
) AS percentage_facility
For clarity, use INNER JOIN unless you really need LEFT.
Can
GROUP BY mq.id
ORDER by mq.sort_order desc, mq.id DESC
be turned into
GROUP by mq.sort_order , mq.id
ORDER by mq.sort_order desc, mq.id desc
If it can, then it avoids a temp table and a sort.
Having columns from two different tables in a single WHERE makes optimization quite difficult.
Can qa.status be anything other than 0 or 1?
BIGINT takes 8 bytes each. There are much smaller sizes. For 9M rows, the savings adds up.
When you have a UNIQUE, consider using last the PRIMARY KEY and avoiding the id altogether.
You will probably never search on middlename, so why have an index? Or consider throwing several text columns into a single FULLTEXT-indexed column.

how to optimize the below query without adding index

the below query has some problems so can we able to optimize the query with the current index format cuz the table size is huge so I don't know what to do.
Query:
select
`sale_amount`,
`provider_transaction_id`,
`id` as `acc_id`,
`status`,
`service_id`,
`provider_status`,
`is_sale_settled`,
CASE WHEN status = 1
and is_sale_settled = 1 then 4 WHEN status = 1
and is_sale_settled = 0 then 3 ELSE 5 END AS acc_ics_status,
`system_transaction_id`,
`engine_transaction_id`,
`transaction_date`,
`created_at` as `transaction_date_time`,
HOUR(created_at) as transaction_hour
from
`st_ret_txn_aeps`
where
`created_at` >= '2022-10-03 14:45:01'
and `created_at` <= '2022-10-03 14:48:00'
and `service_id` = 14
and `transaction_date` = '2022-10-03'
Row scans;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: st_ret_txn_aeps
partitions: NULL
type: index_merge
possible_keys: transaction_id_sevrice_id_unique,st_ret_txn_aeps_service_id_index,idx_txn_date
key: idx_txn_date,st_ret_txn_aeps_service_id_index
key_len: 3,1
ref: NULL
rows: 122328
filtered: 11.11
Extra: Using intersect(idx_txn_date,st_ret_txn_aeps_service_id_index); Using where
1 row in set, 1 warning (0.00 sec)
Table structure;
*************************** 1. row ***************************
Table: st_ret_txn_aeps
Create Table: CREATE TABLE `st_ret_txn_aeps` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`service_id` tinyint(4) NOT NULL,
`transaction_type_id` tinyint(4) NOT NULL,
`business_org_id` int(10) unsigned NOT NULL,
`sub_agent_id` int(11) DEFAULT NULL,
`agent_id` int(10) unsigned DEFAULT NULL,
`business_org_user_id` int(10) unsigned NOT NULL,
`business_org_sub_user_id` int(10) unsigned DEFAULT NULL,
`retailer_category_code` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`rate_profile_id` int(11) DEFAULT '0',
`wallet_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`business_org_pan` varchar(12) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`business_org_gstin` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`operator_id` int(10) unsigned NOT NULL,
`operator_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`product_id` int(11) DEFAULT NULL,
`sub_product_id` int(11) DEFAULT NULL,
`provider_id` int(10) unsigned NOT NULL,
`provider_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`branch_id` int(11) NOT NULL DEFAULT '0',
`branch_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sale_amount` double NOT NULL,
`total_b_org_charges` double NOT NULL,
`total_b_org_comm_tds` double DEFAULT '0',
`total_b_org_commission` double NOT NULL,
`final_debit_value` double NOT NULL,
`balance_after_txn` double NOT NULL,
`system_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`engine_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`provider_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`operator_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_1` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_2` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_3` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_4` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_5` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`business_org_markup` double NOT NULL DEFAULT '0',
`refund_date` date DEFAULT NULL,
`remark` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`is_invoice_data_generated` tinyint(1) NOT NULL,
`status` tinyint(4) NOT NULL COMMENT '1: success ,2: failed',
`provider_status` tinyint(4) DEFAULT NULL,
`provider_status_old` tinyint(4) DEFAULT NULL,
`transaction_date` date NOT NULL,
`customer_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_mobile_no` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`app_type` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '1:WEBPOS 2:ANDROID',
`is_cashout_sale` tinyint(1) NOT NULL,
`is_sale_settled` tinyint(1) NOT NULL,
`settlement_date` date DEFAULT NULL,
`analytics_sync_status` tinyint(1) DEFAULT NULL,
`analytics_sync_status_refund` tinyint(1) DEFAULT NULL,
`invoice_data_id` int(11) DEFAULT NULL,
`credit_note_data_id` int(11) DEFAULT NULL,
`status_updated_at` bigint(20) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `transaction_id_sevrice_id_unique` (`service_id`,`engine_transaction_id`),
UNIQUE KEY `system_transaction_id_unique` (`system_transaction_id`),
KEY `st_ret_txn_aeps_service_id_index` (`service_id`),
KEY `st_ret_txn_aeps_business_org_id_index` (`business_org_id`),
KEY `st_ret_txn_aeps_agent_id_index` (`agent_id`),
KEY `st_ret_txn_aeps_business_org_user_id_index` (`business_org_user_id`),
KEY `st_ret_txn_aeps_system_transaction_id_index` (`system_transaction_id`),
KEY `st_ret_txn_aeps_refund_date_index` (`refund_date`),
KEY `idx_txn_date` (`transaction_date`),
KEY `st_ret_txn_aeps_settlement_date_index` (`settlement_date`),
KEY `status_updated_at_idx` (`status_updated_at`)
) ENGINE=InnoDB AUTO_INCREMENT=122756253 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
yeah I know if we create an index for the created_at column means the problem will solve does this have any other way to optimize?
Well, you could delete all the data, then the query would be very quick (just kidding)!
Honestly, you need to create the index to optimize this query. The best index for this query would be a compound index on (service_id, transaction_date, created_at) in that order.
There are at least three ways to create the index without much downtime:
pt-online-schema-change
gh-ost
Create the index on a replica database, then when the index is ready, switch the app to use the replica
At my last job we used pt-online-schema-change. While I worked there we executed over 75,000 ALTER TABLEs in production during peak hours, without downtime. Some of the tables were larger than yours.

multiple joins SQL query optimization

I have the following query :
SELECT
COUNT(DISTINCT a0_.id) AS sclr0
FROM
account a0_
INNER JOIN customer c1_ ON (c1_.account = a0_.id)
LEFT JOIN sf_user_data s2_ ON (s2_.user_id = a0_.id)
LEFT JOIN address a3_ ON (c1_.customer_address = a3_.id)
WHERE
a3_.city IS NOT NULL
resulting in the following output :
sclr0
+--------+
298279
with the following EXPLAIN :
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
+--+---------------+-------+-----------+-------+--------------------------------------------+-----------------------+-----------+--------------------------------+-------+-----------+-------+
1 SIMPLE c1_ NULL ALL UNIQ_81398E097D3656A4,UNIQ_81398E091193CB3F NULL NULL NULL 405508 100.00 NULL
1 SIMPLE a0_ NULL eq_ref PRIMARY PRIMARY 8 evoportail.c1_.account 1 100.00 Using index
1 SIMPLE s2_ NULL eq_ref UNIQ_E904BFD1A76ED395 UNIQ_E904BFD1A76ED395 8 evoportail.c1_.account 1 100.00 Using index
1 SIMPLE a3_ NULL eq_ref PRIMARY PRIMARY 8 evoportail.c1_.customer_address 1 90.00 Using where
approximative number of rows in the tables :
account : 430000
customer: 430000
sf_user_data : 115000
address : 550000
Right now, this query is running in 3 seconds. Is there any way to speed it up ?
the CREATE statements :
CREATE TABLE `account` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`identifier` varchar(255) collate utf8_bin NOT NULL,
`hash` varchar(255) collate utf8_bin default NULL,
`date_create` datetime default NULL,
`group` varchar(50) collate utf8_bin default NULL,
`sub_group` varchar(50) collate utf8_bin NOT NULL default 'NULL',
`date_last_action` datetime default NULL,
`date_last_connection` datetime default NULL,
`connection_counter` int(10) unsigned default NULL,
`connection_since_customer` int(10) unsigned NOT NULL default '0',
`salt` varchar(255) collate utf8_bin default NULL,
`roles` longtext collate utf8_bin COMMENT '(DC2Type:array)',
`is_v3` tinyint(1) NOT NULL default '1',
`password_token` varchar(255) collate utf8_bin default NULL,
`password_token_expired_at` datetime default NULL,
`is_included_in_newsletters` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `identifier_UNIQUE` (`identifier`)
) ENGINE=MyISAM AUTO_INCREMENT=434243 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `address` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`city` varchar(64) collate utf8_bin default NULL,
`street` varchar(255) collate utf8_bin default NULL,
`complement` varchar(128) collate utf8_bin default NULL,
`zipcode` varchar(16) collate utf8_bin default NULL,
`country_id` int(11) default NULL,
`cedex` tinyint(1) NOT NULL default '0',
`abroad` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `IDX_D4E6F81F92F3E70` (`country_id`)
) ENGINE=MyISAM AUTO_INCREMENT=541873 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `customer` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`account` bigint(20) unsigned NOT NULL,
`source` varchar(250) collate utf8_bin NOT NULL default 'DECLARATION',
`last_source` varchar(250) collate utf8_bin NOT NULL,
`source_domain_name` varchar(255) collate utf8_bin default NULL,
`subscription_offer` varchar(255) collate utf8_bin default NULL,
`formalities_center_address` bigint(20) unsigned default NULL,
`customer_address` bigint(20) unsigned default NULL,
`business_address` bigint(20) unsigned default NULL,
`shipping_address` bigint(20) default NULL,
`activity` text collate utf8_bin,
`state` enum('NONE','CREATE','UPDATE','COMPLETE') collate utf8_bin NOT NULL default 'NONE',
`num_dossier` varchar(255) collate utf8_bin default NULL,
`email` varchar(255) collate utf8_bin NOT NULL,
`lastname` varchar(255) collate utf8_bin NOT NULL,
`firstname` varchar(255) collate utf8_bin NOT NULL,
`sexe` int(1) NOT NULL default '0',
`activityset` int(1) NOT NULL default '0',
`phone` varchar(16) collate utf8_bin NOT NULL,
`business_name` varchar(255) collate utf8_bin default NULL,
`payment_method` enum('NONE','CB_OK','CB_KO','CHEQUE_OK','CHEQUE_KO','WAITING','IMPACTPLUS_OK','PRELEV') collate utf8_bin default 'NONE',
`payment_waiting_comment` text collate utf8_bin,
`sub_sent_recovery` smallint(6) default '0',
`transaction_number` varchar(30) collate utf8_bin default NULL,
`transaction_date` datetime default NULL,
`properties` set('ACCRE','CFE','WANT_WEBSITE','HAVE_WEBSITE','NEWSLETTER','SUBSCRIBE','OLD_CUSTOMER') collate utf8_bin default NULL,
`comments` text collate utf8_bin,
`activity_declaration` varchar(512) collate utf8_bin default NULL COMMENT 'file:///',
`cfe_center` varchar(255) collate utf8_bin default NULL,
`date_create` datetime default NULL,
`date_complete` datetime default NULL,
`date_subscribe` datetime default NULL,
`date_next_payement` datetime default NULL,
`date_ae_subscribe` datetime default NULL,
`siret` varchar(128) collate utf8_bin default NULL,
`current_quotation` bigint(20) unsigned default NULL,
`current_invoice` bigint(20) unsigned default NULL,
`has_create_quotation` tinyint(1) NOT NULL default '0',
`has_create_invoice` tinyint(1) NOT NULL default '0',
`created_by` int(20) NOT NULL default '0',
`updated_by` int(11) NOT NULL default '0',
`updated_date` datetime default NULL,
`abo_running` tinyint(1) NOT NULL default '1',
`show_bn` tinyint(1) NOT NULL default '1',
`taxe_type_activite` enum('NULL','ACHAT','SERVICE','BOTH') collate utf8_bin default NULL,
`taxe_categorie_activite` enum('NULL','COMMERCIALE','ARTISANALE','CIPAV','RSI') collate utf8_bin default NULL,
`taxe_liberatoire` enum('NULL','OUI','NON') collate utf8_bin default NULL,
`taxe_statut_accre` enum('NULL','OUI','NON','DK') collate utf8_bin default NULL,
`know` varchar(50) collate utf8_bin default NULL,
`sms_relance` int(11) default NULL,
`fdae` int(1) NOT NULL default '0',
`display_fdae` tinyint(1) NOT NULL default '0',
`show_dispense_immat` enum('RCS','RM','RSAC') collate utf8_bin default NULL,
`show_dispense_immat_city` varchar(255) collate utf8_bin default NULL,
`subscription_fdae` date default NULL,
`nbsocial` varchar(30) collate utf8_bin default NULL,
`atclic` int(1) NOT NULL default '0',
`merassurance` int(1) NOT NULL default '0',
`dossier_canceled` tinyint(1) NOT NULL default '0',
`dossier_canceled_date` datetime default NULL,
`tva_intra` varchar(20) collate utf8_bin default NULL,
`site_url` varchar(100) collate utf8_bin default NULL,
`freeguide` tinyint(1) NOT NULL default '0',
`hiscox` int(1) NOT NULL default '0',
`assurland` int(1) NOT NULL default '0',
`unpaid_advisor` int(11) default NULL,
`unpaid_date` datetime default NULL,
`ecl_send` tinyint(1) default NULL,
`ecl_date` datetime default NULL,
`birthdateyear` int(11) NOT NULL default '0',
`quaCategorie` varchar(500) collate utf8_bin default NULL,
`quaNature` varchar(500) collate utf8_bin default NULL,
`quaType` varchar(500) collate utf8_bin default NULL,
`april` int(1) NOT NULL default '0',
`date_guide` datetime default NULL,
`no_pub` tinyint(1) NOT NULL default '0',
`campaign_manual` tinyint(1) NOT NULL default '0',
`export_matmut` date default NULL,
`formality_date` datetime default NULL,
`call_count_commerciaux` int(11) default '0',
`call_count_assistance` int(11) default '0',
`call_last` datetime default NULL,
`prospect` tinyint(1) default NULL,
`gender_id` int(11) default NULL,
`birthdate` date default NULL,
`current_customer_source_history_id` int(11) default NULL,
`original_customer_source_history_id` int(11) default NULL,
`bounce` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_81398E097D3656A4` (`account`),
UNIQUE KEY `UNIQ_81398E09E7927C74` (`email`),
UNIQUE KEY `UNIQ_81398E091193CB3F` (`customer_address`),
UNIQUE KEY `UNIQ_81398E09507DD4CC` (`business_address`),
UNIQUE KEY `UNIQ_81398E09BA38C653` (`formalities_center_address`),
KEY `num_dossier` (`num_dossier`),
KEY `sub_send_recovery` (`sub_sent_recovery`),
KEY `dossier_canceled` (`dossier_canceled`),
KEY `freeguide` (`freeguide`),
KEY `IDX_81398E09708A0E0` (`gender_id`),
KEY `IDX_81398E0935655550` (`current_customer_source_history_id`),
KEY `IDX_81398E0981A1F986` (`original_customer_source_history_id`)
) ENGINE=MyISAM AUTO_INCREMENT=433026 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `sf_user_data` (
`id` int(11) NOT NULL auto_increment,
`user_id` bigint(20) unsigned NOT NULL,
`register_id` int(11) default NULL,
`insurance_id` int(11) default NULL,
`activity_started_at` date default NULL,
`accre_request_accepted` tinyint(1) default NULL,
`accre_request_accepted_at` date default NULL,
`declaration_frequency_months` smallint(6) default NULL,
`declaration_reminder` tinyint(1) default NULL,
`activities_number` smallint(6) default NULL,
`computed_main_activity_percent_total` double default NULL,
`computed_secondary_activity_percent_total` double default NULL,
`company_address_is_personal_address` tinyint(1) default NULL,
`register_city` varchar(255) collate utf8_unicode_ci default NULL,
`invoice_last_increment` smallint(6) NOT NULL default '0',
`quotation_last_increment` smallint(6) NOT NULL default '0',
`asset_last_increment` smallint(6) NOT NULL default '0',
`payplug_parameters` varchar(255) collate utf8_unicode_ci default NULL,
`displayed_first_connection_dialog` tinyint(1) NOT NULL default '0',
`displayed_first_invoice_display_dialog` tinyint(1) NOT NULL default '0',
`payplug_parameters_created_at` date default NULL,
`payplug_first_payment_at` date default NULL,
`latest_payplug_http_code` int(11) default NULL,
`register_number` varchar(255) collate utf8_unicode_ci default NULL,
`register_code` varchar(255) collate utf8_unicode_ci default NULL,
`register_bis_city` varchar(255) collate utf8_unicode_ci default NULL,
`register_bis_number` varchar(255) collate utf8_unicode_ci default NULL,
`registerBis_id` int(11) default NULL,
`main_activity_type_id` int(11) default NULL,
`secondary_activity_type_id` int(11) default NULL,
`declaration_reminder_popup` tinyint(1) default NULL,
`declaration_reminder_popup_latest_choice` smallint(6) default NULL COMMENT '1 = Me le rappeler demain, 2 = Ne plus afficher cette alerte',
`declaration_reminder_popup_latest_choice_date` date default NULL,
`main_activity_id` int(11) default NULL,
`secondary_activity_id` int(11) default NULL,
`primary_socio_economic_classification_id` int(11) default NULL,
`secondary_socio_economic_classification_id` int(11) default NULL,
`income_bracket_id` int(11) default NULL,
`main_activity_custom` varchar(255) collate utf8_unicode_ci default NULL,
`secondary_activity_custom` varchar(255) collate utf8_unicode_ci default NULL,
`default_further_information` longtext collate utf8_unicode_ci,
`gclid` varchar(255) collate utf8_unicode_ci default NULL,
`main_activity_type_old_id` smallint(6) default NULL,
`main_activity_nature_old_id` smallint(6) default NULL,
`secondary_activity_type_old_id` smallint(6) default NULL,
`secondary_activity_nature_old_id` smallint(6) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_E904BFD1A76ED395` (`user_id`),
UNIQUE KEY `UNIQ_E904BFD1D1E63CD1` (`insurance_id`),
KEY `IDX_E904BFD14976CB7E` (`register_id`),
KEY `IDX_E904BFD1DBC024CC` (`registerBis_id`),
KEY `IDX_E904BFD12E864BE8` (`main_activity_type_id`),
KEY `IDX_E904BFD132198C62` (`secondary_activity_type_id`),
KEY `IDX_E904BFD15543A800` (`main_activity_id`),
KEY `IDX_E904BFD1798B8812` (`secondary_activity_id`),
KEY `IDX_E904BFD1D17B29D3` (`primary_socio_economic_classification_id`),
KEY `IDX_E904BFD17758CEBC` (`secondary_socio_economic_classification_id`),
KEY `IDX_E904BFD1BAF920D3` (`income_bracket_id`)
) ENGINE=MyISAM AUTO_INCREMENT=116384 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
It seems like this simple amendment will be faster:
SELECT COUNT(DISTINCT a0_.id) sclr0
FROM account a0_
JOIN customer c1_
ON c1_.account = a0_.id
JOIN address a3_
ON c1_.customer_address = a3_.id
And if data integrity is important to you, consider switching to InnoDB.
Change LEFT JOIN address a3 to plain JOIN. That way, the optimizer could consider starting with address. And add INDEX(city) to address.
Currently, the EXPLAIN is showing hitting 405K rows in each of 3 tables. (1.2M total) With the above change, the optimizer would start with the city index, there by looking at, say 1K rows. After that, it would look up 1K rows in each of the other two tables. (3K total)
LEFT JOIN sf_user_data s2_ ON (s2_.user_id = a0_.id) seems to be totally useless; remove it. (Now down to 2K rows)
Don't blindly use (255), use reasonable numbers. In doing so, if identifier is not very big, make it the PRIMARY KEY and get rid of id.
It is extremely rare to have 6 different columns each being UNIQUE. I think you will run into business-logic trouble because of it.

Laravel where method won't works correctly on my server but work right on my Mac

I have a weird problem, this line wont fetch any record on my server but works all right on my computer, mysql and php version are same, and when i cast id field to string it's work on server (Debian), but it is risky because i use where statement with id very much. this is the code:
$myAnswers=Questioner::where('id',$questioner_id)->first()->getLinkedAnsweres()->where("subject_id",Auth::user()->id);
and when i change it to this, it's work right (cast id to string):
$myAnswers=Questioner::where('id',$questioner_id)->first()->getLinkedAnsweres()->where("subject_id",(string)Auth::user()->id);
subject_id and user id are both unsigned_integer with same size.
How can i fix this problem? Should i overwrite where method? or is there any config which cast unsigned int to string in query?
thanks
PS: Tables structure:
users | CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`family` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`gender` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`default_password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`access_level` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'subject',
`phone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`comments` longtext COLLATE utf8_unicode_ci,
`project_limit` int(10) unsigned NOT NULL,
`questioner_limit` int(10) unsigned NOT NULL,
`created_by` int(10) unsigned DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_username_unique` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
answers | CREATE TABLE `answers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(10) unsigned NOT NULL,
`selected_option` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`subject_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

query with multiple joins and indexes stuck on "Copying to tmp table"

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?