Speed up geolocation query - mysql

i tryng to implements a geolocation query that search over the products (150.000) in a table that have latlng id_product and id_categories, and the query run very slow . This is the query:
$prodottip = \App\Models\Prodotti::join('products2sellers', 'products2sellers.id_prodotto', 'prodotti.product_id')->where('prodotti.id_tipologia', 'PRODUCT')->join('aziende', 'products2sellers.id_seller', 'aziende.Id')->select('prodotti.*', 'aziende.*')->whereRaw('match (prodotti.nome, prodotti.categoria_composta, prodotti.descrizione_lunga) against (? in boolean mode)', [$cosa])->selectRaw('( 6371 * acos( cos( radians(?) ) *
cos( radians( products2sellers.lat ) )
* cos( radians( products2sellers.lng ) - radians(?)
) + sin( radians(?) ) *
sin( radians( products2sellers.lat ) ) )
) AS distance', [$lat, $lng, $lat])->OrderBy('distance', 'ASC');
any suggestion to speed it UP?
This is the dump of the 3 table.
I'm on MariaDB but i have also installed MongoDB.
CREATE TABLE `aziende` (
`Id` int(11) NOT NULL,
`contatto` varchar(100) NOT NULL,
`visualizzazioni` int(11) NOT NULL DEFAULT '0',
`categoriapg` varchar(130) NOT NULL DEFAULT '',
`titolo` varchar(50) NOT NULL DEFAULT '' COMMENT 'verrĂ  utilizzato come nome aziendale',
`sottotitolo` varchar(255) NOT NULL DEFAULT '' COMMENT 'slogan aziendale',
`immagine` varchar(50) NOT NULL,
`logo` varchar(50) NOT NULL,
`IndirizzoGeolocalizzato` varchar(250) NOT NULL DEFAULT '',
`email` varchar(100) NOT NULL DEFAULT '' COMMENT 'email ufficiale',
`telefono` varchar(20) NOT NULL DEFAULT '' COMMENT 'telefono ufficiale',
`sitoweb` varchar(150) NOT NULL DEFAULT '' COMMENT 'sito web ufficiale',
`visualizza` int(1) NOT NULL DEFAULT '2',
`scadenzacontratto` datetime NOT NULL,
`ultimouser` varchar(13) NOT NULL DEFAULT '',
`ragionesociale` varchar(255) NOT NULL DEFAULT '',
`piva` varchar(30) NOT NULL DEFAULT '',
`codfisc` varchar(20) NOT NULL DEFAULT '',
`agente` int(11) NOT NULL,
`Provincia` varchar(50) NOT NULL DEFAULT '0',
`Citta` varchar(250) NOT NULL DEFAULT '0',
`attiva` int(11) NOT NULL,
`id_utente` int(11) NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`entry_by` int(11) NOT NULL DEFAULT '0',
`descrizione_azienda` longtext NOT NULL,
`tag_title` varchar(100) NOT NULL,
`tag_description` varchar(100) NOT NULL,
`keywords` varchar(100) NOT NULL,
`coupon` varchar(20) NOT NULL,
`sconto` int(11) NOT NULL,
`partner` int(11) NOT NULL,
`lat` double NOT NULL,
`long` double NOT NULL,
`regione` varchar(30) NOT NULL,
`nazione` varchar(30) NOT NULL,
`via` varchar(100) NOT NULL,
`cap` varchar(10) NOT NULL,
`condizioni_vendita` longtext NOT NULL,
`condizioni_spedizione` longtext NOT NULL,
`cellulare` varchar(50) NOT NULL,
`id_piano` int(11) NOT NULL,
`citta_fatturazione` varchar(300) NOT NULL,
`via_fatturazione` varchar(300) NOT NULL,
`civico_fatturazione` varchar(20) NOT NULL,
`stato_fatturazione` varchar(30) NOT NULL,
`cap_fatturazione` int(10) NOT NULL,
`provincia_fatturazione` varchar(20) NOT NULL,
`regione_fatturazione` varchar(10) NOT NULL,
`lat_fatturazione` double NOT NULL,
`long_fatturazione` double NOT NULL,
`indirizzo_fatturazione` varchar(300) NOT NULL,
`categoria` int(11) NOT NULL,
`numero_civico_fatturazione` varchar(20) NOT NULL,
`numero_civico` varchar(12) NOT NULL,
`sla1` float NOT NULL,
`sla2` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='aziende promotrici';
-- --------------------------------------------------------
--
-- Struttura della tabella `prodotti`
--
CREATE TABLE `prodotti` (
`id` int(11) NOT NULL,
`id_tipologia` varchar(50) NOT NULL,
`categoria_principale` varchar(110) DEFAULT NULL,
`nome` varchar(50) DEFAULT NULL,
`immagini` varchar(1000) DEFAULT NULL,
`entry_by` int(11) NOT NULL,
`id_produttore` varchar(255) DEFAULT NULL,
`id_unita_misura` int(11) NOT NULL,
`prezzo` float DEFAULT NULL,
`link_statico` varchar(100) DEFAULT NULL,
`attivo` int(11) NOT NULL,
`numero_visualizzazioni` int(11) NOT NULL,
`quantita_magazzino` int(11) DEFAULT NULL,
`backorders` int(11) NOT NULL,
`codice_sku` varchar(10) DEFAULT NULL,
`pagamenti_abilitati` varchar(50) NOT NULL,
`spedizioni_abilitate` varchar(50) NOT NULL,
`tags` text NOT NULL,
`tag_title` text NOT NULL,
`tag_keywords` text NOT NULL,
`tag_description` text NOT NULL,
`id_gruppi_utenti` varchar(10) NOT NULL,
`vetrina_on` int(11) NOT NULL,
`numero_acquisti` int(11) DEFAULT '0',
`thumbnail` varchar(500) NOT NULL,
`quantita_minima` int(11) DEFAULT NULL,
`id_tasse` int(11) NOT NULL,
`descrizione_breve` longtext,
`descrizione_lunga` longtext,
`negozio` varchar(50) DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`prezzo_scontato` float DEFAULT NULL,
`alternativi` varchar(200) NOT NULL,
`immagine_principale` varchar(100) DEFAULT NULL,
`rating` float DEFAULT NULL,
`sottocategoria` varchar(500) DEFAULT NULL,
`cod_abbonamento` int(11) NOT NULL,
`numero_acquisto` int(11) NOT NULL,
`featured` int(11) NOT NULL,
`bicolors` varchar(255) DEFAULT NULL,
`genere` varchar(250) DEFAULT NULL,
`model_id` varchar(110) DEFAULT NULL,
`barcode` int(11) DEFAULT NULL,
`prezzo_acquisto` double DEFAULT NULL,
`univoco` varchar(12) DEFAULT NULL,
`color` varchar(255) DEFAULT NULL,
`season` varchar(255) DEFAULT NULL,
`product_id` int(11) NOT NULL,
`slug` varchar(500) NOT NULL,
`showcase` int(11) NOT NULL,
`optimized` int(11) DEFAULT NULL,
`materiale` varchar(300) NOT NULL,
`peso` float NOT NULL,
`model_qty` int(11) NOT NULL,
`fornitore` varchar(100) NOT NULL,
`madein` varchar(100) NOT NULL,
`id_categoria` int(11) NOT NULL,
`categoria_composta` varchar(250) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Struttura della tabella `products2sellers`
--
CREATE TABLE `products2sellers` (
`id` int(11) NOT NULL,
`id_prodotto` int(11) NOT NULL,
`id_seller` int(11) NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`taglia` varchar(300) DEFAULT NULL,
`colore` varchar(200) DEFAULT NULL,
`model_id` varchar(50) DEFAULT NULL,
`qty` int(11) DEFAULT NULL,
`lat` double NOT NULL,
`lng` double NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Indici per le tabelle scaricate
--
--
-- Indici per le tabelle `aziende`
--
ALTER TABLE `aziende`
ADD PRIMARY KEY (`Id`);
--
-- Indici per le tabelle `prodotti`
--
ALTER TABLE `prodotti`
ADD PRIMARY KEY (`id`);
--
-- Indici per le tabelle `products2sellers`
--
ALTER TABLE `products2sellers`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT per le tabelle scaricate
--
--
-- AUTO_INCREMENT per la tabella `prodotti`
--
ALTER TABLE `prodotti`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=52102;
--
-- AUTO_INCREMENT per la tabella `products2sellers`
--
ALTER TABLE `products2sellers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=52101;COMMIT;
Basically i can structure this as well using eager loading but i suppose that the results is the same. Also i can try to use some geolocation/plugin in MariaDB but i need some suggestion

"Find nearest" has 3 speeds:
Painfully slow due to computing the distance to every item and finding the closest.
Possibly OK -- using a "bounding box" and a suitable index.
Fast -- but the code is complex.
More discussion: http://mysql.rjweb.org/doc.php/find_nearest_in_mysql
The solution must be coded at the MySQL/MariaDB level. Laravel may need to use raw SQL to get at it.
(It won't answer the Question, but do use InnoDB, not MyISAM.)

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

#1060 - Duplicate column name 'ID' Why?

CREATE TABLE IF NOT EXISTS `vehicles` (
`UID` int(4) NOT NULL,
`id` int(11) NOT NULL,
`Kofferraum` varchar(50) NOT NULL DEFAULT '0|0|0|0|',
`Typ` int(11) NOT NULL,
`Tuning` varchar(255) NOT NULL,
`Spawnpos_X` varchar(50) NOT NULL,
`Spawnpos_Y` varchar(50) NOT NULL,
`Spawnpos_Z` varchar(50) NOT NULL,
`Spawnrot_X` varchar(50) NOT NULL,
`Spawnrot_Y` varchar(50) NOT NULL,
`Spawnrot_Z` varchar(50) NOT NULL,
`Farbe` varchar(50) NOT NULL,
`Paintjob` varchar(50) NOT NULL DEFAULT '3',
`Benzin` varchar(50) NOT NULL DEFAULT '100',
`Slot` float NOT NULL,
`Special` int(11) NOT NULL DEFAULT '0',
`Lights` varchar(50) NOT NULL DEFAULT '|255|255|255|',
`Distance` double NOT NULL DEFAULT '0',
`STuning` varchar(50) NOT NULL DEFAULT '0|0|0|0|0|0|',
`AuktionsID` int(10) NOT NULL DEFAULT '0',
`GangVehicle` tinyint(1) NOT NULL DEFAULT '0',
`rc` int(1) NOT NULL DEFAULT '0',
`spezcolor` varchar(50) NOT NULL DEFAULT '|0|0|0|0|0|0|',
`Sportmotor` int(1) NOT NULL DEFAULT '0',
`Bremse` varchar(1) NOT NULL DEFAULT '0',
`Antrieb` varchar(10) NOT NULL,
`plate` text NOT NULL,
`ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Because you're using 'id' twice. Either remove the duplicate or change the name of this to something else:
ID int(11) NOT NULL
You have the column id twice. Once at the beginning an one at the end.
CREATE TABLE IF NOT EXISTS `vehicles` (
`UID` int(4) NOT NULL,
`id` int(11) NOT NULL,
...
`ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
...
remove one of them

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;

Mysql query failed after adding category table

When i run this query i'm keeping getting this error Database query failed 1054 - Unknown column 'cv.employeeIDFK' in 'on clause'
This only happen when I add the category in my query
FROM opjb_cv AS cv , opjb_cvCategory AS cv_cat
AND cv_cat.categoryIDFK IN ( 1,2,3,4,5,11,22,24,26,28 )
AND cv_cat.cvIDFK = cv.id
This is my query which is failing as you can see i have added all information but still its failing i cant seem to find anything wrong with this.
SELECT DISTINCT cv.id, cv.targetJobTitle, cv.targetJobTitleAlt, cv.recentEmployer, employee.firstName,
employee.surname, cv.recentJobTitle, cv.modifyAt, cv.city, cv.countryCountyFK, cv.countryStatesFK, cv.countryISO2FK, cv.experienceIDFK,
cv.careerIDFK, cv.areYouAuth, country.countryName, cv.employeeIDFK as user_id ,
match ( cv.title, cv.recentJobTitle, cv.targetJobTitle, cv.targetJobTitleAlt ) AGAINST ('desktop' IN BOOLEAN MODE) AS relevance
FROM opjb_cv AS cv , opjb_cvCategory AS cv_cat
JOIN opjb_employee AS employee ON cv.employeeIDFK = employee.id
JOIN opjb_country AS country ON cv.countryISO2FK=country.iso2
JOIN opjb_experience AS experience ON cv.experienceIDFK = experience.id
JOIN opjb_type AS type ON cv.jobTypeIDFK = type.id
JOIN opjb_education AS education ON cv.educationIDFK = education.id
JOIN opjb_countryStates as countryStates ON cv.countryStatesFK = countryStates.code
WHERE cv.showTo=1
AND cv.status=1
AND cv.countryISO2FK='GB'
AND match ( cv.title, cv.recentJobTitle, cv.targetJobTitle, cv.targetJobTitleAlt ) AGAINST ('desktop' IN BOOLEAN MODE )
AND cv_cat.categoryIDFK IN ( 1,2,3,4,5,11,22,24,26,28 )
AND cv_cat.cvIDFK = cv.id
AND experience.id=5
AND type.id=1
AND education.id=7
AND cv.modifyAt > NOW() - INTERVAL 3 DAY AND ( cv.salaryMin <= 48000 OR cv.salaryMax <= 48000 )
AND cv.countryStatesFK ='EG'
ORDER BY relevance DESC
These are all the tables which is involde in this query.
CREATE TABLE IF NOT EXISTS `opjb_country` (
`iso2` char(2) NOT NULL,
`iso3` char(3) NOT NULL,
`isoNo` smallint(3) NOT NULL,
`countryName` varchar(100) NOT NULL,
`regionIDFK` int(11) NOT NULL,
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`iso2`),
KEY `regionIDFK` (`regionIDFK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_countryStates`
--
CREATE TABLE IF NOT EXISTS `opjb_countryStates` (
`code` varchar(40) NOT NULL default '',
`name` varchar(100) default NULL,
`countryISO2FK` char(2) NOT NULL default 'US',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`code`,`countryISO2FK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_cv`
--
CREATE TABLE IF NOT EXISTS `opjb_cv` (
`id` int(11) NOT NULL auto_increment,
`type` tinyint(1) NOT NULL default '0',
`fileName` varchar(100) NOT NULL,
`fileType` varchar(15) NOT NULL,
`fileExe` varchar(5) NOT NULL,
`fileSize` int(11) NOT NULL default '0',
`filePath` varchar(255) NOT NULL,
`originalName` varchar(100) NOT NULL,
`fileHash` varchar(40) NOT NULL,
`title` varchar(30) NOT NULL,
`description` varchar(255) NOT NULL,
`showTo` tinyint(1) NOT NULL default '0',
`defaultCV` tinyint(1) NOT NULL default '0',
`targetJobTitle` varchar(100) NOT NULL,
`targetJobTitleAlt` varchar(100) NOT NULL,
`educationIDFK` int(6) NOT NULL default '0',
`careerIDFK` int(6) NOT NULL default '0',
`city` varchar(100) NOT NULL,
`areYouAuth` varchar(100) NOT NULL,
`recentJobTitle` varchar(100) NOT NULL,
`recentEmployer` varchar(100) NOT NULL,
`recentIndustry` varchar(100) NOT NULL,
`recentCareer` int(6) NOT NULL default '0',
`recentStartDate` date NOT NULL,
`recentEndDate` varchar(50) NOT NULL,
`jobTypeIDFK` int(6) NOT NULL default '0',
`jobStatusIDFK` int(6) NOT NULL default '0',
`salaryMin` varchar(20) NOT NULL default '0',
`salaryMax` varchar(20) NOT NULL default '0',
`salaryCurrency` varchar(5) NOT NULL default 'GBP',
`salaryType` tinyint(2) NOT NULL default '5',
`relocate` tinyint(1) NOT NULL default '0',
`willing_to_travel` tinyint(2) NOT NULL default '0',
`availability` tinyint(1) NOT NULL default '0',
`startDate` varchar(30) NOT NULL,
`positions` varchar(100) NOT NULL,
`userComments` text,
`noViews` int(7) NOT NULL default '0',
`status` tinyint(1) NOT NULL default '0',
`adminComments` text,
`employeeIDFK` int(11) NOT NULL default '0',
`countryISO2FK` char(2) NOT NULL default 'US',
`countryStatesFK` varchar(100) NOT NULL,
`countryCountyFK` varchar(100) NOT NULL,
`experienceIDFK` int(6) NOT NULL default '0',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `employeeIDFK` (`employeeIDFK`),
KEY `countryISO2FK` (`countryISO2FK`),
FULLTEXT KEY `searchCV` (`title`,`targetJobTitle`,`targetJobTitleAlt`,`recentJobTitle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_cvCategory`
--
CREATE TABLE IF NOT EXISTS `opjb_cvCategory` (
`cvIDFK` int(11) NOT NULL default '0',
`categoryIDFK` int(11) NOT NULL default '0',
PRIMARY KEY (`cvIDFK`,`categoryIDFK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_education`
--
CREATE TABLE IF NOT EXISTS `opjb_education` (
`id` int(6) NOT NULL auto_increment,
`educationName` varchar(100) NOT NULL,
`lang` varchar(50) NOT NULL default 'english',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_employee`
--
CREATE TABLE IF NOT EXISTS `opjb_employee` (
`id` int(11) NOT NULL auto_increment,
`emailAddress` varchar(100) NOT NULL,
`username` varchar(30) NOT NULL,
`passwd` varchar(40) NOT NULL,
`title` varchar(20) NOT NULL,
`firstName` varchar(100) NOT NULL,
`middleName` varchar(50) NOT NULL,
`surname` varchar(100) NOT NULL,
`address` varchar(150) NOT NULL,
`address2` varchar(100) NOT NULL,
`city` varchar(100) NOT NULL,
`countryCountyFK` varchar(100) NOT NULL,
`countryStatesFK` varchar(100) NOT NULL,
`countryISO2FK` char(2) NOT NULL default 'US',
`postCode` varchar(20) NOT NULL,
`careerStatus` tinyint(1) NOT NULL default '0',
`contPref` tinyint(1) NOT NULL default '0',
`webSite` varchar(100) NOT NULL,
`job_title` varchar(255) NOT NULL,
`recent_employer` varchar(255) NOT NULL,
`mobile_no` varchar(30) NOT NULL,
`home_no` varchar(30) NOT NULL,
`categoryIDFK` int(6) default NULL,
`careerDegreeIDFK` int(6) default NULL,
`educationIDFK` int(6) default NULL,
`experienceIDFK` int(6) default NULL,
`pers_statement` text,
`actKey` varchar(100) NOT NULL,
`comments` varchar(255) NOT NULL,
`status` tinyint(1) NOT NULL default '0',
`isActive` tinyint(1) NOT NULL default '0',
`lastVisit` datetime NOT NULL,
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
`createip` varchar(20) NOT NULL default '0',
`loginip` varchar(20) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `emailAddress` (`emailAddress`),
UNIQUE KEY `username` (`username`),
KEY `city` (`city`,`countryCountyFK`),
KEY `countryISO2FK` (`countryISO2FK`),
KEY `idx_fullname` (`firstName`,`surname`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_experience`
--
CREATE TABLE IF NOT EXISTS `opjb_experience` (
`id` int(6) NOT NULL auto_increment,
`experienceName` varchar(100) NOT NULL,
`lang` varchar(50) NOT NULL default 'english',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_type`
--
CREATE TABLE IF NOT EXISTS `opjb_type` (
`id` int(6) NOT NULL auto_increment,
`typeName` varchar(100) NOT NULL,
`lang` varchar(50) NOT NULL default 'english',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;