MySQL Can't create table [errno: 150] - mysql

Getting the above error on the following script:
DROP TABLE IF EXISTS `incident`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `incident` (
`incident_id` int(11) NOT NULL auto_increment,
`patient_id` int(11) default NULL,
`history_id` int(11) default NULL,
`incident_date` datetime default NULL,
`incidentname_id` int(11) default NULL,
`incident_type_id` int(11) default NULL,
`pop_first_symptom` int(11) default NULL,
`first_symptom_date` datetime default NULL,
`similar_symptom_date` datetime default NULL,
`first_consultation_date` datetime default NULL,
`accident` int(1) default NULL,
`accident_date` datetime default NULL,
`pop_accident_state` int(11) default NULL,
`pop_condition_related_to` int(11) default NULL,
`flag_prothesis` char(1) collate utf8_unicode_ci NOT NULL default '0',
`flag_initial_placement` char(1) collate utf8_unicode_ci NOT NULL default '0',
`flag_orthodontics` char(1) collate utf8_unicode_ci NOT NULL default '0',
`flag_occupational_illness` char(1) collate utf8_unicode_ci NOT NULL default '0',
`box19` varchar(80) collate utf8_unicode_ci NOT NULL default '',
`color` varchar(8) collate utf8_unicode_ci NOT NULL default 'ffffff00',
`referrals_id` int(11) default NULL,
`referral_date` datetime default NULL,
`facility_id` int(11) default NULL,
`attorney_id` int(11) default NULL,
`attorney_notes` text collate utf8_unicode_ci,
`attorney_report_sent` date default NULL,
`attorney_deposition_date` date default NULL,
`attorney_trial_date` date default NULL,
`attorney_notice_filing` char(1) collate utf8_unicode_ci default NULL,
`attorney_final_settlement` date default NULL,
`effective_date` datetime default NULL,
`termination_date` datetime default NULL,
`date` datetime default NULL,
`pop_branch` int(11) default NULL,
`pop_status` int(1) default NULL,
`pop_eligibility` int(1) default NULL,
`partial_begin_date` datetime default NULL,
`partial_end_date` datetime default NULL,
`last_work_date` datetime default NULL,
`partial_return_to_work_date` datetime default NULL,
`total_begin_date` datetime default NULL,
`total_end_date` datetime default NULL,
`last_xray_date` datetime default NULL,
`last_seen_date` date default NULL,
`assumed_date` datetime default NULL,
`relinquished_date` datetime default NULL,
`radiographs` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`oral_images` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`models` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`date_appliance_placed` date default NULL,
`date_prior_placement` date default NULL,
`paymentSourceCode` varchar(10) collate utf8_unicode_ci NOT NULL default '',
`locatorCode` varchar(10) collate utf8_unicode_ci NOT NULL default '',
`SAExceptionCode` varchar(10) collate utf8_unicode_ci NOT NULL default '',
`flag_expanded` int(1) default NULL,
`flag_archived` int(1) default NULL,
`cda_reason_code` int(11) default NULL,
`is_extraction` int(11) default '0',
`extraction_tooth` varchar(10) collate utf8_unicode_ci default NULL,
`initial_placement_date` date default NULL,
`initial_placement_location` int(11) default '0',
`prosthesis_material` int(11) default '0',
PRIMARY KEY (`incident_id`),
KEY `patient_id_key` (`patient_id`),
CONSTRAINT `incident_ibfk_1` FOREIGN KEY (`patient_id`) REFERENCES `patient` (`patient_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9854 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET character_set_client = #saved_cs_client;
Here is the patient table giving me the same error:
DROP TABLE IF EXISTS `patient`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `patient` (
`patient_id` int(11) NOT NULL auto_increment,
`history_id` int(11) default NULL,
`person_id` int(11) default NULL,
`account_id` int(11) default NULL,
`display_id` varchar(10) collate utf8_unicode_ci default NULL,
`chart_num` varchar(10) collate utf8_unicode_ci default NULL,
`bridge_num` varchar(10) collate utf8_unicode_ci default NULL,
`provider_id` int(11) default NULL,
`office_id` int(11) default NULL,
`feeschedule_id` int(11) default NULL,
`pop_marital_status` int(11) default NULL,
`pop_employment` int(11) default NULL,
`new_patient_date` date default NULL,
`primary_ins_id` int(11) default NULL,
`primary_ins_type` char(1) collate utf8_unicode_ci default NULL,
`insured_id_number` varchar(30) collate utf8_unicode_ci default NULL,
`flag_special_insurance` char(1) collate utf8_unicode_ci default '0',
`flag_family_planning` char(1) collate utf8_unicode_ci default '0',
`flag_epstd` char(1) collate utf8_unicode_ci default '0',
`pop_relation_to_primary` int(11) default NULL,
`pop_relation_to_secondary` int(11) default NULL,
`pop_hipaa_release` int(11) default NULL,
`hipaa_release_date` date default NULL,
`patientNotes` text collate utf8_unicode_ci,
`autoremind_notification_type_id` int(11) NOT NULL default '0',
`ethnicity_id` int(11) default NULL,
`patient_status` int(1) NOT NULL default '0',
`created_by_module` char(1) collate utf8_unicode_ci NOT NULL default 'P',
`pop_patient_exception_code` int(11) default '0',
`pop_native_language` int(11) default '0',
`school_name` varchar(30) collate utf8_unicode_ci default NULL,
`graduation_date` date default NULL,
`hygienist_id` int(11) default NULL,
`school_address_id` int(11) default NULL,
`language_id` int(11) default NULL,
PRIMARY KEY (`patient_id`),
KEY `account_id_key` (`account_id`),
KEY `person_id_key` (`person_id`),
KEY `patient_status_index` (`patient_status`),
KEY `person_id_index` (`person_id`),
KEY `account_id_index` (`account_id`),
CONSTRAINT `patient_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`),
CONSTRAINT `patient_ibfk_2` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9855 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET character_set_client = #saved_cs_client;

I assume that your foreign key patient_id is not NULL. It must match to your referenced data type.
`patient_id` int(11) default NULL

Related

Improving MySQL Update Performance

I've got a process to import/update data from a CSV file. The process of saving that data from the csv to a "temp" table works fine. (temp_mls_transactions)
The next step is to update the existing records in the production table (tbl_mlsTransactions) with the data from the "temp" table. This step is causing the issue. IF it completes, its about 17 minutes for 7k records.
After the update is complete, we delete the common records from the "temp" table. Then insert the remaining records.
Here is the update query:
UPDATE tbl_MLSTransactions mt USE INDEX(idx_mlsNumber_association) INNER JOIN temp_mls_import temp USE INDEX (idx_mlsAssociationID_mlsID) ON mt.mlsNumber=temp.mlsNumber AND mt.mlsAssociationID=temp.mlsAssociationID
SET mt.activeStatusDate=temp.activeStatusDate,
mt.closeDate=temp.closeDate,
mt.closePrice=temp.closePrice,
mt.coListAgentMLSID=temp.coListAgentMLSID,
mt.coSellingAgentMLSID=temp.coSellingAgentMLSID,
mt.currentPrice=temp.currentPrice,
mt.dateAvailable=temp.dateAvailable,
mt.daysToClose=temp.daysToClose,
mt.daysToContract=temp.daysToContract,
mt.DOM=temp.DOM,
mt.expectedClosingDate=temp.expectedClosingDate,
mt.expirationDate=temp.expirationDate,
mt.address=temp.address,
mt.address2=temp.address2,
mt.city=temp.city,
mt.state=temp.state,
mt.zip=temp.zip,
mt.lastChangeType=temp.lastChangeType,
mt.lastDateAvailable=temp.lastDateAvailable,
mt.lastListPrice=temp.lastListPrice,
mt.lastStatus=temp.lastStatus,
mt.latitude=temp.latitude,
mt.listAgentMLSID=temp.listAgentMLSID,
mt.listPrice=temp.listPrice,
mt.listingContractDate=temp.listingContractDate,
mt.longitude=temp.longitude,
mt.originalListPrice=temp.originalListPrice,
mt.pendingDate=temp.pendingDate,
mt.propertyStatus=temp.propertyStatus,
mt.sellingAgentMLSID=temp.sellingAgentMLSID,
mt.status=temp.status,
mt.statusContractualSearchDate=temp.statusContractualSearchDate,
mt.withdrawnDate=temp.withdrawnDate,
mt.withdrawnStatus=temp.withdrawnStatus,
mt.listOfficeMLSID=temp.listOfficeMLSID,
mt.coListOfficeMLSID=temp.coListOfficeMLSID,
mt.sellingOfficeMLSID=temp.sellingOfficeMLSID,
mt.coSellingOfficeMLSID=temp.coSellingOfficeMLSID;
Here is the create statement for the "temp" table
CREATE TABLE `temp_mls_import` (
`tempID` int(11) NOT NULL AUTO_INCREMENT,
`activeStatusDate` date DEFAULT NULL,
`address` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`address2` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`city` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`state` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`zip` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`availability` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`bathsFull` int(11) DEFAULT NULL,
`bathsHalf` int(11) DEFAULT NULL,
`bathsTotal` decimal(10,2) DEFAULT NULL,
`bedsTotal` decimal(10,2) DEFAULT NULL,
`CDOM` int(11) DEFAULT NULL,
`closeDate` date DEFAULT NULL,
`closePrice` decimal(15,2) DEFAULT NULL,
`coListAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`coListOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`coSellingAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`coSellingOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`contractStatus` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`county` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`currentPrice` decimal(15,2) DEFAULT NULL,
`dateAvailable` date DEFAULT NULL,
`daysToClose` int(11) DEFAULT NULL,
`daysToContract` int(11) DEFAULT NULL,
`DOM` int(11) DEFAULT NULL,
`expectedClosingDate` date DEFAULT NULL,
`expectedLeaseDate` date DEFAULT NULL,
`expirationDate` date DEFAULT NULL,
`expirationRenewalDate` date DEFAULT NULL,
`lastChangeType` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`lastDateAvailable` date DEFAULT NULL,
`lastListPrice` decimal(15,2) DEFAULT NULL,
`lastStatus` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`latitude` float DEFAULT NULL,
`listAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`listOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`listPrice` decimal(15,2) DEFAULT NULL,
`listingContractDate` date DEFAULT NULL,
`longitude` float DEFAULT NULL,
`mlsNumber` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`offMarketDate` date DEFAULT NULL,
`officePrimaryBoardID` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`originalEntryTimestamp` timestamp NULL DEFAULT NULL,
`originalListPrice` decimal(15,2) DEFAULT NULL,
`PDOM` int(11) DEFAULT NULL,
`pendingDate` date DEFAULT NULL,
`photoCount` int(11) DEFAULT NULL,
`postalCodePlus4` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`priceChangeTimestamp` timestamp NULL DEFAULT NULL,
`propertyStatus` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`propertyType` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`providerModificationTimestamp` timestamp NULL DEFAULT NULL,
`realtorComYN` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sellingAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sellingOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`SPLPRatio` decimal(10,2) DEFAULT NULL,
`sqftHeated` decimal(10,2) DEFAULT NULL,
`sqftTotal` decimal(10,2) DEFAULT NULL,
`status` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`statusChangeTimestamp` timestamp NULL DEFAULT NULL,
`statusContractualSearchDate` date DEFAULT NULL,
`teamName` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tempOffMarketDate` date DEFAULT NULL,
`unitNumber` int(11) DEFAULT NULL,
`withdrawnDate` date DEFAULT NULL,
`withdrawnStatus` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`yearBuilt` int(11) DEFAULT NULL,
`mlsAssociationID` int(11) DEFAULT NULL,
PRIMARY KEY (`tempID`),
KEY `idx_mlsAssociationID_mlsID` (`mlsNumber`,`mlsAssociationID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
And here is the create statement for the production table
CREATE TABLE `tbl_mlstransactions` (
`transactionID` int(11) NOT NULL AUTO_INCREMENT,
`activeStatusDate` date DEFAULT NULL,
`address` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`address2` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`city` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`state` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`zip` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`availability` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`bathsFull` int(11) DEFAULT NULL,
`bathsHalf` int(11) DEFAULT NULL,
`bathsTotal` decimal(10,2) DEFAULT NULL,
`bedsTotal` decimal(10,2) DEFAULT NULL,
`CDOM` int(11) DEFAULT NULL,
`closeDate` date DEFAULT NULL,
`closePrice` decimal(15,2) DEFAULT NULL,
`coListAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`coListOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`coSellingAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`coSellingOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`contractStatus` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`county` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`currentPrice` decimal(15,2) DEFAULT NULL,
`dateAvailable` date DEFAULT NULL,
`daysToClose` int(11) DEFAULT NULL,
`daysToContract` int(11) DEFAULT NULL,
`DOM` int(11) DEFAULT NULL,
`expectedClosingDate` date DEFAULT NULL,
`expectedLeaseDate` date DEFAULT NULL,
`expirationDate` date DEFAULT NULL,
`expirationRenewalDate` date DEFAULT NULL,
`lastChangeType` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`lastDateAvailable` date DEFAULT NULL,
`lastListPrice` decimal(15,2) DEFAULT NULL,
`lastStatus` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`latitude` float DEFAULT NULL,
`listAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`listOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`listPrice` decimal(15,2) DEFAULT NULL,
`listingContractDate` date DEFAULT NULL,
`longitude` float DEFAULT NULL,
`mlsNumber` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`offMarketDate` date DEFAULT NULL,
`officePrimaryBoardID` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`originalEntryTimestamp` timestamp NULL DEFAULT NULL,
`originalListPrice` decimal(15,2) DEFAULT NULL,
`PDOM` int(11) DEFAULT NULL,
`pendingDate` date DEFAULT NULL,
`photoCount` int(11) DEFAULT NULL,
`postalCodePlus4` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`priceChangeTimestamp` timestamp NULL DEFAULT NULL,
`propertyStatus` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`propertyType` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`providerModificationTimestamp` timestamp NULL DEFAULT NULL,
`realtorComYN` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sellingAgentMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sellingOfficeMLSID` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`SPLPRatio` decimal(10,2) DEFAULT NULL,
`sqftHeated` decimal(10,2) DEFAULT NULL,
`sqftTotal` decimal(10,2) DEFAULT NULL,
`status` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`statusChangeTimestamp` timestamp NULL DEFAULT NULL,
`statusContractualSearchDate` date DEFAULT NULL,
`teamName` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tempOffMarketDate` date DEFAULT NULL,
`unitNumber` int(11) DEFAULT NULL,
`withdrawnDate` date DEFAULT NULL,
`withdrawnStatus` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`yearBuilt` int(11) DEFAULT NULL,
`mlsAssociationID` int(11) DEFAULT NULL,
PRIMARY KEY (`transactionID`),
KEY `idx_agentListInfo` (`listAgentMLSID`(191),`coListAgentMLSID`(191),`closeDate`,`status`),
KEY `idx_agentSellInfo` (`sellingAgentMLSID`(191),`coSellingAgentMLSID`(191),`closeDate`,`status`),
KEY `IDX_listAgentMLSID` (`listAgentMLSID`(191)) USING BTREE,
KEY `idx_coListAgentMLSID` (`coListAgentMLSID`(191)),
KEY `IDX_sellingAgentMLSID` (`sellingAgentMLSID`(191)) USING BTREE,
KEY `idx_coSellingAgentMLSID` (`coSellingAgentMLSID`(191)),
KEY `idx_productionSearch` (`mlsAssociationID`,`status`,`closeDate`),
KEY `idx_agentIDs` (`listAgentMLSID`(191),`coListAgentMLSID`(191),`sellingAgentMLSID`(191),`coSellingAgentMLSID`(191)),
KEY `idx_closeDate` (`closeDate`),
KEY `idx_mlsNumber_association` (`mlsNumber`,`mlsAssociationID`)
) ENGINE=InnoDB AUTO_INCREMENT=4543783 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
The import is not currently running, but here is the output from the explain statement on the update query
id,select_type,table,partitions,type,possible_keys,key,key_len,ref,rows,filtered,Extra
1,SIMPLE,temp,NULL,ALL,idx_mlsAssociationID_mlsID,NULL,NULL,NULL,1,100.00,"Using where"
1,UPDATE,mt,NULL,ref,idx_mlsNumber_association,idx_mlsNumber_association,128,"agenttracker.temp.mlsNumber,agenttracker.temp.mlsAssociationID",1,100.00,NULL
Any suggestions on how to make this quicker?
UPDATE:
I ran a few different files and when the ID's (MlsNumber, AgentID's, etc) are shorter (i.e. 5-10 characters) the update completes in 0.25 seconds. When they are longer (i.e. 25-27 characters) is when there is a problem.
Here are a couple examples of the long and short MLS Numbers:
20191104230040909159000000
20191104230040909159000000
20191104230040909159000000
Short:
4PRW01
20593419
20698727

How to fix 1709 - Index column size too large. The maximum column size is 767 bytes. in mysql in XAMPP

I'm importing a new table as Users, into my database but it is showing an error
1709 - Index column size too large. The maximum column size is 767 bytes.
I am having
Server version: 10.1.16-MariaDB
PHP version: 7.2.14
phpmyAdmin Version : Version information: 4.8.4
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`username` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email_show` tinyint(1) NOT NULL DEFAULT '0',
`email_confirmed` tinyint(1) NOT NULL DEFAULT '0',
`iso2` varchar(5) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`phone` varchar(16) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0=> Not Specified,1=> Male, 2=> Female',
`phone_show` tinyint(1) NOT NULL DEFAULT '0',
`phone_confirmed` tinyint(1) NOT NULL DEFAULT '0',
`country_id` int(10) UNSIGNED DEFAULT NULL,
`state_id` int(10) UNSIGNED DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`confirmation_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_by` int(10) UNSIGNED DEFAULT NULL,
`updated_by` int(10) UNSIGNED DEFAULT NULL,
`password` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL,
`options` text COLLATE utf8mb4_unicode_ci,
`confirmed` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(1) NOT NULL DEFAULT '0',
`phone_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`provider` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`provider_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`company_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`portfolio` text COLLATE utf8mb4_unicode_ci,
`opening_time` time DEFAULT NULL,
`closing_time` time DEFAULT NULL,
`address` text COLLATE utf8mb4_unicode_ci,
`education` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`notary_number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`monday_opening_time` time DEFAULT NULL,
`monday_closing_time` time DEFAULT NULL,
`thuesday_opening_time` time DEFAULT NULL,
`thuesday_closing_time` time DEFAULT NULL,
`wednesday_opening_time` time DEFAULT NULL,
`wednesday_closing_time` time DEFAULT NULL,
`thursday_opening_time` time DEFAULT NULL,
`thursday_closing_time` time DEFAULT NULL,
`friday_opening_time` time DEFAULT NULL,
`friday_closing_time` time DEFAULT NULL,
`saturday_opening_time` time DEFAULT NULL,
`saturday_closing_time` time DEFAULT NULL,
`sunday_opening_time` time DEFAULT NULL,
`sunday_closing_time` time DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`),
KEY `users_country_id_index` (`country_id`),
KEY `users_area_id_index` (`state_id`),
KEY `users_created_by_index` (`created_by`),
KEY `users_updated_by_index` (`updated_by`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
MySQL said:
1709 - Index column size too large. The maximum column size is 767 bytes.
In the last line of the creation table code try changing this:
ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
To this:
ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COLLATE=utf8mb4_unicode_ci;

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.

How can I optimize view in MySQL?

Load take 60 sec, how can I optimize?
Explain struct:
explain tablename;
IMG : http://i.stack.imgur.com/TfbY3.png
Explain SQL
explain select * from tablename;
IMG : http://i.stack.imgur.com/jHI47.png
REAL DB
DROP TABLE IF EXISTS `afiliados`;
CREATE TABLE `afiliados` (
`id_afiliado` int(10) NOT NULL AUTO_INCREMENT,
`clave_panel` char(15) COLLATE latin1_spanish_ci DEFAULT NULL,
`clave_panel_ask` datetime DEFAULT NULL,
`id_trabajador` char(20) COLLATE latin1_spanish_ci DEFAULT NULL,
`persona` char(1) COLLATE latin1_spanish_ci DEFAULT 'H',
`Estado` int(10) NOT NULL DEFAULT '0',
`doc_fiscal` int(10) DEFAULT NULL,
`id_fiscal` char(20) COLLATE latin1_spanish_ci DEFAULT NULL,
`Nombre` char(25) COLLATE latin1_spanish_ci DEFAULT NULL,
`Apellidos` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`fecha_nacimiento` datetime DEFAULT NULL,
`Telefono Principal` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Telefono Secundario` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fecha_Alta` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Fecha_Aprobacion` datetime DEFAULT NULL,
`id_cuenta_aprobacion` int(11) DEFAULT NULL,
`Fecha_Documentacion` datetime DEFAULT NULL,
`Fecha_Alta_Sindicato` datetime DEFAULT NULL,
`Fecha_Baja_Sindicato` datetime DEFAULT NULL,
`Motivo_Baja_Sindicato` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fecha_Desafiliacion` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fax` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Direccion` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`CP` char(5) COLLATE latin1_spanish_ci DEFAULT NULL,
`Ciudad` char(35) COLLATE latin1_spanish_ci DEFAULT NULL,
`Provincia` char(25) COLLATE latin1_spanish_ci DEFAULT NULL,
`Pais` char(2) COLLATE latin1_spanish_ci DEFAULT NULL,
`Nacionalidad` char(2) COLLATE latin1_spanish_ci NOT NULL DEFAULT '',
`Email` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`Email_secundario` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`Id_delegado` int(10) DEFAULT NULL,
`Comentario` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Interno` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Eliminado` tinyint(1) NOT NULL DEFAULT '0',
`Es_Delegado` tinyint(1) NOT NULL DEFAULT '0',
`id_cuenta` int(10) DEFAULT NULL,
`Fecha_mod` datetime DEFAULT NULL,
`mod_id_cuenta` int(10) DEFAULT NULL,
`noemail` tinyint(1) NOT NULL DEFAULT '0',
`noemail_public` tinyint(1) NOT NULL DEFAULT '0',
`noemail_fact` tinyint(1) NOT NULL DEFAULT '0',
`noti_alta` datetime DEFAULT NULL,
`noti_fact` datetime DEFAULT NULL,
`noti_baja` datetime DEFAULT NULL,
`fecha_star_facturacion` datetime DEFAULT NULL,
`fecha_proxima_facturacion` datetime DEFAULT NULL,
`ciclo_mensual_facturacion` int(10) unsigned NOT NULL DEFAULT '3',
`fecha_sync` datetime DEFAULT NULL,
`fecha_chk` datetime DEFAULT NULL,
`LOPD` tinyint(1) NOT NULL DEFAULT '0',
`ncarnet` int(10) DEFAULT NULL,
`id_caja` int(10) unsigned NOT NULL DEFAULT '0',
`caja_fecha_add` datetime DEFAULT NULL,
`id_cuenta_caja_add` int(10) DEFAULT '0',
`caja_fecha_del` datetime DEFAULT NULL,
`id_cuenta_caja_del` int(10) DEFAULT '0',
PRIMARY KEY (`id_afiliado`),
KEY `apellidos` (`Apellidos`),
KEY `Email` (`Email`),
KEY `es_delegado` (`Es_Delegado`),
KEY `id_caja` (`id_caja`),
KEY `id_cuenta` (`id_cuenta`),
KEY `id_delegado` (`Id_delegado`),
KEY `id_fiscal` (`id_fiscal`),
KEY `id_trabajador` (`id_trabajador`),
KEY `nombre` (`Nombre`)
) ENGINE=MyISAM AUTO_INCREMENT=4467 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=DYNAMIC;
INSERT INTO `afiliados` VALUES (194,NULL,NULL,'XXXXX','M',1,1,'XXXXX','Frank','Poland','1967-11-13 00:00:00','0000000','','2012-08-08 10:35:31','2012-08-08 21:05:59',0,'2012-08-09 00:00:00','2005-04-04 00:00:00',NULL,NULL,NULL,'','Adress','00000','Sevilla','Sevilla','es','es',NULL,NULL,NULL,NULL,NULL,0,0,NULL,NULL,NULL,0,0,0,NULL,NULL,NULL,NULL,NULL,3,NULL,NULL,0,NULL,0,NULL,NULL,NULL,NULL);
DROP TABLE IF EXISTS `contratos`;
CREATE TABLE `contratos` (
`Id_contrato` int(10) NOT NULL AUTO_INCREMENT,
`id_empresa` int(10) DEFAULT NULL,
`Marca` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Departamento` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Localizacion` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`fecha_baja` datetime DEFAULT NULL,
`id_cuenta_add` int(10) DEFAULT NULL,
PRIMARY KEY (`Id_contrato`),
KEY `id_empresa` (`id_empresa`)
) ENGINE=MyISAM AUTO_INCREMENT=1013 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `contratos` VALUES (38,7,'Telefonica','CAOL','2º Planta',NULL,NULL),(193,11,'Vodafone','Atencion al Cliente','Planta Baja / 1ª Planta',NULL,3);
DROP TABLE IF EXISTS `empresa`;
CREATE TABLE `empresa` (
`Id_empresa` int(10) NOT NULL AUTO_INCREMENT,
`cif` char(10) COLLATE latin1_spanish_ci DEFAULT NULL,
`Razon_Social` char(60) COLLATE latin1_spanish_ci DEFAULT NULL,
`empresa` char(40) COLLATE latin1_spanish_ci DEFAULT NULL,
`Centro` char(40) COLLATE latin1_spanish_ci DEFAULT NULL,
`Provincia` char(30) COLLATE latin1_spanish_ci DEFAULT NULL,
`localizacion_sindicato` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_setting` int(10) DEFAULT NULL,
`direccion` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`cp` char(5) COLLATE latin1_spanish_ci DEFAULT NULL,
`ciudad` char(30) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_cuenta_add` int(10) unsigned DEFAULT '0',
`fecha_baja` datetime DEFAULT NULL,
`id_cuenta_baja` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`Id_empresa`),
KEY `cif` (`cif`),
KEY `empresa` (`empresa`),
KEY `id_setting` (`id_setting`)
) ENGINE=MyISAM AUTO_INCREMENT=871 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `empresa` VALUES (7,'A78751997','ATENTO TELESERVICIOS, S.A.','Atento','Indotorre','Sevilla','2º Planta',13,NULL,NULL,NULL,0,NULL,NULL),(11,'B62916077','KONECTA BTO, S.L.','Konecta BTO','Prado de la Torre','Sevilla','1ª Planta (Pasillo Baño)',19,'Prado de la Torre s/n','41110','Bollulos',0,NULL,NULL);
DROP TABLE IF EXISTS `historico`;
CREATE TABLE `historico` (
`Id_historico` int(10) NOT NULL AUTO_INCREMENT,
`id_afiliado` int(10) DEFAULT NULL,
`fecha_alta` datetime DEFAULT NULL,
`tipo_contrato` smallint(5) DEFAULT NULL COMMENT 'Obra y Servicio ,etc ....',
`fecha_baja` datetime DEFAULT NULL,
`motivo_baja` smallint(5) DEFAULT NULL,
`fecha_provision` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`id_contrato` int(10) DEFAULT NULL,
`comentario` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_cuenta` int(10) DEFAULT NULL,
`Fecha_mod` datetime DEFAULT NULL,
`mod_id_cuenta` int(10) DEFAULT NULL,
PRIMARY KEY (`Id_historico`),
KEY `id_afiliado` (`id_afiliado`),
KEY `id_contrato` (`id_contrato`),
KEY `id_cuenta` (`id_cuenta`),
KEY `tipo_contrato` (`tipo_contrato`)
) ENGINE=MyISAM AUTO_INCREMENT=4814 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `historico` VALUES (227,194,'2005-04-04 00:00:00',0,'2014-01-01 00:00:00',-1,'2012-08-08 10:35:47',38,'',16,NULL,NULL),(1586,194,'2014-01-01 00:00:00',0,NULL,NULL,'2015-08-24 21:14:41',193,'',115,NULL,NULL);
VIEWs:
DROP VIEW IF EXISTS `detalle_full_ko`;
CREATE VIEW `detalle_full_ko` AS
select `afiliados`.`id_afiliado` AS `Id_afiliado`,`afiliados`.`ncarnet` AS `ncarnet`,
`afiliados`.`Id_delegado` AS `Id_delegado`,`afiliados`.`Fecha_Alta` AS `Fecha_Alta_Afiliado`,
`afiliados`.`id_trabajador` AS `id_trabajador`,`afiliados`.`persona` AS `persona`,
`afiliados`.`Nombre` AS `Nombre`,`afiliados`.`Apellidos` AS `Apellidos`,
`afiliados`.`id_fiscal` AS `id_fiscal`,`afiliados`.`Email` AS `Email`,
`afiliados`.`Email_secundario` AS `Email_secundario`,
`afiliados`.`noemail` AS `noemail`,`afiliados`.`noemail_public` AS `noemail_public`,
`afiliados`.`Telefono Principal` AS `Telefono Principal`,
`afiliados`.`Telefono Secundario` AS `Telefono Secundario`,
`afiliados`.`Fax` AS `Fax`,`afiliados`.`Ciudad` AS `Ciudad`,
`afiliados`.`CP` AS `CP`,`afiliados`.`Provincia` AS `Provincia_Afiliado`,
`afiliados`.`fecha_nacimiento` AS `fecha_nacimiento`,
`afiliados`.`Estado` AS `Estado`,`afiliados`.`Es_Delegado` AS `Es_Delegado`,
`afiliados`.`Eliminado` AS `Eliminado`,`detalle_historico_ko`.`fecha_alta` AS `Fecha_Alta_Empresa`,
`empresa`.`Razon_Social` AS `Razon_Social`,`empresa`.`empresa` AS `nombre_empresa`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Departamento` AS `Departamento`,
`contratos`.`Marca` AS `Marca`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`localizacion_sindicato` AS `localizacion_sindicato`,
`detalle_historico_ko`.`Id_contrato` AS `id_contrato`,
`contratos`.`id_empresa` AS `id_empresa`,`detalle_historico_ko`.`tipo_contrato` AS `tipo_contrato`,
`afiliados`.`Fecha_Alta_Sindicato` AS `Fecha_Alta_Sindicato`,
`afiliados`.`Fecha_Baja_Sindicato` AS `Fecha_Baja_Sindicato`,
`afiliados`.`fecha_sync` AS `fecha_sync`,`afiliados`.`noti_alta` AS `noti_alta`,
`afiliados`.`noti_fact` AS `noti_fact`,`afiliados`.`noti_baja` AS `noti_baja`,
`afiliados`.`Fecha_mod` AS `Fecha_mod`,`afiliados`.`LOPD` AS `LOPD`
from (((`contratos`
join `empresa` on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`)))
join `detalle_historico_ko` on(((`empresa`.`Id_empresa` = `detalle_historico_ko`.`Id_empresa`)
and (`contratos`.`Id_contrato` = `detalle_historico_ko`.`Id_contrato`))))
join `afiliados` on((`detalle_historico_ko`.`id_afiliado` = `afiliados`.`id_afiliado`))
)
where (`afiliados`.`LOPD` = 0)
group by `afiliados`.`id_afiliado`;
DROP VIEW IF EXISTS `detalle_full_ok`;
CREATE VIEW `detalle_full_ok` AS
select `afiliados`.`id_afiliado` AS `Id_afiliado`,`afiliados`.`ncarnet` AS `ncarnet`,
`afiliados`.`Id_delegado` AS `Id_delegado`,`afiliados`.`Fecha_Alta` AS `Fecha_Alta_Afiliado`,
`afiliados`.`id_trabajador` AS `id_trabajador`,`afiliados`.`persona` AS `persona`,
`afiliados`.`Nombre` AS `Nombre`,`afiliados`.`Apellidos` AS `Apellidos`,
`afiliados`.`id_fiscal` AS `id_fiscal`,`afiliados`.`Email` AS `Email`,
`afiliados`.`Email_secundario` AS `Email_secundario`,
`afiliados`.`noemail` AS `noemail`,`afiliados`.`noemail_public` AS `noemail_public`,
`afiliados`.`Telefono Principal` AS `Telefono Principal`,
`afiliados`.`Telefono Secundario` AS `Telefono Secundario`,
`afiliados`.`Fax` AS `Fax`,`afiliados`.`Ciudad` AS `Ciudad`,
`afiliados`.`CP` AS `CP`,`afiliados`.`Provincia` AS `Provincia_Afiliado`,
`afiliados`.`fecha_nacimiento` AS `fecha_nacimiento`,
`afiliados`.`Estado` AS `Estado`,`afiliados`.`Es_Delegado` AS `Es_Delegado`,
`afiliados`.`Eliminado` AS `Eliminado`,`detalle_historico_ok`.`fecha_alta` AS `Fecha_Alta_Empresa`,
`empresa`.`Razon_Social` AS `Razon_Social`,`empresa`.`empresa` AS `nombre_empresa`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Departamento` AS `Departamento`,
`contratos`.`Marca` AS `Marca`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`localizacion_sindicato` AS `localizacion_sindicato`,
`detalle_historico_ok`.`Id_contrato` AS `id_contrato`,
`contratos`.`id_empresa` AS `id_empresa`,`detalle_historico_ok`.`tipo_contrato` AS `tipo_contrato`,
`afiliados`.`Fecha_Alta_Sindicato` AS `Fecha_Alta_Sindicato`,
`afiliados`.`Fecha_Baja_Sindicato` AS `Fecha_Baja_Sindicato`,
`afiliados`.`fecha_sync` AS `fecha_sync`,`afiliados`.`noti_alta` AS `noti_alta`,
`afiliados`.`noti_fact` AS `noti_fact`,`afiliados`.`noti_baja` AS `noti_baja`,
`afiliados`.`Fecha_mod` AS `Fecha_mod`,`afiliados`.`LOPD` AS `LOPD`
from (((`contratos`
join `empresa` on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`)))
join `detalle_historico_ok` on(((`empresa`.`Id_empresa` = `detalle_historico_ok`.`Id_empresa`)
and (`contratos`.`Id_contrato` = `detalle_historico_ok`.`Id_contrato`))))
join `afiliados` on((`detalle_historico_ok`.`id_afiliado` = `afiliados`.`id_afiliado`))
)
where (`afiliados`.`LOPD` = 0)
group by `afiliados`.`id_afiliado`;
DROP VIEW IF EXISTS `detalle_historico_ko`;
CREATE VIEW `detalle_historico_ko` AS
select high_priority `historico`.`Id_historico` AS `Id_historico`,
`historico`.`id_afiliado` AS `id_afiliado`,`contratos`.`Id_contrato` AS `Id_contrato`,
`historico`.`fecha_provision` AS `fecha_provision`,`empresa`.`Id_empresa` AS `Id_empresa`,
`empresa`.`empresa` AS `Empresa`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Marca` AS `Marca`,
`contratos`.`Departamento` AS `Departamento`,`historico`.`fecha_alta` AS `fecha_alta`,
`historico`.`fecha_baja` AS `fecha_baja`,`historico`.`comentario` AS `comentario`,
`historico`.`tipo_contrato` AS `tipo_contrato`
from (`empresa`
join (`contratos`
join `historico` on((`contratos`.`Id_contrato` = `historico`.`id_contrato`))) on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`))
)
group by `historico`.`id_afiliado`
order by isnull(`historico`.`fecha_baja`) desc,`historico`.`fecha_baja` desc;
DROP VIEW IF EXISTS `detalle_historico_ok`;
CREATE VIEW `detalle_historico_ok` AS
select high_priority `historico`.`Id_historico` AS `Id_historico`,
`historico`.`id_afiliado` AS `id_afiliado`,`contratos`.`Id_contrato` AS `Id_contrato`,
`historico`.`fecha_provision` AS `fecha_provision`,`empresa`.`Id_empresa` AS `Id_empresa`,
`empresa`.`empresa` AS `Empresa`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Marca` AS `Marca`,
`contratos`.`Departamento` AS `Departamento`,`historico`.`fecha_alta` AS `fecha_alta`,
`historico`.`fecha_baja` AS `fecha_baja`,`historico`.`comentario` AS `comentario`,
`historico`.`tipo_contrato` AS `tipo_contrato`
from (`empresa`
join (`contratos`
join `historico` on((`contratos`.`Id_contrato` = `historico`.`id_contrato`))) on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`))
)
group by `historico`.`Id_historico`,`historico`.`id_afiliado`
order by isnull(`historico`.`fecha_baja`) desc,`historico`.`fecha_baja` desc;
When SELECT the view detalle_full_ok all go right, but if SELECT the view detalle_full_ko go wrong.
View view detalle_full_ok is slow and view detalle_full_ko is fast. The problem is view detalle_full_ko retrieve wrong nombre_empresa field.
Rows:
afiliados(4700)
empresa(800)
contratos(4700)
First pare down the query to get only historico.Id_historico and historico.id_afiliado. That is, avoid any columns and tables you don't need in getting just the desired id pair:
SELECT DISTINCT Id_historico, id_afiliado
FROM ...
-- no GROUP BY or ORDER BY
Then use that as subquery to get the rest of the data:
SELECT lots-of-stuff
FROM ( the above select )
JOIN back to all the tables
WHERE ...
GROUP BY -- probably needed again?
ORDER BY ...
However, the GROUP BY does not include all the non-aggregate values in the SELECT, so it is probably 'improper' and will probably be flagged as an error with ONLY_FULL_GROUP_BY.

