MySQL IN语句挂起

时间:2013-05-31 17:21:03

标签: mysql

我正在尝试完成的是在给定过滤器中的表中返回所有重复项的SELECT语句。

我正在尝试运行以下查询,但每次运行它时,我的服务器都会锁定并且它永远不会完成查询。我不知道造成这种情况的原因是什么,如果解决这个问题或以另一种方式实现我的目标,将会非常感谢一些帮助。

编辑:我已根据要求添加了EXPLAIN数据! 编辑2:我已按要求添加了CREATE语句!

SELECT * 
FROM red_flags 
WHERE customer_number IN (SELECT customer_number 
                          FROM red_flags 
                          GROUP BY customer_number 
                          HAVING COUNT(customer_number) > 1);

执行计划:

1   PRIMARY leads   ALL                 80708   Using where
1   PRIMARY customers   eq_ref  PRIMARY PRIMARY 9   apcard_main.leads.customer_number   1   
2   DEPENDENT SUBQUERY  leads   ALL                 80708   Using where; Using temporary; Using filesort
2   DEPENDENT SUBQUERY  customers   eq_ref  PRIMARY PRIMARY 9   apcard_main.leads.customer_number   1   Using index

DDL:

customers

CREATE TABLE `customers` (
  `customer_number` varchar(7) NOT NULL,
  `dealer_id` varchar(32) DEFAULT NULL,
  `first_name` varchar(64) DEFAULT NULL,
  `middle_name` varchar(64) DEFAULT NULL,
  `last_name` varchar(64) DEFAULT NULL,
  `address_one` varchar(128) DEFAULT NULL,
  `address_two` varchar(128) DEFAULT NULL,
  `city` varchar(128) DEFAULT NULL,
  `state` varchar(32) DEFAULT NULL,
  `zip` char(5) DEFAULT NULL,
  `fico` char(3) DEFAULT NULL,
  `phone` varchar(13) DEFAULT NULL,
  `mail_type` varchar(32) DEFAULT NULL,
  `mail_date` date DEFAULT NULL,
  `store` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`customer_number`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1$$

leads

CREATE TABLE `leads` (
  `lead_id` int(11) NOT NULL AUTO_INCREMENT,
  `dealer_id` varchar(32) DEFAULT NULL,
  `customer_number` varchar(7) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `time` time DEFAULT NULL,
  `source` varchar(32) DEFAULT NULL,
  `source_type` varchar(32) DEFAULT NULL,
  `home_phone` varchar(13) DEFAULT NULL,
  `email` varchar(64) DEFAULT NULL,
  `mail_type` varchar(8) DEFAULT NULL,
  `store` varchar(16) DEFAULT NULL,
  `first_name` varchar(64) DEFAULT NULL,
  `last_name` varchar(64) DEFAULT NULL,
  `middle_name` varchar(32) DEFAULT NULL,
  `city` varchar(128) DEFAULT NULL,
  `state` varchar(32) DEFAULT NULL,
  `zip` char(5) DEFAULT NULL,
  `fico` char(3) DEFAULT NULL,
  `mail_date` date DEFAULT NULL,
  `work_phone` varchar(13) DEFAULT NULL,
  `cell_phone` varchar(13) DEFAULT NULL,
  `address_one` varchar(128) DEFAULT NULL,
  `address_two` varchar(128) DEFAULT NULL,
  `comment` varchar(255) DEFAULT NULL,
  `caller_id` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`lead_id`)
) ENGINE=MyISAM AUTO_INCREMENT=125587 DEFAULT CHARSET=latin1$$

查看red_flags

CREATE 
    ALGORITHM=UNDEFINED 
    DEFINER=`apcard`@`97.83.30.118` 
    SQL SECURITY DEFINER 
VIEW `red_flags` AS 
select 
    `leads`.`dealer_id` AS `dealer_id`,
    `customers`.`phone` AS `phone`,
    `leads`.`date` AS `date`,
    `leads`.`time` AS `time`,
    `leads`.`source` AS `source`,
    `leads`.`customer_number` AS `customer_number`,
    `leads`.`caller_id` AS `caller_id`,
    `leads`.`mail_type` AS `mail_type`,
    `leads`.`store` AS `store`,
    `leads`.`last_name` AS `last_name`,
    `leads`.`first_name` AS `first_name`,
    `leads`.`city` AS `city`,
    `leads`.`state` AS `state`,
    `leads`.`zip` AS `zip`,
    `leads`.`fico` AS `fico`,
    `leads`.`mail_date` AS `mail_date`,
    `leads`.`home_phone` AS `home_phone`,
    `leads`.`email` AS `email` 
from (`customers` join `leads`) 
where ((`customers`.`customer_number` = `leads`.`customer_number`) 
  and (`leads`.`date` >= (now() - interval 30 day)))$$

2 个答案:

答案 0 :(得分:0)

您是否尝试过加入而不是使用子查询?

SELECT rf.*
FROM red_flags rf
inner join red_flags rf2
on (rf2.customer_number = rf.customer_number)
group by rf2.customer_number
having count(rf2.customer_number) >1

答案 1 :(得分:0)

我想通了,我有一个格式不正确的日期过滤器,因为某些原因在查询时导致它崩溃。

leads.date >= now() - INTERVAL 30 DAY