mysql ignore index for order by on join - mysql

CREATE TABLE `call_session` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`contact_id` bigint(20) unsigned NOT NULL,
`campaign_id` bigint(20) unsigned DEFAULT NULL,
`user_id` bigint(20) unsigned DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`type` varchar(16) DEFAULT NULL,
`status` varchar(16) DEFAULT NULL,
`continue_reason_id` bigint(20) unsigned DEFAULT NULL,
`continue_by` bigint(20) unsigned DEFAULT NULL,
`end_reason_id` bigint(20) unsigned DEFAULT NULL,
`comment` varchar(255) DEFAULT '',
`call_count` int(11) NOT NULL DEFAULT '0',
`answered_call_count` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_call_session_contact_id` (`contact_id`),
KEY `ix_call_session_user_id` (`user_id`),
KEY `ix_call_session_campaign_id` (`campaign_id`),
KEY `call_session_continue_by_foreign` (`continue_by`),
KEY `call_session_end_reason_id_foreign` (`end_reason_id`),
KEY `ix_call_session_end_time` (`end_time`),
KEY `ix_call_session_start_time` (`start_time`),
KEY `ix_call_session_continue_reason_id` (`continue_reason_id`),
CONSTRAINT `call_session_campaign_id_foreign` FOREIGN KEY (`campaign_id`) REFERENCES `campaign` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_continue_by_foreign` FOREIGN KEY (`continue_by`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_continue_reason_id_foreign` FOREIGN KEY (`continue_reason_id`) REFERENCES `continue_reason` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `call_session_end_reason_id_foreign` FOREIGN KEY (`end_reason_id`) REFERENCES `end_reason` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
)
CREATE TABLE `continue_reason` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`project_id` bigint(20) unsigned DEFAULT NULL,
`sort` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_continue_reason_project_id` (`project_id`),
CONSTRAINT `continue_reason_project_id_foreign` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
The call_session table contain more than 500,000 rows.
The continue_reason table contain only one row.
When I join the continue_reason table for the name column, mysql ignore the start_date index so the query takes between 1.8 to 3 seconds.
I can't figure out why.
The problematic query is:
SELECT `call_session`.`id`,
`continue_reason`.`name`
FROM `call_session`
LEFT JOIN `continue_reason` ON `continue_reason`.`id` = `call_session`.`continue_reason_id`
ORDER BY `call_session`.`start_time` DESC LIMIT 1;
Explain result:
Same query without join works great:
SELECT `call_session`.`id`,
(SELECT `name` FROM `continue_reason` WHERE id = `call_session`.`continue_reason_id`) AS `continue_reason.name`
FROM `call_session`
ORDER BY `call_session`.`start_time` DESC LIMIT 1;
Explain results:
I found many similar questions but I don't have any DISTINCT, no GROUP BY, and there is a very clear benefit for using the index.
Any thoughts?
Thanks in advance
P.S. Force index didn't help.
Here is the Optimizer report

Related

MySQL wrong ORDER BY for hungarian characters

I run the following query:
SELECT name FROM shops ORDER BY name;
The current result:
ABC shop
ÁDE shop
ALT shop
Expected result should be:
ABC shop
ALT shop
ÁDE shop
Looks like A and Á are equal. (Correct order: a < á < e < é < o < ó < ö...)
I tried to use hungarian collation (with mysql 8.0.17):
ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_hu_0900_ai_ci;
I found that the mysql team have fixed this issue in mysql 8.0.1: https://bugs.mysql.com/bug.php?id=12519
SHOW CREATE TABLE shops;
| shops | CREATE TABLE `shops` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`address_id` bigint(20) unsigned DEFAULT NULL,
`shop_category_id` bigint(20) unsigned DEFAULT NULL,
`shop_chain_id` bigint(20) unsigned DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`partner_id` bigint(20) unsigned DEFAULT NULL,
`location_id` bigint(20) unsigned DEFAULT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`user_id` bigint(20) unsigned DEFAULT NULL,
`deputy_user_id` bigint(20) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `shops_address_id_foreign` (`address_id`),
KEY `shops_shop_category_id_foreign` (`shop_category_id`),
KEY `shops_shop_chain_id_foreign` (`shop_chain_id`),
KEY `shops_partner_id_foreign` (`partner_id`),
KEY `shops_location_id_foreign` (`location_id`),
KEY `shops_user_id_foreign` (`user_id`),
KEY `shops_deputy_user_id_foreign` (`deputy_user_id`),
CONSTRAINT `shops_address_id_foreign` FOREIGN KEY (`address_id`) REFERENCES `addresses` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_deputy_user_id_foreign` FOREIGN KEY (`deputy_user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_partner_id_foreign` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE CASCADE,
CONSTRAINT `shops_shop_category_id_foreign` FOREIGN KEY (`shop_category_id`) REFERENCES `shop_categories` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_shop_chain_id_foreign` FOREIGN KEY (`shop_chain_id`) REFERENCES `shop_chains` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=564 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hu_0900_ai_ci
No simple solution:
Plan A: Write your own collation.
Plan B: Write a bug at bugs.mysql.com complaining that even in 8.0.17, utf8mb4_hu_0900_ai_ci is "incorrect".
(Meanwhile, I suspect utf8mb4_hungarian_ci was left with the 5.7 collation for backward compatibility.)
The question itself is wrong. The expected result is:
ABC shop
ÁDE shop
ALT shop
and it's right.
https://helyesiras.mta.hu/helyesiras/default/akh12#F2_4

Foreign key failure when inserting into child table ( referencing composite unique index)

I have problem when trying to insert row into my child database table.
Here is my parent table:
CREATE TABLE `payables` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`currency_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `payables_id_type_unique` (`id`,`type`),
KEY `payables_currency_id_foreign` (`currency_id`),
CONSTRAINT `payables_currency_id_foreign` FOREIGN KEY (`currency_id`) REFERENCES `currencies` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
And here is my child table:
CREATE TABLE `pays` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`transaction_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`value` int(10) unsigned NOT NULL,
`status_id` int(10) unsigned NOT NULL,
`payable_id` int(10) unsigned NOT NULL,
`payable_type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `pays_transaction_id_unique` (`transaction_id`),
KEY `pays_user_id_index` (`user_id`),
KEY `pays_status_id_index` (`status_id`),
KEY `pays_payable_id_index` (`payable_id`),
KEY `pays_payable_type_index` (`payable_type`),
KEY `pays_payable_id_payable_type_foreign` (`payable_id`,`payable_type`),
CONSTRAINT `pays_payable_id_payable_type_foreign` FOREIGN KEY (`payable_id`, `payable_type`) REFERENCES `payables` (`id`, `type`) ON UPDATE CASCADE,
CONSTRAINT `pays_status_id_foreign` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE,
CONSTRAINT `pays_transaction_id_foreign` FOREIGN KEY (`transaction_id`) REFERENCES `transactions` (`id`) ON UPDATE CASCADE,
CONSTRAINT `pays_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
That is the table I'm trying to seed, the parent table is already seeded.
I'm using laravel query builder to create these tables, so if anyone more experienced with databases could review the code, that would be much appreciated, thanks.
Sorry just missed that i was inserting different data type to my column.

not able to apply foreign key constraint in mysql

I can run the below query and there are no nulls in the user_id column in the result -
select c.*,r.user_id from chat_group_users c left join roleuser r
on c.user_id = r.user_id;
But I cannot apply the foreign key constraint and mysql just says
Error Code: 1215. Cannot add foreign key constraint
The command i wrote -
ALTER TABLE chat_group_users
ADD CONSTRAINT fk_roleuser FOREIGN KEY (user_id) REFERENCES roleuser (user_id)
ON DELETE cascade ON UPDATE cascade;
The table structures -
CREATE TABLE `roleuser` (
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`UserNM` varchar(50) NOT NULL,
`UserPS` varchar(150) NOT NULL,
`r_username` varchar(50) DEFAULT NULL,
`UGroup` varchar(5) NOT NULL DEFAULT 'Usar',
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
PRIMARY KEY (`User_ID`),
UNIQUE KEY `Index_2` (`UserNM`),
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=latin1;
CREATE TABLE `chat_group_users` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) DEFAULT NULL,
`user_id` int(10) DEFAULT NULL,
`is_admin` varchar(5) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`auto_id`),
UNIQUE KEY `unique_user_group` (`user_id`,`group_id`),
KEY `fc_group_id` (`group_id`),
CONSTRAINT `fc_group_id` FOREIGN KEY (`group_id`)
REFERENCES `chat_group`(`auto_id`)
ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
missed the unsigned on the chat_group_users -
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, ...