Synchronize Table data with a whole different structure

The goal is to synchronize the customers data and his table structure to our table structure.
The table structure is the following:
CREATE TABLE IF NOT EXISTS `BildSerie_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`BildserieID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Prislista` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BildKod` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`AvdKlassID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=24 ;
CREATE TABLE IF NOT EXISTS `classes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned NOT NULL,
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `classes_client_id_foreign` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `Elever_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`X_ElevID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Efternamn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Förnamn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`GataMedNr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`PostNr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Postort` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`TelefonMobil` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Epost1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Epost2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`X_AvdKlassId` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=18 ;
CREATE TABLE IF NOT EXISTS `Elev_Bild_BildSerie_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Elev_Bild_Bildserie_ID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BildSerieID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`ElevID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=26 ;
CREATE TABLE IF NOT EXISTS `image_codes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned DEFAULT NULL,
`code` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `image_codes_code_unique` (`code`),
KEY `image_codes_client_id_foreign` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=20 ;
CREATE TABLE IF NOT EXISTS `image_series` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`image_code_id` int(10) unsigned NOT NULL,
`pricelist_id` int(10) unsigned NOT NULL DEFAULT '1',
`class_id` int(10) unsigned DEFAULT NULL,
`image_serie` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `image_series_image_code_id_foreign` (`image_code_id`),
KEY `image_series_pricelist_id_foreign` (`pricelist_id`),
KEY `image_series_class_id_foreign` (`class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
The goal is to take the data in "BildSerie_W2L" and populate it in to image_series.
My query looks like this:
SELECT `BildSerie_W2L`.*, `classes`.id, `image_codes`.id as ImageID, `Elever_W2L`.Förnamn, `Elever_W2L`.Efternamn
FROM `BildSerie_W2L`
INNER JOIN `image_codes` ON `BildSerie_W2L`.BildKod = `image_codes`.code
INNER JOIN `classes` ON `BildSerie_W2L`.AvdKlassID = `classes`.class
INNER JOIN `Elev_Bild_BildSerie_W2L` ON `Elev_Bild_BildSerie_W2L`.BildSerieID = `BildSerie_W2L`.BildserieID
INNER JOIN `Elever_W2L` ON `Elever_W2L`.X_ElevID = `Elev_Bild_BildSerie_W2L`.ElevID
WHERE `BildSerie_W2L`.Status = 0
However it gives back duplicated results and not all the data in BildSerie_W2L...