Related
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
When I run this query:
select b.email,
concat('[AAA]: Xin chao QK ', a.client_name, ' HD ',a.account_no, ' vua khoi tao') as subject,
replace( (select email_content from xxff_message where refid=1 and status='O'),'#LOAN_ID#', a.account_no) as message, -- cp init
a.loan_id
from interface.vw_loan_full a
left join interface.xxff_client b
on a.client_id=b.client_id
where 1=1
and a.status=100
and (a.loan_id, email) not in ( select loan_id, receiver from xxff_loan_message_sent where stage='INIT100_MSG_EMAIL')
and date(a.value_date) between DATE_ADD(CURDATE(), INTERVAL -30 day) and date(now());
And I get this error:
Err] 1270 - Illegal mix of collations (utf8_unicode_ci,IMPLICIT),
(utf8_unicode_ci,COERCIBLE), (utf8_general_ci,IMPLICIT) for operation
'replace'
When I research, this cause is different utf8_unicode_ci and utf8_general_ci. But I checked all table, database are using utf8_unicode_ci .
But view table vw_loan_full is utf8_general_ci. Please help me change collation of view table.
Because I run command:
ALTER TABLE vw_loan_full CONVERT TO CHARACTER SET utf8 COLLATE
utf8_unicode_ci;
And get Error:
[Err] 1347 - 'interface.vw_loan_full' is not BASE TABLE
My result from SHOW CREATE TABLE xxff_client:
CREATE TABLE xxff_client ( client_id bigint(20) NOT NULL,
client_name varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
national_id varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
issue_date date DEFAULT NULL, issue_place varchar(120) COLLATE
utf8_unicode_ci DEFAULT NULL, Gender varchar(15) COLLATE
utf8_unicode_ci DEFAULT NULL, dob date DEFAULT NULL, phone
varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, email
varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL, address
varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL, status
varchar(1) COLLATE utf8_unicode_ci DEFAULT NULL, creation_date
date DEFAULT NULL, USER_ID varchar(20) COLLATE utf8_unicode_ci
DEFAULT NULL, company_name varchar(150) COLLATE utf8_unicode_ci
DEFAULT NULL, company_address varchar(255) COLLATE utf8_unicode_ci
DEFAULT NULL, title varchar(50) COLLATE utf8_unicode_ci DEFAULT
NULL, dept_name varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
join_date date DEFAULT NULL, salary_date int(11) DEFAULT NULL,
remain_of_laborcontract int(11) DEFAULT NULL, salary_recently
varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL, eco_userid
bigint(20) DEFAULT NULL, otp_count tinyint(20) DEFAULT '0',
PRIMARY KEY (client_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC
My result from SHOW CREATE TABLE xxff_loan_message_sent:
CREATE TABLE xxff_loan_message_sent ( refid bigint(20) NOT NULL
AUTO_INCREMENT, loan_id int(11) NOT NULL, stage varchar(20)
COLLATE utf8_unicode_ci DEFAULT NULL, sent_status varchar(6)
COLLATE utf8_unicode_ci DEFAULT NULL, created_by varchar(15)
COLLATE utf8_unicode_ci DEFAULT NULL, created_date date DEFAULT
NULL, receiver varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (refid) ) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT
CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC
My result from SHOW CREATE TABLE vw_loan_full:
CREATE ALGORITHM=UNDEFINED DEFINER=root#% SQL SECURITY DEFINER
VIEW vw_loan_full AS select a.id AS loan_id,a.client_id AS
client_id,b.display_name AS client_name,b.account_no AS
cif_no,a.product_id AS product_id,c.short_name AS
product_name,a.currency_code AS
ccy_code,a.annual_nominal_interest_rate AS
rate,a.term_frequency AS term,a.approved_principal AS
approved_principal,a.principal_amount AS
disbursed_amount,a.principal_outstanding_derived AS
pos,a.loan_status_id AS status,a.submittedon_date AS
created_date,a.submittedon_userid AS
created_by,a.expected_disbursedon_date AS
value_date,a.maturedon_date AS
maturity_date,a.approvedon_date AS
approvedon_date,a.disbursedon_date AS
disbursedon_date,a.expected_maturedon_date AS
expected_maturedon_date,a.loanpurpose_cv_id AS
loan_purpose,a.account_no AS account_no from
((mifostenant-default.m_loan a left join
mifostenant-default.m_client b on((a.client_id = b.id)))
left join mifostenant-default.m_product_loan c
on((a.product_id = c.id)))
My result from SHOW CREATE TABLE vw_loan_full:
CREATE TABLE xxff_message ( refid bigint(20) NOT NULL
AUTO_INCREMENT, msg_type varchar(8) COLLATE utf8_unicode_ci
DEFAULT NULL, sms_content varchar(500) COLLATE utf8_unicode_ci
DEFAULT NULL, email_content varchar(2000) COLLATE utf8_unicode_ci
DEFAULT NULL, email_subject varchar(250) COLLATE utf8_unicode_ci
DEFAULT NULL, status varchar(4) COLLATE utf8_unicode_ci DEFAULT
NULL, created_by varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
created_date datetime DEFAULT NULL, updated_by varchar(15)
COLLATE utf8_unicode_ci DEFAULT NULL, updated_date datetime
DEFAULT NULL, stage_code varchar(35) COLLATE utf8_unicode_ci
DEFAULT NULL, email_receiver varchar(120) COLLATE utf8_unicode_ci
DEFAULT NULL, PRIMARY KEY (refid) ) ENGINE=InnoDB
AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
ROW_FORMAT=DYNAMIC
How I can change collation for view table in MySQL? Thanks for your helpfull !!!
MySQL 5.5 in Windows
I have deleted a table from my server called 'companies' that has foreign keys and also has relationships with other tables. Now I'm trying to import the table again with a backup I download, but it throws an error of incorrectly formed foreign keys
I have tried to create just the table with 2 charts a name and an id, but it's not working it also throws the error of Foreign key constraint is incorrectly formed.
CREATE TABLE `companies` (
`id` int(10) UNSIGNED NOT NULL,
`company_name` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`company_email` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`company_phone` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`logo` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`login_background` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` text COLLATE utf8_unicode_ci NOT NULL,
`website` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`currency_id` int(10) UNSIGNED DEFAULT NULL,
`package_id` int(10) UNSIGNED DEFAULT NULL,
`package_type` enum('monthly','annual') COLLATE utf8_unicode_ci NOT NULL
DEFAULT 'monthly',
`timezone` varchar(191) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Asia/Kolkata',
`date_format` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'd-m-Y',
`time_format` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'h:i a',
`locale` varchar(191) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'en',
`latitude` decimal(10,8) NOT NULL DEFAULT 26.91243360,
`longitude` decimal(11,8) NOT NULL DEFAULT 75.78727090,
`leaves_start_from` enum('joining_date','year_start') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'joining_date',
`active_theme` enum('default','custom') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'default',
`status` enum('active','inactive','license_expired') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'active',
`last_updated_by` int(10) UNSIGNED DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`stripe_id` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`card_brand` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`card_last_four` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`trial_ends_at` timestamp NULL DEFAULT NULL,
`licence_expire_on` date DEFAULT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Error Message:
(Error: 150 "Foreign key constraint is incorrectly formed")
First is to get all foreign keys of "Comapanies" table using this query.
Then drop all foreign keys
ALTER TABLE `companies`
DROP FOREIGN KEY `id_name_fk`;
Then execute your create table statement
we have written a query like below and also created proper indexes on table.
QUERY
SELECT ref_order_id, order_id, cams_ref_order_id
FROM cart_entries_archive
WHERE regular_price <> product_price
AND ref_order_id >0
AND cams_ref_order_id > 0;
But the query is performing full table scan due to this we are getting load spikes .
we tried by adding indexes on where clause columns but still performing full scan .Please rewrite the query if possible.
query explain plan
mysql> explain select ref_order_id,order_id,cams_ref_order_id from cart_entries_archive where regular_price <> product_price and ref_order_id >0 and cams_ref_order_id > 0;
+----+-------------+----------------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | cart_entries_archive | ALL | NULL | NULL | NULL | NULL | 6490560 | Using where |
+----+-------------+----------------------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
Table structure:
mysql> show create table cart_entries_archive\G
*************************** 1. row ***************************
Table: cart_entries_archive
Create Table: CREATE TABLE `cart_entries_archive` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`address_id` int(11) DEFAULT NULL,
`backorder_date` datetime DEFAULT NULL,
`cancelled_date` datetime DEFAULT NULL,
`cart_entry_id` int(11) NOT NULL,
`cart_id` int(11) NOT NULL,
`delivery_date` datetime DEFAULT NULL,
`delivery_method` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`delivery_note` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`discount_amount` decimal(8,2) DEFAULT '0.00',
`gift_message` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`occasion` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`order_date` datetime DEFAULT NULL,
`order_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`order_status` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`product_name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`product_extra` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`product_price` decimal(8,2) DEFAULT '0.00',
`product_count` int(11) DEFAULT NULL,
`product_cost` decimal(8,2) DEFAULT '0.00',
`product_sku` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`product_notes` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`parent_product_sku` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`returned_date` datetime DEFAULT NULL,
`release_date` datetime DEFAULT NULL,
`regular_price` decimal(8,2) DEFAULT '0.00',
`shipping_cost` decimal(8,2) DEFAULT '0.00',
`service_charge` decimal(8,2) DEFAULT '0.00',
`sku_option_name` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`sku_option_value` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_company` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_fname` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_lname` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_address` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`shipping_address2` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`shipping_city` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_country` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_province` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_postal_code` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`shipping_phone` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`shipping_phone_ext` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`ship_tracking_number` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`status` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`tax` decimal(8,2) DEFAULT '0.00',
`total_charge` decimal(8,2) DEFAULT '0.00',
`website_id` int(11) DEFAULT NULL,
`microsite_id` int(11) DEFAULT NULL,
`defer_reason` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`defer_key` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`shipping_evening_phone` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`shipping_mobile_phone` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`shipping_email` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`shipping_title` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`shipping_district` varchar(30) COLLATE latin1_bin DEFAULT NULL,
`location_type` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`occasion_id` int(11) DEFAULT NULL,
`occasion_date` datetime DEFAULT NULL,
`occasion_do_remind` int(11) DEFAULT NULL,
`coupon_ref_source_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`delivery_date_verified` int(11) DEFAULT NULL,
`florist_peak_charge` float DEFAULT NULL,
`member_id` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`delivery_location_code` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`simply_iflora` int(11) DEFAULT NULL,
`rotation_weight` int(11) DEFAULT NULL,
`area_charge` decimal(8,2) DEFAULT NULL,
`cf_indicator` varchar(5) COLLATE latin1_bin DEFAULT NULL,
`category_id` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`cms_note` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`qas_queried` int(11) DEFAULT NULL,
`shipping_verified` int(11) DEFAULT NULL,
`occasion_event_name` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`push_date` datetime DEFAULT NULL,
`ref_order_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`relationship` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`std_delivery_id` int(11) DEFAULT NULL,
`opt_delivery_id` int(11) DEFAULT NULL,
`service_date` datetime DEFAULT NULL,
`parameters` varchar(1024) COLLATE latin1_bin DEFAULT NULL,
`order_type` varchar(32) COLLATE latin1_bin DEFAULT NULL,
`cams_ref_order_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`membership_discount` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`cart_entry_id`),
UNIQUE KEY `idx_2204` (`cart_entry_id`,`cart_id`),
UNIQUE KEY `idx_2318` (`cart_entry_id`,`order_id`),
KEY `idx_1049` (`order_date`),
KEY `idx_726` (`cart_id`),
KEY `idx_840` (`order_id`),
KEY `idx_row_create` (`row_create`),
KEY `idx_1035` (`push_date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
Your where clause uses ">" rather than "=", making it far less likely that an index will be useful. What % of rows in that table meet the criteria where ref_order_id>0 and cams_ref_order_id>0 ? If it is a high percentage, a table scan could be the fastest approach. Even if 10% of the records meet that criteria, it might mean the RDBMS has to read almost every page of the table.
If you want it to be "Index Only", you could add the following index:
create index TMP001 on cart_entries_archive
(ref_order_id, cams_ref_order_id, order_id, regular_price, product_price)
The fields from the where clause lead the index and everything else follows. If this is the only query you care about, and if the cost of maintaining the index is negligible, then create it and you're done.
I am struggling with a MySQL query. Here's the problem:
SELECT * from sf_forecasts WHERE contentid='1234' AND fulldate='2012-12-13'
There is one record in my table that matches this criteria, and this query finds it with no difficulty. However, when I move to update it using this query...
UPDATE sf_forecasts
SET listingTitle ='Some Snow Resort', location ='49.23, -115.22'
WHERE contentid='1234' AND fulldate='2012-12-13'
...I always get a notice of "0 rows affected." Strangely, the query is actually updating the record, but it's only touching fields that are NULL. If my example record has a 'location' value of NULL, the query will update the data, but if it already contains a non-NULL value, the query will not change it, even though the query clearly contains new information.
What gives?
P.S. I am using a PRIMARY key of contentid+fulldate (b-tree, unique) if this matters.
P.S.S. Here is the 'SHOW CREATE TABLE sf_forecasts' results as requested below:
sf_forecasts CREATE TABLE `sf_forecasts` (
`contentid` int(11) NOT NULL,
`fulldate` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`listingTitle` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`location` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`dayofmonth` smallint(2) NOT NULL,
`dayofweek` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`minTempF` smallint(4) NOT NULL,
`maxTempF` smallint(4) NOT NULL,
`minTempC` smallint(4) NOT NULL,
`maxTempC` smallint(4) NOT NULL,
`skyCondition` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`precip` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`snowPotential` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`wind` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`minTempF_custom` smallint(4) DEFAULT NULL,
`maxTempF_custom` smallint(4) DEFAULT NULL,
`minTempC_custom` smallint(4) DEFAULT NULL,
`maxTempC_custom` smallint(4) DEFAULT NULL,
`baseH_custom` smallint(4) DEFAULT NULL,
`baseL_custom` smallint(4) DEFAULT NULL,
`topH_custom` smallint(4) DEFAULT NULL,
`topL_custom` smallint(4) DEFAULT NULL,
`skyCondition_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`precip_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`snowPotential_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`wind_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`icon` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`icon_custom` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`shortrange_custom` varchar(5000) COLLATE utf8_unicode_ci DEFAULT NULL,
`longrange_custom` varchar(5000) COLLATE utf8_unicode_ci DEFAULT NULL,
`scCode` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`scUpdate` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`lastUpdate` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`skys` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`contentid`,`fulldate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
The issue is with difference in datatype length defined and length of data being updated
Your location is varchar(10) and you are trying to insert 14 characters
You can always check if there are errors after query execution:
SHOW ERRORS;
SHOW WARNINGS;