Mysql Where子句减慢了查询速度

Mysql Where子句减慢了查询速度,mysql,database,Mysql,Database,下面的查询搜索客户以前所做的选择,并获得正确的结果。无论如何,查询应该尽可能快。在测试时,我意识到where子句会减慢查询速度。我们能解决这个问题吗 SELECT customerselections.customer_id, customerselections.selectedcompany_id, companycampaigns.*, companies.company_logo FROM customerselections INNER JOIN companycampaigns

下面的查询搜索客户以前所做的选择,并获得正确的结果。无论如何,查询应该尽可能快。在测试时,我意识到where子句会减慢查询速度。我们能解决这个问题吗

SELECT 
customerselections.customer_id,
customerselections.selectedcompany_id,
companycampaigns.*,
companies.company_logo
FROM  customerselections

INNER JOIN companycampaigns ON companycampaigns.company_id=customerselections.selectedcompany_id  
INNER JOIN companies ON companies.company_id=customerselections.selectedcompany_id

WHERE customerselections.customer_id='$customerid' LIMIT $offset,$limit
更新:解释结果

客户选择:

id  select_type table   type    possible_keys   key key_len ref rows      Extra
 1  SIMPLE  customerselections  ALL NULL    NULL    NULL    NULL    12799999
公司卡

 id select_type table   type    possible_keys   key key_len ref rows    Extra
 1  SIMPLE  companycampaigns    ALL NULL    NULL    NULL    NULL    2000000
公司

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  companies   ALL NULL    NULL    NULL    NULL    2039500      
查询解释

|id|select_type | table   | type   | possible_keys        |           key |   key_len | ref  | rows | Extra |

|1|SIMPLE | customerselections | ref | selectedcompany_id,customer_id | customer_id | 4   | const  |  2   9 |  |

|1|SIMPLE| companycampaigns  | ref  | company_id   | company_id  | 4  | viptrio.customerselections.selectedcompany_id | 1 |  |

|1|SIMPLE| companies  | eq_ref | PRIMARY  | PRIMARY  | 4 | viptrio.customerselections.selectedcompany_id |1 |  |
更新

  CREATE TABLE IF NOT EXISTS `companies` (
  `company_id` int(11) NOT NULL auto_increment,
  `company_customerid` int(11) default NULL,
  `company_name` tinytext NOT NULL,
  `company_description` tinytext,
  `company_email` tinytext NOT NULL,
  `company_website` tinytext,
   `company_gsm` tinytext,
  `company_landline` tinytext,
  `company_fax` tinytext,
  `company_address` tinytext,
  `company_contactperson` tinytext,
  `company_businessid` smallint(11) NOT NULL,
  `company_cityid` smallint(5) NOT NULL,
  `company_countrycode` char(3) NOT NULL,
  `company_refnum` tinytext,
  `company_regdate` tinytext NOT NULL,
  `company_logo` tinytext,
  `company_keyword` tinytext,
  PRIMARY KEY  (`company_id`),
  KEY `company_cityid` (`company_cityid`),
  KEY `company_countrycode` (`company_countrycode`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2039501 ;



CREATE TABLE IF NOT EXISTS `companycampaigns` (
`campaign_id` int(11) NOT NULL auto_increment,
`company_id` int(11) NOT NULL,
`campaign_title` varchar(40) NOT NULL,
`campaign_detail` mediumtext NOT NULL,
`campaign_startdate` tinytext,
`campaign_enddate` tinytext,
`published` tinyint(1) NOT NULL default '0',
PRIMARY KEY  (`campaign_id`),
KEY `company_id` (`company_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2000001 ;



  CREATE TABLE IF NOT EXISTS `customerselections` (
  `selection_id` int(11) NOT NULL auto_increment,
  `customer_id` int(11) NOT NULL,
 `selectedcompany_id` int(11) NOT NULL,
  PRIMARY KEY  (`selection_id`),
  KEY `selectedcompany_id` (`selectedcompany_id`),
  KEY `customer_id` (`customer_id`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12800006 ;
你能试试下面吗

从中选择*

从customerselections中选择*,其中customer_id='$customerid'作为cs 内部加入公司Campaigns cc ON cc.company\u id=cs.selectedcompany\u id 在c.company\u id=cs.selectedcompany\u id上内部加入公司c 限制$offset,$LIMIT

尝试以下操作:

选择“$customerid”,抄送。*,c徽标 从…起 康帕金斯公司 c.company\u id=cc.company\u id上的内部联接公司c 其中c.company\u id在selectedcompany\u id FROM customers selected FROM customer\u id='$customerid'
限制$offset,$LIMIT

尝试使用客户id索引:

SELECT t1.customer_id, t1.selectedcompany_id,
       cc.*, c.company_logo
FROM   (SELECT customer_id, selectedcompany_id,
        FROM   customerselections 
        WHERE customer_id='$customerid' ) AS t1
INNER JOIN companycampaigns cc ON cc.company_id = t1.selectedcompany_id  
INNER JOIN companies c ON c.company_id = t1.selectedcompany_id
LIMIT $offset, $limit
我更喜欢使用表别名使查询更具可读性。

在我看来类似于“customerselections”。“customer\u id”是一个int,但您正在尝试执行以下操作:

其中customerselections.customer_id='$customerid'

将其与字符串进行比较。您是否尝试过:


WHERE customerselections.customer\u id=$customerid.

即使没有WHERE子句,速度也很慢吗?@AshReva:快多了。如果使用where子句,则大约需要0,30,如果不使用where子句,查询时间约为0,15请尝试在customerselections内部加入公司Campaigns ON customerselections的第一个ON子句中添加where子句。customer\u id=“$customerid”和companycampaigns.company\u id=customerselections.selectedcompany\u id内部加入公司ON Companys.company\u id=CustomerSelectedCompanys\u id限制$offset,$limit希望这有帮助..您真的需要所有表中的所有列吗?客户id上有索引吗?你不是把延迟加载和性能混淆了吗?最后:,你能给我们看看执行计划吗?@Lieven Keersmaekers:我不需要所有列。我编辑了我的问题。请参阅更新。我在必要的列上有必要的索引。这会导致语法错误。使用near“FROM customer_id='14335'作为cs内部加入公司客户在第2行的cc.company_id'上抄送的语法比OP的查询慢。qshola:此查询需要几秒钟,速度非常慢。它抛出语法错误:使用near的语法“FROM customers selection”,其中customer_id='2'抱歉,有一个额外的右括号。修好了。你现在能试试吗?试着修一下。应该是次要的。我太懒了,无法创建DDL和测试。是的,客户id为INT。我已经尝试了你的建议。不走运。