how to prepare steps for below content? - mysql

CREATE TABLE `t_keyword_stat_ga` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`keyword_id` int(11) DEFAULT NULL,
`targeturl_id` int(11) DEFAULT NULL,
`entrances` int(11) DEFAULT NULL,
`track_date` date DEFAULT NULL,
`traffic_date` date DEFAULT NULL,
`own_domain_id` int(11) DEFAULT NULL,
`medium` varchar(255) DEFAULT NULL,
`pageviews` int(11) DEFAULT NULL,
`exits` int(11) DEFAULT NULL,
`bounces` int(11) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
KEY `id` (`id`),
KEY `traffic_date` (`traffic_date`),
KEY `FK_keywordStatGA_targeturl` (`targeturl_id`,`traffic_date`),
KEY `own_domain_id` (`own_domain_id`,`traffic_date`),
KEY `keyword_id` (`keyword_id`,`own_domain_id`,`traffic_date`)
) ENGINE=InnoDB AUTO_INCREMENT=1707411331 DEFAULT CHARSET=utf8;
CREATE TABLE `t_keyword_conversion_ga` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`own_domain_id` int(11) DEFAULT NULL,
`keyword_id` int(11) DEFAULT NULL,
`traffic_date` date DEFAULT NULL,
`targeturl_id` int(11) DEFAULT NULL,
`entrance` int(11) DEFAULT NULL,
`transactions` int(11) DEFAULT NULL,
`item_revenue` decimal(9,2) DEFAULT NULL,
`goal1completions` int(11) DEFAULT NULL,
`goal2completions` int(11) DEFAULT NULL,
`goal3completions` int(11) DEFAULT NULL,
`goal4completions` int(11) DEFAULT NULL,
`goal5completions` int(11) DEFAULT NULL,
`goal6completions` int(11) DEFAULT NULL,
`goal7completions` int(11) DEFAULT NULL,
`goal8completions` int(11) DEFAULT NULL,
`goal9completions` int(11) DEFAULT NULL,
`goal10completions` int(11) DEFAULT NULL,
`goal1Value` decimal(9,2) DEFAULT NULL,
`goal2Value` decimal(9,2) DEFAULT NULL,
`goal3Value` decimal(9,2) DEFAULT NULL,
`goal4Value` decimal(9,2) DEFAULT NULL,
`goal5Value` decimal(9,2) DEFAULT NULL,
`goal6Value` decimal(9,2) DEFAULT NULL,
`goal7Value` decimal(9,2) DEFAULT NULL,
`goal8Value` decimal(9,2) DEFAULT NULL,
`goal9Value` decimal(9,2) DEFAULT NULL,
`goal10Value` decimal(9,2) DEFAULT NULL,
`medium` varchar(255) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
KEY `id` (`id`),
KEY `keyword_id` (`keyword_id`),
KEY `traffic_date` (`traffic_date`),
KEY `own_domain_id` (`own_domain_id`,`traffic_date`),
KEY `targeturl_id` (`targeturl_id`)
) ENGINE=InnoDB AUTO_INCREMENT=526248221 DEFAULT CHARSET=utf8;
i have a tables like _keyword_Stat_ga and t_keyword_Stat_conversion
I Need to export all data after October 31 2011 from the t_keyword_Stat_ga
and t_keyword_Stat_conversion tables.
Import the backup into a ICE instance (for maximum compression) or an ARCHIVE table.
Need to drop the partitions in DB1 on the above tables through October 2011
We then need a before and after summary of disk usage

Related

How to optimize a select query with multiple left joins in a large database

