这是我在我的某个网站上经常使用的SELECT查询的一个广泛示例。由于我们的主机加载页面时间很慢,因此我们遇到了非常糟糕的问题,因此我尝试尽我所能优化网站使用的每一段代码。谈到MySQL,我不是专家,所以我希望你们中的一些人可能会有所帮助。这是我想要进一步优化的查询 -
Select ID, Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11, Col12, Col13, Col14, Col15 From table_1
Where Active = '1' And Col2 LIKE '%Cat%' And Col3 <> 'blah' And Col3 <> 'blah1' And Col3 <> 'blah2' And Col3 <> 'blah3' And Col3 <> 'blah4' And Col3 <> 'blah5' And Col3 <> 'blah6'
And ID Not In (Select t2ID From table_2 Where table_2.t2ID = table_1.ID And table_2.Col1 = '1' And table_2.Col2 = '1')
And ID Not In (Select t3ID From table_3 Where table_3.t3ID = table_1.ID And table_3.Col1 = '1')
And ID Not In (Select t4ID From table_4 Where table_4.t4ID = table_1.ID And table_4.Col1 = '1')
基本上它会检查1个表(table_1)并拉出与table_2,table_3和table_4中找不到的所有匹配的行。我确信除了多个子选择之外,还有一种更有效的方法。任何帮助是极大的赞赏!在此先感谢:)
更新:基本上我很想知道的是,是否有比查询中多个子选择更快的东西?我确信有一些方法可以从一个表中获取结果,这些表在多个其他表中不存在,这比执行子选择更有效...表之间的唯一共性是table_1中的ID,与其他3个表中的每个表中的另一列相同(这是我目前正在使用子选择对其进行检查)。不幸的是,我只是想弄清楚这个查询更有效的方法是什么......感谢大家到目前为止的输入!
表格布局
mysql> show create table campaigns\G
*************************** 1. row ***************************
Table: campaigns
Create Table: CREATE TABLE `campaigns` (
`ID` int(11) NOT NULL auto_increment,
`CreatedOn` datetime NOT NULL,
`AddedBy` varchar(75) default NULL,
`pCampaignName` varchar(255) default NULL,
`CampaignName` varchar(255) default NULL,
`CampaignValue` decimal(65,2) default '0.00',
`CampaignPayout` decimal(65,2) NOT NULL default '0.00',
`CampaignT` double NOT NULL default '0',
`CampaignSD` double NOT NULL default '0',
`ReportingTime` varchar(255) default NULL,
`CampaignExpiration` varchar(100) default NULL,
`DurationType` varchar(100) default NULL,
`Countries` varchar(100) default NULL,
`CampaignDescription` longtext,
`CampaignRequirements` longtext,
`CampaignType` varchar(50) default NULL,
`CampaignID` varchar(255) default NULL,
`BannerImageWidth` int(10) NOT NULL default '0',
`BannerImageHeight` int(10) NOT NULL default '0',
`BannerImageURL` varchar(255) default NULL,
`BannerImageAlternateText` varchar(255) default NULL,
`DisplayBanner` int(1) NOT NULL default '0',
`CampaignCode` longtext,
`CampaignURL` longtext,
`CampaignActive` int(1) NOT NULL default '0',
`Status` varchar(255) default NULL,
`Affiliate` varchar(255) default NULL,
`NewOfferEmailSent` int(1) unsigned NOT NULL default '0',
`NumberApproved` double(65,2) NOT NULL default '0.00',
`NumberLeads` double NOT NULL default '0',
`ThumbsUp` double NOT NULL default '0',
`ThumbsDown` double NOT NULL default '0',
`CampaignPoints` double NOT NULL default '0',
`UserRatingUp` double NOT NULL default '0',
`UserRatingDown` double NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1608 DEFAULT CHARSET=utf8
1 row in set (0.09 sec)
mysql> show indexes from campaigns\G
*************************** 1. row ***************************
Table: campaigns
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: ID
Collation: A
Cardinality: 1596
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.10 sec)
mysql>
mysql> show create table acampaigns\G
*************************** 1. row ***************************
Table: acampaigns
Create Table: CREATE TABLE `acampaigns` (
`ID` int(11) NOT NULL auto_increment,
`CreatedOn` datetime NOT NULL,
`CampaignName` varchar(255) default NULL,
`CampaignRequirements` longtext,
`CampaignURL` longtext,
`CampaignValue` decimal(65,2) NOT NULL,
`CampaignPayout` decimal(65,2) NOT NULL,
`CampaignReferralCommissionTier1` decimal(65,2) NOT NULL default '0.20',
`CampaignReferralCommissionTier2` decimal(65,2) NOT NULL default '0.10',
`CampaignT` double NOT NULL default '0',
`CampaignSD` double NOT NULL default '0',
`CampaignType` varchar(255) default NULL,
`CampaignID` varchar(100) default NULL,
`CampaignExpiration` varchar(100) default NULL,
`CampaignReturnStatus` varchar(100) default NULL,
`CampaignStatus` varchar(255) default NULL,
`pCampaignID` int(11) NOT NULL,
`pCampaignName` varchar(255) default NULL,
`pUserID` int(11) NOT NULL,
`pUsername` varchar(75) default NULL,
`pUserIPAddress` varchar(30) default NULL,
`ApprovedOn` datetime NOT NULL,
`MarkedDone` int(1) NOT NULL default '0',
`Notes` longtext,
`PaidOn` datetime default NULL,
`cBonus` decimal(65,2) NOT NULL default '0.00',
`ReversedReason` varchar(255) default NULL,
`CampaignPoints` double NOT NULL default '0',
`Affiliate` varchar(255) default NULL,
`RC1Paid` int(1) unsigned NOT NULL default '0',
`RC2Paid` int(1) unsigned NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=10996 DEFAULT CHARSET=utf8
1 row in set (0.44 sec)
mysql> show indexes from acampaigns\G
*************************** 1. row ***************************
Table: acampaigns
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: ID
Collation: A
Cardinality: 8936
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.09 sec)
mysql>
mysql> show create table bcampaigns\G
*************************** 1. row ***************************
Table: bcampaigns
Create Table: CREATE TABLE `bcampaigns` (
`ID` int(11) NOT NULL auto_increment,
`CreatedOn` datetime NOT NULL,
`pCampaignID` int(11) NOT NULL,
`ReportedByUserID` int(11) NOT NULL,
`Status` varchar(255) default NULL,
`Notes` longtext,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=375 DEFAULT CHARSET=utf8
1 row in set (0.08 sec)
mysql> show indexes from bcampaigns\G
*************************** 1. row ***************************
Table: bcampaigns
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: ID
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.08 sec)
mysql>
mysql> show create table icampaigns\G
*************************** 1. row ***************************
Table: icampaigns
Create Table: CREATE TABLE `icampaigns` (
`ID` int(11) NOT NULL auto_increment,
`CreatedOn` datetime NOT NULL,
`pCampaignID` int(11) default NULL,
`IgnoredByUserID` int(11) default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=567 DEFAULT CHARSET=utf8
1 row in set (0.09 sec)
mysql> show indexes from icampaigns\G
*************************** 1. row ***************************
Table: icampaigns
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: ID
Collation: A
Cardinality: 532
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.40 sec)
mysql>
mysql> explain Select ID, CreatedOn, pCampaignName,
CampaignName, CampaignRequirements, CampaignURL, Countries,
CampaignPayout, CampaignPoints, CampaignT, CampaignSD, CampaignType,
ReportingTime, NumberApproved, NumberLeads
From campaigns
-> Where CampaignActive = '1' And CampaignType LIKE 'Cat%'
And CampaignType <> 'DS' And CampaignType <> 'CC' And CampaignType <> 'PC'
And CampaignType <> 'PC2' And CampaignType <> 'GCC' And CampaignType <> 'G'
And CampaignType <> 'R'
-> And ID Not In (Select pCampaignID From acampaigns
Where campaigns.ID = acampaigns.pCampaignID And MarkedDone = '1' And campaigns.pUserID = '1')
-> And ID Not In (Select pCampaignID From bcampaigns
Where bcampaigns.pCampaignID = campaigns.ID And bcampaigns.ReportedByUserID = '1')
-> And ID Not In (Select pCampaignID From icampaigns
Where icampaigns.pCampaignID = campaigns.ID And icampaigns.IgnoredByUserID = '1')
答案 0 :(得分:0)
您是否尝试过查询查询。如果您不熟悉EXPLAIN,它将生成一个报告,显示如何执行查询,尤其是使用哪些索引,并且可能显示索引在哪里可取但不存在。
我过去也发现NOT IN有时会非常缓慢。也许重写NOT IN作为NOT EXISTS可能会加快速度。
NOT EXISTS (SELECT *
FROM table_3
WHERE table_3.t3ID = table_1.ID AND
table_3.Col1 = '1')
答案 1 :(得分:0)
IDFMA
请发布以下脚本的结果,以便我们详细分析您的查询。如果你也可以将结果粘贴到http://pastie.org/说{{3}},那也很棒。
感谢
show create table table_1\G
show indexes from table_1\G
show create table table_2\G
show indexes from table_2\G
show create table table_3\G
show indexes from table_3\G
show create table table_4\G
show indexes from table_4\G
explain
Select
ID, Col1, Col2, Col3, Col4, Col5, Col6, Col7,
Col8, Col9, Col10, Col11, Col12, Col13, Col14, Col15
From
table_1
Where
Active = '1' And
Col2 LIKE '%Cat%' And Col3 <> 'blah' And Col3 <> 'blah1' And Col3 <> 'blah2' And Col3 <> 'blah3' And
Col3 <> 'blah4' And Col3 <> 'blah5' And Col3 <> 'blah6' And
ID Not In (Select t2ID From table_2 Where table_2.t2ID = table_1.ID And table_2.Col1 = '1' And table_2.Col2 = '1') And
ID Not In (Select t3ID From table_3 Where table_3.t3ID = table_1.ID And table_3.Col1 = '1') And
ID Not In (Select t4ID From table_4 Where table_4.t4ID = table_1.ID And table_4.Col1 = '1');
答案 2 :(得分:0)
您可以将此单个查询拆分为多个单独的步骤,并使用临时表将它们放在单个存储过程中。这将使其更具可读性,并且可能运行得更快......