n-number of nested inner join - mysql

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.

Related

GROUP BY retrieve from the history table the last event

I have 3 tables and I want to retrieve from the history table the last event, so I wrote this request but the result is not good. or am i mistaken? thank you
CREATE TABLE `equipment` (
`id_equipment` int(10) UNSIGNED NOT NULL,
`reference` varchar(32) DEFAULT NULL COMMENT 'clé AIRO2',
`buying_date` date DEFAULT NULL,
`first_use` date DEFAULT NULL,
`checking_delai` varchar(45) DEFAULT NULL,
`serial_number` varchar(256) DEFAULT NULL,
`lot_number` varchar(256) DEFAULT NULL,
`lapsing_date` date NOT NULL,
`status` varchar(32) NOT NULL,
`inspection` date NOT NULL,
`compteur` int(11) DEFAULT NULL,
`id_model` int(10) UNSIGNED DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`detruit` int(1) DEFAULT NULL,
`date_REEPREUVE` date DEFAULT NULL,
`validite_apragaz` date DEFAULT NULL,
`restituee_AJR` int(11) NOT NULL,
`perdu` int(11) DEFAULT NULL,
`maintenance_catt_o2` date DEFAULT NULL,
`vendu` int(11) DEFAULT NULL,
`colonne_inogen` date DEFAULT NULL COMMENT 'date changement colonne inogen',
`rappel_525KS_dec19` int(1) DEFAULT NULL COMMENT 'rappel_cable_DEVILBISS_525_KS'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='matériel';
CREATE TABLE `equipment_history` (
`id_equipment_history` int(10) UNSIGNED NOT NULL,
`id_user` int(10) UNSIGNED NOT NULL,
`id_equipment` int(10) UNSIGNED DEFAULT NULL,
`id_patient` int(11) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`status_date` datetime DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`tuyau` tinyint(1) DEFAULT NULL,
`cable_alim` tinyint(1) DEFAULT NULL,
`compteur` int(11) DEFAULT NULL,
`verif_alarme` tinyint(1) DEFAULT NULL,
`verif_volume` tinyint(1) DEFAULT NULL,
`verif_pression` tinyint(1) DEFAULT NULL,
`verif_o2` tinyint(1) DEFAULT NULL,
`verif_debit` tinyint(1) DEFAULT NULL,
`remplacer_consommable` tinyint(1) DEFAULT NULL,
`complet` tinyint(1) DEFAULT NULL,
`comment_panne` text,
`ras` tinyint(1) DEFAULT NULL,
`panne` tinyint(1) DEFAULT NULL,
`id_piece_jointe` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='historique de l''utilisation du matériel';
CREATE TABLE `patient_has_equipment` (
`id_patient_has_equipment` int(10) UNSIGNED NOT NULL,
`id_intervention` int(10) UNSIGNED DEFAULT NULL,
`id_equipment` int(10) UNSIGNED NOT NULL,
`id_patient` int(10) UNSIGNED DEFAULT NULL,
`date_attribution` date DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
`version_appli` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
request :
SELECT
equipment.id_equipment,
equipment.reference,
history.updated,
history.`status`
FROM
equipment
LEFT JOIN
(
SELECT
*
FROM
`equipment_history`
GROUP BY
id_equipment
ORDER BY
`equipment_history`.`updated`
DESC
) AS history
ON
history.id_equipment = equipment.id_equipment
WHERE
history.`status` = 3 AND equipment.id_equipment NOT IN(
SELECT
id_equipment
FROM
patient_has_equipment
)
thanks
The group by subquery is the problem. While this is a syntax error in most databases, MySQL - unfortunately - tolerates this syntax (when sql mode ONLY_FULL_GROUP_BY is disabled), but does not do what you think: you actually an arbitary record from the table for each equipment_id (the order by clause has no effect on this behavior).
You need to filter for the last history record rather than aggregate. I think that this should do what you want:
select
e.id_equipment,
e.reference,
eh.updated,
eh.status
from equipment e
inner join equipment_history eh on eh.id_equipment = e.id_equipment
where
eh.status = 3
and eh.updated = (
select max(eh1.updated)
from equipment_history eh1
where eh1.id_equipment = e.id_equipment
)
and not exists (
select 1
from patient_has_equipment phe
where phe.id_equipment = e.id_equipment
)

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 Match…against query performance in InnoDB Mysql 5.6

I need to search records in the table which contains millions of records. I have recently updated Mysql version from 5.1 to 5.6.
I was using like in query which was taking around 15 sec to 30 sec.
Currently I have modified query to use MATCH... AGAINST feature of mysql 5.6 INNODB. My query goes like this.
SELECT JOB.IDJOB, JOB.IDEMPLOYER .....
FROM JOB
WHERE ( ( JOB.ITJOBSTARTTYPE=2 AND JOB.DTPLANNEDEND >= '2015-07-06' )
OR ( JOB.ITJOBSTARTTYPE=1
AND ( ( JOB.ITJOBENDTYPE=2 ) OR ( JOB.DTJOBEND>='2015-07-06') )
)
OR ( JOB.ITJOBSTARTTYPE=3
AND ( (JOB.DTJOBEND >='2015-07-06') OR (JOB.ITJOBENDTYPE=2) )
)
)
AND MATCH(VCJOBTITLE, LVJOBCOMPANYDESCRIPTION, VCCOMPANYNAME,
VCSALARYDESC, VCCITY, VCQUALIFICATIONREQUIRED,VCJOBREFERENCE,
LVJOBKEYWORDS )
AGAINST ('test' IN NATURAL LANGUAGE MODE)
ORDER BY JOB.ITJOBBAND ASC,
JOB.VCRANKING DESC,
JOB.IDJOB DESC,
JOB.FJOBWEIGHT ASC
LIMIT 50;
Which takes 80- 120 seconds (3X slower )
If I remove ORDER BY it will load in 6 to 10 sec.
Is their any setting Or performance tunning we can apply here?
Edit: using show create table JOB;
CREATE TABLE `JOB` (
`idJob` int(11) NOT NULL AUTO_INCREMENT,
`idEmployer` int(10) DEFAULT NULL,
`vcJobTitle` varchar(255) NOT NULL,
`lvJobCompanyDescription` text,
`idBasket` int(10) DEFAULT NULL,
`dtActualgolive` datetime DEFAULT NULL COMMENT 'whenever job status become online',
`boImmediatelygolive` int(11) NOT NULL,
`dtRequestedgolive` date DEFAULT NULL,
`dtActualend` datetime DEFAULT NULL COMMENT 'when job is set to be archieved',
`dtPlannedend` date NOT NULL COMMENT 'when posting a job this is calculated. batch will look at this date. date on which regular jobs will be archievd, or date on which internship jobs will be changed to listing',
`dtRequestedend` date DEFAULT NULL COMMENT 'exact date on which employer wants jobs to be archieved',
`dtApplicationdeadline` date DEFAULT NULL COMMENT 'job will be converted to listing on this date',
`boDurationinweeks` int(11) NOT NULL COMMENT 'whether employer gave duration in weeks or not',
`itDurationweeks` int(2) DEFAULT NULL,
`boIsoncredit` int(11) NOT NULL,
`dtCreditend` date DEFAULT NULL,
`fJobbaseprice` float NOT NULL,
`fJobpriceafteradminitemdiscount` float NOT NULL COMMENT 'price after admin item discount',
`fJobpriceafterpromodiscount` float NOT NULL COMMENT 'price after promo discount',
`vcPromotioncode` varchar(50) DEFAULT NULL,
`vcCompanyname` varchar(100) NOT NULL,
`vcSalarydesc` varchar(255) NOT NULL,
`vcCity` varchar(100) NOT NULL,
`vcCounty` varchar(120) DEFAULT NULL,
`vcQualificationrequired` text,
`boWorkfromhome` int(11) NOT NULL,
`boResidential` int(11) NOT NULL,
`boIndoor` int(11) NOT NULL,
`boOutdoor` int(11) NOT NULL,
`boIndoorandoutdoor` int(11) NOT NULL,
`itJobstarttype` int(11) DEFAULT NULL COMMENT 'date,immidiate,always recruiting',
`dtJobstart` date DEFAULT NULL,
`itJobendtype` int(11) DEFAULT NULL COMMENT 'date,ongoing',
`dtJobend` date DEFAULT NULL,
`dtFeaturedstart` date DEFAULT NULL,
`dtFeaturedend` date DEFAULT NULL,
`itFulltimeparttime` int(11) NOT NULL COMMENT 'fulltime,parttime,both',
`boEveningtime` int(11) NOT NULL,
`boDaytime` int(11) NOT NULL,
`boWeekend` int(11) NOT NULL,
`boNewsletter` int(11) NOT NULL,
`vcApplyemail` varchar(500) DEFAULT NULL,
`vcApplyphone` varchar(200) DEFAULT NULL,
`vcApplyaddress` varchar(100) DEFAULT NULL,
`vcApplyURL` varchar(500) DEFAULT NULL,
`boRequirephone` int(11) DEFAULT NULL,
`boRequireaddress` int(11) DEFAULT NULL,
`boRequirecv` int(11) DEFAULT NULL,
`boFeatured` int(11) NOT NULL,
`blLogo` longblob,
`itJobstatus` int(1) DEFAULT NULL COMMENT 'online,offline',
`itEmailedtofriendcount` int(5) DEFAULT NULL COMMENT 'keeps count of how many times users clicked on link email this to friend',
`itDeadlinetype` int(1) DEFAULT NULL,
`idInternshiptypemaster` int(11) DEFAULT NULL,
`vcLengthofscheme` varchar(100) DEFAULT NULL,
`dtInternshiplistingend` date DEFAULT NULL COMMENT 'date when internship job will archieve',
`itJobband` int(2) DEFAULT NULL,
`boGraduate` int(11) NOT NULL,
`boInternship` int(11) NOT NULL,
`boGaptemp` int(11) NOT NULL,
`boParttimeholiday` int(11) NOT NULL,
`boEntrylevel` int(11) NOT NULL DEFAULT '0',
`boHundredPercentDiscountApplicable` int(11) NOT NULL,
`vcContactdetails` varchar(100) DEFAULT NULL,
`vcJobreference` varchar(100) DEFAULT NULL COMMENT 'for importing totaljobs feeds',
`vcJoburlparam` varchar(100) DEFAULT NULL COMMENT 'for importing totaljobs url params',
`dtPutInCurrentBand` datetime DEFAULT NULL COMMENT 'date when job is online and put in band 3',
`itInitcountycount` int(2) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`lvJobkeywords` text,
`fJobweight` decimal(10,2) DEFAULT '1.00' COMMENT 'Holds the job weight ranging between 0 to 1 (1 being the highest weight)',
`dtReinstatedOn` datetime DEFAULT NULL,
`boIncludeiniframe` int(11) DEFAULT NULL,
`boIsAdvertFeatured` int(11) DEFAULT NULL,
`itincludexml` int(1) DEFAULT '1',
`boAddtojobalert` int(11) DEFAULT '1',
`vcRanking` int(11) DEFAULT '999999',
`itIncludeUkptj` int(11) DEFAULT '1',
PRIMARY KEY (`idJob`),
KEY `JOB_I_1` (`idEmployer`,`itJobstatus`),
KEY `JOB_I_2` (`itJobstatus`,`boGaptemp`,`dtJobstart`),
KEY `JOB_I_3` (`itJobstatus`,`boGraduate`,`dtJobstart`),
KEY `JOB_I_4` (`itJobstatus`,`boInternship`,`dtJobstart`),
KEY `JOB_I_5` (`itJobstatus`,`boParttimeholiday`,`dtJobstart`),
KEY `FI_JOB_idOrder_ORDER_idOrder` (`idBasket`),
KEY `FI_JOB_vcPromotioncode_PROMOTION_vcPromotioncode` (`vcPromotioncode`),
KEY `FI_JOB_idInternshiptype_ITTM_idInternshiptypemaster` (`idInternshiptypemaster`),
KEY `JOB_I_6` (`created_at`),
KEY `boEntrylevel` (`boEntrylevel`),
KEY `itJobband` (`itJobband`),
FULLTEXT KEY `index_ft_search` (`vcJobTitle`,`lvJobCompanyDescription`,`vcCompanyname`,`vcSalarydesc`,`vcCity`,`vcQualificationrequired`,`vcJobreference`,`lvJobkeywords`),
CONSTRAINT `fk_JOB_idEmployer_EMPLOYER_idEmployer` FOREIGN KEY (`idEmployer`) REFERENCES `EMPLOYER` (`idEmployer`),
CONSTRAINT `fk_JOB_idInternshiptype_ITTM_idInternshiptypemaster` FOREIGN KEY (`idInternshiptypemaster`) REFERENCES `INTERNSHIPTYPEMASTER` (`idInternshiptypemaster`),
CONSTRAINT `fk_JOB_idOrder_ORDER_idOrder` FOREIGN KEY (`idBasket`) REFERENCES `BASKET` (`idBasket`),
CONSTRAINT `fk_JOB_vcPromotioncode_PROMOTION_vcPromotioncode` FOREIGN KEY (`vcPromotioncode`) REFERENCES `PROMOTION` (`vcPromotioncode`)
) ENGINE=InnoDB AUTO_INCREMENT=1739324 DEFAULT CHARSET=latin1 |
Edit:
Without sort:
With Sort:

Insert All Columns from one table into another table MYSQL

I am trying to insert multiple columns with their data from one table into another table, without a common column (i would expect a cross product in the result).
I am not sure how that's possible, because each of the two tables have varied number of columns, which rules out UNION on them.
I tried a Create..Select to create a 3rd table with all columns from both the tables but unsure about the syntax (keeps throwing syntax error).
Both Tables' syntax:
CREATE TABLE `t_modul` (
`ID` int(11) NOT NULL,
`Seriennummer` int(11) DEFAULT NULL,
`Bezeichnung` varchar(128) DEFAULT NULL,
`StandortID` int(11) NOT NULL,
`Firmwareversion` varchar(8) DEFAULT NULL,
`Optionen` int(11) DEFAULT NULL,
`Feldstärke` int(11) DEFAULT NULL,
`Platziert` bit(1) DEFAULT NULL,
`Sendeintervall` int(11) DEFAULT NULL,
`Batteriekapazitaet` int(11) DEFAULT NULL,
`Modulzusatzinfo` varchar(255) DEFAULT NULL,
`ModulUserinfo` varchar(128) DEFAULT NULL,
`ReadOutDate` varchar(40) DEFAULT NULL,
`ReduktionsIntervall` int(11) DEFAULT NULL,
`Startdatum` varchar(40) DEFAULT NULL,
`SAMTemperaturEin` double DEFAULT NULL,
`SAMTemperaturAus` double DEFAULT NULL,
`SAMCoolingOff` int(11) DEFAULT NULL,
`SAMCoolingOn` int(11) DEFAULT NULL,
`SAMCoolingOffMax` double DEFAULT NULL,
`SAMCoolingOnMin` double DEFAULT NULL,
`SAMParameterField` int(11) DEFAULT NULL,
`SAMIsAlarmMelder` bit(1) DEFAULT NULL,
`SAMNv27` int(11) DEFAULT NULL,
`Hardwarekonfiguration` int(11) DEFAULT NULL,
`Anwendungskonfiguration` int(11) DEFAULT NULL,
`Logkonfiguration` int(11) DEFAULT NULL,
`Archivsendezeit_0` int(11) DEFAULT NULL,
`Archivsendezeit_1` int(11) DEFAULT NULL,
`IsLogger` bit(1) DEFAULT NULL,
`Datenpunktkennung` varchar(128) DEFAULT NULL,
`TTRFtpARCHPollDoneDate` varchar(40) DEFAULT NULL,
`Repeaternummer` int(11) DEFAULT NULL,
`StammdatenID` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `ID_Standort_idx` (`StandortID`),
CONSTRAINT `ID_StandOrt` FOREIGN KEY (`StandortID`) REFERENCES `t_standorte` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `sensors` (
`sensorID` bigint(11) NOT NULL,
`Number` bigint(20) unsigned NOT NULL,
`ValueIDs` text NOT NULL,
`MeasureTime` datetime NOT NULL,
`StartTime` datetime NOT NULL,
`Configuration` int(10) unsigned NOT NULL,
`LogCycle` int(11) NOT NULL,
`Alarms` text NOT NULL,
`AlarmType` int(11) NOT NULL,
PRIMARY KEY (`GesySenseID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
UNION query:
CREATE TABLE sensors_1
SELECT * FROM `sensors`
UNION
SELECT * FROM `t_modul`;
could someone help me out?
thank you
CREATE TABLE sensors_1
AS (SELECT * FROM `sensors`
UNION
SELECT * FROM `t_modul`);
this answer suggested to me worked:
CREATE TABLE t_cross_result AS
SELECT *
FROM t_modul CROSS JOIN sensors

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;