function sum multiple columns in sql - sql

`statistiques` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`pag_id` INT(11) NULL DEFAULT NULL,
`likes` VARCHAR(45) NULL DEFAULT NULL,
`comments` VARCHAR(45) NULL DEFAULT NULL,
`likepost` VARCHAR(45) NULL DEFAULT NULL,
`posts` VARCHAR(45) NULL DEFAULT NULL,
`talk` VARCHAR(45) NULL DEFAULT NULL,
`char` VARCHAR(50) NULL DEFAULT NULL,
`engagement` VARCHAR(50) NULL DEFAULT '0',
`created` DATE NULL DEFAULT NULL,
PRIMARY KEY (`id`),
and i have data in table
i went to do this query for sum chemps in my table
select sum(likes) from statistiques where pag_id in (12,20) and created="2013-12-02"
but i went have multiple sum like
select sum(likes,posts) from statistiques where pag_id in (12,20) and created="2013-12-02"
but he isn't worked
English is not my native language, sorry for any mistakes.

Your syntax is wrong. Try
select sum(likes), sum(posts) from ...

Related

MySQL: All parts of primary key must be NOT NULL; if you need NULL in a key, use UNIQUE instead fivem

can someone fix the whole sql for me because i don't really understand sql
this is for my fivem server
https://www.mediafire.com/file/4hqnihgpxu7gfsd/mdt.sql/file
CREATE TABLE IF NOT EXISTS `mdt_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` VARCHAR(20) NOT NULL,
`information` MEDIUMTEXT DEFAULT NULL,
`tags` TEXT NOT NULL,
`gallery` TEXT NOT NULL,
`jobtype` VARCHAR(25) DEFAULT 'police',
`pfp` TEXT DEFAULT NULL,
`fingerprint` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`cid`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_bulletin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` TEXT NOT NULL,
`desc` TEXT NOT NULL,
`author` varchar(50) NOT NULL,
`time` varchar(20) NOT NULL,
`jobtype` VARCHAR(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`type` varchar(50) DEFAULT NULL,
`details` text DEFAULT NULL,
`tags` text DEFAULT NULL,
`officersinvolved` text DEFAULT NULL,
`civsinvolved` text DEFAULT NULL,
`gallery` text DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_bolos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`plate` varchar(50) DEFAULT NULL,
`owner` varchar(50) DEFAULT NULL,
`individual` varchar(50) DEFAULT NULL,
`detail` text DEFAULT NULL,
`tags` text DEFAULT NULL,
`gallery` text DEFAULT NULL,
`officersinvolved` text DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) NOT NULL DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_convictions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` varchar(50) DEFAULT NULL,
`linkedincident` int(11) NOT NULL DEFAULT 0,
`warrant` varchar(50) DEFAULT NULL,
`guilty` varchar(50) DEFAULT NULL,
`processed` varchar(50) DEFAULT NULL,
`associated` varchar(50) DEFAULT '0',
`charges` text DEFAULT NULL,
`fine` int(11) DEFAULT 0,
`sentence` int(11) DEFAULT 0,
`recfine` int(11) DEFAULT 0,
`recsentence` int(11) DEFAULT 0,
`time` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_incidents` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) NOT NULL DEFAULT '',
`title` varchar(50) NOT NULL DEFAULT '0',
`details` text NOT NULL,
`tags` text NOT NULL,
`officersinvolved` text NOT NULL,
`civsinvolved` text NOT NULL,
`evidence` text NOT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) NOT NULL DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` text NOT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_vehicleinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plate` varchar(50) DEFAULT NULL,
`information` text NOT NULL ,
`stolen` tinyint(1) NOT NULL DEFAULT 0,
`code5` tinyint(1) NOT NULL DEFAULT 0,
`image` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_impound` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicleid` int(11) NOT NULL,
`linkedreport` int(11) NOT NULL,
`fee` int(11) DEFAULT NULL,
`time` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
made changes in mdt_data table and mdt_vehicleinfo table

SQL query for getting subscription dates

I have these tables into which I would like to check user subscription period:
Entities:
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(30) DEFAULT NULL,
`CONTENT` text DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`discount` decimal(8,2) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`grand_total` decimal(8,2) DEFAULT NULL,
`item_discount` decimal(8,2) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`line1` varchar(50) DEFAULT NULL,
`line2` varchar(50) DEFAULT NULL,
`middle_name` varchar(50) DEFAULT NULL,
`mobile` varchar(15) DEFAULT NULL,
`promo` varchar(100) DEFAULT NULL,
`province` varchar(50) DEFAULT NULL,
`session_id` int(11) DEFAULT NULL,
`shipping` decimal(8,2) DEFAULT NULL,
`status` varchar(100) DEFAULT NULL,
`sub_total` decimal(8,2) DEFAULT NULL,
`tax` decimal(8,2) DEFAULT NULL,
`token` varchar(100) DEFAULT NULL,
`total` decimal(8,2) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=latin1
CREATE TABLE `subscription` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(19,2) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`currency` varchar(20) DEFAULT NULL,
`duration` bigint(20) DEFAULT NULL,
`end_at` datetime(6) DEFAULT NULL,
`error` varchar(200) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`product` varchar(20) DEFAULT NULL,
`run_at` datetime(6) DEFAULT NULL,
`start_at` datetime(6) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`title` varchar(20) DEFAULT NULL,
`parent_transaction_id` int(11) DEFAULT NULL,
`parent_transactionId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23443556 DEFAULT CHARSET=latin1
CREATE TABLE `payment_transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(19,2) DEFAULT NULL,
`code` varchar(100) DEFAULT NULL,
`CONTENT` text DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`currency` varchar(20) DEFAULT NULL,
`error` varchar(200) DEFAULT NULL,
`external_id` varchar(255) DEFAULT NULL,
`gateway` varchar(20) DEFAULT NULL,
`mode` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`reconciled_at` datetime(6) DEFAULT NULL,
`reference_transaction_id` int(11) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`type` varchar(20) DEFAULT NULL,
`unique_transactionId` varchar(50) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`unique_transaction_id` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=latin1
I'm new to database table design so I need to ask what table relations relations should I use to get the user subscription period using user id?
Is it a good idea to use Orders table as a main table and make SQL queries with JOIN to get the subscription period using user id?
JSFFiddle: http://sqlfiddle.com/#!9/262df6/1
As per your requirement, you need to get the user subscription period using user id.
You can get user subscription period and other data from SUBSCRIPTION table by joining ORDERS table, as they have order_id you can directly join the two tables by querying for user_id in where condition.
select subs.* from subscription subs
left join orders odr on odr.id = subs.order_id
where odr.user_id = 221 -- pass the required user id
specify the required columns and query for the userid in where condition
verified with JSFiddle you shared by adding a record in subscription table : http://sqlfiddle.com/#!9/b834fc/2
The main thing to consider here is how you'll normally be wanting to access these tables in the future. Based on what you've posted, I'm going to assume there are users (I don't see a users table), and that they place orders and also have subscriptions.
The thing these share in common - the relation between the two - is the user. Having the user be the focal point also creates a 1-to-many relationship, as opposed to a many-to-many relationship, which is much easier to manage. So, rather than having orders tied to subscriptions, which could get confusing, I would recommend having them both tied to the user. What if a single user has multiple orders, and multiple subscriptions? Which order would you relate to which subscription?
If you have a users table, then I would structure it so each user gets their own unique primary key. Let's call it user_id. Then, have the user_id in both the subscriptions (relating the subscriptions back to the user) and also the orders (relating orders back to the user).
With the user_id relating the two, you could relate the orders and subscriptions easily.

problem in getting count

Here is my table complete schema , i want to get the each category sale count ,
How to get that each category sale count ,
CREATE TABLE IF NOT EXISTS `tblbasket` (
`BID` int(20) NOT NULL
AUTO_INCREMENT,
`BasketSessionID` varchar(100) NOT NULL DEFAULT '0',
`ProductCode` varchar(50) NOT NULL,
`Quantity` int(20) NOT NULL,
`AfterDiscount` double NOT NULL DEFAULT '0',
`ProductCost` double NOT NULL,
`SaleMode` varchar(10) NOT NULL DEFAULT 'p' COMMENT 'p Price f Free',
`BillType` varchar(5) NOT NULL DEFAULT 's' COMMENT 's sale r Returns',
`EntryUser` int(20) NOT NULL,
`EntryTimestamp` int(50) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdatedUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(3) NOT NULL DEFAULT '1',
PRIMARY KEY (`BID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `tblcategory` (
`CatID` int(11) NOT NULL AUTO_INCREMENT,
`CatName` varchar(20) NOT NULL,
`Discount` int(11) NOT NULL,
`EntryUser` int(10) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdateUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(3) NOT NULL DEFAULT '1',
PRIMARY KEY (`CatID`),
KEY `CatName` (`CatName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
CREATE TABLE IF NOT EXISTS `tblsale` ( `SaleID` int(20) NOT NULL AUTO_INCREMENT,
`BillNo` varchar(30) NOT NULL DEFAULT '0',
`BasketSessionID` varchar(200) NOT NULL,
`AfterDiscount` double NOT NULL,
`ProductCost` double NOT NULL, `VAT` int(20) NOT NULL DEFAULT '0',
`BillType` varchar(5) NOT NULL DEFAULT 's' COMMENT 'same like tblbasket table',
`EntryUser` int(20) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdatedUser` int(20) NOT NULL,
`UpdateDate` datetime NOT NULL,
`OfficeNotesTS` text NOT NULL,
`Status` int(5) NOT NULL DEFAULT '1',
PRIMARY KEY (`SaleID`),
UNIQUE KEY `BillNo` (`BillNo`),
UNIQUE KEY `BasketSessionID` (`BasketSessionID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `tblstock` (
`StockID` int(20) NOT NULL AUTO_INCREMENT,
`ProductCode` varchar(50) NOT NULL,
`ManufacturerID` int(20) NOT NULL,
`CategoryID` int(20) NOT NULL,
`ProductTitle` varchar(150) NOT NULL,
`Timestamp` int(50) NOT NULL,
`EntryUser` int(20) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdateUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(3) NOT NULL DEFAULT '1',
PRIMARY KEY (`StockID`),
KEY `ManufacturerID` (`ManufacturerID`),
KEY `CategoryID` (`CategoryID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2175 ;
CREATE TABLE IF NOT EXISTS `tblstockhistory` (
`STID` int(20) NOT NULL AUTO_INCREMENT,
`StockID` int(20) NOT NULL,
`SupplierID` int(20) NOT NULL,
`InvoiceNO` varchar(20) NOT NULL,
`InvoiceDate` datetime NOT NULL,
`InvoiceAmount` float NOT NULL DEFAULT '0',
`Quantity` int(20) NOT NULL,
`Cost` float NOT NULL,
`MRP` float NOT NULL,
`Discount` int(20) NOT NULL DEFAULT '0',
`VAT` float NOT NULL,
`EntryUser` int(10) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdateUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(10) NOT NULL DEFAULT '1',
PRIMARY KEY (`STID`),
KEY `StockID` (`StockID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2190 ;
ALTER TABLE `tblstock`
ADD CONSTRAINT `tblStock_ibfk_1`
FOREIGN KEY (`ManufacturerID`)
REFERENCES `tblmanufacturer` (`ManufacturerID`);
ALTER TABLE `tblstockhistory`
ADD CONSTRAINT `tblStockHistory_ibfk_1`
FOREIGN KEY (`StockID`)
REFERENCES `tblstock` (`StockID`);
try something like that (its hard to understand without foreign key, and little explain ):
SELECT st.CategoryID,SUM(b.ProductCost*b.Quantity) FROM tblsale s
LEFT JOIN tblbasket b ON b.BasketSessionID = s.BasketSessionID
LEFT JOIN tblstock st ON st.ProductCode = b.ProductCode GROUP BY st.CategoryID

How can I optimise this query in MYSQL? What needs to be done

Please scroll down to the "25/08/2010 Update".
I have a query which I have turned into a view. It runs very slowly because (as far as I understand) there are several issues with indexes on the joined tables.
explain select * from view_ed_abc_print
As you can see, I have table "a" using where, temporary and filesort with 4659 rows, which from what I understand means it has to run through 4659 rows for every single row it returns.
I have omitted the query because it is really really long and formats horribly here (it comes from a view so thats causing some issues
Anyone have any ideas?
Thanks for your answers so far. I have created indexes for all the foreign keys and added a primary key to publicationtypes and audittypes tables (oops, that was silly)
Anyway here is the new explain
And this is the where/from part of the query (sorry the formatting get's lost for some reason)
from
(
(
(
(
(
`auau7859_aba`.`ed_abc_publication_audits` `a` use index(pubid)
left join `auau7859_aba`.`public_publications` `p` on
(
(`p`.`pubid` = `a`.`audit_pubid`)
)
)
left join `auau7859_aba`.`ed_aba_frequencies` on
(
(
`auau7859_aba`.`ed_aba_frequencies`.`frequencyid` = `p`.`pub_frequencyid`
)
)
)
left join `auau7859_aba`.`ed_abc_publicationtypes` on
(
(
`auau7859_aba`.`ed_abc_publicationtypes`.`publicationtypeid` = `p`.`pub_type`
)
)
)
left join `auau7859_aba`.`ed_abc_audittypes` on
(
(`a`.`audit_type` = `auau7859_aba`.`ed_abc_audittypes`.`audittypeid`)
)
)
left join `auau7859_aba`.`Members` `m` on((`m`.`MemID` = `p`.`pub_memid`))
) where ((`a`.`audit_active` = 1) and (`p`.`pub_unfinancial` = 0))
order by `a`.`audit_anps` desc
The query time is much better now, however I am wondering if it is possible to get table "a" to start using that key, instead of "Using filesort" which i've read is really slow.
As requested, here are the definition for the relevant tables.
ed_abc_publication_audits
CREATE TABLE `ed_abc_publication_audits` (
`auditid` INT(11) NOT NULL AUTO_INCREMENT,
`audit_period` INT(11) NULL DEFAULT '0',
`audit_year` INT(11) NULL DEFAULT '0',
`audit_pubid` INT(11) NULL DEFAULT '0',
`audit_frequencyid` INT(11) NULL DEFAULT '0',
`audit_issues` VARCHAR(50) NULL DEFAULT NULL,
`audit_exclusions` TINYINT(4) NULL DEFAULT '0',
`deprecated_dayspublished` INT(11) NULL DEFAULT '0',
`audit_specialpublishingday` VARCHAR(50) NULL DEFAULT NULL,
`audit_bumperissues` TINYINT(4) NULL DEFAULT '0',
`audit_bumperissuedates` TEXT NULL,
`audit_bumperissuelinked` TINYINT(4) NULL DEFAULT '0',
`audit_excludeddates` TEXT NULL,
`audit_coverprice` DECIMAL(10,2) NULL DEFAULT '0.00',
`audit_coverpriceday` VARCHAR(100) NULL DEFAULT '0',
`audit_coverprice2` DECIMAL(10,2) NULL DEFAULT '0.00',
`audit_coverprice2day` VARCHAR(100) NULL DEFAULT '0',
`audit_coverprice3` DECIMAL(10,2) NULL DEFAULT '0.00',
`audit_coverprice3day` VARCHAR(100) NULL DEFAULT '0',
`audit_osmoney` INT(11) NULL DEFAULT '0',
`audit_type` INT(11) NULL DEFAULT '0',
`audit_anps` INT(11) NULL DEFAULT '0',
`audit_inexcess` TINYINT(4) NULL DEFAULT '0',
`audit_periodadjustment` TINYINT(4) NULL DEFAULT '0',
`audit_periodadjustmentvalue` INT(11) NULL DEFAULT '0',
`audit_nznps` INT(11) NULL DEFAULT '0',
`audit_nzinexcess` TINYINT(4) NULL DEFAULT '0',
`audit_othercountries` INT(11) NULL DEFAULT '0',
`audit_ocinexcess` TINYINT(4) NULL DEFAULT '0',
`audit_inclaccomairlinesales` DOUBLE NULL DEFAULT '0',
`audit_incleducationalsales` DOUBLE NULL DEFAULT '0',
`audit_incleventsales` DOUBLE NULL DEFAULT '0',
`audit_inclmultiplepublicationsales` DOUBLE NULL DEFAULT '0',
`audit_bundledsales` DOUBLE NULL DEFAULT NULL,
`audit_exclaustraliaother` INT(11) NULL DEFAULT '0',
`audit_exclinexcess` TINYINT(4) NULL DEFAULT '0',
`audit_nimcopiesprinted` INT(11) NULL DEFAULT '0',
`audit_nimcopiesdelivered` INT(11) NULL DEFAULT '0',
`audit_nimcopiesmailed` INT(11) NULL DEFAULT '0',
`audit_remarks` TEXT NULL,
`audit_coverprice4day` INT(10) NULL DEFAULT NULL,
`audit_coverprice4` DECIMAL(10,2) NULL DEFAULT NULL COMMENT 'CoverPrice4',
`audit_issuesaudited` INT(10) NULL DEFAULT NULL,
`audit_nonpublishingdates` VARCHAR(45) NULL DEFAULT NULL,
`audit_digital_anps` INT(10) NULL DEFAULT NULL,
`audit_digital_inclaccomairlinesales` DOUBLE NULL DEFAULT NULL,
`audit_digital_incleducationalsales` DOUBLE NULL DEFAULT NULL,
`audit_digital_incleventsales` DOUBLE NULL DEFAULT NULL,
`audit_digital_inclmultiplepublicationsales` DOUBLE NULL DEFAULT NULL,
`audit_digital_bundledsalesdigital` DOUBLE NOT NULL,
`deprecated_FirstSignatoryName` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_FirstSignatoryEmail` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_SecondSignatoryName` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_SecondSignatoryEmail` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_FormStatus` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_dateSubmitted` VARCHAR(255) NULL DEFAULT NULL,
`audit_datecreated` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`deprecated_dateSignatoryApproved` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_AuditorName` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_AuditorEmail` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_dateAuditorApproved` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_nPeriod` INT(11) NULL DEFAULT NULL,
`deprecated_nYear` INT(10) NULL DEFAULT NULL,
`audit_totalissuesaudited` INT(10) NULL DEFAULT NULL,
`audit_parentpublication` INT(10) NULL DEFAULT NULL,
`audit_auditenddate` VARCHAR(20) NULL DEFAULT NULL,
`audit_digital_bumperissuedates` TEXT NULL,
`audit_digital_bumperissues` TINYINT(4) NULL DEFAULT NULL,
`audit_digital_bumperissueslinked` TINYINT(4) NULL DEFAULT NULL,
`audit_digital_coverprice2` DECIMAL(10,2) NULL DEFAULT NULL,
`audit_digital_coverprice2day` VARCHAR(25) NULL DEFAULT NULL,
`audit_digital_coverprice3` DECIMAL(10,2) NULL DEFAULT NULL,
`audit_digital_coverprice3day` VARCHAR(25) NULL DEFAULT NULL,
`audit_digital_coverprice4` DECIMAL(10,2) NULL DEFAULT NULL,
`audit_digital_coverprice4day` VARCHAR(25) NULL DEFAULT NULL,
`audit_digital_coverprice` DECIMAL(18,2) NULL DEFAULT NULL,
`audit_digital_coverpriceday` VARCHAR(25) NULL DEFAULT NULL,
`audit_daysbetween` INT(11) NULL DEFAULT NULL,
`audit_digital_excludeddates` TEXT NULL,
`audit_digital_issuesaudited` INT(11) NULL DEFAULT NULL,
`audit_digital_issues` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_lodgementstatus` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_lodgementtype` VARCHAR(255) NULL DEFAULT NULL,
`audit_nimaveragecopiesdelivered` INT(11) NULL DEFAULT NULL,
`audit_nimaveragecopiesmailed` INT(11) NULL DEFAULT NULL,
`audit_nimaveragecopiesprinted` INT(11) NULL DEFAULT NULL,
`audit_digital_nonpublishingdates` VARCHAR(45) NULL DEFAULT NULL,
`Remarks` TEXT NULL,
`audit_digital_remarks` TEXT NULL,
`deprecated_s2complete` INT(11) NULL DEFAULT NULL,
`deprecated_s3complete` INT(11) NULL DEFAULT NULL,
`audit_stageid` INT(11) NULL DEFAULT NULL,
`audit_periodenddate` DATE NULL DEFAULT NULL,
`audit_periodstartdate` DATE NULL DEFAULT NULL,
`audit_dayspublished` SET('mon','tue','wed','thu','fri','sat','sun') NULL DEFAULT NULL,
`audit_active` TINYINT(1) UNSIGNED NULL DEFAULT '1',
`deprecated_auditor_name` VARCHAR(255) NULL DEFAULT NULL COMMENT 'abc_status',
`audit_auditor_id` INT(10) NULL DEFAULT NULL COMMENT 'This is the auditor ID that has been pulled from the abc status table. WARNING: THIS IS from abcUSERS',
`deprecated_auditor_email` VARCHAR(255) NULL DEFAULT NULL COMMENT 'abc_status',
`deprecated_publisher_name` VARCHAR(255) NULL DEFAULT NULL COMMENT 'abc_status',
`audit_author_id` INT(10) NULL DEFAULT NULL COMMENT 'the author id from the abc_status. WARNING: ABCUsers TAble, not scmod',
`deprecated_publisher_email` VARCHAR(255) NULL DEFAULT NULL COMMENT 'abc_status',
PRIMARY KEY (`auditid`),
INDEX `pubid` (`audit_pubid`),
INDEX `audit_type` (`audit_type`),
INDEX `audit_periodenddate` (`audit_periodenddate`),
INDEX `audit_periodstartdate` (`audit_periodstartdate`),
INDEX `audit_anps` (`audit_anps`),
INDEX `orderby` (`audit_pubid`, `audit_anps`)
)
COMMENT='aba_cmt'
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=5483
public_publications table (p)
CREATE TABLE `public_publications` (
`pubid` INT(11) NOT NULL AUTO_INCREMENT,
`pub_memid` DOUBLE NULL DEFAULT NULL,
`pub_ledger_code` VARCHAR(255) NULL DEFAULT NULL,
`pub_title` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_previousname` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_tSummaryTitle` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_bBillPublisher` DOUBLE NULL DEFAULT NULL,
`deprecated_bBillOtherMember` DOUBLE NULL DEFAULT NULL,
`deprecated_BillMemID` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_BBillAdHocAddress` DOUBLE NULL DEFAULT NULL,
`pub_address` VARCHAR(255) NULL DEFAULT NULL,
`pub_suburb` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_state1` VARCHAR(255) NULL DEFAULT NULL,
`pub_mlocation` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_postcode` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_membership_amount` DOUBLE NULL DEFAULT NULL,
`deprecated_receipt_date` VARCHAR(255) NULL DEFAULT NULL,
`pub_abc` DOUBLE NULL DEFAULT NULL,
`pub_unfinancial` TINYINT(1) NULL DEFAULT '0',
`pub_auditmemid` DOUBLE NULL DEFAULT NULL,
`deprecated_auditorid` DOUBLE NULL DEFAULT NULL,
`pub_audittype` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_PubType` DOUBLE NULL DEFAULT NULL,
`pub_classification` INT(11) NULL DEFAULT NULL,
`deprecated_OldPubID` DOUBLE NULL DEFAULT NULL,
`deprecated_OldCompanyID` DOUBLE NULL DEFAULT NULL,
`pub_contactid` DOUBLE NULL DEFAULT NULL,
`pub_auditremarks` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_bGovernment` DOUBLE NULL DEFAULT NULL,
`deprecated_bExGovernment` DOUBLE NULL DEFAULT NULL,
`deprecated_dDateJoined` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_tDaysPublished` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_stateid` VARCHAR(50) NULL DEFAULT NULL,
`deprecated_PublisherCost` DOUBLE NULL DEFAULT NULL,
`pub_magazinecategory` DOUBLE NULL DEFAULT NULL,
`deprecated_dayspublished` DOUBLE NULL DEFAULT NULL,
`pub_frequency` VARCHAR(255) NULL DEFAULT NULL,
`pub_type` INT(11) NULL DEFAULT NULL,
`pub_subtype` VARCHAR(255) NULL DEFAULT NULL,
`deprecated_MYOBExport` DOUBLE NULL DEFAULT NULL,
`deprecated_periodJAN-JUNE` DOUBLE NULL DEFAULT NULL,
`deprecated_periodJUL-DEC` DOUBLE NULL DEFAULT NULL,
`deprecated_periodJAN-MAR` DOUBLE NULL DEFAULT NULL,
`deprecated_periodAPR-JUN` DOUBLE NULL DEFAULT NULL,
`deprecated_periodJUL-SEPT` DOUBLE NULL DEFAULT NULL,
`deprecated_periodOCT-DEC` DOUBLE NULL DEFAULT NULL,
`deprecated_periodApr-Mar` DOUBLE NULL DEFAULT NULL,
`deprecated_periodOct-Sept` DOUBLE NULL DEFAULT NULL,
`deprecated_UnfinancialDate` VARCHAR(255) NULL DEFAULT NULL,
`pub_location` VARCHAR(255) NULL DEFAULT NULL,
`Address` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Address',
`deprecated_AuditorName` VARCHAR(255) NULL DEFAULT NULL COMMENT 'use pub_auditorid instead. ',
`deprecated_AuditorEmail` VARCHAR(255) NULL DEFAULT NULL COMMENT 'use pub_auditorid instead. ',
`pub_auditflag` VARCHAR(45) NULL DEFAULT NULL,
`pub_datemodified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`pub_website` VARCHAR(255) NULL DEFAULT NULL,
`pub_mastheadurl` VARCHAR(255) NULL DEFAULT NULL,
`pub_frequencyid` INT(11) NULL DEFAULT NULL,
`pub_nimparentpubid` INT(11) NULL DEFAULT NULL,
`pub_active` TINYINT(4) NULL DEFAULT NULL,
`remove_datemodified` TIMESTAMP NULL DEFAULT NULL,
`pub_datecreated` TIMESTAMP NULL DEFAULT NULL,
`pub_dayspublished` SET('mon','tue','wed','thu','fri','sat','sun') NULL DEFAULT NULL,
`pub_state` VARCHAR(10) NULL DEFAULT NULL,
`pub_contact_vote` INT(10) NULL DEFAULT NULL,
`pub_contact_auditletter1` INT(10) NULL DEFAULT NULL,
`pub_contact_auditletter2` INT(10) NULL DEFAULT NULL,
`pub_auditorid` INT(10) NULL DEFAULT NULL COMMENT 'Put the user id of the auditor in this field for ABC elodgement. (see sc_module_users user_id, listed in the module > users page in edata backend)',
`pub_next_audit_period` INT(10) NULL DEFAULT NULL,
`pub_next_audit_type` INT(10) NULL DEFAULT NULL,
PRIMARY KEY (`pubid`),
INDEX `Index_2` (`pub_title`),
INDEX `Unfinancial` (`pub_unfinancial`),
INDEX `lPublicationType` (`pub_type`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=20000
Members (m) table
CREATE TABLE `Members` (
`MemID` INT(11) NOT NULL AUTO_INCREMENT,
`ABC` INT(11) NOT NULL,
`Mem_Type_ID` SMALLINT(6) NOT NULL,
`Membership Status` VARCHAR(80) NULL DEFAULT NULL,
`reference` VARCHAR(255) NULL DEFAULT NULL,
`CABReference` VARCHAR(50) NULL DEFAULT NULL,
`Company_Name` VARCHAR(255) NOT NULL,
`Street_Address` VARCHAR(255) NULL DEFAULT NULL,
`Street_Address_Line2` VARCHAR(100) NULL DEFAULT NULL,
`Suburb` VARCHAR(255) NULL DEFAULT NULL,
`State` VARCHAR(255) NULL DEFAULT NULL,
`Post_Code` VARCHAR(255) NULL DEFAULT NULL,
`unfinancial` TINYINT(1) NULL DEFAULT NULL,
`file` VARCHAR(255) NULL DEFAULT NULL,
`Phone` VARCHAR(255) NULL DEFAULT NULL,
`Fax` VARCHAR(255) NULL DEFAULT NULL,
`WebsiteAddress` VARCHAR(70) NULL DEFAULT NULL,
`TempAddress` VARCHAR(255) NULL DEFAULT NULL,
`ABCWebsitePassword` VARCHAR(10) NULL DEFAULT NULL,
`ABCWebsiteUsername` VARCHAR(10) NULL DEFAULT NULL,
`CABWebsiteUsername` VARCHAR(10) NULL DEFAULT NULL,
`CABWebsitePassword` VARCHAR(10) NULL DEFAULT NULL,
`MembershipAmount` DOUBLE NULL DEFAULT NULL,
`MYOBExport` TINYINT(1) NULL DEFAULT NULL,
`MYOBTaxCode` VARCHAR(3) NULL DEFAULT NULL,
`Date_Last_Payment` VARCHAR(40) NULL DEFAULT NULL,
`Date_Admitted` VARCHAR(40) NULL DEFAULT NULL,
`Date_Resigned` VARCHAR(40) NULL DEFAULT NULL,
`masthead_url` VARCHAR(255) NULL DEFAULT NULL,
`masthead_report` TINYINT(1) NULL DEFAULT '0',
`masthead_image` VARCHAR(255) NULL DEFAULT NULL,
`masthead_description` TEXT NULL,
`masthead_title` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`MemID`),
INDEX `MemID` (`MemID`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=12349
As for the other tables, they are fairly irrelevant, although happy to post them if you think they are relevant, just comment on it.
Thanks. Here is the latest explain query result:
And a quick summary
Added indexes to All foreign keys and added primary keys to publicationtypes and audittypes tables (these are just lookup tables)
Added an index on the sort for public_publications and publications_audit table
these have greatly decreased the query time, but it still runs in about 0.4 seconds, and there's only a total of around 4300 rows.
Any advice you have would be greatly appreciated!
25/08/2010 Update
After a bit more debugging, I realise that if I remove all the guff from my select clause that the query runs quite fast. Now I am wondering if there is something within my select clause that is making it run so slow.
Can anyone point out some slowness to do with this select query:
`p`.`pub_memid` AS `pub_memid`,
`a`.`audit_inexcess` AS `audit_inexcess`,
`a`.`auditid` AS `auditid`,
`a`.`audit_period` AS `audit_period`,
year(`a`.`audit_periodenddate`) AS `audit_year`,
`p`.`pub_unfinancial` AS `pub_unfinancial`,
`p`.`pub_title` AS `pub_title`,
concat(`p`.`pub_title`,_latin1'{%}',`p`.`pubid`) AS `pub_title#display`,
`p`.`pubid` AS `pubid`,
`a`.`audit_periodstartdate` AS `audit_periodstartdate`,
date_format(`a`.`audit_periodstartdate`,_latin1'%b %y') AS `audit_periodstartdate#display`,
`a`.`audit_periodenddate` AS `audit_periodenddate`,
date_format(`a`.`audit_periodenddate`,_latin1'%b %y') AS `audit_periodenddate#display`,
`a`.`audit_type` AS `audit_type`,
`auau7859_aba`.`ed_abc_audittypes`.`audittype_title` AS `audittype_title`,
`p`.`pub_state` AS `pub_state`,
`p`.`pub_location` AS `pub_location`,
`p`.`pub_dayspublished` AS `pub_dayspublished`,
`auau7859_aba`.`ed_aba_frequencies`.`frequency_title` AS `frequency_title`,
`a`.`audit_issues` AS `audit_issues`,
`a`.`audit_issuesaudited` AS `audit_issuesaudited`,
if(((`a`.`audit_exclusions` <> 0) or (`a`.`audit_bumperissues` <> 0)),concat(_utf8'*',`a`.`audit_issuesaudited`),`a`.`audit_issuesaudited`) AS `audit_issuesaudited#display`,
`a`.`audit_coverprice` AS `audit_coverprice`,
concat(_utf8'$',`a`.`audit_coverprice`) AS `audit_coverprice#display`,
`a`.`audit_coverpriceday` AS `audit_coverpriceday`,
date_format(`a`.`audit_periodenddate`,_latin1'%b %y') AS `period_ending`,
`a`.`audit_remarks` AS `audit_remarks`,
`p`.`pub_type` AS `pub_type`,
`auau7859_aba`.`ed_abc_publicationtypes`.`publicationtype_title` AS `publicationtype_title`,
`a`.`audit_anps` AS `audit_anps`,
if(`a`.`audit_inexcess`,concat(_utf8'+',format(`a`.`audit_anps`,0)),format(`a`.`audit_anps`,0)) AS `audit_anps#display`,
if((`a`.`audit_inclaccomairlinesales` > 0),concat(format((`a`.`audit_inclaccomairlinesales` * 100),2),_utf8'%'),_utf8'') AS `audit_inclaccomairlinesales#display`,
if((`a`.`audit_incleducationalsales` > 0),concat(format((`a`.`audit_incleducationalsales` * 100),2),_utf8'%'),_utf8'') AS `audit_incleducationalsales#display`,
if((`a`.`audit_incleventsales` > 0),concat(format((`a`.`audit_incleventsales` * 100),2),_utf8'%'),_utf8'') AS `audit_incleventsales#display`,
if((`a`.`audit_inclmultiplepublicationsales` > 0),concat(format((`a`.`audit_inclmultiplepublicationsales` * 100),2),_utf8'%'),_utf8'') AS `audit_inclmultiplepublicationsales#display`,
if((`a`.`audit_bundledsales` > 0),concat(format((`a`.`audit_bundledsales` * 100),2),_utf8'%'),_utf8'') AS `audit_bundledsales#display`,
if((`a`.`audit_nznps` > 0),format(`a`.`audit_nznps`,0),_utf8'') AS `audit_nznps#display`,
if((`a`.`audit_othercountries` > 0),format(`a`.`audit_othercountries`,0),_utf8'') AS `audit_othercountries#display`,
if((`a`.`audit_exclaustraliaother` > 0),format(`a`.`audit_exclaustraliaother`,0),_utf8'') AS `audit_exclaustraliaother#display`,
`a`.`audit_inclaccomairlinesales` AS `audit_inclaccomairlinesales`,
`a`.`audit_incleducationalsales` AS `audit_incleducationalsales`,
`a`.`audit_incleventsales` AS `audit_incleventsales`,
`a`.`audit_inclmultiplepublicationsales` AS `audit_inclmultiplepublicationsales`,
`a`.`audit_bundledsales` AS `audit_bundledsales`,
`a`.`audit_nznps` AS `audit_nznps`,
`a`.`audit_othercountries` AS `audit_othercountries`,
`a`.`audit_exclaustraliaother` AS `audit_exclaustraliaother`,
`p`.`pub_mastheadurl` AS `pub_mastheadurl`,
`a`.`auditid` AS `audit_mastheadlink`,
concat(ifnull(`a`.`audit_bumperissuedates`,_latin1''),_latin1'{%}',ifnull(`a`.`audit_excludeddates`,_latin1''),_latin1'{%}',ifnull(`a`.`audit_remarks`,_latin1'')) AS `audit_remarks#display`,
if((ifnull(`a`.`audit_specialpublishingday`,_latin1'') <> _latin1''),concat(ifnull(`p`.`pub_dayspublished`,_latin1''),_latin1'{%}',ifnull(`a`.`audit_specialpublishingday`,_latin1'')),`p`.`pub_dayspublished`) AS `pub_dayspublished#display`,
`a`.`audit_parentpublication` AS `audit_parentpublication`,
if((ifnull(`p`.`pub_nimparentpubid`,0) > 0),(select `nimpub`.`pub_title` AS `pub_title` from `auau7859_aba`.`public_publications` `nimpub` where (`nimpub`.`pubid` = `p`.`pub_nimparentpubid`)),_latin1'') AS `audit_parentpublication#display`,
`a`.`audit_digital_anps` AS `audit_digital_anps`,
if((`a`.`audit_digital_anps` > 0),format(`a`.`audit_digital_anps`,0),_utf8'') AS `audit_digital_anps#display`,
`a`.`audit_digital_bumperissuedates` AS `audit_digital_bumperissuedates`,
`a`.`audit_digital_bumperissues` AS `audit_digital_bumperissues`,
`a`.`audit_digital_bumperissueslinked` AS `audit_digital_bumperissueslinked`,
`a`.`audit_digital_coverprice` AS `audit_digital_coverprice`,
concat(_utf8'$',`a`.`audit_digital_coverprice`) AS `audit_digital_coverprice#display`,
`a`.`audit_digital_coverprice2` AS `audit_digital_coverprice2`,
`a`.`audit_digital_coverprice2day` AS `audit_digital_coverprice2day`,
`a`.`audit_digital_coverprice3` AS `audit_digital_coverprice3`,
`a`.`audit_digital_coverprice3day` AS `audit_digital_coverprice3day`,
`a`.`audit_digital_coverprice4` AS `audit_digital_coverprice4`,
`a`.`audit_digital_coverprice4day` AS `audit_digital_coverprice4day`,
`a`.`audit_digital_coverpriceday` AS `audit_digital_coverpriceday`,
`a`.`audit_digital_excludeddates` AS `audit_digital_excludeddates`,
`a`.`audit_digital_inclaccomairlinesales` AS `audit_digital_inclaccomairlinesales`,
if((`a`.`audit_digital_inclaccomairlinesales` > 0),concat(format(`a`.`audit_digital_inclaccomairlinesales`,2),_utf8'%'),_utf8'') AS `audit_digital_inclaccomairlinesales#display`,
`a`.`audit_digital_incleducationalsales` AS `audit_digital_incleducationalsales`,
if((`a`.`audit_digital_incleducationalsales` > 0),concat(format(`a`.`audit_digital_incleducationalsales`,2),_utf8'%'),_utf8'') AS `audit_digital_incleducationalsales#display`,
`a`.`audit_digital_incleventsales` AS `audit_digital_incleventsales`,
if((`a`.`audit_digital_incleventsales` > 0),concat(format(`a`.`audit_digital_incleventsales`,2),_utf8'%'),_utf8'') AS `audit_digital_incleventsales#display`,
`a`.`audit_digital_inclmultiplepublicationsales` AS `audit_digital_inclmultiplepublicationsales`,
if((`a`.`audit_digital_inclmultiplepublicationsales` > 0),concat(format(`a`.`audit_digital_inclmultiplepublicationsales`,0),_utf8'%'),_utf8'') AS `audit_digital_inclmultiplepublicationsales#display`,
`a`.`audit_digital_bundledsalesdigital` AS `audit_digital_bundledsalesdigital`,
if((`a`.`audit_digital_bundledsalesdigital` > 0),concat(format(`a`.`audit_digital_bundledsalesdigital`,2),_utf8'%'),_utf8'') AS `audit_digital_bundledsalesdigital#display`,
`a`.`audit_digital_issues` AS `audit_digital_issues`,
`a`.`audit_digital_issuesaudited` AS `audit_digital_issuesaudited`,
if(((ifnull(`a`.`audit_digital_excludeddates`,_latin1'') <> _latin1'') or (`a`.`audit_digital_bumperissues` <> 0)),concat(_utf8'*',`a`.`audit_digital_issuesaudited`),`a`.`audit_digital_issuesaudited`) AS `audit_digital_issuesaudited#display`,
`a`.`audit_digital_nonpublishingdates` AS `audit_digital_nonpublishingdates`,
`a`.`audit_digital_remarks` AS `audit_digital_remarks`,
concat(ifnull(`a`.`audit_digital_bumperissuedates`,_latin1''),_latin1'{%}',ifnull(`a`.`audit_digital_excludeddates`,_latin1''),_latin1'{%}',ifnull(`a`.`audit_digital_remarks`,_latin1'')) AS `audit_digital_remarks#display`,
`m`.`Company_Name` AS `publisher`,
`auau7859_aba`.`ed_abc_publicationtypes`.`publicationtype_abbreviation` AS `publicationtype_abbreviation`
More of a detailed question...
From what I'm reading in the query, you are doing all left joins ... thus indicating all records from the left side table, regardless of there being a match in the right-side table based on its join. In this case, the inner most from...
ed_abc_publication_audits left-joined to public_publications
yet your where clause is qualifying a value on BOTH sides via
(a.audit_active = 1) and (p.pub_unfinancial = 0)
To me, that would imply an INNER JOIN where the record MUST exist on each side...
You then do LEFT JOINs to auau7859_aba.ed_aba_frequencies, ed_abc_publicationtypes, ed_abc_audittypes and Members....
Are you expecting some non-matches at these deeper level joins and thus expecting NULL values, or is what you want is where they ALL ultimately join together at all levels.
HOWEVER, it looks like the left joins to the other tables are more of a lookup/reference table where the record should ALWAYS exist.. Such as
Each publication audit will ALWAYS have a valid "Audit Type"
Each publication will ALWAYS have a "Frequency ID", "Type of Publication" and "Member"
Is this correct in my interpretation??? The lookup tables will ALWAYS have matching record? If so, which ones... if Optional which ones...
Based on your answers, try the following query... The STRAIGHT_JOIN can be powerful if you know your data, relationships and can optimize on your own which is the key table driving the rest of the system...
SELECT STRAIGHT_JOIN
a.*,
p.*
FROM
auau7859_aba.ed_abc_publication_audits a
left join auau7859_aba.ed_abc_audittypes atypes
ON a.audit_type = atypes.audittypeid,
auau7859_aba.public_publications p
left join auau7859_aba.ed_aba_frequencies f
ON p.pub_frequencyid = f.frequencyid
left join auau7859_aba.ed_abc_publicationtypes t
ON p.pub_type = t.publicationtypeid
left join auau7859_aba.Members m
on p.pub_memid = m.MemID
where
a.audit_pubid = p.pubid
and a.Audit_Active = 1
and p.pub_unfinancial = 0
Sorting and fetching that amount of records should not be a problem for MySQL.
As I can see from your print screen not all tables have indexes. Create Index for each field that involved with JOIN or WHERE statements. And overall performance should increase.
If you want to avoid the filesort, put the fields in your ORDER BY in an index you can use.
In your case, a.audit_anps isn't available in an index, so the filesort kicks in. Put an index on (pubid, audit_anps) and it will do what you want.
Be mindful when adding indexes, though; each index will have to be updated on every write to, so simply throwing indices around may result in beautiful EXPLAINs but horrible app-performance. Remember: EXPLAIN only tells you what the optimizer does with your query and a given set of tables and indexes. It can help you achieve good app performance, but a nice looking EXPLAIN is not your real goal.
To get the value for audit_parentpublication#display, it's doing a query on auau7859_aba.public_publications for every row.
Try modifying your main query to outer join auau7859_aba.public_publications instead.

mass replace of values in more tables by a highest value

I have a table with OWN_ID and OWN_Email -
own_id | own_email
-----------------------------------------------
3ace7cf80edd | email#example.com
3acf6af33ff7 | email#example.com
3acda2524e00 | email#example.com
3ad75583c9a7 | spam#example.com
3ad74b018999 | spam#example.com
etc.
the problem is that it should contain only a single ID per Email, also I need to replace all OWN_ID values in another table by highest OWN_ID value of the OWN_Email
sql create:
CREATE TABLE `blahblah.`eventowner` (
`OWN_ID` varchar(12) COLLATE utf8_czech_ci NOT NULL DEFAULT '',
`OWN_Email` varchar(120) COLLATE utf8_czech_ci DEFAULT NULL,
`OwnDateFormat` varchar(16) COLLATE utf8_czech_ci DEFAULT NULL,
`OwnWeekStart` int(11) DEFAULT NULL,
`OwnDayStart` int(11) DEFAULT NULL,
`OwnDayEnd` int(11) DEFAULT NULL,
PRIMARY KEY (`OWN_ID`),
KEY `OwnerEmailIndex` (`OWN_Email`),
KEY `OwnerIndex` (`OWN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
CREATE TABLE `blahblah`.`event` (
`EVN_ID` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNGRP_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNOWN_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNRCR_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`Evn_EditCounter` int(11) DEFAULT NULL,
`Evn_Created` int(11) DEFAULT NULL,
`Evn_Modified` int(11) DEFAULT NULL,
`EvnFolder` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnTitle` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnNote` text COLLATE utf8_czech_ci,
`EvnLocation` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnPriority` int(11) DEFAULT NULL,
`EvnComplete` int(11) DEFAULT NULL,
`EvnColor` varchar(2) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnClass` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnShareType` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnTimeFormat` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnType` varchar(127) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnStatus` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnOrganizer` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnContact` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnURL` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnStartDate` int(11) DEFAULT NULL,
`EvnStartTime` int(11) DEFAULT NULL,
`EvnEndDate` int(11) DEFAULT NULL,
`EvnEndTime` int(11) DEFAULT NULL,
`EvnRID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnUID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnExpire` int(11) DEFAULT NULL,
`EvnSequence` int(11) DEFAULT NULL,
`EvnFlags` int(11) DEFAULT NULL,
KEY `EventGroupClassIndex` (`EVNGRP_ID`,`EvnClass`),
KEY `EventGroupFolderIndex` (`EVN_ID`,`EVNGRP_ID`,`EvnFolder`),
KEY `EventRIDIndex` (`EvnRID`),
KEY `EventUIDIndex` (`EvnUID`),
KEY `EventGroupRCRIndex` (`EVN_ID`,`EVNGRP_ID`,`EVNRCR_ID`),
KEY `EventExpireIndex` (`EvnExpire`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
delete t1 from table t1, table t2
where t1.own_email= t2.own_email
and t1.own_id < t2.own_id
update othertable, table
set othertable.own_id= table.own_id
where othertable.own_email= table.own_email;
Could accomplish this fairly simple using a transaction that nukes the whole table.
BEGIN;
SELECT own_email, max(own_Id) INTO temptable FROM table GROUP BY own_email
TRUNCATE table;
INSERT INTO table SELECT * FROM temptable;
DELETE temptable;
COMMIT;
But, that Isn't the best way -- you can also do it by deleting just the right rows.
DELETE FROM table AS d WHERE EXISTS (
SELECT own_id FROM table AS t
WHERE NOT EXISTS (
SELECT max(own_id) FROM table AS i
GROUP BY own_email
WHERE t.own_email = i.own_email
)
AND d.own_id = t.own_id
AND d.own_email = t.own_email
);
Here we delete all own_ids, that aren't the max(own_id) for an E-mail.
This can be accomplished with NOT IN (understanding the caveat on nulls), or an anti-join on the table being deleted from