Duplicate Entry When Update - mysql

I want to remove space characters at the end of my data. So I update it with below query
UPDATE dwhdb.DimProductConsolidate
SET LoyaltyVariantArticleCode = REPLACE(LoyaltyVariantArticleCode, ' ', '')
WHERE LoyaltyVariantArticleCode LIKE '% '
It runs for a while, then this error message popping up.
It should be impossible for a duplicate value in this table because I already set a unique key with a combination of CompanyReference and LoyaltyVariantArticleCode. I check it and there was no duplicate value with CompanyReference 9914 and LoyaltyVariantArticleCode PGL/P13471 and it didn't even have a space at the end of the characters.
My question is, why this article code get inside my query when I already using where '% ' ? And is there a way for me to update the data?
Edit: Below is my CREATE code
CREATE TABLE `DimProductConsolidate` (
`ID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`GenericArticleCode` VARCHAR(50) NOT NULL,
`CompanyReference` VARCHAR(10) NOT NULL,
`EcomVariantArticleCode` VARCHAR(50) NOT NULL,
`LoyaltyVariantArticleCode` VARCHAR(50) NOT NULL,
`VariantArticleName` VARCHAR(255) NULL DEFAULT NULL,
`BarcodeEAN` VARCHAR(50) NULL DEFAULT NULL,
`MerchandisingCode` VARCHAR(50) NULL DEFAULT NULL,
`BrandName` VARCHAR(200) NULL DEFAULT NULL,
`GroupName` VARCHAR(200) NULL DEFAULT NULL,
`CategoryName` VARCHAR(200) NULL DEFAULT NULL,
`EcomCategoryLvl1` VARCHAR(100) NULL DEFAULT NULL,
`EcomCategoryLvl2` VARCHAR(100) NULL DEFAULT NULL,
`EcomCategoryLvl3` VARCHAR(100) NULL DEFAULT NULL,
`EcomCategoryLvl4` VARCHAR(100) NULL DEFAULT NULL,
`DivisionName` VARCHAR(200) NULL DEFAULT NULL,
`ColourInternal` VARCHAR(10) NULL DEFAULT NULL,
`SizeInternal` VARCHAR(10) NULL DEFAULT NULL,
`ColourExternal` VARCHAR(10) NULL DEFAULT NULL,
`SizeExternal` VARCHAR(10) NULL DEFAULT NULL,
`ProductGender` VARCHAR(100) NULL DEFAULT NULL,
`CompanyName` VARCHAR(50) NULL DEFAULT NULL,
`ProductImageURL` VARCHAR(512) NULL DEFAULT NULL,
`ProductType` VARCHAR(200) NULL DEFAULT NULL,
`ListPrice` DECIMAL(10,0) NULL DEFAULT NULL,
`Quantity` INT(11) NULL DEFAULT NULL,
`ListedTime` DATETIME NULL DEFAULT NULL,
`CreatedDate` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
UNIQUE INDEX `CompanyReference` (`CompanyReference`, `LoyaltyVariantArticleCode`) USING BTREE,
INDEX `GenericArticleCode` (`GenericArticleCode`) USING BTREE,
INDEX `EcomVariantArticleCode` (`EcomVariantArticleCode`) USING BTREE,
INDEX `BarcodeEAN` (`BarcodeEAN`) USING BTREE,
INDEX `MerchandisingCode` (`MerchandisingCode`) USING BTREE,
INDEX `BrandName` (`BrandName`) USING BTREE,
INDEX `GroupName` (`GroupName`) USING BTREE,
INDEX `CategoryName` (`CategoryName`),
INDEX `EcomCategoryLvl1` (`EcomCategoryLvl1`),
INDEX `EcomCategoryLvl2` (`EcomCategoryLvl2`),
INDEX `EcomCategoryLvl3` (`EcomCategoryLvl3`),
INDEX `EcomCategoryLvl4` (`EcomCategoryLvl4`),
INDEX `DivisionName` (`DivisionName`),
INDEX `LoyaltyVariantArticleCode` (`LoyaltyVariantArticleCode`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=22497389
;

Related

Slow query with NULL IS NULL OR condition syntax using Mysql 5.7

I'm getting some strange timing values from Mysql running a "simple" query.
This is the DDL of the table:
CREATE TABLE `frame` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`createdBy` varchar(255) DEFAULT NULL,
`createdDate` datetime(6) NOT NULL,
`lastModifiedBy` varchar(255) DEFAULT NULL,
`lastModifiedDate` datetime(6) DEFAULT NULL,
`sid` varchar(36) NOT NULL,
`version` bigint(20) NOT NULL,
`brand` varchar(255) DEFAULT NULL,
`category` varchar(255) DEFAULT NULL,
`colorCode` varchar(255) DEFAULT NULL,
`colorDescription` varchar(255) DEFAULT NULL,
`description` longtext,
`imageUrl` varchar(255) DEFAULT NULL,
`lastPurchase` datetime(6) DEFAULT NULL,
`lastPurchasePrice` decimal(19,2) DEFAULT NULL,
`lastSell` datetime(6) DEFAULT NULL,
`lastSellPrice` decimal(19,2) DEFAULT NULL,
`line` varchar(255) DEFAULT NULL,
`manufacturer` varchar(255) DEFAULT NULL,
`manufacturerCode` varchar(255) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`preset` bit(1) NOT NULL DEFAULT b'0',
`purchasePrice` decimal(19,2) DEFAULT NULL,
`salesPrice` decimal(19,2) DEFAULT NULL,
`sku` varchar(255) NOT NULL,
`stock` bit(1) NOT NULL DEFAULT b'1',
`thumbUrl` varchar(255) DEFAULT NULL,
`upc` varchar(255) DEFAULT NULL,
`arm` int(11) DEFAULT NULL,
`bridge` int(11) DEFAULT NULL,
`caliber` int(11) DEFAULT NULL,
`gender` varchar(255) DEFAULT NULL,
`lensColor` varchar(255) DEFAULT NULL,
`material` varchar(255) DEFAULT NULL,
`model` varchar(255) NOT NULL,
`sphere` decimal(10,2) DEFAULT NULL,
`type` varchar(255) NOT NULL,
`taxRate_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_k7s4esovkoacsc264bcjrre13` (`sid`),
UNIQUE KEY `UK_ajh6mr6a6qg6mgy8t9nevdym1` (`sku`),
UNIQUE KEY `UK_boqikmg9o89j8q0o5ujkj33b3` (`upc`),
KEY `idx_manufacturer` (`manufacturer`),
KEY `idx_brand` (`brand`),
KEY `idx_line` (`line`),
KEY `idx_colorcode` (`colorCode`),
KEY `idx_preset` (`preset`),
KEY `idx_manufacturer_model_color_caliber` (`manufacturer`,`model`,`colorCode`,`caliber`),
KEY `FK1nau29fd70s1nq905dgs6ft85` (`taxRate_id`),
CONSTRAINT `FK1nau29fd70s1nq905dgs6ft85` FOREIGN KEY (`taxRate_id`) REFERENCES `taxrate` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=392179 DEFAULT CHARSET=utf8;
The query is created programatically from my application. The "strange" syntax (NULL IS NULL OR condition) is very convenient to me in order to make more compact my code and removing the need to create a different query based on the numbers of parameters.
For who understand how Hibernate HQL and JPA works, this is the query:
This query is generated when the user is not setting any filter, so all parameters in my condition are null and this is how the query comes out.
SELECT SQL_NO_CACHE COUNT(frame0_.`id`) AS col_0_0_ FROM `Frame` frame0_
WHERE (NULL IS NULL OR NULL LIKE CONCAT('%', NULL, '%') OR frame0_.`manufacturer` LIKE CONCAT('%', NULL, '%') OR frame0_.`manufacturerCode`=NULL OR frame0_.`sku`=NULL OR frame0_.`upc`=NULL OR frame0_.`line` LIKE CONCAT('%', NULL, '%') OR frame0_.`model` LIKE CONCAT('%', NULL, '%')) AND (NULL IS NULL OR frame0_.`manufacturer`=NULL) AND (NULL IS NULL OR frame0_.`line`=NULL) AND (NULL IS NULL OR frame0_.`caliber`=NULL) AND (NULL IS NULL OR frame0_.`type`=NULL) AND (NULL IS NULL OR frame0_.`material`=NULL) AND (NULL IS NULL OR frame0_.`model`=NULL) AND (NULL IS NULL OR frame0_.`colorCode`=NULL)
The query takes about 0.105s on a table of 137548 rows.
The EXPLAIN of the previous query returns:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE frame0_ \N ALL \ N \N \N \N 137548 100.00 \N
The previous query is identical to this one:
SELECT SQL_NO_CACHE COUNT(frame0_.`id`) AS col_0_0_ FROM `Frame` frame0_
This query takes just 0.05s for the same result in the same table.
Why for Mysql they are different and the first is taking so much time? Is there a way to improve performance of the first query keeping the syntax "NULL IS NULL or condition"?
I think Ryan has right, so the many or statement made the query so bad.
You should programatically build queries for better performance. So if the user not select by a possible filter, than you shouldn't include to the query!
(HQL)
if(!StringUtils.isEmpty(manufacturer)) {
query.and(m.manufacturer.eq(manufacturer))
}

mysql 1215 - Cannot add foreign key constraint

I have created the schema with Workbench 6.1 (and MySQL 5.6) and it outputs the code below. But this produces a foreign key constraint. Last time it worked but produced a composite primary key made of the other foreign keys which is overkill I think.
Does anyone have an idea what is wrong here?
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `yii_jobmedisearch` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`Person` (
`PersId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`Vorname` VARCHAR(25) NOT NULL,
`MiddleName` VARCHAR(25) NOT NULL,
`Nachname` VARCHAR(25) NOT NULL,
`Anrede` ENUM('Herr','Frau','Herr Dr.','Frau Dr.','Herr Prof.','Frau Prof.') NOT NULL,
`Titel` ENUM('Dr.','Prof.') NULL DEFAULT NULL,
`Geschlecht` ENUM('Frau','Mann','Transident') NOT NULL,
`Geburtsdatum` DATE NOT NULL,
`Nationalitaet` VARCHAR(2) NOT NULL,`enter code here`
`Nationalitaet2` VARCHAR(2) NULL DEFAULT NULL,
`Heimatort` VARCHAR(60) NULL DEFAULT NULL,
`Strasse` VARCHAR(60) NULL DEFAULT NULL,
`PLZ` VARCHAR(10) NOT NULL,
`Wohnort` VARCHAR(60) NOT NULL,
`Kanton` VARCHAR(4) NULL DEFAULT NULL,
`TelHome` VARCHAR(15) NULL DEFAULT NULL,
`TelMobil` VARCHAR(15) NULL DEFAULT NULL,
`Zivilstand` ENUM('ledig','verheiratet','geschieden','verwittwet') NULL DEFAULT NULL,
`Email` VARCHAR(80) NOT NULL,
`SkypeId` VARCHAR(30) NULL DEFAULT NULL,
`FacebookId` VARCHAR(30) NULL DEFAULT NULL,
`GooglePlusId` VARCHAR(30) NULL DEFAULT NULL,
`XingId` VARCHAR(30) NULL DEFAULT NULL,
`LinkedInId` VARCHAR(30) NULL DEFAULT NULL,
`TwitterId` VARCHAR(30) NULL DEFAULT NULL,
`IstSuchend` ENUM('Sucht','Sucht nicht') NULL DEFAULT NULL,
`AbsenzVon` DATE NULL DEFAULT NULL,
`AbsenzBis` DATE NULL DEFAULT NULL,
`PersonCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`PersonModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`PersId`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`User` (
`UserId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`UserName` VARCHAR(25) NOT NULL,
`UserPass` VARCHAR(255) NOT NULL,
`UserRolle` ENUM('guest','AGeber','ANehmer','ProAGeber','ProAGeberAdmin','ProANehmer','ProVermittler','ProVermittlerAdmin','Maintainer','Admin') NOT NULL,
`Person_PersId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`JobFirma_Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`UserCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UserModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`UserVerified` TINYINT(3) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`UserId`),
INDEX `fk_User_Person_idx` (`Person_PersId` ASC),
INDEX `fk_User_Filiale1_idx` (`Filiale_FilialeId` ASC, `Filiale_HQFirma_HQFirmaId` ASC),
UNIQUE INDEX `UserName_UNIQUE` (`UserName` ASC),
CONSTRAINT `fk_User_Person`
FOREIGN KEY (`Person_PersId`)
REFERENCES `yii_jobmedisearch`.`Person` (`PersId`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_User_Filiale1`
FOREIGN KEY (`Filiale_FilialeId` , `Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`Filiale` (`FilialeId` , `HQFirma_HQFirmaId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`HQFirma` (
`HQFirmaId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`HQFirmenname` VARCHAR(80) NOT NULL,
`HQAnzFilialen` SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
`HQGesellschaftsform` ENUM('AG','GmbH','Einzelfirma') NOT NULL,
`HQBranche` ENUM('Spital','Klinik','Stellenvermittler','Versicherung') NOT NULL,
`HQFirmenbeschrieb` VARCHAR(512) NOT NULL,
`HQStrasse` VARCHAR(60) NOT NULL,
`HQPLZ` VARCHAR(10) NOT NULL,
`HQPostfach` VARCHAR(20) NULL DEFAULT NULL,
`HQOrt` VARCHAR(60) NOT NULL,
`HQFax` VARCHAR(15) NULL DEFAULT NULL,
`HQTelefon` VARCHAR(15) NOT NULL,
`HQTelefon2` VARCHAR(15) NULL DEFAULT NULL,
`HQEmail` VARCHAR(80) NOT NULL,
`HQEmail2` VARCHAR(80) NULL DEFAULT NULL,
`HQWebURL` VARCHAR(255) NULL DEFAULT NULL,
`HQWebURL2` VARCHAR(255) NULL DEFAULT NULL,
`HQAnzLikes` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`HQSkypeId` VARCHAR(30) NULL DEFAULT NULL,
`HQFacebookId` VARCHAR(30) NULL DEFAULT NULL,
`HQGooglePlusId` VARCHAR(60) NULL DEFAULT NULL,
`HQXingId` VARCHAR(60) NULL DEFAULT NULL,
`HQLinkedInId` VARCHAR(60) NULL DEFAULT NULL,
`HQTwitterId` VARCHAR(60) NULL DEFAULT NULL,
`HQFirmaCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`HQFirmaModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`HQFirmaId`),
UNIQUE INDEX `HQFirmenname_UNIQUE` (`HQFirmenname` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`Filiale` (
`FilialeId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`Strasse` VARCHAR(60) NOT NULL,
`Postfach` VARCHAR(20) NULL DEFAULT NULL,
`PLZ` VARCHAR(10) NOT NULL,
`Ort` VARCHAR(60) NOT NULL,
`Fax` VARCHAR(15) NULL DEFAULT NULL,
`Telefon` VARCHAR(15) NOT NULL,
`Telefon2` VARCHAR(15) NULL DEFAULT NULL,
`Email` VARCHAR(80) NOT NULL,
`Email2` VARCHAR(80) NULL DEFAULT NULL,
`WebURL` VARCHAR(255) NULL DEFAULT NULL,
`WebURL2` VARCHAR(255) NULL DEFAULT NULL,
`SkypeId` VARCHAR(30) NULL DEFAULT NULL,
`FacebookId` VARCHAR(60) NULL DEFAULT NULL,
`GooglePlusId` VARCHAR(60) NULL DEFAULT NULL,
`XingId` VARCHAR(60) NULL DEFAULT NULL,
`LinkedInId` VARCHAR(60) NULL DEFAULT NULL,
`TwitterId` VARCHAR(60) NULL DEFAULT NULL,
`AnzLikes` MEDIUMINT(9) NULL DEFAULT NULL,
`HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`FilialeName` VARCHAR(80) NOT NULL,
`FilialeCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`FilialeModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`FilialeId`),
INDEX `fk_Filiale_HQFirma1_idx` (`HQFirma_HQFirmaId` ASC),
UNIQUE INDEX `FilialenName_UNIQUE` (`FilialeName` ASC),
CONSTRAINT `fk_Filiale_HQFirma1`
FOREIGN KEY (`HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`HQFirma` (`HQFirmaId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`Promotions` (
`PromoId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT,
`PromoTyp` ENUM('TopList','Highlighting') NOT NULL,
`PromoBeginn` TIMESTAMP NULL DEFAULT NULL,
`PromoEnd` TIMESTAMP NULL DEFAULT NULL,
`JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`User_UserId` BIGINT(19) UNSIGNED NOT NULL,
`User_Person_PersId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`PromoCreationDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`PromoModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`PromoId`),
INDEX `fk_Promotions_JobFirma1_idx` (`JobFirma_JobId` ASC),
INDEX `fk_Promotions_User1_idx` (`User_UserId` ASC, `User_Person_PersId` ASC, `User_JobFirma_JobId` ASC, `User_JobFirma_HQFirma_HQFirmaId` ASC, `User_JobFirma_Filiale_FilialeId` ASC, `User_JobFirma_Filiale_HQFirma_HQFirmaId` ASC),
CONSTRAINT `fk_Promotions_JobFirma1`
FOREIGN KEY (`JobFirma_JobId`)
REFERENCES `yii_jobmedisearch`.`JobFirma` (`JobId`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_Promotions_User1`
FOREIGN KEY (`User_UserId` , `User_Person_PersId` , `User_JobFirma_JobId` , `User_JobFirma_HQFirma_HQFirmaId` , `User_JobFirma_Filiale_FilialeId` , `User_JobFirma_Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`User` (`UserId` , `Person_PersId` , `JobFirma_JobId` , `JobFirma_HQFirma_HQFirmaId` , `JobFirma_Filiale_FilialeId` , `JobFirma_Filiale_HQFirma_HQFirmaId`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `yii_jobmedisearch`.`JobFirma` (
`JobId` BIGINT(19) UNSIGNED NOT NULL,
`JobCreateDatum` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`JobModifDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`JobPostDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`JobBeginnDatum` DATE NOT NULL,
`JobExpireDatum` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`JobTitel` VARCHAR(60) NOT NULL,
`JobBranche` ENUM('Spital','Stellenvermittler','Klinik','Versicherung') NOT NULL,
`JobSynopsis` VARCHAR(255) NULL DEFAULT NULL,
`JobAnforderungen` VARCHAR(2048) NOT NULL,
`JobDescription` VARCHAR(2048) NOT NULL,
`JobAngebot` VARCHAR(2048) NULL DEFAULT NULL,
`JobEduLevel` ENUM('PhD','Master','Lehre','Berufserfahrung') NOT NULL,
`JobEduLevel2` ENUM('PhD','Master','Lehre','Berufserfahrung') NULL DEFAULT NULL,
`JobEduLevel3` ENUM('PhD','Master','Lehre','Berufserfahrung') NULL DEFAULT NULL,
`JobAgeRange` ENUM('18-25','25-35','35-45','45-65','egal') NOT NULL,
`JobNationalitaet` VARCHAR(2) NOT NULL,
`JobPlace` VARCHAR(60) NOT NULL,
`JobTimeModel` ENUM('Teilzeit','Vollzeit','auf Abruf') NOT NULL,
`JobTimePercent` FLOAT(10) UNSIGNED NOT NULL,
`JobContractModel` ENUM('Permanent','Temporär','Freelancer') NOT NULL,
`JobAnzViews` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`JobAnzBewerbungen` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`JobAnzStars` MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
`JobAnz1Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz2Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz3Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz4Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz5Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobAnz6Star` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobStars` FLOAT(10) UNSIGNED NULL DEFAULT NULL,
`JobWebURL` VARCHAR(255) NULL DEFAULT NULL,
`JobWebPDF` VARCHAR(255) NULL DEFAULT NULL,
`Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`User_UserId` BIGINT(19) UNSIGNED NOT NULL,
`User_Person_PersId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_JobId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_FilialeId` BIGINT(19) UNSIGNED NOT NULL,
`User_JobFirma_Filiale_HQFirma_HQFirmaId` BIGINT(19) UNSIGNED NOT NULL,
PRIMARY KEY (`JobId`),
INDEX `fk_JobFirma_Filiale1_idx` (`Filiale_FilialeId` ASC, `Filiale_HQFirma_HQFirmaId` ASC),
INDEX `fk_JobFirma_User1_idx` (`User_UserId` ASC, `User_Person_PersId` ASC, `User_JobFirma_JobId` ASC, `User_JobFirma_HQFirma_HQFirmaId` ASC, `User_JobFirma_Filiale_FilialeId` ASC, `User_JobFirma_Filiale_HQFirma_HQFirmaId` ASC),
CONSTRAINT `fk_JobFirma_Filiale1`
FOREIGN KEY (`Filiale_FilialeId` , `Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`Filiale` (`FilialeId` , `HQFirma_HQFirmaId`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_JobFirma_User1`
FOREIGN KEY (`User_UserId` , `User_Person_PersId` , `User_JobFirma_JobId` , `User_JobFirma_HQFirma_HQFirmaId` , `User_JobFirma_Filiale_FilialeId` , `User_JobFirma_Filiale_HQFirma_HQFirmaId`)
REFERENCES `yii_jobmedisearch`.`User` (`UserId` , `Person_PersId` , `JobFirma_JobId` , `JobFirma_HQFirma_HQFirmaId` , `JobFirma_Filiale_FilialeId` , `JobFirma_Filiale_HQFirma_HQFirmaId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
COMMENT = /* comment truncated */ /* */;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
LATEST FOREIGN KEY ERROR
2014-04-07 15:16:31 ce0 Error in foreign key constraint of table yii_jobmedisearch/user:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT "fk_User_Filiale1" FOREIGN KEY ("Filiale_FilialeId", "Filiale_HQFirma_HQFirmaId") REFERENCES "filiale" ("FilialeId", "HQFirma_HQFirmaId") ON DELETE NO ACTION ON UPDATE NO ACTION
The index in the foreign key in table is "fk_User_Filiale1_idx"
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html for correct foreign key definition.

How to optimize a MySQL JOIN Query

I have this MySQL query that I want to optimize:
SELECT r.WarehouseLocation,sum(sir.qty)
FROM repairableissue as r
INNER JOIN SIR ON r.sirno=sir.sirno
AND r.region=sir.region
AND r.ItemName=sir.Itemdesc
AND r.SerialNo=sir.Serialno
WHERE r.status='Pending'
GROUP BY r.warehouseLocation
How do I optimize this query? I read about optimization and found out that indexes might help but still could not achieve the desired performance.
Which index should be used and which should be removed?
Below is the explain of query:
Repairableissue
CREATE TABLE `repairableissue` (
`Vendor` varchar(40) NOT NULL,
`ItemName` varchar(200) NOT NULL,
`SerialNo` varchar(50) NOT NULL,
`person` varchar(200) NOT NULL,
`siteid` varchar(10) NOT NULL,
`invuser` varchar(50) NOT NULL,
`region` varchar(50) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`Dated` date NOT NULL,
`Sirno` varchar(50) NOT NULL,
`status` varchar(30) NOT NULL DEFAULT 'Pending',
`trackthrough` varchar(30) NOT NULL,
`reason` varchar(100) NOT NULL,
`ckh` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`WarehouseType` varchar(20) NOT NULL,
`WarehouseLocation` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `I1` (`status`),
KEY `ind2` (`ItemName`),
KEY `ind3` (`region`),
KEY `ind5` (`SerialNo`),
KEY `ind4` (`Sirno`)
) ENGINE=MyISAM AUTO_INCREMENT=63029 DEFAULT CHARSET=latin1
sir
CREATE TABLE `sir` (
`SirNo` varchar(50) NOT NULL,
`SiteId` varchar(80) NOT NULL,
`Vendor` varchar(70) NOT NULL,
`Type` varchar(15) NOT NULL,
`ItemDesc` varchar(200) NOT NULL,
`ItemCode` varchar(25) NOT NULL,
`SerialNo` varchar(50) NOT NULL,
`Unit` varchar(15) NOT NULL,
`AssetCode` varchar(50) NOT NULL,
`Qty` decimal(11,0) NOT NULL,
`Region` varchar(15) NOT NULL,
`Status` varchar(20) NOT NULL DEFAULT 'Installed',
`FaultInfo` varchar(100) NOT NULL DEFAULT 'date()',
`chk` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Phase` varchar(15) NOT NULL,
`Category` varchar(200) NOT NULL,
`Issue_Vendor` varchar(30) NOT NULL,
`AssetName` varchar(150) NOT NULL,
`Ownership` varchar(20) NOT NULL,
`Dated` date NOT NULL,
`PersonName` varchar(150) NOT NULL,
`Remarks` varchar(300) NOT NULL,
`po` varchar(100) NOT NULL,
`invuser` varchar(50) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`grnno` varchar(30) NOT NULL,
`WarehouseType` varchar(20) NOT NULL,
`WarehouseLocation` varchar(20) NOT NULL,
`mainpartserial` varchar(200) NOT NULL,
PRIMARY KEY (`Vendor`,`Type`,`ItemCode`,`ItemDesc`,`SerialNo`,`Ownership`,`SirNo`,`Region`,`WarehouseType`,`WarehouseLocation`,`po`,`Qty`,`id`),
KEY `id` (`id`),
KEY `ind4` (`ItemDesc`),
KEY `ind6` (`SerialNo`),
KEY `ind7` (`SerialNo`)
) ENGINE=MyISAM AUTO_INCREMENT=228007 DEFAULT CHARSET=latin1
One multi-column index on r.status + r.warehouseLocation, in that order.
One multi-column index on sir.sirno + sir.region + sir.Itemdesc + sir.Serialno, in order of most cardinality to least cardinality, with sir.qty tacked on the end.
This assumes the fields are small enough to fit (combined) into an index.
Still, join seeks are unavoidable. The number of records that match r.status='Pending' is going to dictate the speed of this query.

why Mysql is giving me error 1280 "Wrong Index"

Can anyone explain why Mysql is giving me error 1280 ("wrong index for 'fk_chart_aid_aid' ") error whend I try to create the "CHART OF ACCOUNTS" table. I'm completly confused here. How can I fix this so I can create the table? The "ACCOUNT" table already exists in the database and has data in it.
Thanks for the help.
MYSQL Server version: 5.1.54
CHART OF ACCOUNTS:
DROP TABLE IF EXISTS `rst`.`acctg_chart_of_accounts` ;
CREATE TABLE IF NOT EXISTS `rst`.`acctg_chart_of_accounts` (
`acctg_chart_of_accounts_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`account_id` INT UNSIGNED NOT NULL ,
`account_nbr` VARCHAR(45) NULL ,
`description` VARCHAR(45) NULL ,
`account_type` INT UNSIGNED NULL ,
`commissionable` TINYINT UNSIGNED NULL ,
`hidden` TINYINT UNSIGNED NULL ,
`deduct_balance_from_owner_check` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`acctg_chart_of_accounts_id`) ,
CONSTRAINT `fk_chart_aid_aid`
FOREIGN KEY (`account_id` )
REFERENCES `rst`.`account` (`account_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `fk_chart_aid_aid` ON `rst`.`acctg_chart_of_accounts` (`account_id` ASC) ;
ACCOUNTS TABLE THAT IS BEING REFERENCED:
CREATE TABLE IF NOT EXISTS `account` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_status_id` int(10) unsigned NOT NULL,
`company_name` varchar(155) DEFAULT NULL,
`address1` varchar(155) DEFAULT NULL,
`address2` varchar(155) DEFAULT NULL,
`city` varchar(155) DEFAULT NULL,
`state` varchar(155) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`work_phone` varchar(45) DEFAULT NULL,
`mobile_phone` varchar(45) DEFAULT NULL,
`time_zone` varchar(45) DEFAULT NULL,
`subdomain` varchar(155) DEFAULT NULL,
`cname_URL` varchar(255) DEFAULT NULL,
`promotion_code` varchar(45) DEFAULT NULL,
`can_we_contact_you` tinyint(4) DEFAULT NULL COMMENT '0=false, 1=true',
`units_managed_nbr` varchar(10) DEFAULT NULL,
`a_hear_about_us_list_id` tinyint(3) unsigned DEFAULT NULL COMMENT 'populated from dropdown list.',
`receive_special_offers` tinyint(4) DEFAULT NULL,
`receive_announcements` tinyint(4) DEFAULT NULL,
`receive_newsletter` tinyint(4) DEFAULT NULL,
`create_ts` timestamp NULL DEFAULT NULL,
`expires` timestamp NULL DEFAULT NULL,
`storage_capacity` varchar(255) DEFAULT NULL COMMENT '1073741824 = 1GB',
`logo` varchar(455) DEFAULT NULL,
`max_active_connections` int(11) DEFAULT '3',
`_product_id` int(11) DEFAULT NULL,
`report_footer` varchar(455) DEFAULT NULL,
`welcome_dialog` tinyint(4) DEFAULT '1',
`ARB_subscription_id` int(11) DEFAULT NULL,
`trashbin` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`account_id`),
KEY `fk_account_account_status_id` (`account_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
Are you getting the error after the CREATE TABLE statement, or after the subsequent CREATE INDEX?
Looks like you are attempting to name both a FOREIGN KEY constraint and an INDEX fk_chart_aid_aid. Try choosing a different name for either one of them.
Also, in the accounts table, account_id is INT(10). Try also to change the column definition in acctg_chart_of_accounts to:
`account_id` INT(10) UNSIGNED NOT NULL ,
Though, I think that mysql defaults type INT to INT(10) anyway...
I met the same issue; tried manually rename the index to a different name but don't like the idea of 'manually' and neither I don't really understand why we need to generate the index separately. so I decide to generate it within the create statement by unchecking the option of 'generate separate index statement' in 'forwarding engineer', and this fix the issue.

MySql query optimization help

I have few queries and am not able to figure out how to optimize them,
QUERY 1
select *
from t_twitter_tracking
where classified is null and tweetType='ENGLISH'
order by id limit 500;
QUERY 2
Select
count(*) as cnt,
DATE_FORMAT(CONVERT_TZ(wrdTrk.createdOnGMTDate,'+00:00','+05:30'),'%Y-%m-%d')
as dat
from
t_twitter_tracking wrdTrk
where
wrdTrk.word like ('dell')
and CONVERT_TZ(wrdTrk.createdOnGMTDate,'+00:00','+05:30')
between '2010-12-12 00:00:00' and '2010-12-26 00:00:00'
group by dat;
Both these queries run on the same table,
CREATE TABLE `t_twitter_tracking` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`word` VARCHAR(200) NOT NULL,
`tweetId` BIGINT(100) NOT NULL,
`twtText` VARCHAR(800) NULL DEFAULT NULL,
`language` TEXT NULL,
`links` TEXT NULL,
`tweetType` VARCHAR(20) NULL DEFAULT NULL,
`source` TEXT NULL,
`sourceStripped` TEXT NULL,
`isTruncated` VARCHAR(40) NULL DEFAULT NULL,
`inReplyToStatusId` BIGINT(30) NULL DEFAULT NULL,
`inReplyToUserId` INT(11) NULL DEFAULT NULL,
`rtUsrProfilePicUrl` TEXT NULL,
`isFavorited` VARCHAR(40) NULL DEFAULT NULL,
`inReplyToScreenName` VARCHAR(40) NULL DEFAULT NULL,
`latitude` BIGINT(100) NOT NULL,
`longitude` BIGINT(100) NOT NULL,
`retweetedStatus` VARCHAR(40) NULL DEFAULT NULL,
`statusInReplyToStatusId` BIGINT(100) NOT NULL,
`statusInReplyToUserId` BIGINT(100) NOT NULL,
`statusFavorited` VARCHAR(40) NULL DEFAULT NULL,
`statusInReplyToScreenName` TEXT NULL,
`screenName` TEXT NULL,
`profilePicUrl` TEXT NULL,
`twitterId` BIGINT(100) NOT NULL,
`name` TEXT NULL,
`location` VARCHAR(100) NULL DEFAULT NULL,
`bio` TEXT NULL,
`url` TEXT NULL COLLATE 'latin1_swedish_ci',
`utcOffset` INT(11) NULL DEFAULT NULL,
`timeZone` VARCHAR(100) NULL DEFAULT NULL,
`frenCnt` BIGINT(20) NULL DEFAULT '0',
`createdAt` DATETIME NULL DEFAULT NULL,
`createdOnGMT` VARCHAR(40) NULL DEFAULT NULL,
`createdOnServerTime` DATETIME NULL DEFAULT NULL,
`follCnt` BIGINT(20) NULL DEFAULT '0',
`favCnt` BIGINT(20) NULL DEFAULT '0',
`totStatusCnt` BIGINT(20) NULL DEFAULT NULL,
`usrCrtDate` VARCHAR(200) NULL DEFAULT NULL,
`humanSentiment` VARCHAR(30) NULL DEFAULT NULL,
`replied` BIT(1) NULL DEFAULT NULL,
`replyMsg` TEXT NULL,
`classified` INT(32) NULL DEFAULT NULL,
`createdOnGMTDate` DATETIME NULL DEFAULT NULL,
`locationDetail` TEXT NULL,
`geonameid` INT(11) NULL DEFAULT NULL,
`country` VARCHAR(255) NULL DEFAULT NULL,
`continent` CHAR(2) NULL DEFAULT NULL,
`placeLongitude` FLOAT NULL DEFAULT NULL,
`placeLatitude` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `id` (`id`, `word`),
INDEX `createdOnGMT_index` (`createdOnGMT`) USING BTREE,
INDEX `word_index` (`word`) USING BTREE,
INDEX `location_index` (`location`) USING BTREE,
INDEX `classified_index` (`classified`) USING BTREE,
INDEX `tweetType_index` (`tweetType`) USING BTREE,
INDEX `getunclassified_index` (`classified`, `tweetType`) USING BTREE,
INDEX `timeline_index` (`word`, `createdOnGMTDate`, `classified`) USING BTREE,
INDEX `createdOnGMTDate_index` (`createdOnGMTDate`) USING BTREE,
INDEX `locdetail_index` (`country`, `id`) USING BTREE,
FULLTEXT INDEX `twtText_index` (`twtText`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=12608048;
The table has more than 10 million records. How can I optimize it?
EDITED
Explain on 2nd query
"id";"select_type";"table";"type";"possible_keys";"key";"key_len";"ref";"rows";"Extra"
"1";"SIMPLE";"wrdTrk";"range";"word_index,word_createdOnGMT";"word_index";"602";NULL;"222847";"Using where; Using temporary; Using filesort"
Regards,
Rohit
In Query2, I suggest that:
1. remove DATE_FORMAT and CONVERT_TZ. You can process in PHP to be an output or between's condition.
2. like ('dell'): I don't see any '%', so you can use wrdTrk.word = 'dell' to let it faster.
The convert_tz in the where condition needs to be removed,
Select
count(*) as cnt,
DATE_FORMAT(CONVERT_TZ(wrdTrk.createdOnGMTDate,'+00:00','+05:30'),'%Y-%m-%d')
as dat
from
t_twitter_tracking wrdTrk
where
wrdTrk.word like ('dell')
and CONVERT_TZ(wrdTrk.createdOnGMTDate,'+00:00','+05:30')
between '2010-12-12 00:00:00' and '2010-12-26 00:00:00'
group by dat;
This will lead to comparing each row and finding out the right result, hence a tremendous improvement in the query result. Just passed the converted data to the query.
Select
count(*) as cnt,
DATE_FORMAT(CONVERT_TZ(wrdTrk.createdOnGMTDate,'+00:00','+05:30'),'%Y-%m-%d')
as dat
from
t_twitter_tracking wrdTrk
where
wrdTrk.word like ('dell')
and CONVERT_TZ(wrdTrk.createdOnGMTDate,'+00:00','+05:30')
between '2010-12-12 00:00:00' and '2010-12-26 00:00:00'
group by dat;