MySql vs MariaDB index query - mysql

I have a mysql database 5.1.73-0 ubuntu0.10.04.1-log that run this sentence in 0,1 s :
select date_format(if(isnull(acctstoptime),CURDATE(),acctstoptime),
'%Y-%m-%d') as fecha ,count(*
)
from radacct
where radacct.username like '%_ins_98\_%'
and ((acctstoptime)>=
( SELECT max(fFecNav)+ INTERVAL 1 day as fFecNav
from swb_NavegaDias
where idInstalacion=98
)
or isnull(acctstoptime)
)
group by if(isnull(acctstoptime),CURDATE(),date(acctstoptime))
order by acctstoptime;
explain:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY radacct range acctstoptime acctstoptime 9 8646 Using where; Using temporary; Using filesort
2 SUBQUERY swb_NavegaDias ALL 54400 Using where
Then I do a mysql dump and update on MariaDB 10.1.8-MariaDB-log CentOS7
and same query takes 5 seconds to execute.
Explain
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY radacct ALL acctstoptime 4621901 Using where; Using temporary; Using filesort
2 SUBQUERY swb_NavegaDias ALL 53946 Using where
The problema is whith the subquery, that executed single takes 0,06s but makes slow the main one.
select max(fFecNav)+ INTERVAL 1 day as fFecNav from swb_NavegaDias where idInstalacion=98;
If I change the main query without subquery it takes 0,1s:
select date_format(if(isnull(acctstoptime),CURDATE(),acctstoptime),
'%Y-%m-%d') as fecha ,count(*
)
from radacct
where radacct.username like '%_ins_98\_%'
and ((acctstoptime)>= '2015-12-20'
or isnull(acctstoptime)
)
group by if(isnull(acctstoptime),CURDATE(),date(acctstoptime))
order by acctstoptime;
Table definition are exactly the same on both databases .. because i've done a mysql dump.
I've tryed ANALYZE TABLE, force indexs but none makes run the query like with mysql.
What else i can check ?
**** EDITED ***
I've tryed with SQL_NO_CACHE and same results.
The tables are exactly the same as i have donne a full restore of the database.
Show create table Mysql:
CREATE TABLE `radacct` (
`radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
`acctsessionid` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctuniqueid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`username` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`groupname` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`realm` varchar(64) COLLATE utf8_unicode_ci DEFAULT '',
`nasipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`nasportid` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`nasporttype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctstarttime` datetime DEFAULT NULL,
`acctstoptime` datetime DEFAULT NULL,
`acctsessiontime` int(12) DEFAULT NULL,
`acctauthentic` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_start` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_stop` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctinputoctets` bigint(20) DEFAULT NULL,
`acctoutputoctets` bigint(20) DEFAULT NULL,
`calledstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`callingstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctterminatecause` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`servicetype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedprotocol` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctstartdelay` int(12) DEFAULT NULL,
`acctstopdelay` int(12) DEFAULT NULL,
`xascendsessionsvrkey` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`radacctid`),
KEY `framedipaddress` (`framedipaddress`),
KEY `acctsessionid` (`acctsessionid`),
KEY `acctsessiontime` (`acctsessiontime`),
KEY `acctuniqueid` (`acctuniqueid`),
KEY `acctstarttime` (`acctstarttime`),
KEY `acctstoptime` (`acctstoptime`),
KEY `nasipaddress` (`nasipaddress`),
KEY `username` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT=8162219 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
And Show Create table MariaDB:
CREATE TABLE `radacct` (
`radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
`acctsessionid` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctuniqueid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`username` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`groupname` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`realm` varchar(64) COLLATE utf8_unicode_ci DEFAULT '',
`nasipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`nasportid` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`nasporttype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctstarttime` datetime DEFAULT NULL,
`acctstoptime` datetime DEFAULT NULL,
`acctsessiontime` int(12) DEFAULT NULL,
`acctauthentic` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_start` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_stop` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctinputoctets` bigint(20) DEFAULT NULL,
`acctoutputoctets` bigint(20) DEFAULT NULL,
`calledstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`callingstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctterminatecause` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`servicetype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedprotocol` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctstartdelay` int(12) DEFAULT NULL,
`acctstopdelay` int(12) DEFAULT NULL,
`xascendsessionsvrkey` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`radacctid`),
KEY `framedipaddress` (`framedipaddress`),
KEY `acctsessionid` (`acctsessionid`),
KEY `acctsessiontime` (`acctsessiontime`),
KEY `acctuniqueid` (`acctuniqueid`),
KEY `acctstarttime` (`acctstarttime`),
KEY `acctstoptime` (`acctstoptime`),
KEY `nasipaddress` (`nasipaddress`),
KEY `username` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT=8162213 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Thanks