my query is
select cp_emp_personal_detail.id
,cp_emp_personal_detail.emp_code
,cp_emp_personal_detail.emp_name
,cp_department.department
,GROUP_CONCAT(TIME(devicelogs_3_2018.LogDate) ORDER BY devicelogs_3_2018.LogDate) present
from cp_emp_personal_detail
left join cp_emp_official_detail on cp_emp_official_detail.emp_id = cp_emp_personal_detail.id
left join devicelogs_3_2018 on devicelogs_3_2018.DeviceId = cp_emp_personal_detail.org_id and cp_emp_personal_detail.id AND date(devicelogs_3_2018.LogDate) = CURRENT_DATE()
left join cp_department on cp_emp_official_detail.department = cp_department.id
where cp_emp_personal_detail.org_id = 1
group by cp_emp_personal_detail.id
and database design is:
CREATE TABLE `cp_emp_personal_detail` (
`id` int(11) NOT NULL,
`org_id` int(11) NOT NULL,
`emp_code` varchar(100) DEFAULT NULL,
`emp_name` varchar(100) NOT NULL,
`emp_guardian_name` varchar(100) DEFAULT NULL,
`emp_dob` date NOT NULL,
`emp_email` varchar(100) DEFAULT NULL,
`emp_password` varchar(100) NOT NULL,
`emp_phone` bigint(15) DEFAULT NULL,
`emp_mobile` bigint(15) NOT NULL,
`emp_emergency_no` int(11) DEFAULT NULL,
`emp_address1` longtext NOT NULL,
`emp_address2` longtext,
`emp_city` int(11) NOT NULL,
`emp_pan` varchar(30) NOT NULL,
`emp_country` int(11) NOT NULL,
`emp_state` int(11) NOT NULL,
`emp_aadhar` varchar(30) DEFAULT NULL,
`emp_tax_status` varchar(100) NOT NULL,
`emp_pic` varchar(255) DEFAULT NULL,
`emp_gender` varchar(50) NOT NULL,
`emp_status` varchar(100) NOT NULL DEFAULT 'activate',
`crm_status` varchar(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY LIST (org_id)
(
PARTITION P1 VALUES IN (1) ENGINE=InnoDB,
PARTITION P2 VALUES IN (2) ENGINE=InnoDB
);
CREATE TABLE `devicelogs_3_2018` (
`DeviceLogId` bigint(20) NOT NULL,
`DownloadDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`DeviceId` bigint(20) NOT NULL,
`UserId` varchar(50) NOT NULL,
`LogDate` timestamp NOT NULL DEFAULT '1970-12-31 18:30:01',
`Direction` varchar(255) DEFAULT NULL,
`AttDirection` varchar(255) DEFAULT NULL,
`C1` varchar(255) DEFAULT NULL,
`C2` varchar(255) DEFAULT NULL,
`C3` varchar(255) DEFAULT NULL,
`C4` varchar(255) DEFAULT NULL,
`C5` varchar(255) DEFAULT NULL,
`C6` varchar(255) DEFAULT NULL,
`C7` varchar(255) DEFAULT NULL,
`WorkCode` varchar(255) DEFAULT NULL,
`hrapp_syncstatus` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `cp_department` (
`id` int(11) NOT NULL,
`org_id` int(11) NOT NULL,
`department` varchar(20) NOT NULL,
`depart_abb` varchar(10) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY LIST (org_id)
(
PARTITION P1 VALUES IN (1) ENGINE=InnoDB,
PARTITION P2 VALUES IN (2) ENGINE=InnoDB
);
CREATE TABLE `cp_emp_official_detail` (
`id` int(11) NOT NULL,
`emp_id` int(11) NOT NULL,
`org_id` int(11) NOT NULL,
`joining_date` date DEFAULT NULL,
`confirmation_periode` int(11) DEFAULT NULL,
`job_type` varchar(100) DEFAULT NULL,
`payment_mode` varchar(100) DEFAULT NULL,
`department` int(11) DEFAULT NULL,
`acc_no` int(11) DEFAULT NULL,
`designation` int(11) DEFAULT NULL,
`ess_status` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY LIST (org_id)
(
PARTITION P1 VALUES IN (1) ENGINE=InnoDB,
PARTITION P2 VALUES IN (2) ENGINE=InnoDB
);
can someone explain me how to optimize th query currently it is taking 3 to 4 min to execute with more than 8k records

MySQL performance, should this take hours?

I am running the following query in MySQL Workbench:
UPDATE directorylistings INNER JOIN postcodelatlng ON PostalCode = postcode
set directoryListings.Latitude = postcodelatlng.latitude, directoryListings.Longitude = postcodelatlng.longitude
WHERE PostalCode = postcode;
Table directorylistings has 9000 records, postcodelatlng has nearly 2 million records.
directorylistings has a postcode (zip code) field and I'm looking this up in postcodelatlng to retrieve and update the latitude and longitude fields in directorylistings.
Ater timeout issues I found I should change DBMS connection read time out, I set this to 36000 (10 hours). The query has been running of 5 hours so far.
My question is, is this time reasonable? Should I be indexing something? Could the query be optimised?
I did make a 2 record version of the postcodelatlng and ran the query, 16 rows were correctly changed in 0.04 seconds, so I know the query is at least workable.
I have looked at a number of Q/A's on indexing and optimising, but it's taken me over a day to get this far and if it fails again I would like to know what approach to follow. I can't keep trying things and waiting hours for the failure! I bet this query should really take seconds. I'm using a MacBook Pro with a 2.5GHz i7, 16 GB RAM and ssd drive.
Many Thanks.
cpu is at 99%. I aborted after 12 hours! to run the show create tables:
CREATE TABLE `postcodelatlng` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`postcode` varchar(8) NOT NULL,
`latitude` decimal(18,15) NOT NULL,
`longitude` decimal(18,15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1734490 DEFAULT CHARSET=latin1
CREATE TABLE `directorylistings` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Active` tinyint(1) DEFAULT NULL,
`Tag11` tinyint(1) DEFAULT NULL,
`Minyan` tinyint(1) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`AffiliatedDate` double DEFAULT NULL,
`RegisteredDate` datetime DEFAULT NULL,
`AccountID` varchar(10) DEFAULT NULL,
`ShowListing` tinyint(1) DEFAULT NULL,
`Bloguser` tinyint(1) DEFAULT NULL,
`Trusted` tinyint(1) DEFAULT NULL,
`ClassifiedUser` tinyint(1) DEFAULT NULL,
`HostingID` int(11) DEFAULT NULL,
`EndDate` double DEFAULT NULL,
`EndPromoDate` datetime DEFAULT NULL,
`BaseID` int(11) DEFAULT NULL,
`Banner` varchar(50) DEFAULT NULL,
`PrivateListing` tinyint(1) DEFAULT NULL,
`Reserved1` tinyint(1) DEFAULT NULL,
`Reserved2` tinyint(1) DEFAULT NULL,
`Reserved3` tinyint(1) DEFAULT NULL,
`Reserved4` tinyint(1) DEFAULT NULL,
`Reserved5` tinyint(1) DEFAULT NULL,
`CommunityOrg` tinyint(1) DEFAULT NULL,
`Tag1` tinyint(1) DEFAULT NULL,
`Tag2` tinyint(1) DEFAULT NULL,
`Tag3` tinyint(1) DEFAULT NULL,
`Tag4` tinyint(1) DEFAULT NULL,
`Tag5` tinyint(1) DEFAULT NULL,
`Tag6` tinyint(1) DEFAULT NULL,
`Tag7` tinyint(1) DEFAULT NULL,
`Tag8` tinyint(1) DEFAULT NULL,
`Tag9` tinyint(1) DEFAULT NULL,
`Tag10` tinyint(1) DEFAULT NULL,
`Tag12` tinyint(1) DEFAULT NULL,
`Tag13` tinyint(1) DEFAULT NULL,
`Tag14` tinyint(1) DEFAULT NULL,
`ShomerShabbos` tinyint(1) DEFAULT NULL,
`CategoryID1` int(11) DEFAULT NULL,
`CategoryID2` int(11) DEFAULT NULL,
`CategoryID3` int(11) DEFAULT NULL,
`CategoryID4` int(11) DEFAULT NULL,
`CategoryID5` int(11) DEFAULT NULL,
`CategoryID6` int(11) DEFAULT NULL,
`HiddenCategory` int(11) DEFAULT NULL,
`Company` varchar(100) DEFAULT NULL,
`Slogan` varchar(250) DEFAULT NULL,
`Description` varchar(2000) DEFAULT NULL,
`CharShown` int(11) DEFAULT NULL,
`Address` varchar(100) DEFAULT NULL,
`Unit` varchar(50) DEFAULT NULL,
`Address2` varchar(150) DEFAULT NULL,
`Streetno` varchar(50) DEFAULT NULL,
`Building` varchar(50) DEFAULT NULL,
`Parade` varchar(50) DEFAULT NULL,
`Locality` varchar(50) DEFAULT NULL,
`City` varchar(50) DEFAULT NULL,
`Province` varchar(50) DEFAULT NULL,
`PostalCode` varchar(10) DEFAULT NULL,
`Country` varchar(25) DEFAULT NULL,
`PhoneNumber` varchar(70) DEFAULT NULL,
`Ext` varchar(25) DEFAULT NULL,
`PhoneNumber2` varchar(70) DEFAULT NULL,
`FaxNumber` varchar(30) DEFAULT NULL,
`CellNumber` varchar(30) DEFAULT NULL,
`WebUrl` varchar(100) DEFAULT NULL,
`Email` varchar(75) DEFAULT NULL,
`Password` varchar(30) DEFAULT NULL,
`ContactName` varchar(100) DEFAULT NULL,
`Keywords` varchar(355) DEFAULT NULL,
`Dcount` int(11) DEFAULT NULL,
`LocationID` int(11) DEFAULT NULL,
`Comments` varchar(1500) DEFAULT NULL,
`ApplicantName` varchar(200) DEFAULT NULL,
`ApplicantPhone` varchar(200) DEFAULT NULL,
`ApplicantEmail` varchar(200) DEFAULT NULL,
`Photo1` varchar(250) DEFAULT NULL,
`pRating` int(11) DEFAULT NULL,
`sRating` int(11) DEFAULT NULL,
`vRating` int(11) DEFAULT NULL,
`Private` tinyint(1) NOT NULL DEFAULT '0',
`VeryPrivate` tinyint(1) NOT NULL DEFAULT '0',
`SemiSecure` tinyint(1) NOT NULL DEFAULT '0',
`Secure` tinyint(1) NOT NULL DEFAULT '0',
`Secret` tinyint(1) NOT NULL DEFAULT '0',
`TrustHigh` tinyint(1) NOT NULL DEFAULT '0',
`FL` tinyint(1) NOT NULL DEFAULT '1',
`GJ` tinyint(1) NOT NULL DEFAULT '1',
`Recommended` tinyint(1) NOT NULL DEFAULT '0',
`NonGeo` tinyint(1) NOT NULL DEFAULT '0',
`Edited` datetime DEFAULT NULL,
`Editor` varchar(40) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=27167 DEFAULT CHARSET=utf8

Rails 4 Migrations from MySQL Tables

I have a pre seeded database for Countries/Regions/Cities. Is it possible to generate the migration file automatically for these tables?
CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`country_id` int(11) unsigned NOT NULL,
`region_id` int(11) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`Latitude` float NOT NULL,
`Longitude` float NOT NULL,
`TimeZone` varchar(10) NOT NULL,
`DmaId` smallint(6) DEFAULT NULL,
`County` varchar(25) DEFAULT NULL,
`Code` varchar(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=42965 ;
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`FIPS104` varchar(2) NOT NULL,
`ISO2` varchar(2) NOT NULL,
`ISO3` varchar(3) NOT NULL,
`ISON` varchar(4) NOT NULL,
`Internet` varchar(2) NOT NULL,
`Capital` varchar(25) DEFAULT NULL,
`MapReference` varchar(50) DEFAULT NULL,
`NationalitySingular` varchar(35) DEFAULT NULL,
`NationalityPlural` varchar(35) DEFAULT NULL,
`Currency` varchar(30) DEFAULT NULL,
`CurrencyCode` varchar(3) DEFAULT NULL,
`Population` bigint(20) DEFAULT NULL,
`Title` varchar(50) DEFAULT NULL,
`Comment` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=276 ;
CREATE TABLE IF NOT EXISTS `regions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`country_id` int(11) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`Code` varchar(8) NOT NULL,
`ADM1Code` char(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5400 ;
You can use
rake db:schema:dump

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;

query with multiple joins and indexes stuck on "Copying to tmp table"

I have this query that doesn't run under odd circumstances. It's doing two joins, I have two indexes of the same name on two different tables (but this shouldn't matter). When I remove one of the indexes from either tables, the query runs fine. Something else that makes it run is removing the order by clause, but I definitely need that. When I run show processlist the state is stuck on "Copying to tmp table". The indexes I'm talking about are channelID.
the query
SELECT twitterTweets.*,
COUNT(twitterRetweets.id) AS retweets,
sTwitter.followers,
sTwitter.date
FROM twitterTweets
LEFT JOIN twitterRetweets
ON twitterTweets.id = twitterTweetsID
JOIN sTwitter
ON DATE(twitterTweets.dateCreated) = sTwitter.date
AND twitterTweets.channelID = sTwitter.channelID
WHERE twitterTweets.channelID = 32
AND type = 'tweet'
AND DATE(dateCreated) >= '2013-12-05'
AND DATE(dateCreated) <= '2014-01-05'
GROUP BY twitterTweets.id
ORDER BY dateCreated
explain results
1 SIMPLE sTwitter ref channelID channelID 5 const 1162 Using where; Using temporary; Using filesort
1 SIMPLE twitterTweets ref channelID channelID 5 const 17456 Using where
1 SIMPLE twitterRetweets ref twitterTweetsID twitterTweetsID 5 social.twitterTweets.id 3
create for the three tables
CREATE TABLE `twitterTweets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`type` enum('tweet','reply','direct in','direct out') COLLATE utf8_unicode_ci DEFAULT 'tweet',
`subType` enum('reply beginning','retweet beginning','reply middle','retweet middle') COLLATE utf8_unicode_ci DEFAULT NULL,
`tweetID` bigint(20) DEFAULT NULL,
`tweet` text COLLATE utf8_unicode_ci,
`replies` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`hash` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`retweet` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`media` text COLLATE utf8_unicode_ci,
`tweetUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`expandedUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`replyToStatus` bigint(20) DEFAULT NULL,
`user` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`followers` int(11) DEFAULT NULL,
`following` int(11) DEFAULT NULL,
`updates` int(11) DEFAULT NULL,
`group1` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`campaign` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`segment` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`destination` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`retweets` smallint(6) DEFAULT '0',
`favorites` smallint(6) DEFAULT NULL,
`dateCreated` datetime DEFAULT NULL,
`dateAdded` datetime DEFAULT NULL,
`dateUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=296264 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `twitterRetweets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`twitterTweetsID` int(11) DEFAULT NULL,
`tweetID` bigint(20) DEFAULT NULL,
`screenName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`followers` int(11) DEFAULT NULL,
`following` int(11) DEFAULT NULL,
`updates` int(11) DEFAULT NULL,
`favorites` smallint(6) DEFAULT NULL,
`dateAdded` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `twitterTweetsID` (`twitterTweetsID`)
) ENGINE=MyISAM AUTO_INCREMENT=93821 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `sTwitter` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`following` int(11) DEFAULT '0',
`followers` int(11) DEFAULT '0',
`updates` int(11) DEFAULT '0',
`date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=35615 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
So how do I make this thing run with both indexes in place? Do I have to ignore one of them?