SQL Error After Table Import/Export - mysql

Hey I am trying to export the product tables from one opencart store to another,but I have had some issues doing that.
The majority of tables had 0 problems to import/export,however the product table gives the following error.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 37
Here is the code itself.
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(13) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL,
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL,
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL,
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL,
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`viewed` int(5) NOT NULL,
`measurement_class_id` int(11) NOT NULL DEFAULT '0',
`maximum` int(11) NOT NULL DEFAULT '0',
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'COMMENT
) ;

Hi you are getting error because you have not completed last line comment code for "style" column do one of the below options:
a) Add comment description
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0' COMMENT "comment description"
b)remove comment description
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'

remove the COMMENT at the last line:
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(13) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL,
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL,
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL,
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL,
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`viewed` int(5) NOT NULL,
`measurement_class_id` int(11) NOT NULL DEFAULT '0',
`maximum` int(11) NOT NULL DEFAULT '0',
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'
) ;
sample
MariaDB []> CREATE TABLE `product` ( `product_id` int(11) NOT NULL, `model` varchar(64) NOT NULL, `sku` varchar(64) NOT NULL, `upc` varchar(12) NOT NULL, `ean` varchar(14) NOT NULL, `jan` varchar(13) NOT NULL, `isbn` varchar(13) NOT NULL, `mpn` varchar(64) NOT NULL, `location` varchar(128) NOT NULL, `quantity` int(4) NOT NULL, `stock_status_id` int(11) NOT NULL, `image` varchar(255) DEFAULT NULL, `manufacturer_id` int(11) NOT NULL, `shipping` tinyint(1) NOT NULL DEFAULT '1', `price` decimal(15,4) NOT NULL DEFAULT '0.0000', `points` int(8) NOT NULL, `tax_class_id` int(11) NOT NULL, `date_available` date NOT NULL, `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000', `weight_class_id` int(11) NOT NULL, `length` decimal(15,8) NOT NULL DEFAULT '0.00000000', `width` decimal(15,8) NOT NULL DEFAULT '0.00000000', `height` decimal(15,8) NOT NULL DEFAULT '0.00000000', `length_class_id` int(11) NOT NULL, `subtract` tinyint(1) NOT NULL DEFAULT '1', `minimum` int(11) NOT NULL DEFAULT '1', `sort_order` int(11) NOT NULL, `status` tinyint(1) NOT NULL, `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `viewed` int(5) NOT NULL, `measurement_class_id` int(11) NOT NULL DEFAULT '0', `maximum` int(11) NOT NULL DEFAULT '0', `cost` decimal(15,4) NOT NULL DEFAULT '0.0000', `style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0' );
Query OK, 0 rows affected (0.32 sec)
MariaDB []> select version();
+---------------------+
| version() |
+---------------------+
| 10.1.10-MariaDB-log |
+---------------------+
1 row in set (0.00 sec)
MariaDB []>

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)

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.

Auto increment it's not working

I have a problem on my website with auto increment and I can't find a way to fix it...
It works with this table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`loginhash` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_level` int(10) NOT NULL DEFAULT '1',
`random` int(10) DEFAULT '0',
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`language` tinyint(4) NOT NULL DEFAULT '1',
`style` tinyint(4) NOT NULL DEFAULT '1',
`joined` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastconnect` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lip` bigint(11) DEFAULT '0',
`downloaded` bigint(20) DEFAULT '0',
`uploaded` bigint(20) DEFAULT '0',
`avatar` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`pid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flag` tinyint(1) unsigned NOT NULL DEFAULT '0',
`topicsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`postsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`torrentsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`cip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`time_offset` varchar(4) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`temp_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flags` bigint(20) unsigned NOT NULL DEFAULT '7345036',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id_level` (`id_level`),
KEY `pid` (`pid`),
KEY `cip` (`cip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
But it doesn't work with this table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`loginhash` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_level` int(10) NOT NULL DEFAULT '1',
`random` int(10) DEFAULT '0',
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`language` tinyint(4) NOT NULL DEFAULT '1',
`style` tinyint(4) NOT NULL DEFAULT '1',
`joined` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastconnect` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lip` bigint(11) DEFAULT '0',
`downloaded` bigint(20) DEFAULT '0',
`uploaded` bigint(20) DEFAULT '0',
`avatar` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`pid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flag` tinyint(1) unsigned NOT NULL DEFAULT '0',
`topicsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`postsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`torrentsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`cip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`time_offset` varchar(4) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`temp_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flags` bigint(20) unsigned NOT NULL DEFAULT '7345036',
`rank_switch` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`old_rank` varchar(12) COLLATE utf8_unicode_ci NOT NULL DEFAULT '3',
`timed_rank` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`seedbonus` decimal(5,1) NOT NULL,
`on_forum` int(11) NOT NULL DEFAULT '0',
`on_topic` int(11) NOT NULL DEFAULT '0',
`custom_title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`invites` int(10) NOT NULL DEFAULT '0',
`invited_by` int(10) NOT NULL DEFAULT '0',
`invitedate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`donor` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`banfromforum` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id_level` (`id_level`),
KEY `pid` (`pid`),
KEY `cip` (`cip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Can anyone tell me where the problem is?

Err 1292 - Truncated incorrect DOUBLE value

I have this two tables:
OLD:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`crdate` int(11) DEFAULT NULL,
`fb_id` text,
`email` varchar(64) DEFAULT NULL,
`fb_access_token` varchar(256) DEFAULT NULL,
`display_name` varchar(128) DEFAULT NULL,
`first_name` varchar(128) DEFAULT NULL,
`middle_name` varchar(128) DEFAULT NULL,
`last_name` varchar(128) DEFAULT NULL,
`gender` varchar(128) DEFAULT NULL,
`timezone` tinyint(4) DEFAULT NULL,
`locale` varchar(16) DEFAULT NULL,
`fb_profile_url` text,
`balanced_id` text,
`token` varchar(100) DEFAULT NULL,
`address_id` int(11) DEFAULT NULL,
`admin` tinyint(1) DEFAULT '0',
`zip` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
NEW:
CREATE TABLE IF NOT EXISTS `friendzy`.`users_new` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(64) NULL DEFAULT NULL,
`display_name` VARCHAR(128) NULL DEFAULT NULL,
`first_name` VARCHAR(128) NULL DEFAULT NULL,
`middle_name` VARCHAR(128) NULL DEFAULT NULL,
`last_name` VARCHAR(128) NULL DEFAULT NULL,
`gender` VARCHAR(128) NULL DEFAULT NULL,
`redis_token` VARCHAR(100) NULL DEFAULT NULL,
`admin` TINYINT(1) NULL DEFAULT '0',
`api_token` VARCHAR(255) NULL DEFAULT NULL,
`password` VARCHAR(255) NOT NULL,
`salt` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT '0',
`last_login` TIMESTAMP NULL DEFAULT NULL,
`locked` TINYINT(1) NULL DEFAULT NULL,
`expired` TINYINT(1) NOT NULL DEFAULT '0',
`expired_at` TIMESTAMP NULL DEFAULT NULL,
`confirmation_token` VARCHAR(255) NULL DEFAULT NULL,
`password_requested_at` TIMESTAMP NULL DEFAULT NULL,
`credentials_expired` TINYINT(1) NULL DEFAULT NULL,
`credentials_expire_at` TIMESTAMP NULL DEFAULT NULL,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modified` TIMESTAMP NULL DEFAULT NULL,
`modified_by` VARCHAR(255) NULL DEFAULT NULL,
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 164
DEFAULT CHARACTER SET = latin1;
And im trying yo copy the data from the old one to the new one with this sentence:
INSERT INTO `users_new` (`id`, `email`, `display_name`, `first_name`, `middle_name`, `last_name`, `gender`, `redis_token`, `admin`, `api_token`, `password`, `salt`, `enabled`, `last_login`, `locked`, `expired`, `expired_at`, `confirmation_token`, `password_requested_at`, `credentials_expired`, `credentials_expire_at`, `created`, `modified`, `modified_by`, `deleted_at`)
SELECT `users`.`id`, `users`.`email`, `users`.`display_name`, `users`.`first_name`, `users`.`middle_name`, `users`.`last_name`, `users`.`gender`, if(`users`.`token`, `users`.`token`, md5(1)), `users`.`admin`, md5(1), md5(1), md5(1), 1, NULL, 0, 0, NULL, md5(1), NULL, 0, NULL, NOW(), NOW(), 'UPDATE SCRIPT', NULL
FROM users;
But i get this error:
[Err] 1292 - Truncated incorrect DOUBLE value:
'4043a809f18ffc8be824064934797d3a'
This is one of those really crappy errors that gets thrown that might not have anything to do with the actual problem. My suggestion would be to change your
if(`users`.`token`, `users`.`token`, md5(1))
to
COALESCE(`users`.`token`, md5(1))
And see if it doesn't fix itself.

mysql query can run but some row is missing

Select sale.preselRef As ref,
sale.name,
sale.saledate,
sale.grandtotal,
sale.secid,
sale.invoicetype As typ,
Sum(sellnote.Paid) As cash,
Sum(sellnote.CreCard) As card,
Sum(sellnote.PCheck) As cheque,
Sum(sellnote.Compens) As compen,
sale.grandtotal - Sum(sellnote.Paid) - Sum(sellnote.CreCard) -
Sum(sellnote.PCheck) - Sum(sellnote.Compens) As balanc
From sale, sellnote
Where sale.preselRef = sellnote.Code
Group By sellnote.Code
Having sale.grandtotal - Sum(sellnote.Paid) - Sum(sellnote.CreCard) -
Sum(sellnote.PCheck) - Sum(sellnote.Compens) <> 0
CREATE TABLE IF NOT EXISTS `sale` (
`preselRef` int(12) NOT NULL,
`nameid` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`cusaddress` varchar(255) DEFAULT NULL,
`saledate` date DEFAULT NULL,
`datesent` date DEFAULT NULL,
`vat` decimal(4,2) DEFAULT NULL,
`total` decimal(10,2) DEFAULT NULL,
`vatinbill` decimal(10,2) DEFAULT NULL,
`grandtotal` decimal(10,2) DEFAULT NULL,
`distcount` decimal(10,2) NOT NULL DEFAULT '0.00',
`secid` int(11) DEFAULT NULL,
`invoicetype` enum('d','r','u','i','z') NOT NULL DEFAULT 'u',
`remark` varchar(255) DEFAULT NULL,
`paid` decimal(10,2) DEFAULT NULL,
`cardno` varchar(200) DEFAULT NULL,
`creditcardamount` decimal(10,2) DEFAULT NULL,
`Bankcard` varchar(200) DEFAULT NULL,
`cheqno` varchar(200) DEFAULT NULL,
`chequeamount` decimal(11,2) DEFAULT NULL,
`bankcheque` varchar(200) DEFAULT NULL,
`asgreeno` varchar(200) DEFAULT NULL,
`checkcleardate` date DEFAULT NULL,
`Checktype` enum('r','s') NOT NULL,
`datetime` datetime DEFAULT NULL,
`timestamp` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`preselRef`),
UNIQUE KEY `salein` (`preselRef`),
UNIQUE KEY `saleid` (`secid`,`invoicetype`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `sellnote` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Code` varchar(11) NOT NULL DEFAULT '',
`Pdtail` varchar(200) DEFAULT NULL,
`Paid` decimal(12,2) NOT NULL DEFAULT '0.00',
`CreCard` decimal(12,2) NOT NULL DEFAULT '0.00',
`PCheck` decimal(12,2) NOT NULL DEFAULT '0.00',
`Compens` decimal(12,2) NOT NULL DEFAULT '0.00',
`DateDue` date DEFAULT NULL,
`BillCan` enum('Y','N') NOT NULL DEFAULT 'N',
`Remark` varchar(150) DEFAULT NULL,
`staff` varchar(50) DEFAULT NULL,
`Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
KEY `Code` (`Code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Transaction Header' AUTO_INCREMENT=4943 ;
I have one row intable sale
4058 613 บริษัท เดอ ละไม จำกัด 171/4-9 หมู่4 ต.มะเร็ต อ.เกาะสมุย
จ.สุราษฎร์ธานี 2011-08-01 NULL 7.00 3271.03 228.97 3500.00 0.00 10069 i NULL 0.00 0.00 0.00 NULL NULL NULL
and do not have sellnote.Code = 4058 but the row of the table sale that have sale.preselRef it should show but it do not show I have a bout 5000 record and only this row that i found the problem plese help