Related

How to import a table that was deleted with foreign keys?

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

Create Hibernate For Joining Table

I have to join 2 tables. The column is want to join is email from candidate table and password from user table. How to do it. Can Someone Help me. candidate
table is Shown Below:
CREATE TABLE `candidate` (`candidate_id` bigint(12) NOT NULL,`user_id`int(11) DEFAULT NULL,`firstname` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`lastname` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`dob` date DEFAULT NULL,`gender` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,`phone` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `address_id` int(11) DEFAULT NULL, `profile_title` varchar(120) COLLATE utf8_unicode_ci DEFAULT NULL, `profile_summary` varchar(10000) COLLATE utf8_unicode_ci DEFAULT NULL, `total_experience` int(11) DEFAULT NULL COMMENT 'year,month', `current_location` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `current_salary` double DEFAULT NULL, `expected_salary` double DEFAULT NULL, `status` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `percentage` int(45) DEFAULT NULL,`updated_on` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `noticeperiod` int(11) DEFAULT NULL, `martialstatus` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `currentoffer` double DEFAULT NULL, `idproof` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `industry` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,`functionalarea` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL, `functionalrole` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL, `resign` bit(1) DEFAULT NULL,`preferlocation` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,`type` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`notice_period_update` timestamp NULL DEFAULT NULL `passport` bit(1) DEFAULT NULL,`gross_salary` double DEFAULT NULL,`profile_type` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,`rrm_user_id` int(11) DEFAULT NULL, PRIMARY KEY (`candidate_id`), KEY `Candidate_Address_idx` (`address_id`),KEY `Can_user` (`user_id`),KEY `CandidateStatus_idx` (`status`),KEY `CandidatePercent_idx` (`percentage`),KEY `user_id_idx` (`rrm_user_id`),CONSTRAINT `Can_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `Candidate_Address`FOREIGN KEY (`address_id`) REFERENCES `address` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `user_id` FOREIGN KEY (`rrm_user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
User is:
CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`password` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`first_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `last_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,`type` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`active` bit(1) DEFAULT NULL,`deleted` bit(1) DEFAULT NULL,`timezone` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`created_on` timestamp NULL DEFAULT NULL,`updated_on` timestamp NULL DEFAULT CURRENT_TIMESTAMP,`activationcode` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`secure_key` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `username_UNIQUE` (`username`)) ENGINE=InnoDB AUTO_INCREMENT=1505 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You need a very simple SQL query to do this:
SELECT
c.candidate_id,
c.user_id,
c.email,
u.password
FROM
candidate as c
JOIN user as u ON c.user_id = u.user_id
Working example:
http://sqlfiddle.com/#!9/0ba876/1
Try to learn some SQL before you try to progress in this field.
https://www.w3schools.com/sql/

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.

Mysql Index COLLATE utf8_unicode_ci

