MySQL UNION query is taking time - mysql

In below query there are three UNIONs. each union is taking 4 seconds to execute and total query execution time is 12 seconds. is there any way to fine tune or re write this query? Each union has different columns in where clause, so adding more index also not good choice.
Select distinct el.ProblemID,el.SLAID,et.Esca_level from escalation_lookup el, sd_servicereqmaster s,escalation_timeslots et, escalation_details ed where s.ProblemID = el.ProblemID and el.IsEsca1_Breached = 0 and el.SLAID = et.SLA_ID and et.Esca_level = 1 and ed.SLA_ID = et.SLA_ID and ed.SLA_ID = el.SLAID and ed.IsSLARuleActive = 1 and Estimated_Esca1_BreachTime <> '0000-00-00 00:00:00' and Interval_time=1 and NBD_cutoff_time=0
and ((el.TotalTimeConsider > et.EscaTime_Min and el.IsTicketActive = 0) OR (el.TotalTimeConsider > et.EscaTime_Min and el.Previous_Status = 0 and el.IsTicketActive = 1) OR (el.Previous_Status = 1 and el.IsTicketActive = 1 and Estimated_Esca1_BreachTime < now()))
union all
Select distinct el.ProblemID,el.SLAID,et.Esca_level from escalation_lookup el, sd_servicereqmaster s,escalation_timeslots et, escalation_details ed where s.ProblemID = el.ProblemID and el.IsEsca2_Breached = 0 and el.SLAID = et.SLA_ID and et.Esca_level = 2 and ed.SLA_ID = et.SLA_ID and ed.SLA_ID = el.SLAID and ed.IsSLARuleActive = 1 and Estimated_Esca2_BreachTime <> '0000-00-00 00:00:00' and Interval_time=1 and NBD_cutoff_time=0
and ((el.TotalTimeConsider > et.EscaTime_Min and el.IsTicketActive = 0) OR (el.TotalTimeConsider > et.EscaTime_Min and el.Previous_Status = 0 and el.IsTicketActive = 1) OR (el.Previous_Status = 1 and el.IsTicketActive = 1 and Estimated_Esca2_BreachTime < now()))
union all
Select distinct el.ProblemID,el.SLAID,et.Esca_level from escalation_lookup el, sd_servicereqmaster s,escalation_timeslots et, escalation_details ed where s.ProblemID = el.ProblemID and el.IsEsca3_Breached = 0 and el.SLAID = et.SLA_ID and et.Esca_level = 3 and ed.SLA_ID = et.SLA_ID and ed.SLA_ID = el.SLAID and ed.IsSLARuleActive = 1 and Estimated_Esca3_BreachTime <> '0000-00-00 00:00:00' and Interval_time=1 and NBD_cutoff_time=0
and ((el.TotalTimeConsider > et.EscaTime_Min and el.IsTicketActive = 0) OR (el.TotalTimeConsider > et.EscaTime_Min and el.Previous_Status = 0 and el.IsTicketActive = 1) OR (el.Previous_Status = 1 and el.IsTicketActive = 1 and Estimated_Esca3_BreachTime < now()))
different column used in each union are
UNION 1
IsEsca1_Breached
Esca_level =1
Estimated_Esca1_BreachTime
UNION 2
IsEsca2_Breached
Esca_level =2
Estimated_Esca2_BreachTime
UNION 3
IsEsca3_Breached
Esca_level =3
Estimated_Esca3_BreachTime
table1,
CREATE TABLE escalation_lookup (
SiteID int(11) NOT NULL DEFAULT '1',
slno int(10) unsigned NOT NULL AUTO_INCREMENT,
SlaID int(11) unsigned NOT NULL DEFAULT '0',
ProblemID int(11) unsigned NOT NULL DEFAULT '0',
Esca_1 int(10) unsigned NOT NULL DEFAULT '0',
Esca_2 int(10) unsigned NOT NULL DEFAULT '0',
EmailEsca_1 int(11) NOT NULL DEFAULT '0',
EmailEsca_2 int(11) NOT NULL DEFAULT '0',
SLA_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Esca1_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Esca1_SendTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Esca2_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Esca2_SendTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
LastCalculatedTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
Previous_Status tinyint(4) DEFAULT '1',
TotalTimeConsider int(11) DEFAULT '0',
IsTicketActive tinyint(4) DEFAULT '1',
TicketGenStatus varchar(8) DEFAULT '00',
Estimated_SLABreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Estimated_Esca1_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Estimated_Esca2_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Estimated_GracePeriod_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
IsSLA_Breached tinyint(4) DEFAULT '0',
IsEsca1_Breached tinyint(4) DEFAULT '0',
IsEsca2_Breached tinyint(4) DEFAULT '0',
IsActualResolutionTimeConsider tinyint(4) DEFAULT '0',
Esca_3 int(10) unsigned NOT NULL DEFAULT '0',
EmailEsca_3 int(10) unsigned NOT NULL DEFAULT '0',
Esca3_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Esca3_SendTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Estimated_Esca3_BreachTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
IsEsca3_Breached tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (slno),
KEY Problem_ID (ProblemID),
KEY SLAStatus_ProblemID (ProblemID,IsSLA_Breached),
KEY Combined_key (ProblemID,IsEsca1_Breached,IsEsca2_Breached,SlaID),
KEY Estimated_SLABreachTime (Estimated_SLABreachTime),
KEY probleimid_slaid (ProblemID,SlaID),
KEY TotalTimeConsider (TotalTimeConsider),
KEY slaid (SlaID),
KEY slaid_problemid (SlaID,ProblemID)
) ENGINE=InnoDB AUTO_INCREMENT=7755116 DEFAULt utf8.
table 2
CREATE TABLE sd_servicereqmaster (
ProblemId int(11) NOT NULL AUTO_INCREMENT,
Title varchar(2048) CHARACTER SET utf8 NOT NULL DEFAULT '',
SubmittedBy int(11) NOT NULL,
Owner int(11) NOT NULL DEFAULT '0',
CurrentStatus int(11) NOT NULL,
Category int(11) NOT NULL,
SubCategory int(11) NOT NULL,
Platform int(11) NOT NULL DEFAULT '0',
Severity int(11) NOT NULL DEFAULT '0',
CreationTime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
ProbDescription mediumtext CHARACTER SET utf8 NOT NULL,
CcMailId text CHARACTER SET utf8,
AlternativeEmail varchar(128) CHARACTER SET utf8 DEFAULT '',
ContactNumber varchar(256) CHARACTER SET utf8 DEFAULT '',
Attachment varchar(512) CHARACTER SET utf8 NOT NULL DEFAULT '',
Enclosure varchar(200) CHARACTER SET utf8 NOT NULL DEFAULT '',
Priority int(11) NOT NULL DEFAULT '0',
ApprovalManager int(10) unsigned NOT NULL DEFAULT '0',
LocationID int(10) unsigned NOT NULL DEFAULT '1',
DepartmentID int(10) unsigned NOT NULL DEFAULT '1',
Resolution_CauseID int(10) unsigned NOT NULL DEFAULT '0',
Resolution_Days varchar(45) CHARACTER SET utf8 NOT NULL DEFAULT '0',
Resolution_Hours varchar(45) CHARACTER SET utf8 NOT NULL DEFAULT '0',
Resolution_Minutes varchar(45) CHARACTER SET utf8 NOT NULL DEFAULT '0',
SubmittedThrough int(10) unsigned NOT NULL DEFAULT '0',
AlertID int(11) NOT NULL DEFAULT '0',
LastOperatedTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
GroupID int(11) NOT NULL DEFAULT '0',
RepeatCall int(11) NOT NULL DEFAULT '0',
ResolutionComment text CHARACTER SET utf8,
REQUESTTYPE int(10) unsigned NOT NULL DEFAULT '1',
AssetNumber int(10) unsigned NOT NULL DEFAULT '0',
SLAViolationComment varchar(1024) CHARACTER SET utf8 NOT NULL DEFAULT '',
ParentID int(11) NOT NULL DEFAULT '0',
ConversionType tinyint(3) unsigned NOT NULL DEFAULT '0',
DerivedField1 int(10) unsigned NOT NULL DEFAULT '0',
DerivedField2 int(10) unsigned NOT NULL DEFAULT '0',
DerivedField3 int(10) unsigned NOT NULL DEFAULT '0',
RequestID varchar(3000) CHARACTER SET utf8 NOT NULL DEFAULT ' ',
ProjectID int(10) unsigned NOT NULL DEFAULT '1',
ServiceID int(10) unsigned NOT NULL DEFAULT '1',
IsTransferred int(10) unsigned NOT NULL DEFAULT '0',
ReferenceID varchar(1024) CHARACTER SET utf8 NOT NULL DEFAULT '',
LevelOneStatus tinyint(4) NOT NULL DEFAULT '0',
LevelTwoStatus tinyint(4) NOT NULL DEFAULT '0',
ProxySubmittedBy int(10) unsigned NOT NULL DEFAULT '0',
DelegatedFrom_Owner int(10) unsigned NOT NULL DEFAULT '0',
DelegatedFrom_ApprovalManager int(10) unsigned NOT NULL DEFAULT '0',
UrgencyID int(10) unsigned NOT NULL DEFAULT '0',
ImpactID int(10) unsigned NOT NULL DEFAULT '0',
isLinked int(10) unsigned NOT NULL DEFAULT '0',
CSI int(10) unsigned NOT NULL DEFAULT '0',
IncidentType int(10) unsigned NOT NULL DEFAULT '0',
ExpectedClosureTime varchar(45) CHARACTER SET utf8 NOT NULL DEFAULT '',
ProjectTransferComment text CHARACTER SET utf8,
IsRead tinyint(3) unsigned NOT NULL DEFAULT '0',
IsRead_UserID int(10) unsigned NOT NULL DEFAULT '0',
SchedulerecordType int(10) NOT NULL DEFAULT '0',
VERSION int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (ProblemId),
KEY Owner_SubmittedBy (SubmittedBy,Owner),
KEY Current_Status (CurrentStatus),
KEY Sub_Category (SubCategory),
KEY Creation_Time (CreationTime),
KEY FK_sd_servicereqmaster_1 (ProjectID),
KEY FK_sd_servicereqmaster_2 (ServiceID),
KEY FK_sd_servicereqmaster_3 (Category),
KEY index_LocationID (LocationID),
KEY index_DepartmentID (DepartmentID),
KEY index_ImpactID (ImpactID),
KEY index_UrgencyID (UrgencyID),
KEY index_Priority (Priority),
KEY index_GroupID (GroupID),
KEY index_SubmittedThrough (SubmittedThrough),
KEY index_ApprovalManager (ApprovalManager),
KEY index_DerivedField1 (DerivedField1),
KEY index_DerivedField2 (DerivedField2),
KEY index_DerivedField3 (DerivedField3),
KEY index_RequestID (RequestID(255)),
KEY ParentID (ParentID),
KEY owner (Owner),
KEY isLinked (isLinked),
CONSTRAINT FK_sd_servicereqmaster_1 FOREIGN KEY (ProjectID) REFERENCES sd_project_master (ProjectID) ON DELETE CASCADE,
CONSTRAINT FK_sd_servicereqmaster_2 FOREIGN KEY (ServiceID) REFERENCES sd_service (ServiceID) ON DELETE CASCADE
);
table 3
CREATE TABLE escalation_details (
SiteID int(11) NOT NULL DEFAULT '1',
SLA_ID int(11) NOT NULL AUTO_INCREMENT,
Name varchar(256) DEFAULT '',
Formula varchar(255) DEFAULT '',
Rule varchar(4096) DEFAULT '',
ResolveTime varchar(255) DEFAULT '',
ResolveTime_Min int(11) DEFAULT '0',
GraceTime varchar(255) DEFAULT '0 Day(s) 0 Hour(s) 0 Mins',
GraceTime_Min int(11) DEFAULT '0',
Description varchar(255) DEFAULT '',
TicketGenID int(10) DEFAULT '0',
IsSLARuleActive int(11) DEFAULT '1',
BusinessHrs_Profile int(11) DEFAULT '0',
Holidays_Profile int(11) DEFAULT '0',
ProjectID int(10) unsigned NOT NULL DEFAULT '1',
IsResponseRequired int(10) NOT NULL DEFAULT '1' COMMENT '1-Enable Alert Message, 0-Disable Alert Message',
Interval_time int(10) NOT NULL DEFAULT '1',
NBD_cutoff_time int(10) NOT NULL DEFAULT '0',
NBD_without_cutofftime_Businessday int(10) NOT NULL DEFAULT '0',
NBD_With_Cutofftime time NOT NULL DEFAULT '00:00:00',
NBD_With_before_cutofftime_Businessday int(10) NOT NULL DEFAULT '0',
NBD_With_After_cutofftime_Businessday int(10) NOT NULL DEFAULT '0',
Additionalfieldid int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (SLA_ID),
KEY FK_escalation_details_1 (ProjectID),
KEY NBD_cutoff_time (NBD_cutoff_time),
KEY sla_id (SLA_ID),
CONSTRAINT FK_escalation_details_1 FOREIGN KEY (ProjectID) REFERENCES sd_project_master (ProjectID) ON DELETE CASCADE
) ;

