Join on large table getting slower - mysql

I have 2 innoDB tables, One has 15166897 records and an other has 700000 records, sometime when there is load on the server a simple join query based on email address on these tables took much time to execute and some time is executes quickly. However, both the tables are properly indexed.
We have around 800 tables into our database, DB size is 40GB. We are running t2.medium RDS instance in AWS having 4GB of RAM in which 3GB is allocated for INNO DB BUFFER POOL.
I have the following questions, Please suggest on it.
Is it a good idea to break the large table into small tables so that records on the one table become less, Does this will improve the query performance?
Does the large table over the time downgrade the performance of the query?
Is this RDS configuration is enough for DB, OR should need to improve the configuration?
EDIT:
Below is my table schema and index definition:
Table 1: Total Records: 7,00,000
CREATE TABLE IF NOT EXISTS `tbl_contact_master` (
`id` int(11) NOT NULL,
`first_name` varchar(60) NOT NULL,
`last_name` varchar(60) NOT NULL,
`email_address` varchar(250) NOT NULL,
`agency_name` varchar(150) NOT NULL,
`state` varchar(30) NOT NULL,
`zip_code` varchar(20) NOT NULL,
`srch_zip` varchar(20) NOT NULL,
`title` varchar(100) NOT NULL,
`street` varchar(255) NOT NULL,
`street2` varchar(255) NOT NULL,
`city` varchar(30) NOT NULL,
`country` varchar(30) NOT NULL,
`phone` varchar(20) NOT NULL,
`ext` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`years_of_experience` varchar(50) DEFAULT NULL,
`owner_manager` varchar(100) NOT NULL,
`agency_type` varchar(100) NOT NULL,
`agency_sales` varchar(100) NOT NULL,
`agency_website` varchar(100) NOT NULL,
`agency_host` varchar(100) NOT NULL,
`agency_host_name` varchar(100) NOT NULL,
`agency_affiliation` varchar(100) NOT NULL,
`agent_sales` varchar(100) NOT NULL,
`id_number_type` varchar(16) NOT NULL,
`id_number` varchar(15) NOT NULL,
`destination1` varchar(100) NOT NULL,
`destination2` varchar(100) NOT NULL,
`destination3` varchar(100) NOT NULL,
`travel_type1` varchar(100) NOT NULL,
`travel_type2` varchar(100) NOT NULL,
`travel_type3` varchar(100) NOT NULL,
`travel_type4` varchar(100) NOT NULL,
`update_first_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_last_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_email_address` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_state` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_zip_code` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_title` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_street` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_street2` datetime NOT NULL,
`update_city` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_country` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_phone` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_ext` datetime NOT NULL,
`update_fax` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_years_of_experience` datetime NOT NULL,
`update_owner_manager` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_type` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_sales` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_website` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_host` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_host_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_affiliation` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agent_sales` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_id_number_type` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_id_number` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_destination1` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_destination2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_destination3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type1` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type4` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`create_status` tinyint(1) NOT NULL COMMENT '1 for website or 0 for add by csv',
`created_date` datetime NOT NULL,
`modified_date` datetime NOT NULL,
`submited_date` datetime NOT NULL,
`FirstPageDate` datetime NOT NULL,
`is_retired` tinyint(1) NOT NULL COMMENT '0 = No, 1 = Yes',
`update_is_retired` datetime NOT NULL,
`ip_address` varchar(30) NOT NULL,
`is_bademail` tinyint(1) NOT NULL,
`update_is_bademail` datetime NOT NULL,
`is_mice` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice` datetime NOT NULL,
`mice_company_disc` mediumint(5) unsigned NOT NULL,
`update_mice_company_disc` datetime NOT NULL,
`mice_meeting_region_plan` text NOT NULL,
`update_mice_meeting_region_plan` datetime NOT NULL,
`mice_ida` text NOT NULL,
`update_mice_ida` datetime NOT NULL,
`mice_associations` text NOT NULL,
`update_mice_associations` datetime NOT NULL,
`mice_meeting_regions` text NOT NULL,
`update_mice_meeting_regions` datetime NOT NULL,
`mice_average_attendance` mediumint(5) unsigned NOT NULL,
`update_mice_average_attendance` datetime NOT NULL,
`mice_annual_budget` mediumint(5) unsigned NOT NULL,
`update_mice_annual_budget` datetime NOT NULL,
`mice_facilities` text NOT NULL,
`update_mice_facilities` datetime NOT NULL,
`mice_annual_peak_rooms` mediumint(5) unsigned NOT NULL,
`update_mice_annual_peak_rooms` datetime NOT NULL,
`mice_nof_meetings` mediumint(5) unsigned NOT NULL,
`update_mice_nof_meetings` datetime NOT NULL,
`mice_job_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_job_resp_level` datetime NOT NULL,
`mice_event_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_event_resp_level` datetime NOT NULL,
`mice_is_planning` mediumint(5) unsigned NOT NULL,
`update_mice_is_planning` datetime NOT NULL,
`mice_experience` mediumint(5) unsigned NOT NULL,
`update_mice_experience` datetime NOT NULL,
`mice_primary_job` mediumint(5) unsigned NOT NULL,
`update_mice_primary_job` datetime NOT NULL,
`mice_company_size` mediumint(5) unsigned NOT NULL,
`update_mice_company_size` datetime NOT NULL,
`mice_primary_business` mediumint(5) unsigned NOT NULL,
`update_mice_primary_business` datetime NOT NULL,
`mice_primary_business_other` text NOT NULL,
`mice_event_specialty` text NOT NULL,
`update_mice_event_specialty` datetime NOT NULL,
`mice_created_date` datetime NOT NULL,
`mice_modified_date` datetime NOT NULL,
`mice_submitted_date` datetime NOT NULL,
`is_ta` tinyint(4) NOT NULL DEFAULT '0',
`update_is_ta` datetime NOT NULL,
`ta_created_date` datetime NOT NULL,
`ta_modified_date` datetime NOT NULL,
`ta_submitted_date` datetime NOT NULL,
`is_mice_retired` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice_retired` datetime NOT NULL,
`not_ta_by` tinyint(2) NOT NULL COMMENT '0 for default, 1 for superadmin, 2 for contact',
`update_not_ta_by` datetime NOT NULL,
`not_mice_by` tinyint(2) NOT NULL COMMENT '0 for default, 1 for superadmin, 2 for contact',
`update_not_mice_by` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1096438 DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_contact_master`
--
ALTER TABLE `tbl_contact_master`
ADD PRIMARY KEY (`id`),
ADD KEY `email_address` (`email_address`),
ADD KEY `is_ta` (`is_ta`),
ADD KEY `mice_created_date` (`mice_created_date`),
ADD KEY `mice_modified_date` (`mice_modified_date`),
ADD KEY `mice_submitted_date` (`mice_submitted_date`),
ADD KEY `ta_created_date` (`ta_created_date`),
ADD KEY `ta_modified_date` (`ta_modified_date`),
ADD KEY `ta_submitted_date` (`ta_submitted_date`),
ADD KEY `is_mice_retired` (`is_mice_retired`),
ADD KEY `is_mice` (`is_mice`),
ADD KEY `is_bademail` (`is_bademail`),
ADD KEY `is_retired` (`is_retired`),
ADD KEY `created_date` (`created_date`),
ADD KEY `modified_date` (`modified_date`),
ADD KEY `submited_date` (`submited_date`),
ADD KEY `srch_zip` (`srch_zip`);
Table 2:
CREATE TABLE IF NOT EXISTS `tbl_master_partition` (
`contact_id` int(10) unsigned NOT NULL,
`inactive_group` varchar(200) DEFAULT NULL COMMENT '1=15,2=30,3=45,4=60',
`inactive_from` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_master_partition`
--
ALTER TABLE `tbl_master_partition`
ADD KEY `contact_id` (`contact_id`), ADD KEY `inactive_group` (`inactive_group`);
Table 3: Total Records: 14400000
CREATE TABLE IF NOT EXISTS `tbl_mandrill_email_mapping` (
`id` bigint(20) NOT NULL,
`log_id` int(11) NOT NULL,
`batch_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`m_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT 'unique mandrill message id',
`s_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`open` int(11) NOT NULL DEFAULT '0',
`click` int(11) NOT NULL DEFAULT '0',
`send` int(11) NOT NULL DEFAULT '0',
`hard_bounce` int(11) NOT NULL DEFAULT '0',
`soft_bounce` int(11) NOT NULL DEFAULT '0',
`reject` int(11) NOT NULL DEFAULT '0',
`spam` int(11) NOT NULL DEFAULT '0',
`unsub` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=15131814 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_mandrill_email_mapping`
--
ALTER TABLE `tbl_mandrill_email_mapping`
ADD PRIMARY KEY (`id`),
ADD KEY `m_id` (`m_id`),
ADD KEY `email` (`email`),
ADD KEY `hard_bounce` (`hard_bounce`),
ADD KEY `reject` (`reject`),
ADD KEY `open` (`open`),
ADD KEY `click` (`click`),
ADD KEY `send` (`send`),
ADD KEY `log_id` (`log_id`),
ADD KEY `batch_id` (`batch_id`),
ADD KEY `s_id` (`s_id`);
Missing TABLE added
CREATE TABLE IF NOT EXISTS `tbl_contact_173_230` (
`id` int(11) NOT NULL,
`contact_id` int(11) NOT NULL,
`salutation` varchar(10) NOT NULL,
`first_name` varchar(60) NOT NULL,
`last_name` varchar(60) NOT NULL,
`email_address` varchar(250) NOT NULL,
`secondary_email_address` varchar(250) NOT NULL,
`agency_name` varchar(150) NOT NULL,
`state` varchar(30) NOT NULL,
`zip_code` varchar(20) NOT NULL,
`srch_zip` varchar(20) NOT NULL,
`title` varchar(255) NOT NULL,
`street` varchar(255) NOT NULL,
`street2` varchar(255) NOT NULL,
`city` varchar(30) NOT NULL,
`country` varchar(30) NOT NULL,
`phone` varchar(20) NOT NULL,
`ext` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`owner_manager` varchar(140) DEFAULT NULL,
`years_of_experience` varchar(40) DEFAULT NULL,
`agency_type` varchar(140) NOT NULL,
`agency_sales` varchar(140) NOT NULL,
`agency_website` varchar(140) NOT NULL,
`agency_host` varchar(140) DEFAULT NULL,
`agency_host_name` varchar(140) NOT NULL,
`agency_affiliation` varchar(140) NOT NULL,
`agent_sales` varchar(140) NOT NULL,
`id_number_type` varchar(16) NOT NULL,
`id_number` varchar(15) NOT NULL,
`destination1` varchar(140) NOT NULL,
`destination2` varchar(140) NOT NULL,
`destination3` varchar(140) NOT NULL,
`travel_type1` varchar(140) NOT NULL,
`travel_type2` varchar(140) NOT NULL,
`travel_type3` varchar(140) NOT NULL,
`travel_type4` varchar(140) NOT NULL,
`update_first_name` datetime NOT NULL,
`update_last_name` datetime NOT NULL,
`update_email_address` datetime NOT NULL,
`update_agency_name` datetime NOT NULL,
`update_state` datetime NOT NULL,
`update_zip_code` datetime NOT NULL,
`update_title` datetime NOT NULL,
`update_street` datetime NOT NULL,
`update_street2` datetime NOT NULL,
`update_city` datetime NOT NULL,
`update_country` datetime NOT NULL,
`update_phone` datetime NOT NULL,
`update_ext` datetime NOT NULL,
`update_fax` datetime NOT NULL,
`update_owner_manager` datetime NOT NULL,
`update_years_of_experience` datetime NOT NULL,
`update_agency_type` datetime NOT NULL,
`update_agency_sales` datetime NOT NULL,
`update_agency_website` datetime NOT NULL,
`update_agency_host` datetime NOT NULL,
`update_agency_host_name` datetime NOT NULL,
`update_agency_affiliation` datetime NOT NULL,
`update_agent_sales` datetime NOT NULL,
`update_id_number_type` datetime NOT NULL,
`update_id_number` datetime NOT NULL,
`update_destination1` datetime NOT NULL,
`update_destination2` datetime NOT NULL,
`update_destination3` datetime NOT NULL,
`update_travel_type1` datetime NOT NULL,
`update_travel_type2` datetime NOT NULL,
`update_travel_type3` datetime NOT NULL,
`update_travel_type4` datetime NOT NULL,
`opt_in_marketing` varchar(10) DEFAULT NULL,
`travel_pro_user` varchar(10) DEFAULT NULL,
`create_status` tinyint(1) NOT NULL,
`crm_created_date` datetime NOT NULL,
`crm_modified_date` datetime NOT NULL,
`crm_submited_date` datetime NOT NULL,
`crm_sync_date` datetime NOT NULL,
`created_date` datetime NOT NULL,
`modified_date` datetime NOT NULL,
`submited_date` datetime NOT NULL,
`offer_entry` datetime NOT NULL,
`sync_date` datetime NOT NULL,
`crm_status` tinyint(1) NOT NULL,
`created_by` int(11) NOT NULL,
`is_retired` tinyint(1) NOT NULL,
`update_is_retired` datetime NOT NULL,
`is_bademail` tinyint(1) NOT NULL,
`update_is_bademail` datetime NOT NULL,
`is_ta` tinyint(4) NOT NULL DEFAULT '0',
`update_is_ta` datetime NOT NULL,
`ta_created_date` datetime NOT NULL,
`ta_modified_date` datetime NOT NULL,
`ta_submitted_date` datetime NOT NULL,
`mice_company_disc` text NOT NULL,
`update_mice_company_disc` datetime NOT NULL,
`mice_meeting_region_plan` text NOT NULL,
`update_mice_meeting_region_plan` datetime NOT NULL,
`mice_ida` text NOT NULL,
`update_mice_ida` datetime NOT NULL,
`mice_associations` text NOT NULL,
`update_mice_associations` datetime NOT NULL,
`mice_meeting_regions` text NOT NULL,
`update_mice_meeting_regions` datetime NOT NULL,
`mice_average_attendance` mediumint(5) unsigned NOT NULL,
`update_mice_average_attendance` datetime NOT NULL,
`mice_annual_budget` mediumint(5) unsigned NOT NULL,
`update_mice_annual_budget` datetime NOT NULL,
`mice_facilities` text NOT NULL,
`update_mice_facilities` datetime NOT NULL,
`mice_annual_peak_rooms` mediumint(5) unsigned NOT NULL,
`update_mice_annual_peak_rooms` datetime NOT NULL,
`mice_nof_meetings` mediumint(5) unsigned NOT NULL,
`update_mice_nof_meetings` datetime NOT NULL,
`mice_job_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_job_resp_level` datetime NOT NULL,
`mice_event_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_event_resp_level` datetime NOT NULL,
`mice_is_planning` mediumint(5) unsigned NOT NULL,
`update_mice_is_planning` datetime NOT NULL,
`mice_experience` mediumint(5) unsigned NOT NULL,
`update_mice_experience` datetime NOT NULL,
`mice_primary_job` mediumint(5) unsigned NOT NULL,
`update_mice_primary_job` datetime NOT NULL,
`mice_company_size` mediumint(5) unsigned NOT NULL,
`update_mice_company_size` datetime NOT NULL,
`mice_primary_business` mediumint(5) unsigned NOT NULL,
`mice_primary_business_other` text NOT NULL,
`update_mice_primary_business` datetime NOT NULL,
`mice_event_specialty` text NOT NULL,
`update_mice_event_specialty` datetime NOT NULL,
`is_mice` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice` datetime NOT NULL,
`mice_created_date` datetime NOT NULL,
`mice_modified_date` datetime NOT NULL,
`mice_submitted_date` datetime NOT NULL,
`is_mice_retired` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice_retired` datetime NOT NULL,
`recurring_opt_out` tinyint(1) unsigned NOT NULL DEFAULT '0',
`custom_648` varchar(255) NOT NULL,
`custom_649` varchar(255) NOT NULL,
`custom_650` varchar(255) NOT NULL,
`custom_717` varchar(255) NOT NULL,
`custom_718` varchar(255) NOT NULL,
`custom_719` varchar(255) NOT NULL,
`custom_1369` varchar(255) NOT NULL,
`custom_1454` varchar(255) NOT NULL,
`custom_1455` varchar(255) NOT NULL,
`custom_1469` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=103603 DEFAULT CHARSET=latin1;
--
-- Indexes for table `tbl_contact_173_230`
--
ALTER TABLE `tbl_contact_173_230`
ADD PRIMARY KEY (`id`),
ADD KEY `email_address` (`email_address`),
ADD KEY `contact_id` (`contact_id`),
ADD KEY `zip_code` (`zip_code`),
ADD KEY `srch_zip` (`srch_zip`),
ADD KEY `ta_submitted_date` (`ta_submitted_date`);
Below are the queries which getting slower or sometime just get stuck for an hrs.
1.
SELECT cm.id AS contact_id, mem.email, mem.open
FROM tbl_mandrill_email_mapping AS mem
JOIN tbl_contact_master AS cm ON cm.email_address = mem.email
WHERE mem.log_id = XXXXX
2.
SELECT COUNT(crm.id) as total_record, SUM(UPPER(opt_in_marketing)='NO') as unsub_email_count,
SUM(is_bademail=1) as invalid_email_count, COUNT(DISTINCT mp.`contact_id`) as inactive_email_count,
SUM(is_retired=1
OR is_mice_retired=1
) as retired_count
FROM (`tbl_contact_173_230` AS crm)
INNER JOIN `tbl_mandrill_email_mapping` as mem
ON `mem`.`email` = `crm`.`email_address`
LEFT JOIN`tbl_master_partition` AS mp
ON `mp`.`contact_id` = `crm`.`contact_id`
AND crm.`ta_submitted_date` < '2016-10-19'
AND FIND_IN_SET(5, mp.`inactive_group`) > 0
WHERE `crm`.`email_address` != '' AND`mem`.`log_id` = 'xxxx'
AND `mem`.`open` = 0
AND `mem`.`hard_bounce` = 0
AND `mem`.`reject` = 0
AND `mem`.`spam` = 0
ORDER BY `crm`.`id` ASC
Edit Note:
I have notice that, it mostly creates the issue when there is very frequent update/write on tbl_mandrill_email_mapping table. It's a log table which handles the email event's(sent,open,click,bounce etc.. log). In my case this table gets very busy once any bulk email sent out(approx 1,00,000 email) and it start handling all above events, sometimes this events are very frequent, In this case all the queries which reference the tbl_mandrill_email_mapping get stuck.
Any suggestion to handle this kind of situation? I am thinking about read replica so that all update/insert will handle by different server and read handle by different server, Will it work? Or is there any batter solution to handle this situation?
I am open for any other suggestion, Please suggest.

No! Do not break big tables into smaller ones. You already may have "too many tables". Instead, focus on indexing. Let's see SHOW CREATE TABLE and some of the important queries.
A table growing in size will slow down queries that fail to make good use of indexes. Not so for good indexes. (There are exceptions; let's see your queries.)
You have only 4GB? And you are shoehorning a 3GB buffer_pool into it? That does not leave much room for other structure, nor for other applications. This can lead to swapping, which is terrible for performance. Drop innodb_buffer_pool_size to 1500M.
Increasing the 'machine' size (and buffer_pool) may or may not help. The buffer_pool is a "cache", so the 40GB is not totally relevant. What we need is the "working set size", for which there is no good metric.
Do you know if you are I/O-bound? Or CPU-bound?
Based on the title, I recommend discussing the query.
More
(Now looking at Schema)
More indexes --> slower loading. Normally, a few good indexes is well worth the extra load time. But the emphasis is on few. Do not index flags; they will almost never be useful. (I assume your is_* columns are flags?)
Does your app really care when each column was updated? I see apps that start with such, but eventually abandon the idea.
Be careful not to mix CHARACTER SETs or COLLATIONs when JOINing. That is, in FROM a JOIN b ON a.x = b.y if x and y are no the same CHARACTER SET and COLLATION, then no index will be used.
Query 1 would benefit from this composite (and covering) index: INDEX(log_id, email, open).
I don't see the CREATE TABLE tbl_contact_173_230??

Related

i want to optimize mysql query

Please guys help me to optimize the below query
SELECT
`dti`.`CompanyId`,
`dti`.`Samiti`,
`dti`.`toll_date`,
`dti`.`MajorFee`,
`dti`.`MinorFee`,
`dti`.`SawalFee`,
SUM(dti.Tmwt) as Tmwt,
SUM(dti.Localminor) as Localminor,
SUM(dti.Swt) as Swt,
SUM(dti.Twt) as Twt,
SUM(((dti.Tmwt * dti.MajorFee) + (dti.Localminor * dti.MinorFee) + (dti.Swt * dti.SawalFee))) as total_wages,
SUM((dti.Twt * dti.govt_charges)) as govt_deduction,
SUM((((dti.Tmwt * dti.MajorFee) + (dti.Localminor * dti.MinorFee) + (dti.Swt * dti.SawalFee)) - (dti.Twt * dti.govt_charges))) as net_amount,
(SELECT (SUM(ld.amount) + SUM(ld.advance_deduction))
FROM psac_liability_deduction ld
WHERE ld.status = "Active" AND
ld.from_date >="2017-08-24" AND
ld.to_date <="2017-08-31" AND
ld.deducted_for = dti.CompanyId
) as group_liability_deduction,
(SELECT CONCAT(SUM(wi.GroupLiabilityDeduction), "|", SUM(wi.AdvanceWagesDeduction))
FROM psac_wagesitem wi
WHERE wi.status="Active" AND
wi.from_date >= "2017-08-24" AND
wi.to_date <= "2017-08-31" AND
wi.MainGroup=dti.Samiti AND
wi.FishermanId=dti.CompanyId
) as wages_deduction,
(SELECT CONCAT(SUM(cdp.product_liability), "|", SUM(cdp.wages_liability))
FROM psac_cash_deposited_payment cdp
WHERE cdp.status="Active" AND
cdp.deposit_date >= "2017-08-24" AND
cdp.deposit_date <= "2017-08-31" AND
cdp.maingroup_id=dti.Samiti AND
cdp.fisherman_id=dti.CompanyId
) as cash_deposited,
`fm`.`Name` as `fishername`,
`fm`.`Code` as `fishername_code`,
`fm`.`Bank`,
`fm`.`IfscCode`,
`fm`.`AccountNo`
FROM `psac_dailytollinfo` `dti`
LEFT JOIN `psac_fisherman` `fm` ON `fm`.`ID`=`dti`.`CompanyId`
WHERE
`dti`.`status` = 'Active' AND
`dti`.`toll_date` >= '2017-08-24' AND
`dti`.`toll_date` <= '2017-08-31'
GROUP BY `dti`.`toll_date`, `dti`.`CompanyId`
ORDER BY `dti`.`toll_date` ASC
please help me to optimize this query. If i remove sub queries it will works perfect but with subqueries it takes too much time.
below are table structures
psac_dailytollinfo table
CREATE TABLE `psac_dailytollinfo` (
`ID` int(11) NOT NULL,
`toll_date` date NOT NULL,
`Point` int(11) NOT NULL,
`group_type` int(11) NOT NULL,
`Samiti` int(11) NOT NULL,
`DailytollId` int(11) NOT NULL,
`CompanyId` int(11) NOT NULL,
`Name` varchar(250) NOT NULL,
`govt_charges` float(15,2) NOT NULL,
`MajorFee` float(15,2) NOT NULL,
`MinorFee` float(15,2) NOT NULL,
`SawalFee` float(15,2) NOT NULL,
`Cqty` varchar(150) NOT NULL,
`Cwt` varchar(150) NOT NULL,
`Rqty` varchar(150) NOT NULL,
`Rwt` varchar(150) NOT NULL,
`Mqty` varchar(150) NOT NULL,
`Mwt` varchar(150) NOT NULL,
`Kqty` varchar(150) NOT NULL,
`Kwt` varchar(150) NOT NULL,
`Aqty` varchar(150) NOT NULL,
`Awt` varchar(150) NOT NULL,
`Sqty` varchar(11) NOT NULL,
`Swt` varchar(11) NOT NULL,
`Lqty` varchar(150) NOT NULL,
`Lwt` varchar(150) NOT NULL,
`Localminor` varchar(150) NOT NULL,
`Tmqty` varchar(150) NOT NULL,
`Tmwt` varchar(150) NOT NULL,
`Tqty` varchar(150) NOT NULL,
`Twt` varchar(150) NOT NULL,
`added_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(20) NOT NULL,
`status` enum('Active','Inactive','Deleted') NOT NULL DEFAULT 'Active'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
psac_liability_deduction table
CREATE TABLE `psac_liability_deduction` (
`ID` bigint(20) NOT NULL,
`wages_id` int(11) NOT NULL,
`wages_item_id` int(11) NOT NULL,
`amount` float(15,2) NOT NULL,
`advance_deduction` float(11,2) NOT NULL,
`group_type_id` int(11) NOT NULL,
`maingroup_id` int(11) NOT NULL,
`deducted_by` int(11) NOT NULL,
`deducted_for` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
`status` enum('Active','Inactive','Deleted') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
`added_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(50) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
psac_wagesitem table
CREATE TABLE `psac_wagesitem` (
`ID` int(11) NOT NULL,
`wages_for` enum('Fisherman','Group') NOT NULL DEFAULT 'Fisherman',
`wages_id` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
`group_type_id` int(11) NOT NULL,
`MainGroup` int(11) NOT NULL,
`FishermanId` int(11) NOT NULL,
`MajorFee` float(15,2) NOT NULL,
`MinorFee` float(15,2) NOT NULL,
`SawalFee` float(15,2) NOT NULL,
`major_wt` float(15,2) NOT NULL,
`minor_wt` float(15,2) NOT NULL,
`sawal_wt` float(15,2) NOT NULL,
`major_wage` float(15,2) NOT NULL,
`minor_wage` float(15,2) NOT NULL,
`sawal_wage` float(15,2) NOT NULL,
`TotalWage` float(15,2) NOT NULL,
`group_liability` float(15,2) NOT NULL,
`advance_wages` float(15,2) NOT NULL,
`GovDeduction` float(15,2) NOT NULL,
`GroupLiabilityDeduction` float(15,2) NOT NULL,
`AdvanceWagesDeduction` float(15,2) NOT NULL,
`final_wages` float(15,2) NOT NULL,
`added_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(20) NOT NULL,
`status` enum('Active','Inactive','Deleted') NOT NULL DEFAULT 'Active',
`editable` enum('Lock','Unlock') NOT NULL DEFAULT 'Unlock'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
psac_cash_deposited_payment table
CREATE TABLE `psac_cash_deposited_payment` (
`deposit_id` int(11) NOT NULL,
`deposited_by` enum('Fisherman','Group') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Fisherman',
`deposit_date` date NOT NULL,
`group_type_id` int(11) NOT NULL,
`maingroup_id` int(11) NOT NULL,
`fisherman_id` int(11) NOT NULL,
`product_liability` float(11,2) NOT NULL,
`wages_liability` float(11,2) NOT NULL,
`receipt_number` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`remark` text COLLATE utf8_unicode_ci NOT NULL,
`status` enum('Active','Inactive','Deleted') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
`editable` enum('Lock','Unlock') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Unlock',
`added_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_by` int(11) NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(20) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You understand that you are looking at a full 8 days? If you only wanted one week, change <= to <.
Switch all tables to InnoDB.
Have a PRIMARY KEY for every table -- either a 'natural' PK (composed of one or more columns that, together, uniquely define each row), or an AUTO_INCREMENT.
dti needs INDEX(status, toll_date)
Don't use (m,n) on FLOAT, it leads to an extra roundoff.
Don't use FLOAT for money, it leads to a roundoff.
FLOAT (with or without (m,n)) contains no more than 7 significant digits.
Consider DECIMAL(11,2) instead of float(11,2).
Be cautious about using latin1 in one table and utf8 in another -- if you need to JOIN on a VARCHAR; it must have the same charset and collation in order to use an index.
Where practical, make the ORDER BY identical to the GROUP BY.
These composite indexes that are likely to help performance:
dti: INDEX(status, toll_date)
ld: INDEX(status, deducted_for, from_date)
ld: INDEX(status, deducted_for, to_date)
wi: INDEX(status, MainGroup, FishermanId, from_date)
wi: INDEX(status, MainGroup, FishermanId, to_date)
cdp: INDEX(status, maingroup_id, fisherman_id, deposit_date)
(The date must be last; the other column(s) can be in any order.)
If you still have performance problems with the subqueries after you have added those indexes, let's see EXPLAIN SELECT ... so we can look again.
Don't splay an array across columns:
`Cqty` varchar(150) NOT NULL,
`Cwt` varchar(150) NOT NULL,
etc
Consider having qty and wt as two columns in another table.
Could there be two different values for MajorFee in a single day for a single company? That, and other things say that the GROUP BY is improperly formed.

MYSQL : How can we optimize the select query to fetch more than 1 million data using date range

I have a table containing more than 1 million data and I am trying to fetch using a date range. I have indexed the date column and still its searching the entire rows. Can someone give a best solution to fix this up.
/** Table Structure **/
CREATE TABLE `claim_history` (
`claim_history_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`consultation_id` varchar(50) NOT NULL,
`member_id` int(11) unsigned DEFAULT NULL,
`card_number` char(18) DEFAULT NULL,
`member_name` varchar(200) DEFAULT NULL,
`network_code` int(11) unsigned NOT NULL DEFAULT '0',
`mobile_number` varchar(50) DEFAULT NULL,
`soap_number` varchar(50) DEFAULT NULL,
`diagnosis_id` varchar(50) DEFAULT NULL,
`diagnosis_code` varchar(50) DEFAULT NULL,
`diagnosis_description` text,
`activity_id` int(11) unsigned DEFAULT NULL,
`activity_code` varchar(50) DEFAULT NULL,
`activity_description` text,
`activity_comments` text,
`activity_quantity` int(11) unsigned NOT NULL DEFAULT '0',
`is_erx` tinyint(1) NOT NULL DEFAULT '0',
`lab_id` int(11) unsigned DEFAULT NULL,
`lab_name` varchar(100) DEFAULT NULL,
`session_no` tinyint(1) unsigned NOT NULL DEFAULT '0',
`net_amount` decimal(10,2) DEFAULT NULL,
`copay_pct` decimal(10,2) DEFAULT NULL,
`copay_amt` decimal(10,2) DEFAULT NULL,
`total_cost` decimal(10,2) DEFAULT NULL,
`is_edited` tinyint(1) unsigned DEFAULT '0',
`edit_comments` text,
`is_assigned_mcc` tinyint(1) DEFAULT '0'
`mcc_user` int(11) unsigned DEFAULT NULL,
`mcc_user_name` varchar(50) DEFAULT NULL,
`rule_name` varchar(200) DEFAULT NULL,
`mcu_assigned_time` timestamp NULL DEFAULT NULL,
`mcu_action_performed_time` timestamp NULL DEFAULT NULL,
`mcu_open_time` timestamp NULL DEFAULT NULL,
`is_reappealed` tinyint(1) unsigned NOT NULL DEFAULT '0',
`reappeal_count` int(11) unsigned NOT NULL,
`denial_code` varchar(50) DEFAULT NULL,
`denial_description` text,
`rejection_comments` text,
`justification_comment` text,
`justification_reply` text,
`justification_count` tinyint(1) unsigned NOT NULL DEFAULT '0',
`edit_comment_reply` text,
`is_referral` tinyint(1) unsigned NOT NULL DEFAULT '0',
`is_physiotherapy` tinyint(1) unsigned NOT NULL DEFAULT '0',
`facility_id` int(11) unsigned DEFAULT NULL,
`provider_code` varchar(50) DEFAULT NULL,
`provider_name` varchar(100) DEFAULT NULL,
`doctor_user_id` int(11) DEFAULT NULL,
`doctor_name` varchar(100) DEFAULT NULL,
`referral_doctor_name` varchar(100) DEFAULT NULL,
`referral_clinic_name` varchar(100) DEFAULT NULL,
`pic_id` int(11) unsigned DEFAULT NULL,
`pic_name` varchar(100) DEFAULT NULL,
`ig_id` int(11) unsigned DEFAULT NULL,
`ig_name` varchar(100) DEFAULT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`history_created_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`history_last_modified_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) unsigned NOT NULL DEFAULT '0',
`last_modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_by` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`claim_history_id`),
KEY `idx_consultation_id` (`consultation_id`),
KEY `idx_card_number` (`member_id`,`card_number`),
KEY `idx_provider` (`facility_id`),
KEY `idx_soap_number` (`soap_number`),
KEY `idx_created_on` (`created_on`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
/** SQL QUERY **/
SELECT fields FROM claim_history
WHERE created_on BETWEEN '2017-01-01 00:00:00'
AND '2017-05-01 00:00:00';
The query you provided has a syntax error.
Dependent on the range you put into your query, the query optimiser might use the index or not.
Try a really short range like one day and then use one year, you'll probably already see the difference.
Check this thread for more details:
MySQL ignores my index on a timestamp column

Two MySql tables have correct indexes yet JOIN takes 9 seconds on small tables

mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
I am taking over a project. It is very old and the original programmer is long gone. No one has any idea why certain decisions were made.
The following query runs (on my Mac) in 9.5 seconds but if I remove the last JOIN then it drops to 2.5 seconds. What is wrong with that last JOIN?
select `ttl`.`id` AS `id`,
`ttl`.`name` AS `name`,
`ttl`.`updated_at` AS `last_update_on`,
`ttl`.`user_id` AS `list_creator`,
`ttl`.`retailer_nomination_list` AS `nomination_list`,
`ttl`.`created_at` AS `created_on`,
count(distinct `tlb`.`title_id`) AS `title_count`
from `haha_title_lists` `ttl`
left join `haha_title_list_to_users` `tltu` on((`ttl`.`id` = `tltu`.`title_list_id`))
left join `users` `u` on((`tltu`.`user_id` = `u`.`id`))
left join `users` `u2` on((`tltu`.`user_id` = `u2`.`id`))
left join `haha_title_list_to_venues` `tlv` on((`ttl`.`id` = `tlv`.`title_list`))
left join `haha_venue_properties` `tvp` on((`tlv`.`venue_id` = `tvp`.`id`))
join `haha_title_list_to_books` `tlb` on((`ttl`.`id` = `tlb`.`title_list_id`))
join `wawa_title` `ot` on((`tlb`.`title_id` = `ot`.`title_id`))
group by `ttl`.`id`;
The tables:
CREATE TABLE `haha_title_list_to_books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title_id` int(11) NOT NULL,
`title_list_id` int(11) NOT NULL,
`sdk` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`created_at` datetime NOT NULL,
`promo_start_date` date DEFAULT NULL,
`promo_end_date` date DEFAULT NULL,
`promo_price` float DEFAULT NULL,
`confirmations` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nominations` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`title_note` text COLLATE utf8_unicode_ci,
`executed` int(11) DEFAULT NULL,
`event_created` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_promo_start_date` (`promo_start_date`),
KEY `idx_promo_end_date` (`promo_end_date`),
KEY `idx_title_list_to_books_title_id` (`title_id`),
KEY `idx_title_list_to_books_title_list_id` (`title_list_id`)
) ENGINE=MyISAM AUTO_INCREMENT=21847 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
and:
CREATE TABLE `wawa_title` (
`title_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
`title_alpha` varchar(25) NOT NULL,
`display_title` varchar(200) NOT NULL,
`subtitle` text NOT NULL,
`sdk10` varchar(13) DEFAULT '',
`sdk13` varchar(15) DEFAULT NULL,
`primary_sdk13` varchar(15) DEFAULT NULL,
`asin` varchar(10) DEFAULT NULL,
`pub_season` varchar(15) NOT NULL,
`pub_year` varchar(15) NOT NULL,
`bisac1` varchar(15) NOT NULL,
`bisac2` varchar(15) NOT NULL,
`bisac3` varchar(15) NOT NULL,
`barcode` varchar(30) DEFAULT NULL,
`dewey_decimal` varchar(15) NOT NULL,
`lib_of_congress` varchar(15) NOT NULL,
`spanish_language` tinyint(4) NOT NULL,
`target_audience` tinyint(3) unsigned DEFAULT NULL,
`language` varchar(20) DEFAULT NULL,
`edition` varchar(45) DEFAULT NULL,
`pages` int(11) DEFAULT NULL,
`number_in_series` int(11) DEFAULT NULL,
`trimsize` varchar(10) DEFAULT NULL,
`filesize` varchar(10) DEFAULT NULL,
`duration_hours` int(11) DEFAULT NULL,
`duration_minutes` int(11) DEFAULT NULL,
`discs` int(11) DEFAULT NULL,
`download` date DEFAULT NULL,
`size_unit` varchar(15) NOT NULL DEFAULT '',
`digitization_date` date NOT NULL,
`us_on_sale_date` date NOT NULL,
`aus_on_sale_date` date NOT NULL,
`can_on_sale_date` date NOT NULL,
`uk_on_sale_date` date NOT NULL,
`us_list_price` varchar(10) NOT NULL,
`aus_list_price` varchar(10) NOT NULL,
`can_list_price` varchar(10) NOT NULL,
`uk_list_price` varchar(10) NOT NULL,
`isPrimary` varchar(1) DEFAULT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modifier` int(11) NOT NULL,
`activated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`active` varchar(3) NOT NULL DEFAULT 'N',
`flagged_string` text,
`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`assets_id` varchar(20) DEFAULT NULL,
`book_details` text,
`book_keynote` text,
`exclude_goodreads` char(1) NOT NULL DEFAULT 'N',
`series_description` text,
`review_quote1` text,
`territory_id` int(11) DEFAULT '27',
`featured_newsletter_id` tinyint(3) unsigned DEFAULT '0',
`retailer_discovery_check` datetime DEFAULT NULL,
`suppress_retailer_approval` tinyint(1) DEFAULT '0',
`suppress_retailer_approval_reason` varchar(255) DEFAULT NULL,
`ebb_description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`slug` varchar(150) DEFAULT NULL,
`legacy_slug` varchar(150) DEFAULT NULL,
`us_agency_price` varchar(10) DEFAULT NULL,
`firebrand_title_id` int(11) DEFAULT NULL,
`ebb_label` varchar(200) DEFAULT NULL,
`ebb_end_sale_date` date DEFAULT NULL,
`ebb_downprice` decimal(10,2) DEFAULT NULL,
`book_club` varchar(1) DEFAULT NULL,
`best_seller` varchar(1) DEFAULT NULL,
`award_winner` varchar(1) DEFAULT NULL,
`discovery` char(1) NOT NULL DEFAULT 'Y',
`narrator_id` int(11) DEFAULT NULL,
`suppress_series_data` varchar(255) DEFAULT NULL,
PRIMARY KEY (`title_id`),
KEY `active_index` (`active`),
KEY `fk_title_series_id_idx` (`series_id`),
KEY `series_id` (`series_id`),
KEY `idx_title_sdk13` (`sdk13`),
KEY `idx_title_active_isprimary` (`active`,`isPrimary`),
KEY `bisac1` (`bisac1`),
KEY `bisac2` (`bisac2`),
KEY `bisac3` (`bisac3`),
KEY `idx_primary_sdk13` (`primary_sdk13`),
KEY `idx_territory_id` (`territory_id`),
CONSTRAINT `fk_title_series_id` FOREIGN KEY (`series_id`) REFERENCES `wawa_series` (`series_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=19700 DEFAULT CHARSET=utf8 |
If I remove this line:
join `wawa_title` `ot` on((`tlb`.`title_id` = `ot`.`title_id`))
The query speed drops from 9.5 seconds to 2.5 seconds. Not great, but a huge improvement.
And yet, both tables have indexes on table_id, so why would that line be a problem?
I notice that one table is InnoDB and the other is MyISAM. Would that have an effect?
Do not JOIN to tables that you don't use.
A JOIN often "explodes" the number of rows, then a GROUP BY like you have reels in the number of rows. To see this, leave all the JOINs there, but remove the GROUP BY. See how many rows you get.
To avoid part of that explosion, change
count(distinct `tlb`.`title_id`) AS `title_count`
to
( SELECT count(distinct `title_id`)
FROM `haha_title_list_to_books`
WHERE `ttl`.`id` = `title_list_id`
) AS `title_count`
and remove the current JOIN to tlb.
Mixing MyISAM and InnoDB should not have any direct impact on this SELECT. However, you should consider moving all of your tables to InnoDB.

how to optimize update statement using subquery?

I am running a update statement:
UPDATE ACTION a
INNER JOIN subscriberinfo s ON a.subscriberId=s.id
SET a.exceedusage = (SELECT FORMAT(((SUM(dataVolumeDownLink + dataVolumeUpLink))/1048576),2)
FROM cdr c
WHERE c.msisdn=s.msisdn
AND c.eventDate>=a.createdon
AND c.eventDate <= a.actionTakenOn)
WHERE a.remark='Reason : Data limit crossed'
AND a.exceedusage IS NULL;
But its taking too much time because of CDR table(millions of row). Is there any way I can rewrite this query so it can work fast?
EDIT:
Table structure of Action:
CREATE TABLE `action` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`actionTakenOn` datetime DEFAULT NULL,
`actionType` varchar(255) DEFAULT NULL,
`cdrCreatedOn` datetime DEFAULT NULL,
`cdrEventDate` datetime DEFAULT NULL,
`createdOn` datetime DEFAULT NULL,
`errorDescription` longtext,
`params` longtext,
`remark` longtext,
`requestedOn` datetime DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`subscriberDetails` longtext,
`takenBy` varchar(255) DEFAULT NULL,
`subscriberId` bigint(20) DEFAULT NULL,
`ticketId` bigint(20) DEFAULT NULL,
`dataPlanEndTime` datetime DEFAULT NULL,
`dataPlanStartTime` datetime DEFAULT NULL,
`dataUsage` bigint(20) DEFAULT NULL,
`dataplanName` varchar(255) DEFAULT NULL,
`exceedUsage` bigint(20) DEFAULT NULL,
`isNotified` bit(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKAB2F7E36E90F678D` (`subscriberId`),
KEY `FKAB2F7E3664633B07` (`ticketId`)
) ENGINE=MyISAM AUTO_INCREMENT=81534 DEFAULT CHARSET=latin1;
for Subscriberinfo:
CREATE TABLE `subscriberinfo` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`imsi` varchar(255) DEFAULT NULL,
`simType` varchar(45) DEFAULT NULL,
`dataPlanStartTime` datetime DEFAULT NULL,
`dataPlanEndTime` datetime DEFAULT NULL,
`dataplan` varchar(255) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`validDays` varchar(255) DEFAULT NULL,
`deviceName` varchar(500) DEFAULT NULL,
`lastDataPlanUpdatedOn` datetime DEFAULT NULL,
`lastDeviceUpdatedOn` datetime DEFAULT NULL,
`createdOn` datetime DEFAULT NULL,
`dataplanType` varchar(255) DEFAULT NULL,
`msisdn` bigint(20) DEFAULT NULL,
`dataLeft` bigint(20) DEFAULT NULL,
`billingSysDataPlanEndTime` datetime DEFAULT NULL,
`billingSysDataPlanStartTime` datetime DEFAULT NULL,
`billingSysValidDays` int(11) DEFAULT NULL,
`dataUsage` bigint(20) DEFAULT NULL,
`planDetail` varchar(255) DEFAULT NULL,
`currentSpeedLimit` varchar(255) DEFAULT NULL,
`lastBillingSysUpdatedOn` datetime DEFAULT NULL,
`lastUpdatedOn` datetime DEFAULT NULL,
`deviceType` varchar(255) DEFAULT NULL,
`lastImsiUpdatedOn` datetime DEFAULT NULL,
`skipCheck` tinyint(1) NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `msisdn_UNIQUE` (`msisdn`)
) ENGINE=InnoDB AUTO_INCREMENT=49032 DEFAULT CHARSET=latin1;
for CDR:
CREATE TABLE `cdr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`dataPacketDownLink` bigint(20) DEFAULT NULL,
`dataPacketUpLink` bigint(20) DEFAULT NULL,
`dataPlanEndTime` datetime DEFAULT NULL,
`dataPlanStartTime` datetime DEFAULT NULL,
`dataVolumeDownLink` bigint(20) DEFAULT NULL,
`dataVolumeUpLink` bigint(20) DEFAULT NULL,
`dataplan` varchar(255) DEFAULT NULL,
`dataplanType` varchar(255) DEFAULT NULL,
`createdOn` datetime DEFAULT NULL,
`deviceName` varchar(500) DEFAULT NULL,
`duration` int(11) NOT NULL,
`effectiveDuration` int(11) NOT NULL,
`hour` int(11) DEFAULT NULL,
`eventDate` datetime DEFAULT NULL,
`msisdn` bigint(20) DEFAULT NULL,
`quarter` int(11) DEFAULT NULL,
`validDays` int(11) DEFAULT NULL,
`dataLeft` bigint(20) DEFAULT NULL,
`completedOn` datetime DEFAULT NULL,
`causeForRecClosing` bigint(20) DEFAULT NULL,
`roaming` tinyint(1) DEFAULT NULL,
`servedBSAddress` varchar(255) DEFAULT NULL,
`simType` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_msisdn` (`msisdn`)
) ENGINE=MyISAM AUTO_INCREMENT=2580713 DEFAULT CHARSET=latin1;
UPDATE ACTION a
INNER JOIN subscriberinfo s ON a.subscriberId=s.id
INNER JOIN (SELECT FORMAT(((SUM(dataVolumeDownLink + dataVolumeUpLink))/1048576),2) as val,
msisdn
FROM cdr c
WHERE
c.eventDate>=a.createdon
AND c.eventDate <= a.actionTakenOn) sub on sub.msisdn=s.msisdn
SET a.exceedusage = sub.val
WHERE a.remark='Reason : Data limit crossed'
AND a.exceedusage IS NULL;
I would move the subquery into FROM (actually FROM in select and UPDATE section for the query) to let it be executed just once.
Possibly change the whole thing to a series of JOIN.
This is very much a guess (I am not sure on how your tables hang together, hence not sure on the GROUP BY), but maybe something like this:-
UPDATE ACTION a
INNER JOIN subscriberinfo s ON a.subscriberId=s.id
INNER JOIN cdr c ON c.msisdn=s.msisdn AND c.eventDate BETWEEN a.createdon AND a.actionTakenOn
SET a.exceedusage = FORMAT(((SUM(c.dataVolumeDownLink + c.dataVolumeUpLink))/1048576),2)
WHERE a.remark='Reason : Data limit crossed'
AND a.exceedusage IS NULL
GROUP BY a.subscriberId;

n-number of nested inner join

I have a sql-statement which correctly fetches data from one table. However, I need to fetch in the same way from n number of tables named table_n. All of these tables contain a 3-field primary key of projid, docid and revnr. I need to return n as docType as well, to differentiate the tables. The result will be sorted by projid and/or docid.
I tried sorting all the outputs from the different queries in PHP but it was way too slow (at least a few seconds on a 3MB database). I'm convinced MySQL/MSSQL will do it faster.
This is my current query:
SELECT a.* FROM `table_1` a
INNER JOIN (SELECT docid,
Max(revnr) max_val
FROM `table_1`
WHERE ( projid = something )
GROUP BY docid) b
ON a.docid = b.docid
AND a.revnr = b.max_val ORDER BY docid DESC
My current query gets the rows with highest revnr for each docid and projid.
I'm developing on MySQL but I need it to work on MSSQL as well. A general SQL solution would be great.
Thanks!
EDIT: Table schemas of the tables i currently have:
CREATE TABLE IF NOT EXISTS `table_1` (
`projid` int(11) NOT NULL,
`docid` int(11) NOT NULL,
`revnr` int(11) NOT NULL,
`revname` varchar(64) NOT NULL,
`signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sign` int(11) NOT NULL,
`ritningsnr` varchar(128) NOT NULL,
`moment` varchar(256) NOT NULL,
`omrade` varchar(256) NOT NULL,
`start` datetime NOT NULL,
`stop` datetime NOT NULL,
`extTodo` int(11) NOT NULL,
PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='egenkontroll';
CREATE TABLE IF NOT EXISTS `table_2` (
`projid` int(11) NOT NULL,
`docid` int(11) NOT NULL,
`revnr` int(11) NOT NULL,
`revname` varchar(64) NOT NULL,
`signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sign` int(11) NOT NULL,
`extWater` int(11) NOT NULL,
`extRisk` int(11) NOT NULL,
`extSystem` int(11) NOT NULL,
`extHelp` int(11) NOT NULL,
`extProvtryck` int(11) NOT NULL,
`extDoc` int(11) NOT NULL,
`extEgenkontroll` int(11) NOT NULL COMMENT 'exttabell',
`extOther` int(11) NOT NULL,
`extMontorer` int(11) NOT NULL,
PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='arbetsberedning';
CREATE TABLE IF NOT EXISTS `table_3` (
`projid` int(11) NOT NULL,
`docid` int(11) NOT NULL,
`revnr` int(11) NOT NULL,
`revname` varchar(64) NOT NULL,
`adress` varchar(256) NOT NULL,
`pipesMark` tinyint(1) NOT NULL,
`pipesKulvert` tinyint(1) NOT NULL,
`pipesBasement` tinyint(1) NOT NULL,
`pipesVaning` tinyint(1) NOT NULL,
`pipesIngjutna` tinyint(1) NOT NULL,
`ledningTappvatten` tinyint(1) NOT NULL,
`ledningVarmevatten` tinyint(1) NOT NULL,
`ledningHetvatten` tinyint(1) NOT NULL,
`ledningKylaPrim` tinyint(1) NOT NULL,
`ledningKylaSek` tinyint(1) NOT NULL,
`ledningGas` tinyint(1) NOT NULL,
`ledningLuft` tinyint(1) NOT NULL,
`ledningAvlopp` tinyint(1) NOT NULL,
`ledningOther` varchar(512) NOT NULL,
`materialGjutjarn` tinyint(1) NOT NULL,
`materialSteel` tinyint(1) NOT NULL,
`materialKoppar` tinyint(1) NOT NULL,
`materialPlast` tinyint(1) NOT NULL,
`materialRostfritt` tinyint(1) NOT NULL,
`materialOther` varchar(512) NOT NULL,
`omfattningLength` int(11) NOT NULL COMMENT 'meter',
`omfattningDimension` varchar(16) NOT NULL,
`omfattningRitningnr` varchar(128) NOT NULL,
`doneWithPump` tinyint(1) NOT NULL,
`doneWithVattenledning` tinyint(1) NOT NULL,
`doneWithKompressor` tinyint(1) NOT NULL,
`doneWithTathetsprovare` tinyint(1) NOT NULL,
`tryckmedieVatten` tinyint(1) NOT NULL,
`tryckmedieLuft` tinyint(1) NOT NULL,
`tryckmedieOther` varchar(128) NOT NULL,
`manometerDiameter` int(11) NOT NULL COMMENT 'mm',
`manometerGradering` int(11) NOT NULL COMMENT 'kPa',
`manometerReadPressure` int(11) NOT NULL,
`manometerTid` int(11) NOT NULL COMMENT 'sekunder',
`testedOn` datetime NOT NULL,
`testedBy` varchar(128) NOT NULL COMMENT '"id_" + (userid)',
`comments` varchar(1024) NOT NULL,
`commentsBy` varchar(128) NOT NULL COMMENT '"id_" + (userid)',
`signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sign` int(11) NOT NULL,
PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
The fields I need are projid, docid, revnr, revname, signedOn, sign and are present in all current and future tables.