I updated database from freeradius and now there is a sql sentence that take more tan 30 seconds to execute while before takes only 0,5s to execute.
This is the OLD definition tables:
CREATE TABLE `radacct` (
`radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
`acctsessionid` varchar(64) NOT NULL DEFAULT '',
`acctuniqueid` varchar(32) NOT NULL DEFAULT '',
`username` varchar(64) NOT NULL DEFAULT '',
`groupname` varchar(64) NOT NULL DEFAULT '',
`realm` varchar(64) DEFAULT '',
`nasipaddress` varchar(15) NOT NULL DEFAULT '',
`nasportid` varchar(15) DEFAULT NULL,
`nasporttype` varchar(32) DEFAULT NULL,
`acctstarttime` datetime DEFAULT NULL,
`acctstoptime` datetime DEFAULT NULL,
`acctsessiontime` int(12) DEFAULT NULL,
`acctauthentic` varchar(32) DEFAULT NULL,
`connectinfo_start` varchar(50) DEFAULT NULL,
`connectinfo_stop` varchar(50) DEFAULT NULL,
`acctinputoctets` bigint(20) DEFAULT NULL,
`acctoutputoctets` bigint(20) DEFAULT NULL,
`calledstationid` varchar(50) NOT NULL DEFAULT '',
`callingstationid` varchar(50) NOT NULL DEFAULT '',
`acctterminatecause` varchar(32) NOT NULL DEFAULT '',
`servicetype` varchar(32) DEFAULT NULL,
`framedprotocol` varchar(32) DEFAULT NULL,
`framedipaddress` varchar(15) NOT NULL DEFAULT '',
`acctstartdelay` int(12) DEFAULT NULL,
`acctstopdelay` int(12) DEFAULT NULL,
`xascendsessionsvrkey` varchar(10) DEFAULT NULL,
PRIMARY KEY (`radacctid`),
KEY `username` (`username`),
KEY `framedipaddress` (`framedipaddress`),
KEY `acctsessionid` (`acctsessionid`),
KEY `acctsessiontime` (`acctsessiontime`),
KEY `acctuniqueid` (`acctuniqueid`),
KEY `acctstarttime` (`acctstarttime`),
KEY `acctstoptime` (`acctstoptime`),
KEY `nasipaddress` (`nasipaddress`)
) ENGINE=InnoDB AUTO_INCREMENT=3514770 DEFAULT CHARSET=latin1;
CREATE TABLE `userinfo` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(128) DEFAULT NULL,
`firstname` varchar(200) DEFAULT NULL,
`lastname` varchar(200) DEFAULT NULL,
`email` varchar(200) DEFAULT NULL,
`department` varchar(200) DEFAULT NULL,
`company` varchar(200) DEFAULT NULL,
`workphone` varchar(200) DEFAULT NULL,
`homephone` varchar(200) DEFAULT NULL,
`mobilephone` varchar(200) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`city` varchar(200) DEFAULT NULL,
`state` varchar(200) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`zip` varchar(200) DEFAULT NULL,
`notes` varchar(200) DEFAULT NULL,
`changeuserinfo` varchar(128) DEFAULT NULL,
`portalloginpassword` varchar(128) DEFAULT '',
`enableportallogin` int(32) DEFAULT '0',
`creationdate` datetime DEFAULT '0000-00-00 00:00:00',
`creationby` varchar(128) DEFAULT NULL,
`updatedate` datetime DEFAULT '0000-00-00 00:00:00',
`updateby` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`),
KEY `company` (`company`)
) ENGINE=MyISAM AUTO_INCREMENT=188493 DEFAULT CHARSET=latin1;
This is the NEW definition tables:
CREATE TABLE `radacct` (
`radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
`acctsessionid` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctuniqueid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`username` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`groupname` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`realm` varchar(64) COLLATE utf8_unicode_ci DEFAULT '',
`nasipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`nasportid` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`nasporttype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctstarttime` datetime DEFAULT NULL,
`acctstoptime` datetime DEFAULT NULL,
`acctsessiontime` int(12) DEFAULT NULL,
`acctauthentic` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_start` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_stop` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctinputoctets` bigint(20) DEFAULT NULL,
`acctoutputoctets` bigint(20) DEFAULT NULL,
`calledstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`callingstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctterminatecause` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`servicetype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedprotocol` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctstartdelay` int(12) DEFAULT NULL,
`acctstopdelay` int(12) DEFAULT NULL,
`xascendsessionsvrkey` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`radacctid`),
KEY `username` (`username`),
KEY `framedipaddress` (`framedipaddress`),
KEY `acctsessionid` (`acctsessionid`),
KEY `acctsessiontime` (`acctsessiontime`),
KEY `acctuniqueid` (`acctuniqueid`),
KEY `acctstarttime` (`acctstarttime`),
KEY `acctstoptime` (`acctstoptime`),
KEY `nasipaddress` (`nasipaddress`)
) ENGINE=MyISAM AUTO_INCREMENT=3519495 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `userinfo` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(128) DEFAULT NULL,
`firstname` varchar(200) DEFAULT NULL,
`lastname` varchar(200) DEFAULT NULL,
`email` varchar(200) DEFAULT NULL,
`department` varchar(200) DEFAULT NULL,
`company` varchar(200) DEFAULT NULL,
`workphone` varchar(200) DEFAULT NULL,
`homephone` varchar(200) DEFAULT NULL,
`mobilephone` varchar(200) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`city` varchar(200) DEFAULT NULL,
`state` varchar(200) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`zip` varchar(200) DEFAULT NULL,
`notes` varchar(200) DEFAULT NULL,
`changeuserinfo` varchar(128) DEFAULT NULL,
`portalloginpassword` varchar(128) DEFAULT '',
`enableportallogin` int(32) DEFAULT '0',
`creationdate` datetime DEFAULT '0000-00-00 00:00:00',
`creationby` varchar(128) DEFAULT NULL,
`updatedate` datetime DEFAULT '0000-00-00 00:00:00',
`updateby` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`),
KEY `company` (`company`)
) ENGINE=MyISAM AUTO_INCREMENT=188894 DEFAULT CHARSET=latin1;
Notice than the OLD table have both CHARSET=latin1, while the NEW tables by definition have different charset and collate: CHARSET=utf8 COLLATE=utf8_unicode_ci and CHARSET=latin1. That should be the difference.
Now this is the sentence I run:
select TIMESTAMPDIFF(SECOND,max(acctstarttime),now()) as segons,(department) as idClient,(userinfo.username)
from
(select radacct.username as id
from radacct,userinfo
where userinfo.company=98
and radacct.username = userinfo.username
group by userinfo.username
order by max(radacct.radacctid) desc limit 0,6) as tbl,radacct,userinfo
where radacct.username=tbl.id and radacct.username = userinfo.username
group by radacct.username
order by max(radacct.radacctid) desc;
And this is the DESCRIBE for the OLD TABLES - FAST query (0.4s):
id * select_type * table type possible_keys key key_len ref rows Extra *
1 PRIMARY <derived2> ALL {null} {null} {null} {null} 6 Using temporary; Using filesort
1 PRIMARY radacct ref username username 66 tbl.id 5
1 PRIMARY userinfo ref username username 131 radius.radacct.username 1 Using where
2 DERIVED radacct index username username 66 {null} 28768 Using index; Using temporary; Using filesort
2 DERIVED userinfo ref username,company username 131 radius.radacct.username 1 Using where
And this is the DESCRIBE for the NEW TABLES - SLOW query (30s):
id * select_type * table type possible_keys key key_len ref rows Extra *
1 PRIMARY <derived2> ALL {null} {null} {null} {null} 6 Using temporary; Using filesort
1 PRIMARY radacct ref username username 194 tbl.id 11
1 PRIMARY userinfo ALL {null} {null} {null} {null} 188911 Using where; Using join buffer
2 DERIVED userinfo ALL company {null} {null} {null} 188911 Using where; Using temporary; Using filesort
2 DERIVED radacct ref username username 194 func 11 Using where
Thanks.
EDIT:
I change charset on userinfo:
alter table radius.userinfo convert to character set utf8 collate utf8_unicode_ci;
Now the NEW - Slow query runs in 3 seconds but still not as fast as the OLD database.
Also I change VARCHAR lenght to be the same on both querys and still not going by index like is does with the OLD database.
ALTER TABLE radius.userinfo CHANGE username username VARCHAR(64);
I'm just running the subquery that goes slow:
select radacct.username as id
from radacct,userinfo
where userinfo.company=98
and radacct.username = userinfo.username
group by userinfo.username
order by max(radacct.radacctid) desc limit 0,6
This is the DESCRIBE OLD - Fast:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE radacct index username username 66 37545 Using index; Using temporary; Using filesort
1 SIMPLE userinfo ref username,company username 131 radius.radacct.username 1 Using where
This is the DESCRIBE NEW - Slow:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE radacct ALL username 56879 Using temporary; Using filesort
1 SIMPLE userinfo ref company,username username 195 radius.radacct.username 1 Using where
Why is not getting the radacct.username index ?
EDIT 2: Add new definition with new COLLATION-CHARSET.
CREATE TABLE `radacct` (
`radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
`acctsessionid` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctuniqueid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`username` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`groupname` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`realm` varchar(64) COLLATE utf8_unicode_ci DEFAULT '',
`nasipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`nasportid` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`nasporttype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctstarttime` datetime DEFAULT NULL,
`acctstoptime` datetime DEFAULT NULL,
`acctsessiontime` int(12) DEFAULT NULL,
`acctauthentic` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_start` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`connectinfo_stop` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`acctinputoctets` bigint(20) DEFAULT NULL,
`acctoutputoctets` bigint(20) DEFAULT NULL,
`calledstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`callingstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctterminatecause` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`servicetype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedprotocol` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`framedipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`acctstartdelay` int(12) DEFAULT NULL,
`acctstopdelay` int(12) DEFAULT NULL,
`xascendsessionsvrkey` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`radacctid`),
KEY `framedipaddress` (`framedipaddress`),
KEY `acctsessionid` (`acctsessionid`),
KEY `acctsessiontime` (`acctsessiontime`),
KEY `acctuniqueid` (`acctuniqueid`),
KEY `acctstarttime` (`acctstarttime`),
KEY `acctstoptime` (`acctstoptime`),
KEY `nasipaddress` (`nasipaddress`),
KEY `username` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT=3607301 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `userinfo` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`firstname` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`lastname` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`department` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`company` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`workphone` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`homephone` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`mobilephone` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`notes` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`changeuserinfo` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
`portalloginpassword` varchar(128) COLLATE utf8_unicode_ci DEFAULT '',
`enableportallogin` int(32) DEFAULT '0',
`creationdate` datetime DEFAULT '0000-00-00 00:00:00',
`creationby` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
`updatedate` datetime DEFAULT '0000-00-00 00:00:00',
`updateby` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `company` (`company`),
KEY `username` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT=191546 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I see 3 issues:
company = 98
`company` varchar(200) DEFAULT NULL,
If it is a number then use a numeric datatype.
Replace
KEY `company` (`company`)
with
INDEX(company, username)
But perhaps the real problem is this:
radacct.username = userinfo.username
with one being latin1 and the other being utf8.

how to improve performance of my query?

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.