我有一个查询,我已经变成了一个视图。它运行得非常慢,因为(据我所知),连接表上的索引有几个问题。
explain select * from view_ed_abc_print
正如你所看到的,我有表“a”使用where,temporary和filesort有4659行,据我所知,这意味着它必须为它返回的每一行运行4659行。
我省略了查询,因为它真的非常长并且在这里格式可怕(它来自一个视图,因此导致一些问题
有人有什么想法吗?
嗨,大家好,
到目前为止,感谢您的回答。我已经为所有外键创建了索引,并为publicationtypes和audittypes表添加了一个主键(oops,这很愚蠢)
无论如何这里是新的解释
这是查询的部分/部分(遗憾的是格式化因某些原因而丢失)
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
现在查询时间要好得多,但是我想知道是否有可能让表“a”开始使用该键,而不是“我正在阅读的”使用filesort“非常慢。
根据要求,以下是相关表格的定义。 的 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
会员(m)表
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
至于其他表格,它们相当无关紧要,虽然如果您认为它们是相关的,但很乐意发布它们,只需对它进行评论。
谢谢大家,这是最新的解释查询结果
快速总结一下 1.为所有外键添加索引,并将主键添加到publicationtypes和audittypes表(这些只是查找表) 2.为public_publications和publications_audit表
添加了索引这些大大缩短了查询时间,但它仍然在大约0.4秒内运行,并且总共只有大约4300行。
非常感谢您的任何建议!
25/08/2010更新
Hey Guys,经过一些调试后,我意识到如果我从select子句中删除所有guff,那么查询运行得非常快。现在我想知道我的select子句中是否有某些东西使它运行得如此之慢。
任何人都可以指出这个选择查询有些缓慢:
`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`
答案 0 :(得分:1)
更详细的问题...
从我在查询中读到的内容,您正在执行所有左连接...因此指示左侧表中的所有记录,而不管右侧表中是否存在基于其连接的匹配。在这种情况下,最内层来自......
ed_abc_publication_audits left-joined to public_publications
然而你的where子句通过
在两个方面对一个值进行限定(a.audit_active = 1) and (p.pub_unfinancial = 0)
对我而言,这意味着INNER JOIN必须在每一方都有记录......
然后你左下角加入auau7859_aba.ed_aba_frequencies,ed_abc_publicationtypes,ed_abc_audittypes和Members ....
您是否期望在这些更深层次的联接中出现一些不匹配,从而期望出现NULL值,或者您想要的是它们最终在所有级别上连接在一起的位置。
但是,看起来左边连接到其他表更像是一个查找/引用表,其中记录应始终存在..例如
每次出版审核都将始终具有有效的“审核类型”
每份出版物将始终具有“频率ID”,“出版物类型”和“会员”
这在我的解释中是否正确?查找表总是会有匹配的记录吗?如果是这样,那些......如果可选哪些......
根据您的答案,尝试以下查询...如果您了解自己的数据,关系并且可以自行优化STRAIGHT_JOIN,那么STRAIGHT_JOIN可以很强大,这是驱动系统其余部分的关键表...
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
答案 1 :(得分:0)
对MySQL进行排序和获取该数量的记录应该不是问题。 从我的打印屏幕中可以看出,并非所有表都有索引。为JOIN或WHERE语句涉及的每个字段创建索引。整体表现应该会增加。
答案 2 :(得分:0)
如果您想避开文件排序,请将ORDER BY中的字段放在您可以使用的索引中。
在你的情况下,a.audit_anps在索引中不可用,因此filesort会启动。在(pubid,audit_anps)上放一个索引,它就能完成你想做的事。
但是在添加索引时要小心;每次写入时都必须更新每个索引,因此简单地抛出索引可能会导致漂亮的EXPLAIN,但可怕的应用程序性能。请记住:EXPLAIN只告诉您优化器对您的查询以及给定的一组表和索引执行的操作。它可以帮助您实现良好的应用程序性能,但一个漂亮的EXPLAIN不是您的真正目标。
答案 3 :(得分:0)
要获取audit_parentpublication@display
的值,它会针对每一行auau7859_aba.public_publications
进行查询。
尝试将主要查询修改为外部联接auau7859_aba.public_publications
。