Related

How can I optimize this query further?

This is mysql query to which I have added the indexes wherever possible. This is written by some previous coders and I need to optimize it as coming in slow logs. I have following stats for this query:
rows_sent: 1
query_time: 00:10:31
lock_time: 00:00:00
rows_examined: 628241089
db: singledb_ed
last_insert_id: 0
insert_id: 0
server_id: 1789791470
sql_text: SELECT cs.delivery_consultant_id AS user_id,
interview.id AS interview_id,
date(interview.interview_date) AS interview_date,
date(interview.mod_date) AS interview_mod_date,
count(interview.id) AS kpi_item_count,
cu.division_id,
bu.organisation_id,
ctu.team_id
FROM es_shortlist AS cs
LEFT JOIN tms_vacancies AS vac ON cs.vacancy_id = vac.id
LEFT JOIN es_applications AS app ON app.vacancy_id = vac.id AND app.candidate_id = cs.candidate_id
LEFT JOIN tms_interviews AS interview ON app.id = interview.application_id
LEFT JOIN company_users AS cu ON cu.id = cs.delivery_consultant_id
LEFT JOIN base_users AS bu ON bu.company_user_id = cs.delivery_consultant_id AND bu.company_id = cu.base_company_id
LEFT JOIN company_team_users AS ctu ON ctu.user_id = cs.delivery_consultant_id
WHERE (ctu.end_date is null
AND cs.delivery_consultant_id ='53521')
AND (ctu.end_date IS NULL)
AND (ctu.status = 2)
GROUP BY cs.delivery_consultant_id, ctu.team_id
EXPLAIN RESULT:
+----+-------------+-----------+--------+-----------------------------------------------------------+------------------------+---------+---------------------------------------+------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+--------+-----------------------------------------------------------+------------------------+---------+---------------------------------------+------+------------------------------------+
| 1 | SIMPLE | cs | ref | delivery_consultant_id | delivery_consultant_id | 5 | const | 1421 | Using temporary; Using filesort |
| 1 | SIMPLE | vac | eq_ref | PRIMARY | PRIMARY | 4 | singledb_ed.cs.vacancy_id | 1 | Using index |
| 1 | SIMPLE | app | ref | vacancy_id,candidate_stage_sync | vacancy_id | 5 | singledb_ed.vac.id | 1 | Using where |
| 1 | SIMPLE | interview | ref | application_id | application_id | 4 | singledb_ed.app.id | 1 | NULL |
| 1 | SIMPLE | cu | eq_ref | PRIMARY | PRIMARY | 4 | singledb_ed.cs.delivery_consultant_id | 1 | NULL |
| 1 | SIMPLE | bu | ref | company_user_id,company_id,getOrganisation,get_index_comp | company_user_id | 5 | singledb_ed.cs.delivery_consultant_id | 1 | Using where |
| 1 | SIMPLE | ctu | ref | user_id,status,end_date | end_date | 6 | const | 40 | Using index condition; Using where |
+----+-------------+-----------+--------+-----------------------------------------------------------+------------------------+---------+---------------------------------------+------+------------------------------------+
Please see this image for EXPLAIN result of this query
The CREATE INFO for all Tables
CREATE TABLE `es_shortlist` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vacancy_id` int(11) DEFAULT NULL,
`candidate_id` int(11) DEFAULT NULL,
`shortlist_status` tinyint(2) DEFAULT NULL COMMENT '1 = open / 0 = closed / 2 = inprocess',
`created_user_id` int(11) DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`entity_id` int(11) DEFAULT NULL,
`base_company_id` int(11) DEFAULT NULL,
`delivery_consultant_id` int(11) DEFAULT NULL,
`is_cb_migrated` tinyint(2) DEFAULT NULL,
`rs_app_id` int(11) DEFAULT NULL,
`shortlist_source` tinyint(4) DEFAULT '1' COMMENT '1=''System'', 2=''Web Response''',
`shortlist_source_id` int(11) DEFAULT NULL COMMENT '24=''job board'', 43=''indeed'', 44=''google'', ''other is direct''',
PRIMARY KEY (`id`),
KEY `delivery_consultant_id` (`delivery_consultant_id`),
KEY `candidate_id` (`candidate_id`),
KEY `vacancy_id` (`vacancy_id`),
KEY `created_user_id` (`created_user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39388 DEFAULT CHARSET=utf8;
CREATE TABLE `tms_vacancies` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`base_company_id` int(11) NOT NULL,
`organisation_id` int(11) DEFAULT NULL,
`ref_id` int(11) DEFAULT NULL COMMENT 'ref for vacancy_id',
`eb_reff_id` int(11) DEFAULT NULL,
`team_id` int(11) DEFAULT '0' COMMENT 'attach id of tms_teams table',
`function_id` int(11) DEFAULT NULL,
`division_id` int(11) DEFAULT NULL,
`qualified` tinyint(4) DEFAULT NULL,
`lead_type` tinyint(4) DEFAULT '0' COMMENT '0 - was not a lead,1 - was a lead initially',
`lead_category` tinyint(1) DEFAULT NULL COMMENT 'If in lead_type field value set as 1 then must be set 5 = Market Info, 6 = Hot Lead, 7 = Job Ad',
`source` tinyint(2) DEFAULT NULL,
`search_type` tinyint(4) DEFAULT NULL,
`job_type` tinyint(4) DEFAULT NULL COMMENT '1-contract, 2-permanent and 3-part time etc\n',
`reason` tinyint(4) DEFAULT NULL COMMENT 'Reason for vacancy, try to get that info at the time of creating lead\n\nEg.\nNew Role / \r\n\r\nReplacement - Resignation / Maternity / Sickness / Replacement - Internal Transfer / Replacement - Dismissed',
`job_title` varchar(100) DEFAULT NULL,
`account_id` int(11) DEFAULT NULL COMMENT 'look up - ACCOUNTS OBJECT (NOTICE. An account have to be created before adding a LEAD)',
`start_date` timestamp NULL DEFAULT NULL,
`end_date` timestamp NULL DEFAULT NULL,
`base_salary` double DEFAULT NULL COMMENT 'This field is used to store min value of base salary for permanent type of job',
`base_salary_upto` double DEFAULT NULL COMMENT 'This field is used to store max value of base salary for permanent type of job',
`base_salary_to` double DEFAULT NULL COMMENT 'This field is used to store min value of total package for permanent type of job',
`base_salary_to_upto` double DEFAULT NULL COMMENT 'This field is used to store max value of total package for permanent type of job',
`salary_in_base_currency` double DEFAULT NULL COMMENT 'Base Salary converted into base currecy',
`package_in_base_currency` double DEFAULT NULL COMMENT 'Total Package converted into base currecy',
`application_form_status` tinyint(1) DEFAULT NULL COMMENT 'This Field will show if a vacancy has application created against it or not',
`application_auto_reject_flag` tinyint(1) DEFAULT NULL,
`application_reject_score` tinyint(3) DEFAULT NULL,
`branch_office` int(11) DEFAULT NULL,
`approval_status` tinyint(1) DEFAULT NULL COMMENT '0=>Unapproved, 1=>Approved',
`role_id` int(11) DEFAULT NULL,
`rs_sc_id` int(11) DEFAULT NULL,
`rs_sc_name` varchar(255) DEFAULT NULL,
`rs_sc_email` varchar(255) DEFAULT NULL,
`endorsed_vacancy` tinyint(1) NOT NULL DEFAULT '0',
`percentage_agreed` varchar(255) DEFAULT NULL,
`fee_agreed` double DEFAULT NULL,
`fee_agreed_in_base_currency` double DEFAULT NULL COMMENT 'Fee converted into base currecy',
`excange_rate` double DEFAULT NULL COMMENT 'curreny exchange rate for current month',
`fee_calculation_from` tinyint(2) DEFAULT NULL,
`other_fee` varchar(55) DEFAULT NULL,
`flag_qualified` varchar(55) DEFAULT NULL,
`desc_background_cover` varchar(255) DEFAULT NULL,
`currency` smallint(6) DEFAULT NULL,
`bonus` varchar(250) DEFAULT NULL,
`benefits` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`no_required` tinyint(4) DEFAULT NULL COMMENT 'how many positions are to fill',
`placement_nos` int(11) DEFAULT '0' COMMENT 'no of placement done against vacancy_id',
`summary` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`jobpost_summary` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '1-poen, 2-closed, 3-placed',
`priority` tinyint(1) DEFAULT NULL,
`status_reason` tinyint(4) DEFAULT NULL,
`3ff_status` tinyint(1) DEFAULT '0',
`contract_length` tinyint(4) DEFAULT NULL,
`base_rate` double DEFAULT NULL,
`function` int(11) DEFAULT NULL,
`duration` int(5) DEFAULT NULL,
`expenses` varchar(255) DEFAULT NULL,
`working_commitment` int(11) DEFAULT '100',
`base_rate_to` double DEFAULT NULL,
`daily_client_rate` double DEFAULT NULL COMMENT 'calculate if rate is hourly we save client rate * 8 otherwise as it is client rate',
`daily_candidate_rate` double DEFAULT NULL COMMENT 'calculate if rate is hourly we save candidate rate * 8 otherwise as it is candidate rate',
`client_rate_in_base_curreny` double DEFAULT NULL COMMENT 'Client rate converted into base currecy',
`candidate_rate_in_base_currency` double DEFAULT NULL COMMENT 'Candidate rate converted into base currecy',
`candidate_base_rate` double DEFAULT NULL,
`rate_type` int(11) DEFAULT NULL,
`exp_included` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`base_rate_currency` tinyint(4) DEFAULT NULL,
`job_description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`client_job_description` text,
`interview_template_id` int(11) DEFAULT NULL,
`application_template_id` int(11) DEFAULT NULL,
`last_mod_user_id` int(11) DEFAULT NULL,
`mod_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`created_user_id` int(11) DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`closed_date` timestamp NULL DEFAULT NULL,
`flag_post_jobboard` tinyint(1) DEFAULT '0',
`flag_post_supplier` tinyint(1) DEFAULT '0',
`flag_post_tpool` tinyint(1) DEFAULT '0',
`flag_post_social` tinyint(1) DEFAULT '0',
`flag_post_referral` tinyint(1) DEFAULT '0',
`flag_relocate` int(11) DEFAULT '0' COMMENT '0=> job will allow candidate that are not wiiling to relocate ,1=> job will allow candidate that are willing to relocate',
`rs_summary` text,
`created_a_date` timestamp NULL DEFAULT NULL,
`created_b_date` timestamp NULL DEFAULT NULL,
`created_lead_date` timestamp NULL DEFAULT NULL,
`is_teaser` tinyint(1) DEFAULT '0',
`is_pitched` tinyint(4) NOT NULL DEFAULT '0',
`is_block` tinyint(4) NOT NULL DEFAULT '0',
`job_role_id` varchar(56) DEFAULT NULL COMMENT 'combination of jobrole_id and category_id(like 8117##8), ## is seperator',
`search_by` tinyint(4) DEFAULT NULL COMMENT '1 : All of the Skill, 2: Any of the skill',
`is_removed` tinyint(2) DEFAULT NULL,
`is_third_rep` tinyint(1) DEFAULT '0',
`is_cb_migrated` tinyint(4) DEFAULT '0',
`is_eb_migrated` tinyint(4) DEFAULT '0',
`is_posting_approved` tinyint(2) DEFAULT NULL,
`is_posted` tinyint(2) DEFAULT NULL,
`merge_account_id` int(11) DEFAULT NULL,
`rs_vacancy_id` int(11) DEFAULT NULL,
`migrated_date` datetime DEFAULT NULL,
`is_onhold_closeplaced` tinyint(1) DEFAULT '0' COMMENT '0=Not, 1=Close Application of on hold & closed placed jobs more than 30 days old',
`cron_first_grade_a_job_dm` tinyint(4) NOT NULL,
`cron_first_grade_a_job_acc` tinyint(4) NOT NULL,
`fee_agreed_gbp` double DEFAULT NULL,
`fee_agreed_usd` double DEFAULT NULL,
`fee_agreed_eur` double DEFAULT NULL,
`fee_cron_updated` tinyint(1) DEFAULT '0',
`org_id_updated` tinyint(4) DEFAULT '0',
`teamid_updated` tinyint(4) NOT NULL DEFAULT '0',
`is_digital` int(4) NOT NULL DEFAULT '0' COMMENT '0=>deactive,1=>active',
`is_new` tinyint(2) DEFAULT NULL COMMENT '1-New Job',
PRIMARY KEY (`id`),
KEY `base_company_id` (`base_company_id`),
KEY `tms_vacancy_idx` (`function_id`,`status`,`flag_post_jobboard`,`flag_post_supplier`,`flag_post_tpool`),
KEY `organisation_id` (`organisation_id`),
KEY `status` (`status`),
KEY `created_date` (`created_date`),
KEY `job_close` (`organisation_id`,`status`,`created_date`),
KEY `is_new` (`is_new`)
) ENGINE=InnoDB AUTO_INCREMENT=80611 DEFAULT CHARSET=utf8 COMMENT='Tabe for storing lead details by recruiters.\n\nA lead is a co';
CREATE TABLE `es_applications` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`candidate_id` int(11) DEFAULT NULL,
`base_company_id` int(11) DEFAULT NULL,
`dm_id` int(11) DEFAULT NULL,
`current_status` varchar(11) CHARACTER SET latin1 DEFAULT NULL,
`is_active_batch` tinyint(11) DEFAULT NULL COMMENT '1 if current batch, 0 if batch deactive',
`rejection_stage` tinyint(5) DEFAULT '0',
`rejection_status` tinyint(5) DEFAULT NULL COMMENT 'reason for rejection',
`rejection_status_reason` text CHARACTER SET latin1 COMMENT 'comments for rejection',
`rejection_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) DEFAULT NULL,
`to_user_id` int(11) DEFAULT NULL,
`to_user_type` tinyint(4) DEFAULT NULL COMMENT 'dm/HR or Line Manager',
`from_user_type` tinyint(5) DEFAULT '0' COMMENT 'dm/HR or Line Manager',
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`sharedby_user` int(11) DEFAULT NULL,
`sharedby_date` timestamp NULL DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`mod_by` int(11) DEFAULT NULL,
`mod_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`current_sub_status` tinyint(2) DEFAULT NULL,
`application_id` int(11) DEFAULT '0',
`app_stage` int(11) DEFAULT '0' COMMENT 'application stage',
`enterprise_user_id` int(11) DEFAULT NULL,
`enterprise_user_company_id` int(11) DEFAULT NULL,
`cv_send_id` int(11) DEFAULT NULL,
`edmgr_vacancy_id` int(11) DEFAULT NULL,
`vacancy_id` int(11) DEFAULT NULL,
`job_branch_id` int(11) DEFAULT NULL COMMENT 'Branch id of job',
`talent_id` int(11) DEFAULT NULL,
`app_status` int(4) DEFAULT NULL,
`stage_reached` int(4) DEFAULT NULL,
`stage_reached_date` datetime DEFAULT NULL,
`summary` text CHARACTER SET latin1,
`is_available` tinyint(5) DEFAULT '1' COMMENT '1 for available and 0 for not available',
`candidate_type` tinyint(5) DEFAULT '2' COMMENT '2=permanent, 3= contract',
`send_list_id` int(11) DEFAULT NULL COMMENT 'Primary id of es_endorsed_send_list table ',
`is_direct` tinyint(1) DEFAULT '0' COMMENT '0 - not direct, 1 - direct application',
`is_hot` tinyint(1) DEFAULT '0' COMMENT '1 - Hot, 0 - Not Hot',
`organisation_id` int(11) DEFAULT NULL,
`candidate_source` int(11) DEFAULT NULL,
`candidate_source_id` int(11) DEFAULT NULL,
`rs_app_id` int(11) DEFAULT NULL,
`is_teaser` tinyint(1) DEFAULT '0',
`branch_id` int(11) DEFAULT NULL,
`merge_account_id` int(11) DEFAULT NULL,
`entity_org_id` int(5) NOT NULL DEFAULT '0',
`team_id` int(11) DEFAULT NULL,
`orgid_teamid_updated` tinyint(4) DEFAULT '0',
`application_type` smallint(4) DEFAULT NULL,
`es_talent_profile` int(11) DEFAULT NULL,
`entity_id` int(11) DEFAULT NULL,
`is_sourcechain_account` int(11) DEFAULT NULL,
`team_cron` tinyint(2) DEFAULT '0',
`is_cb_migrated` tinyint(4) DEFAULT NULL,
`is_eb_migrated` tinyint(5) NOT NULL DEFAULT '0',
`is_tr_cron` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `dm_id` (`dm_id`),
KEY `current_status` (`current_status`,`cv_send_id`,`vacancy_id`),
KEY `vacancy_id` (`vacancy_id`),
KEY `candidate_stage_sync` (`candidate_id`,`app_status`,`current_sub_status`),
KEY `start_date` (`base_company_id`,`dm_id`) USING BTREE,
KEY `start_date_2` (`start_date`)
) ENGINE=InnoDB AUTO_INCREMENT=112073 DEFAULT CHARSET=utf8;
CREATE TABLE `tms_interviews` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`base_company_id` int(11) NOT NULL,
`stage` int(11) NOT NULL COMMENT 'whether it is 1st or 2nd or 3rd or final.',
`sub_stage` int(11) DEFAULT NULL COMMENT 'it will hold total count of interview for same application',
`ref_id` int(11) DEFAULT NULL COMMENT 'ref for interviewer_id',
`interviewer_id` int(11) NOT NULL,
`interview_date` datetime DEFAULT NULL COMMENT 'interview date .',
`interview_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'interview time .',
`interview_type` tinyint(2) DEFAULT NULL COMMENT '1=>FTF, 2=>telephonic',
`status` int(11) DEFAULT NULL COMMENT 'could be accepted or rejected.',
`status_date` datetime DEFAULT NULL,
`status_reason` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'the reason why candidate is rejected at this stage.',
`application_id` int(11) NOT NULL,
`candidate_id` int(11) NOT NULL,
`vacancy_id` int(11) NOT NULL,
`account_id` int(11) NOT NULL,
`account_table` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`client_feedback` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`candidate_feedback` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`cp_feedback` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`note` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`created_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_user_id` int(11) NOT NULL,
`mod_date` timestamp NULL DEFAULT NULL,
`last_mod_user_id` int(11) DEFAULT NULL,
`reff_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`performance_cron_status` tinyint(1) NOT NULL DEFAULT '0',
`time_zone` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`confirmation_flag` tinyint(2) DEFAULT '0',
`interview_where` tinyint(4) DEFAULT NULL COMMENT 'used for where field in interview scheduling',
`location` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`is_cancelled` int(1) NOT NULL DEFAULT '0',
`no_of_reschedules` int(3) DEFAULT '0' COMMENT 'No of reschedules',
`merge_account_id` int(11) DEFAULT NULL,
`team_id` int(11) DEFAULT NULL,
`team_cron` tinyint(2) DEFAULT '0',
`is_cb_migrated` tinyint(4) DEFAULT NULL,
`is_eb_migrated` tinyint(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `tms_interviews_idx` (`stage`,`interviewer_id`,`status`) USING BTREE,
KEY `application_id` (`application_id`),
KEY `candidate_id` (`candidate_id`),
KEY `vacancy_id` (`vacancy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=60447 DEFAULT CHARSET=utf8 COMMENT='store interview stages for candidates for particular applica';
CREATE TABLE `company_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dpn_sent` tinyint(4) DEFAULT '1' COMMENT 'Pending(1) | Sent(2) | Bounced(3) | Verified(4)',
`email_verification_status` tinyint(4) DEFAULT NULL COMMENT '1:Good, 2: Bad',
`dpn_due_date` datetime DEFAULT '2018-06-24 00:00:00',
`base_company_id` int(11) DEFAULT NULL,
`ref_id` int(11) DEFAULT NULL,
`base_user_id` int(11) DEFAULT NULL,
`branch_id` int(11) DEFAULT NULL,
`client_portal_id` int(11) DEFAULT NULL COMMENT 'client portal id if user is of client portal',
`crm_people_id` int(11) DEFAULT NULL COMMENT 'client user id-people_id',
`education_level` int(11) DEFAULT NULL,
`division_id` int(11) DEFAULT NULL,
`job_level` int(11) DEFAULT NULL,
`fname` varchar(50) DEFAULT NULL,
`lname` varchar(50) DEFAULT NULL,
`title` tinyint(4) DEFAULT NULL,
`job_title` varchar(100) DEFAULT NULL,
`sex` tinyint(1) DEFAULT NULL,
`dob` datetime DEFAULT NULL,
`country_id` int(4) DEFAULT NULL COMMENT 'no constraint should be made on this',
`picture` varchar(255) DEFAULT NULL,
`timezone` varchar(255) DEFAULT NULL,
`function_id` int(11) DEFAULT NULL,
`default_module_id` int(11) DEFAULT NULL,
`status` tinyint(1) DEFAULT NULL COMMENT 'verified, activated, deactivated, approved',
`endorsed_user` tinyint(1) DEFAULT '0',
`endorsed_dm_status` int(11) DEFAULT '1',
`created_user_id` int(11) DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_mod_user_id` int(11) DEFAULT NULL,
`mod_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`is_onboarding_message` tinyint(1) DEFAULT '0' COMMENT '0=>not onboarding,1=>onboarded',
`is_onboarding_connection` tinyint(1) DEFAULT '0' COMMENT '0=>not onboarding,1=>onboarded',
`is_onboarding_profile` tinyint(1) DEFAULT '0' COMMENT '0=>not onboarding,1=>onboarded',
`hub_integrated` tinyint(4) DEFAULT '0' COMMENT '0:NotIntegrated, 1:Integrated',
`hub_id` bigint(20) DEFAULT NULL,
`cloud_integrated` tinyint(4) DEFAULT '0',
`cloud_contact_id` varchar(255) DEFAULT NULL,
`cloud_integrated_eb` tinyint(4) DEFAULT '0',
`cloud_contact_id_eb` varchar(255) DEFAULT NULL,
`cloud_integrated_tr` tinyint(4) DEFAULT '0',
`cloud_contact_id_tr` varchar(255) DEFAULT NULL,
`is_cb_migrated` tinyint(4) DEFAULT '0',
`is_eb_migrated` tinyint(5) NOT NULL DEFAULT '0',
`eb_reff_id` int(11) DEFAULT NULL,
`is_marketing_org_sync` tinyint(4) DEFAULT '2',
`field_hub_sync` tinyint(4) DEFAULT '0' COMMENT '0:No;1:Yes',
`is_jt_synced` tinyint(4) DEFAULT '0' COMMENT '0:no;1:yes',
`cloud_integrated_vado` tinyint(4) DEFAULT '0',
`cloud_contact_id_vado` int(11) DEFAULT NULL,
`merge_account_id` int(11) DEFAULT NULL,
`is_dm_emp_hub_sync` tinyint(4) DEFAULT '0',
`flag_missing_name` tinyint(2) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `status` (`status`,`client_portal_id`),
KEY `mod_date` (`mod_date`),
KEY `base_company_id` (`base_company_id`),
KEY `created_user_id` (`created_user_id`),
KEY `last_mod_user_id` (`last_mod_user_id`),
KEY `hub_id` (`hub_id`),
KEY `branch_id` (`branch_id`),
KEY `endorsed_user_status` (`endorsed_user`,`status`),
KEY `base_user_id` (`base_user_id`),
KEY `endorsed_user` (`endorsed_user`),
KEY `hub_integrated` (`hub_integrated`),
KEY `dm_hub_employer` (`hub_id`,`is_dm_emp_hub_sync`,`endorsed_user`),
KEY `cv_mod_created_date` (`created_date`,`is_eb_migrated`),
KEY `job_level` (`job_level`),
KEY `function_id` (`function_id`)
) ENGINE=InnoDB AUTO_INCREMENT=555665 DEFAULT CHARSET=utf8;
CREATE TABLE `base_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`is_eb_migrated` tinyint(4) DEFAULT '0',
`eb_reff_id` bigint(20) DEFAULT NULL,
`account_number` varchar(255) DEFAULT NULL,
`emp_uniq_id` varchar(255) DEFAULT NULL,
`company_id` int(11) DEFAULT NULL,
`company_user_id` int(11) DEFAULT NULL,
`joblevel_id` int(11) DEFAULT NULL,
`type` tinyint(4) DEFAULT NULL,
`username` varchar(100) DEFAULT NULL,
`password` char(32) DEFAULT NULL,
`fname` varchar(45) DEFAULT NULL,
`lname` varchar(45) DEFAULT NULL,
`endorsed_user_email` varchar(255) DEFAULT NULL,
`verification_code` varchar(100) DEFAULT NULL,
`pass_recovery_code` varchar(100) DEFAULT NULL,
`status` tinyint(4) DEFAULT NULL COMMENT 'verified, activated, deactivated, approved',
`ip_addresses` text,
`flag_eb` tinyint(1) DEFAULT NULL COMMENT '0 - NOT EB, 1 - EB User',
`flag_ed_manual` tinyint(2) DEFAULT '0' COMMENT '0 - NOT READ, 1 - READ. Its for verifying whether the user/DM is aware of Endorsed client manual.',
`ip_verify` tinyint(1) DEFAULT '0',
`hubspot_flag` int(11) DEFAULT NULL COMMENT '1=active, 2=deactive',
`organisation_id` int(11) DEFAULT NULL COMMENT 'xero organisation id for ed manager users',
`last_mod_user_id` int(11) DEFAULT NULL,
`mod_date` timestamp NULL DEFAULT NULL,
`created_user_id` int(11) DEFAULT NULL,
`created_date` datetime DEFAULT NULL,
`rs_company_user_id` int(11) DEFAULT NULL,
`rs_base_user_id` int(11) DEFAULT NULL,
`firsttime_get_started` tinyint(1) NOT NULL DEFAULT '0',
`is_cb_migrated` tinyint(4) DEFAULT '0',
`zendesk_user_id` varchar(255) DEFAULT NULL,
`is_sync_zendesk` tinyint(4) DEFAULT '0' COMMENT '0:not sync;1:synced;2:error',
`zendesk_sync_date` timestamp NULL DEFAULT NULL,
`flag_company` int(11) DEFAULT NULL COMMENT '1:CB;2:EB;3:TR',
`merge_account_id` int(11) DEFAULT NULL,
`interact_people_uid` varchar(255) DEFAULT NULL,
`interact_people_id` varchar(255) DEFAULT NULL,
`is_sync_to_interact` tinyint(4) DEFAULT '0' COMMENT '0:not synced;1:synced;2:xml-error',
`sync_interact_date` timestamp NULL DEFAULT NULL,
`update_current_team` tinyint(1) DEFAULT '0',
`parsed_org_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `company_user_id` (`company_user_id`),
KEY `org` (`organisation_id`),
KEY `flag_company` (`flag_company`),
KEY `company_id` (`company_id`,`company_user_id`),
KEY `zendesk_user_id` (`zendesk_user_id`,`is_sync_zendesk`),
KEY `zendesk_sync_date` (`zendesk_sync_date`),
KEY `mod_date` (`mod_date`),
KEY `sync_to_interact` (`is_sync_to_interact`,`interact_people_uid`,`status`) USING BTREE,
KEY `sync_interact_date` (`sync_interact_date`,`mod_date`),
KEY `status` (`account_number`,`status`) USING BTREE,
KEY `username` (`status`,`username`(4)) USING BTREE,
KEY `rs_comp_user` (`rs_company_user_id`,`company_id`,`is_eb_migrated`),
KEY `getOrganisation` (`company_id`,`company_user_id`),
KEY `username_2` (`username`),
KEY `eb_reff_id` (`eb_reff_id`),
KEY `get_index_comp` (`company_user_id`,`is_eb_migrated`)
) ENGINE=InnoDB AUTO_INCREMENT=557225 DEFAULT CHARSET=utf8;
CREATE TABLE `company_team_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`base_company_id` int(11) DEFAULT NULL,
`team_id` int(11) NOT NULL,
`status` int(11) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`last_mod_user_id` int(11) NOT NULL,
`mod_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created_user_id` int(11) NOT NULL,
`created_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`is_cb_migrated` tinyint(4) DEFAULT '0',
`merge_account_id` int(11) DEFAULT NULL,
`is_eb_migrated` tinyint(4) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `teamID` (`team_id`),
KEY `user_id` (`user_id`),
KEY `status` (`status`),
KEY `end_date` (`end_date`),
KEY `getTeam` (`base_company_id`,`user_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=586851 DEFAULT CHARSET=utf8;
Try this one, move company_team_users next to the base table and use JOIN(this will short list the result based on the filter) instead of LEFT JOIN to shorten the result of the first two tables. Then remove other column filter in where clause and include it in your join.
SELECT
cs.delivery_consultant_id AS user_id,
interview.id AS interview_id,
date(interview.interview_date) AS interview_date,
date(interview.mod_date) AS interview_mod_date,
count(interview.id) AS kpi_item_count,
cu.division_id,
bu.organisation_id,
ctu.team_id
FROM es_shortlist AS cs
JOIN company_team_users AS ctu ON ctu.user_id = cs.delivery_consultant_id AND ISNULL(ctu.end_date) AND ctu.status = 2
LEFT JOIN tms_vacancies AS vac ON cs.vacancy_id = vac.id
LEFT JOIN es_applications AS app ON app.vacancy_id = vac.id AND app.candidate_id = cs.candidate_id
LEFT JOIN tms_interviews AS interview ON app.id = interview.application_id
LEFT JOIN company_users AS cu ON cu.id = cs.delivery_consultant_id
LEFT JOIN base_users AS bu ON bu.company_user_id = cs.delivery_consultant_id AND bu.company_id = cu.base_company_id
WHERE cs.delivery_consultant_id ='53521'
GROUP BY cs.delivery_consultant_id, ctu.team_id
Additional Note
Add index to those table columns that are commonly used in joins and where clause;
Ex:
es_shortlist (delivery_consultant_id)
company_team_users (user_id, end_date, status)
es_applications (vacancy_id, candidate_id)
tms_interviews (application_id)
base_users (company_user_id, company_id)

Indexes in Mysql table

I have such tables:
CREATE TABLE `skadate_newsfeed_action` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`entityId` int(11) NOT NULL,
`entityType` varchar(100) NOT NULL,
`feature` varchar(100) NOT NULL,
`data` longtext NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'active',
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`visibility` int(11) NOT NULL,
`privacy` enum('everybody','friends_only') NOT NULL DEFAULT 'everybody',
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
KEY `privacy` (`visibility`),
KEY `updateTime` (`updateTime`),
KEY `entity` (`entityType`,`entityId`)
) ENGINE=MyISAM;
CREATE TABLE `skadate_profile` (
`profile_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(32) NOT NULL DEFAULT '',
`password` varchar(40) NOT NULL,
`sex` bigint(20) DEFAULT NULL,
`match_sex` bigint(20) DEFAULT NULL,
`birthdate` date NOT NULL DEFAULT '0000-00-00',
`headline` varchar(128) DEFAULT '',
`general_description` text,
`match_agerange` varchar(6) DEFAULT NULL,
`custom_location` varchar(255) DEFAULT NULL,
`country_id` char(2) NOT NULL DEFAULT '',
`zip` varchar(10) DEFAULT NULL,
`state_id` varchar(5) DEFAULT NULL,
`city_id` int(11) DEFAULT '0',
`join_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`activity_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`membership_type_id` int(10) unsigned NOT NULL DEFAULT '18',
`affiliate_id` int(8) unsigned NOT NULL DEFAULT '0',
`email_verified` enum('undefined','yes','no') NOT NULL DEFAULT 'undefined',
`reviewed` enum('n','y') NOT NULL DEFAULT 'n',
`has_photo` enum('n','y') NOT NULL DEFAULT 'n',
`has_media` enum('n','y') NOT NULL DEFAULT 'n',
`status` enum('active','on_hold','suspended') NOT NULL DEFAULT 'active',
`featured` enum('n','y') NOT NULL DEFAULT 'n',
`register_invite_score` tinyint(3) NOT NULL DEFAULT '0',
`rate_score` tinyint(3) unsigned NOT NULL DEFAULT '0',
`rates` bigint(20) unsigned NOT NULL DEFAULT '0',
`language_id` int(10) unsigned NOT NULL DEFAULT '0',
`join_ip` int(11) unsigned NOT NULL DEFAULT '0',
`neigh_location` enum('country','state','city','zip') DEFAULT NULL,
`neigh_location_distance` int(10) unsigned NOT NULL DEFAULT '0',
`bg_color` varchar(32) DEFAULT NULL,
`bg_image` varchar(32) DEFAULT NULL,
`bg_image_url` varchar(255) DEFAULT NULL,
`bg_image_mode` tinyint(1) DEFAULT NULL,
`bg_image_status` enum('active','approval') NOT NULL DEFAULT 'active',
`has_music` enum('n','y') NOT NULL DEFAULT 'n',
`is_private` tinyint(1) NOT NULL DEFAULT '0',
`subscription_id_offerit` text,
PRIMARY KEY (`profile_id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
KEY `membership_id` (`membership_type_id`),
KEY `zip` (`zip`),
KEY `country_id` (`country_id`),
KEY `state_id` (`state_id`),
KEY `city_id` (`city_id`),
KEY `sex` (`sex`),
KEY `match_sex` (`match_sex`),
KEY `activity_stamp` (`activity_stamp`),
KEY `join_stamp` (`join_stamp`),
KEY `birthdate` (`birthdate`),
KEY `featured` (`featured`,`has_photo`,`activity_stamp`)
) ENGINE=MyISAM;
And try to perform this query:
SELECT DISTINCT `na`.*
FROM `skadate_newsfeed_action` AS `na`
LEFT JOIN `skadate_profile` AS `profile` ON ( `na`.`userId` = `profile`.`profile_id` )
WHERE ( profile.email_verified='yes' OR profile.email_verified='no' OR profile.email_verified='undefined' )
AND `profile`.`status`='active' AND `na`.`status`='active' AND `na`.`privacy`='everybody'
AND `na`.`visibility` & 1 AND `na`.`updateTime` < 1455885224
ORDER BY `na`.`updateTime` DESC, `na`.`id` DESC
LIMIT 0, 10
But when I see EXPLAIN:
Maybe someone can help me, how I can improve this query?
If you want records from only one table, then use exists rather than a join and select distinct. So:
SELECT na.*
FROM `skadate_newsfeed_action` na
WHERE EXISTS (SELECT 1
FROM skadate_profile p
WHERE na.userId = p.profile_id AND
p.email_verified IN ('yes', 'no', 'undefined') AND
p.status = 'active'
) AND
na.status = 'active' AND
na.privacy = 'everybody' AND
na.visibility & 1 > 0 AND
na.updateTime < 1455885224
ORDER BY na.`updateTime` DESC, na.`id` DESC
LIMIT 0, 10;
For this query, you want an index on skadate_profile(profile_id, status, verified). Also, the following index is probably helpful: skadate_newsfeed_action(status, privacy, updateTime, visibility, userId).
This is probably because of the DISTICT keyword. To remove duplicates MySQL needs to sort the result by every selected column.

Mysql errno 150 cannot create buddies

I tired checking the relationship of the two tables, and matched everything up to see if they were using different engines. But for some reason I cannot create the "buddies" table:
DROP TABLE IF EXISTS `buddies`;
CREATE TABLE `buddies` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`characterid` int(11) NOT NULL,
`buddyid` int(11) NOT NULL,
`pending` tinyint(4) NOT NULL DEFAULT '0',
`groupname` varchar(16) NOT NULL DEFAULT 'ETC',
PRIMARY KEY (`id`),
KEY `buddies_ibfk_1` (`characterid`),
CONSTRAINT `buddies_ibfk_1` FOREIGN KEY (`characterid`) REFERENCES `characters` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7998 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
DROP TABLE IF EXISTS `characters`;
CREATE TABLE `characters` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`accountid` int(11) NOT NULL DEFAULT '0',
`world` tinyint(1) NOT NULL DEFAULT '0',
`name` varchar(13) NOT NULL DEFAULT '',
`level` int(3) NOT NULL DEFAULT '0',
`exp` int(11) NOT NULL DEFAULT '0',
`str` int(5) NOT NULL DEFAULT '0',
`dex` int(5) NOT NULL DEFAULT '0',
`luk` int(5) NOT NULL DEFAULT '0',
`int` int(5) NOT NULL DEFAULT '0',
`hp` int(5) NOT NULL DEFAULT '0',
`mp` int(5) NOT NULL DEFAULT '0',
`maxhp` int(5) NOT NULL DEFAULT '0',
`maxmp` int(5) NOT NULL DEFAULT '0',
`meso` int(11) NOT NULL DEFAULT '0',
`hpApUsed` int(5) NOT NULL DEFAULT '0',
`job` int(5) NOT NULL DEFAULT '0',
`skincolor` tinyint(1) NOT NULL DEFAULT '0',
`gender` tinyint(1) NOT NULL DEFAULT '0',
`fame` int(5) NOT NULL DEFAULT '0',
`hair` int(11) NOT NULL DEFAULT '0',
`face` int(11) unsigned NOT NULL DEFAULT '0',
`faceMarking` int(11) NOT NULL DEFAULT '0',
`ap` int(11) NOT NULL DEFAULT '0',
`map` int(11) NOT NULL DEFAULT '0',
`spawnpoint` int(3) NOT NULL DEFAULT '0',
`gm` int(3) NOT NULL DEFAULT '0',
`party` int(11) NOT NULL DEFAULT '0',
`buddyCapacity` int(11) NOT NULL DEFAULT '25',
`createdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`guildid` int(10) unsigned NOT NULL DEFAULT '0',
`guildrank` tinyint(1) unsigned NOT NULL DEFAULT '5',
`allianceRank` tinyint(1) unsigned NOT NULL DEFAULT '5',
`guildContribution` int(11) NOT NULL DEFAULT '0',
`pets` varchar(13) NOT NULL DEFAULT '-1,-1,-1',
`sp` varchar(255) NOT NULL DEFAULT '0,0,0,0,0,0,0,0,0,0',
`subcategory` int(11) NOT NULL DEFAULT '0',
`rank` int(11) NOT NULL DEFAULT '1',
`rankMove` int(11) NOT NULL DEFAULT '0',
`jobRank` int(11) NOT NULL DEFAULT '1',
`jobRankMove` int(11) NOT NULL DEFAULT '0',
`marriageId` int(11) NOT NULL DEFAULT '0',
`familyid` int(11) NOT NULL DEFAULT '0',
`seniorid` int(11) NOT NULL DEFAULT '0',
`junior1` int(11) NOT NULL DEFAULT '0',
`junior2` int(11) NOT NULL DEFAULT '0',
`currentrep` int(11) NOT NULL DEFAULT '0',
`totalrep` int(11) NOT NULL DEFAULT '0',
`gachexp` int(11) NOT NULL DEFAULT '0',
`fatigue` tinyint(4) NOT NULL DEFAULT '0',
`charm` mediumint(7) NOT NULL DEFAULT '0',
`craft` mediumint(7) NOT NULL DEFAULT '0',
`charisma` mediumint(7) NOT NULL DEFAULT '0',
`will` mediumint(7) NOT NULL DEFAULT '0',
`sense` mediumint(7) NOT NULL DEFAULT '0',
`insight` mediumint(7) NOT NULL DEFAULT '0',
`totalWins` int(11) NOT NULL DEFAULT '0',
`totalLosses` int(11) NOT NULL DEFAULT '0',
`pvpExp` int(11) NOT NULL DEFAULT '0',
`pvpPoints` int(11) NOT NULL DEFAULT '0',
`rebirths` int(11) NOT NULL DEFAULT '0',
`prefix` varchar(45) DEFAULT NULL,
`reborns` int(11) NOT NULL DEFAULT '0',
`apstorage` int(11) NOT NULL DEFAULT '0',
`elf` int(11) NOT NULL DEFAULT '0',
`honourExp` int(11) NOT NULL DEFAULT '0',
`honourLevel` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `accountid` (`accountid`),
KEY `guildid` (`guildid`),
KEY `familyid` (`familyid`),
KEY `marriageId` (`marriageId`),
KEY `seniorid` (`seniorid`)
) ENGINE=InnoDB AUTO_INCREMENT=124 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
Well every time I execute above, I get:Error Code: 1005. Can't create table (errno: 150) 0.000 sec
Put the buddies last so it can reference characters.
http://sqlfiddle.com/#!2/b359c
Have you tried changing the order of the create tables? Buddies is trying to create a table with a foreign key constraint off of characters which may not exist yet.

MySQL foreign key to another foreign key

The idea is quite simple: i have three (or more) tables
- master_tbl (id, something, somethingelse)
- tbl2 (id, ....)
- tbl3 (id, ....)
Now what i want is a foreign key relationship, such as tbl3.id would point to tbl2.id and tbl2.id would point to master_tbl.id - all foreign keys are ON UPDATE CASCADE and ON DELETE CASCADE. What I'll get from this is that when I delete a record from tbl2, its tbl3 equivalent will get erased as well, but not master_tbl. When I delete a record from master_tbl, all three tables get erased.
When I try to create the foreign key on tbl3.id->tbl2.id, I get mysql error 150 - can't create table (the tbl2.id->master_tbl.id is already created).
My MySQL version is 5.1.46. Any ideas why this might be?
EDIT: table definitions
smf_members aka master_table
-- Table "smf_members" DDL
CREATE TABLE `smf_members` (
`id_member` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`member_name` varchar(80) NOT NULL DEFAULT '',
`date_registered` int(10) unsigned NOT NULL DEFAULT '0',
`posts` mediumint(8) unsigned NOT NULL DEFAULT '0',
`id_group` smallint(5) unsigned NOT NULL DEFAULT '0',
`lngfile` varchar(255) NOT NULL DEFAULT '',
`last_login` int(10) unsigned NOT NULL DEFAULT '0',
`real_name` varchar(255) NOT NULL DEFAULT '',
`instant_messages` smallint(5) NOT NULL DEFAULT '0',
`unread_messages` smallint(5) NOT NULL DEFAULT '0',
`new_pm` tinyint(3) unsigned NOT NULL DEFAULT '0',
`buddy_list` text NOT NULL,
`pm_ignore_list` varchar(255) NOT NULL DEFAULT '',
`pm_prefs` mediumint(8) NOT NULL DEFAULT '0',
`mod_prefs` varchar(20) NOT NULL DEFAULT '',
`message_labels` text NOT NULL,
`passwd` varchar(64) NOT NULL DEFAULT '',
`openid_uri` text NOT NULL,
`email_address` varchar(255) NOT NULL DEFAULT '',
`personal_text` varchar(255) NOT NULL DEFAULT '',
`gender` tinyint(4) unsigned NOT NULL DEFAULT '0',
`birthdate` date NOT NULL DEFAULT '0001-01-01',
`website_title` varchar(255) NOT NULL DEFAULT '',
`website_url` varchar(255) NOT NULL DEFAULT '',
`location` varchar(255) NOT NULL DEFAULT '',
`icq` varchar(255) NOT NULL DEFAULT '',
`aim` varchar(255) NOT NULL DEFAULT '',
`yim` varchar(32) NOT NULL DEFAULT '',
`msn` varchar(255) NOT NULL DEFAULT '',
`hide_email` tinyint(4) NOT NULL DEFAULT '0',
`show_online` tinyint(4) NOT NULL DEFAULT '1',
`time_format` varchar(80) NOT NULL DEFAULT '',
`signature` text NOT NULL,
`time_offset` float NOT NULL DEFAULT '0',
`avatar` varchar(255) NOT NULL DEFAULT '',
`pm_email_notify` tinyint(4) NOT NULL DEFAULT '0',
`karma_bad` smallint(5) unsigned NOT NULL DEFAULT '0',
`karma_good` smallint(5) unsigned NOT NULL DEFAULT '0',
`usertitle` varchar(255) NOT NULL DEFAULT '',
`notify_announcements` tinyint(4) NOT NULL DEFAULT '1',
`notify_regularity` tinyint(4) NOT NULL DEFAULT '1',
`notify_send_body` tinyint(4) NOT NULL DEFAULT '0',
`notify_types` tinyint(4) NOT NULL DEFAULT '2',
`member_ip` varchar(255) NOT NULL DEFAULT '',
`member_ip2` varchar(255) NOT NULL DEFAULT '',
`secret_question` varchar(255) NOT NULL DEFAULT '',
`secret_answer` varchar(64) NOT NULL DEFAULT '',
`id_theme` tinyint(4) unsigned NOT NULL DEFAULT '0',
`is_activated` tinyint(3) unsigned NOT NULL DEFAULT '1',
`validation_code` varchar(10) NOT NULL DEFAULT '',
`id_msg_last_visit` int(10) unsigned NOT NULL DEFAULT '0',
`additional_groups` varchar(255) NOT NULL DEFAULT '',
`smiley_set` varchar(48) NOT NULL DEFAULT '',
`id_post_group` smallint(5) unsigned NOT NULL DEFAULT '0',
`total_time_logged_in` int(10) unsigned NOT NULL DEFAULT '0',
`password_salt` varchar(255) NOT NULL DEFAULT '',
`ignore_boards` text NOT NULL,
`warning` tinyint(4) NOT NULL DEFAULT '0',
`passwd_flood` varchar(12) NOT NULL DEFAULT '',
`pm_receive_from` tinyint(4) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`id_member`),
KEY `member_name` (`member_name`),
KEY `real_name` (`real_name`),
KEY `date_registered` (`date_registered`),
KEY `id_group` (`id_group`),
KEY `birthdate` (`birthdate`),
KEY `posts` (`posts`),
KEY `last_login` (`last_login`),
KEY `lngfile` (`lngfile`(30)),
KEY `id_post_group` (`id_post_group`),
KEY `warning` (`warning`),
KEY `total_time_logged_in` (`total_time_logged_in`),
KEY `id_theme` (`id_theme`)
) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=utf8;
cyp_users aka tbl2
-- Table "cyp_users" DDL
CREATE TABLE `cyp_users` (
`id` mediumint(8) unsigned NOT NULL,
`role` varchar(255) NOT NULL DEFAULT 'unregistered',
PRIMARY KEY (`id`),
CONSTRAINT `cyp_users_ibfk_1` FOREIGN KEY (`id`) REFERENCES `smf_members` (`id_member`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
cyp_vip_users aka tbl3
-- Table "cyp_vip_users" DDL
CREATE TABLE `cyp_vip_users` (
`id` mediumint(8) NOT NULL,
`od` date NOT NULL,
`do` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The ID's of tbl2 and tbl3 are different types.
Foreign Keys need to be of the same type.
tbl2.ID is UNSIGNED
tbl3.ID is SIGNED

mysql query times out after certain periodof inactivity - query cache issue i think

using latest mysql server,
after some period of inactivity my website sqls times out (only some not all)
these queries are not newly written existing queries.
Testing results
when execute a simple sql like (select count(*) from products) this works fine all the time.
when execute below sql
SELECT products.pid
FROM
products INNER JOIN
catalog ON products.cid=catalog.cid
WHERE
products.is_visible = 'Yes' AND (
products.inventory_control = 'No' OR
products.stock > 0 OR
products.inventory_rule = 'OutOfStock' OR (
products.inventory_control = 'AttrRuleInc' AND
products.stock >= 0
)
) AND products.is_home = 'Yes'
GROUP BY products.pid
it times out ( Note it will not timeout all the time, this happens after 1 hour or 2 hour of inactivity)
The very first execution takes 30+ seconds and it times out and after that the above executing SQL 2 to 3 times the 4th time onwards it executes fast in 3 to 5 seconds
after 1 or 2 hour inactivity this pattern repeats.
I did not do any changes in settings.
i have 2 sites on this server.
1st server has 20,000 rows of in database ( this one works fine ) 2nd site has 150,000 rows in the databse ( this one is having this issue)
So this is something "query time out"
i do not think this is something on SQL settings, if it is then i should see this behavior on both sites
**** here is the table structure for which the issue with timeout / taking 40 seconds
#this structure has 2 additional keys we created
# KEY `product_id` (`product_id`),
# KEY `product_no` (`product_no`)
# this one is having issues
--
-- Table structure for table `products` on lpbatt database server
--
DROP TABLE IF EXISTS `products`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `products` (
`pid` int(10) unsigned NOT NULL auto_increment,
`cid` int(10) unsigned NOT NULL default '0',
`manufacturer_id` int(10) unsigned NOT NULL default '0',
`is_visible` enum('Yes','No') NOT NULL default 'Yes',
`is_hotdeal` enum('Yes','No') NOT NULL default 'No',
`is_home` enum('Yes','No') NOT NULL default 'No',
`is_taxable` enum('Yes','No') NOT NULL default 'Yes',
`is_dollar_days` enum('Yes','No') NOT NULL default 'No',
`is_google_co` enum('Yes','No') NOT NULL default 'Yes',
`is_doba` enum('Yes','No') NOT NULL default 'No',
`is_locked` enum('Yes','No') NOT NULL default 'No',
`inventory_control` enum('Yes','AttrRuleExc','AttrRuleInc','No') NOT NULL default 'No',
`inventory_rule` enum('Hide','OutOfStock') NOT NULL default 'Hide',
`stock` int(10) NOT NULL default '0',
`stock_warning` int(10) NOT NULL default '0',
`weight` decimal(10,2) unsigned NOT NULL default '0.00',
`free_shipping` enum('Yes','No') NOT NULL default 'No',
`digital_product` enum('Yes','No') NOT NULL default 'No',
`digital_product_file` varchar(255) NOT NULL default '',
`cost` decimal(20,5) unsigned NOT NULL default '0.00000',
`price` decimal(20,5) unsigned NOT NULL default '0.00000',
`price2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_1` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_3` decimal(20,5) unsigned NOT NULL default '0.00000',
`shipping_price` decimal(20,5) unsigned NOT NULL default '0.00000',
`tax_class_id` int(10) unsigned NOT NULL default '0',
`tax_rate` decimal(20,5) NOT NULL default '-1.00000',
`call_for_price` enum('Yes','No') NOT NULL default 'No',
`priority` int(11) NOT NULL default '0',
`attributes_count` int(11) NOT NULL default '0',
`min_order` int(10) NOT NULL default '1',
`max_order` int(10) unsigned NOT NULL default '0',
`added` datetime NOT NULL default '0000-00-00 00:00:00',
`products_location_id` int(10) unsigned NOT NULL default '0',
`url_hash` varchar(32) NOT NULL default '',
`url_default` varchar(128) NOT NULL default '',
`url_custom` varchar(128) NOT NULL default '',
`product_id` int(64) NOT NULL default '0',
`product_sku` varchar(64) NOT NULL default '',
`product_upc` varchar(64) NOT NULL default '',
`case_pack` int(11) NOT NULL default '-1',
`inter_pack` int(11) NOT NULL default '-1',
`gift_quantity` int(10) unsigned NOT NULL default '0',
`dimension_width` decimal(10,2) NOT NULL default '0.00',
`dimension_length` decimal(10,2) NOT NULL default '0.00',
`dimension_height` decimal(10,2) NOT NULL default '0.00',
`image_location` enum('Local','Web') NOT NULL default 'Local',
`image_url` varchar(255) NOT NULL default '',
`image_alt_text` varchar(255) NOT NULL default '',
`tmp_manufacturer` varchar(30) default NULL,
`tmp_family` varchar(30) default NULL,
`tmp_series` varchar(30) default NULL,
`tmp_model` varchar(30) default NULL,
`tmp_ptype` varchar(30) default NULL,
`product_no` varchar(40) default NULL,
`part_no` varchar(300) default NULL,
`spec_1` varchar(7) default NULL,
`spec_2` varchar(7) default NULL,
`spec_3` varchar(7) default NULL,
`spec_4` varchar(40) default NULL,
`title` varchar(255) NOT NULL default '',
`meta_keywords` text NOT NULL,
`meta_title` text NOT NULL,
`meta_description` text NOT NULL,
`overview` text,
`description` text,
`zoom_option` enum('global','none','zoom','magnify','magicthumb','imagelayover') NOT NULL default 'global',
PRIMARY KEY (`pid`),
KEY `cid` (`cid`),
KEY `is_visible` (`is_visible`),
KEY `url_hash` (`url_hash`),
KEY `product_id` (`product_id`),
KEY `product_no` (`product_no`)
) ENGINE=MyISAM AUTO_INCREMENT=1630746530 DEFAULT CHARSET=utf8;
SET character_set_client = #saved_cs_client;
below is the table structure which is running on another server but working fine
#see the products table structure and catalog table structure
#this is fine on this server
# --------------------------------------------------------
# Host: laptopnbparts.com
# Database: laptopnbpartscom
# Server version: 5.0.77
# Server OS: redhat-linux-gnu
# HeidiSQL version: 5.0.0.3222
# Date/time: 2010-06-25 18:13:33
# --------------------------------------------------------
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
# Dumping structure for table laptopnbpartscom.catalog
CREATE TABLE IF NOT EXISTS `catalog` (
`cid` int(10) unsigned NOT NULL auto_increment,
`parent` int(10) unsigned NOT NULL default '0',
`level` int(10) unsigned NOT NULL default '0',
`priority` smallint(5) unsigned NOT NULL default '5',
`is_visible` enum('Yes','No') NOT NULL default 'Yes',
`list_subcats` enum('Yes','No') NOT NULL default 'No',
`url_hash` varchar(32) NOT NULL default '',
`url_default` varchar(128) NOT NULL default '',
`url_custom` varchar(128) NOT NULL default '',
`key_name` varchar(255) NOT NULL default '',
`meta_keywords` text,
`meta_title` text,
`meta_description` text,
`category_header` varchar(255) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`description` text,
`description_bottom` text,
`category_path` text,
PRIMARY KEY (`cid`),
KEY `parent` (`parent`),
KEY `level` (`level`),
KEY `priority` (`priority`),
KEY `url_hash` (`url_hash`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# Data exporting was unselected.
# Dumping structure for table laptopnbpartscom.products
CREATE TABLE IF NOT EXISTS `products` (
`pid` int(10) unsigned NOT NULL auto_increment,
`cid` int(10) unsigned NOT NULL default '0',
`manufacturer_id` int(10) unsigned NOT NULL default '0',
`is_visible` enum('Yes','No') NOT NULL default 'Yes',
`is_hotdeal` enum('Yes','No') NOT NULL default 'No',
`is_home` enum('Yes','No') NOT NULL default 'No',
`is_taxable` enum('Yes','No') NOT NULL default 'Yes',
`is_dollar_days` enum('Yes','No') NOT NULL default 'No',
`is_google_co` enum('Yes','No') NOT NULL default 'Yes',
`is_doba` enum('Yes','No') NOT NULL default 'No',
`is_locked` enum('Yes','No') NOT NULL default 'No',
`inventory_control` enum('Yes','AttrRuleExc','AttrRuleInc','No') NOT NULL default 'No',
`inventory_rule` enum('Hide','OutOfStock') NOT NULL default 'Hide',
`stock` int(10) NOT NULL default '0',
`stock_warning` int(10) NOT NULL default '0',
`weight` decimal(10,2) unsigned NOT NULL default '0.00',
`free_shipping` enum('Yes','No') NOT NULL default 'No',
`digital_product` enum('Yes','No') NOT NULL default 'No',
`digital_product_file` varchar(255) NOT NULL default '',
`cost` decimal(20,5) unsigned NOT NULL default '0.00000',
`price` decimal(20,5) unsigned NOT NULL default '0.00000',
`price2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_1` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_3` decimal(20,5) unsigned NOT NULL default '0.00000',
`shipping_price` decimal(20,5) unsigned NOT NULL default '0.00000',
`tax_class_id` int(10) unsigned NOT NULL default '0',
`tax_rate` decimal(20,5) NOT NULL default '-1.00000',
`call_for_price` enum('Yes','No') NOT NULL default 'No',
`priority` int(11) NOT NULL default '0',
`attributes_count` int(11) NOT NULL default '0',
`min_order` int(10) NOT NULL default '1',
`max_order` int(10) unsigned NOT NULL default '0',
`added` datetime NOT NULL default '0000-00-00 00:00:00',
`products_location_id` int(10) unsigned NOT NULL default '0',
`url_hash` varchar(32) NOT NULL default '',
`url_default` varchar(128) NOT NULL default '',
`url_custom` varchar(128) NOT NULL default '',
`product_id` varchar(64) NOT NULL default '',
`product_sku` varchar(64) NOT NULL default '',
`product_upc` varchar(64) NOT NULL default '',
`case_pack` int(11) NOT NULL default '-1',
`inter_pack` int(11) NOT NULL default '-1',
`gift_quantity` int(10) unsigned NOT NULL default '0',
`dimension_width` decimal(10,2) NOT NULL default '0.00',
`dimension_length` decimal(10,2) NOT NULL default '0.00',
`dimension_height` decimal(10,2) NOT NULL default '0.00',
`image_location` enum('Local','Web') NOT NULL default 'Local',
`image_url` varchar(255) NOT NULL default '',
`image_alt_text` varchar(255) NOT NULL default '',
`tmp_manufacturer` varchar(30) default NULL,
`tmp_series` varchar(30) default NULL,
`tmp_model` varchar(30) default NULL,
`tmp_ptype` varchar(30) default NULL,
`product_no` varchar(60) default NULL,
`part_no` varchar(60) default NULL,
`watt_volt_amp` varchar(100) default NULL,
`title` varchar(255) NOT NULL default '',
`meta_keywords` text NOT NULL,
`meta_title` text NOT NULL,
`meta_description` text NOT NULL,
`overview` text,
`description` text,
`zoom_option` enum('global','none','zoom','magnify','magicthumb','imagelayover') NOT NULL default 'global',
PRIMARY KEY (`pid`),
KEY `cid` (`cid`),
KEY `is_visible` (`is_visible`),
KEY `url_hash` (`url_hash`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# Data exporting was unselected.
/*!40101 SET SQL_MODE=#OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
Try using CASE WHEN instead of the multiple OR statements in the WHERE clause and evaluate on the client side.
OR is a well-known performance killer as MySQL cannot usually apply INDEXes and must check every single statement in order to filter out rows.