Mysql innerjoin very slow

I have an SQL query that takes over 4 hours to complete.
The query is as follows
UPDATE carepathaction a1
INNER JOIN carepathaction a2
ON a1.sourceoid = a2.oid
INNER JOIN carepathgroup g
ON g.sourceoid = a2.carepathgroupoid
INNER JOIN carepathphase f
ON f.oid = g.carepathphaseoid
SET a1.carepathgroupoid = g.oid
WHERE f.code = 'EDIT'
He has to update 65000 items. Is there some optimalization i can do in this query?
CarePathAction
CREATE TABLE `carepathaction`
(
`oid` INT(11) NOT NULL,
`typeid` CHAR(1) NOT NULL DEFAULT 'X',
`sequencenumber` INT(11) NOT NULL,
`description` VARCHAR(1024) NOT NULL,
`duration` INT(11) NOT NULL comment 'In minutes',
`deadline` INT(11) NOT NULL comment
'# dagen in combinatie met deadlineType (sinds begin traject of sinds begin deze actie)'
,
`deadlinetype` INT(11) NOT NULL comment
'1) = deadline sinds zorgpad\n2) = deadline sinds start deze actie\n3) = deadline sinds zorgpadfase'
,
`repeatperweek` INT(11) DEFAULT NULL,
`numbertorepeat` INT(11) DEFAULT NULL,
`carepathoid` INT(11) DEFAULT NULL,
`usertaskoid` INT(11) DEFAULT NULL,
`templateattachment` VARCHAR(45) DEFAULT NULL,
`partyrolecategoryoid` INT(11) DEFAULT NULL,
`automatic` BIT(1) NOT NULL DEFAULT b'0',
`carepathphaseoid` INT(11) DEFAULT NULL,
`parentoid` INT(11) DEFAULT NULL,
`documentoid` INT(11) DEFAULT NULL,
`epdtemplateoid` INT(11) DEFAULT NULL,
`producttypeoid` INT(11) DEFAULT NULL,
`evaluateby` INT(11) DEFAULT NULL,
`pathlock` BIT(1) NOT NULL DEFAULT b'0',
`carepathgroupoid` INT(11) NOT NULL,
`activitygroupoid` INT(11) DEFAULT NULL,
`title` VARCHAR(45) DEFAULT NULL,
`carepathactionswitchtype` INT(11) DEFAULT NULL,
`modulesdomaincode` VARCHAR(10) DEFAULT NULL,
`startday` INT(11) NOT NULL DEFAULT '0',
`sourceoid` INT(11) DEFAULT NULL,
`templatefilename` VARCHAR(100) DEFAULT NULL,
`istask` BIT(1) NOT NULL DEFAULT b'0',
`typecontact` INT(11) DEFAULT NULL,
`carepathactionepdoid` INT(11) DEFAULT NULL,
`carepathactiontreatmentplanoid` INT(11) DEFAULT NULL,
`clientportalenabled` BIT(1) DEFAULT NULL,
`startafternumberofdaysepddatefield` INT(11) DEFAULT NULL,
`startafterdateepdfieldoid` INT(11) DEFAULT NULL,
`epdtemplatefieldoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathactionfk1_idx` (`carepathoid`),
KEY `carepathactionfk2_idx` (`carepathphaseoid`),
KEY `carepathactionfk3_idx` (`documentoid`),
KEY `carepathactionfk4_idx` (`epdtemplateoid`),
KEY `carepathactionfk5_idx` (`producttypeoid`),
KEY `carepathactionfk6_idx` (`carepathgroupoid`),
KEY `carepathactionfk6_idx1` (`activitygroupoid`),
KEY `carepathactionfk7_idx` (`parentoid`),
KEY `carepathactionfk8_idx` (`usertaskoid`),
KEY `carepathactionfk10_idx` (`partyrolecategoryoid`),
KEY `carepathactionfk11_idx` (`carepathactionepdoid`),
KEY `carepathactionfk12_idx` (`carepathactiontreatmentplanoid`),
KEY `carepathactionfk13_idx` (`startafterdateepdfieldoid`),
KEY `carepathactionfk14_idx` (`epdtemplatefieldoid`),
CONSTRAINT `carepathactionfk14` FOREIGN KEY (`epdtemplatefieldoid`)
REFERENCES `epdtemplatefield` (`oid`) ON DELETE no action ON UPDATE no
action,
CONSTRAINT `carepathactionfk1` FOREIGN KEY (`carepathoid`) REFERENCES
`carepath` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk10` FOREIGN KEY (`partyrolecategoryoid`)
REFERENCES `partyrolecategory` (`oid`) ON DELETE no action ON UPDATE no
action,
CONSTRAINT `carepathactionfk11` FOREIGN KEY (`carepathactionepdoid`)
REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action
,
CONSTRAINT `carepathactionfk12` FOREIGN KEY (
`carepathactiontreatmentplanoid`) REFERENCES `carepathaction` (`oid`) ON
DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk13` FOREIGN KEY (`startafterdateepdfieldoid`)
REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action
,
CONSTRAINT `carepathactionfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES
`carepathphase` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk3` FOREIGN KEY (`documentoid`) REFERENCES
`document` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk4` FOREIGN KEY (`epdtemplateoid`) REFERENCES
`epdtemplate` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk5` FOREIGN KEY (`producttypeoid`) REFERENCES
`producttype` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk6` FOREIGN KEY (`carepathgroupoid`) REFERENCES
`carepathgroup` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk7` FOREIGN KEY (`parentoid`) REFERENCES
`carepathaction` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk8` FOREIGN KEY (`usertaskoid`) REFERENCES
`tasktoschedule` (`oid`) ON DELETE no action ON UPDATE no action
)
CarePathGroup
CREATE TABLE `carepathgroup`
(
`oid` INT(11) NOT NULL,
`sequencenumber` INT(11) NOT NULL,
`title` VARCHAR(50) NOT NULL,
`carepathoid` INT(11) DEFAULT NULL,
`carepathphaseoid` INT(11) NOT NULL,
`sourceoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathgroupfk1_idx` (`carepathoid`),
KEY `carepathgroupfk2_idx` (`carepathphaseoid`),
CONSTRAINT `carepathgroupfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES
`carepathphase` (`oid`) ON DELETE no action ON UPDATE no action
)
CarePathPhase
CREATE TABLE `carepathphase`
(
`oid` INT(11) NOT NULL,
`code` VARCHAR(6) NOT NULL,
`description` VARCHAR(45) NOT NULL,
`sequencenumber` INT(11) NOT NULL DEFAULT '0',
`carepathoid` INT(11) DEFAULT NULL,
`carepathphasetype` INT(11) NOT NULL DEFAULT '0',
`sourceoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathphasefk1_idx` (`carepathoid`),
CONSTRAINT `carepathphasefk1` FOREIGN KEY (`carepathoid`) REFERENCES
`carepath` (`oid`) ON DELETE no action ON UPDATE no action
)
explain
1 SIMPLE f ALL PRIMARY 61925 Using where
1 SIMPLE g ref CarePathGroupFK2_idx CarePathGroupFK2_idx 4 amis.f.Oid 1
1 SIMPLE a1 ALL 166582 Using join buffer
1 SIMPLE a2 eq_ref PRIMARY,CarePathActionFK6_idx PRIMARY 4 amis.a1.SourceOid 1 Using where
There are two ALL join types in the explain, these are the ones you need to focus (for details see mysql documentation on explain output).
The create table statements show that:
You do not have index on the carepathphase.code field, which is used in the where criteria, so you should rectify that.
There is no index on carepathaction.sourceoid, which is used in the join, so you should create one there. You may try to create a combined index on oid, sourceoid fields (or the other way around) because mysql may be able to use the combined index to match the rows.
Run explain on the select (if you have mysql v5.6 or newer, then you can run it directly on the update) after adding the indexes and see, if the ALLs change. The 2nd all next to a2 seems to be the real critical stuff to me.

In My Project i have x_user_module_perm table for which i need to keep one primary key and one composite primary key

i am using a syntax :-
CREATE TABLE `x_user_module_perm` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NULL DEFAULT NULL,
`module_id` bigint(20) NULL DEFAULT NULL,
`create_time` datetime NULL DEFAULT NULL,
`update_time` datetime NULL DEFAULT NULL,
`added_by_id` bigint(20) NULL DEFAULT NULL,
`upd_by_id` bigint(20) NULL DEFAULT NULL,
`is_allowed` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
PRIMARY KEY (`user_id`,`module_id`),
KEY `x_user_module_perm_idx_module_id` (`module_id`),
KEY `x_user_module_perm_idx_user_id` (`user_id`),
CONSTRAINT `x_user_module_perm_FK_module_id` FOREIGN KEY (`module_id`) REFERENCES `x_modules_master` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `x_user_module_perm_FK_user_id` FOREIGN KEY (`user_id`) REFERENCES `x_portal_user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ;
I am getting a error cannot use two primary key's
Do is it possible, Guy's please Guide me !!!
Try changing PRIMARY KEY (user_id,module_id) to UNIQUE INDEX(user_id,module_id)