MySQL带有意想不到的结果的奇怪问题

use*_*469 5 mysql

我有一个用于报告的MySQL表,引擎是innoDB,并且有一些分区:/*!50100 PARTITION BY HASH(年(日期)*12 +月(日期))PARTITIONS 48*/

该表有一个日期字段,一个dealId字段,一个placementId字段(以及一些指标列).

我有一个dealId,它出现在2017-09-10:

select count(*) from report_deal_plac where date = "2017-09-10" and dealId = 11983;
+----------+
| count(*) |
+----------+
|       96 |
+----------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

但是当我试图获得这个日期的所有dealId时,我找不到这个dealId:

SELECT DISTINCT(dealId) FROM report_deal_plac WHERE date = "2017-09-10";
+--------+
| dealId |
+--------+
|   1938 |
|   3620 |
|   5892 |
|   6360 |
|   6814 |
|   8928 |
|   9010 |
|   9193 |
|   9282 |
|   9583 |
|   9676 |
|  10129 |
|  10300 |
|  10615 |
|  10858 |
|  11259 |
|  11388 |
|  11563 |
+--------+
18 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我尝试使用group by,以防我遇到明显的问题,但它没有帮助:

SELECT dealId FROM report_deal_plac WHERE date = "2017-09-10" group by dealId;
+--------+
| dealId |
+--------+
|   1938 |
|   3620 |
|   5892 |
|   6360 |
|   6814 |
|   8928 |
|   9010 |
|   9193 |
|   9282 |
|   9583 |
|   9676 |
|  10129 |
|  10300 |
|  10615 |
|  10858 |
|  11259 |
|  11388 |
|  11563 |
+--------+
Run Code Online (Sandbox Code Playgroud)

使用where子句也无济于事:

SELECT DISTINCT(dealId) FROM report_deal_plac WHERE date = "2017-09-10" and dealId = 11983;
Empty set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我做了一个CHECK TABLE,说没关系.我尝试了一个OPTIMIZE表(它确实重新创建了+ analyze,并且得到了一个OK结果).但这没有用.

这个问题可以从哪里来?

这是表定义:

CREATE TABLE `report_deal_plac` (
  `date` date NOT NULL,
  `dealId` int(11) NOT NULL,
  `placementId` int(11) NOT NULL,
  `impressions` int(11) NOT NULL,
  `impressionsKept` int(11) NOT NULL,
  `impressionsResold` int(11) NOT NULL,
  `dollarRevenue` decimal(12,6) NOT NULL DEFAULT '0.000000',
  `revenue` decimal(12,6) NOT NULL DEFAULT '0.000000',
  `netDollarRevenue` decimal(12,6) NOT NULL DEFAULT '0.000000',
  `netRevenue` decimal(12,6) NOT NULL DEFAULT '0.000000',
  `impressionsMeasured` int(11) DEFAULT '0',
  `impressionsViewed` int(11) DEFAULT '0',
  `impressionsSoldMeasured` int(11) DEFAULT '0',
  `impressionsSoldViewed` int(11) DEFAULT '0',
  `clicks` int(11) DEFAULT '0',
  PRIMARY KEY (`date`,`dealId`,`placementId`),
  KEY `dealid` (`dealId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY HASH ( YEAR(date) * 12 + MONTH(date))
PARTITIONS 48 */
Run Code Online (Sandbox Code Playgroud)

这是我的查询的EXPLAIN PARTITIONS

explain partitions select DISTINCT(dealId) FROM report_deal_plac WHERE date = '2017-09-10';
+----+-------------+------------------+------------+-------+----------------+--------+---------+------+------+---------------------------------------+
| id | select_type | table            | partitions | type  | possible_keys  | key    | key_len | ref  | rows | Extra                                 |
+----+-------------+------------------+------------+-------+----------------+--------+---------+------+------+---------------------------------------+
|  1 | SIMPLE      | report_deal_plac | p21        | range | PRIMARY,dealid | dealid | 4       | NULL |  533 | Using where; Using index for group-by |
+----+-------------+------------------+------------+-------+----------------+--------+---------+------+------+---------------------------------------+
Run Code Online (Sandbox Code Playgroud)

更新:对于这个结局:

  • 使用BETWEEN '2017-09-10' AND '2017-09-11'工作(11日没有数据).我不知道为什么.(并BETWEEN '2017-09-10' AND '2017-09-10'没有奏效)

  • 使用聚合函数与GROUP BY工作

  • SET optimizer_switch = 'index_merge_intersection=off'; 什么也没做

  • REPAIR或者ALTER TABLE t1 ENGINE = InnoDB;没有改变任何东西

我得到的所有DealIds都是我在表格中只有一行日期的那些.

我修复了这个问题,方法是创建另一个具有完全相同名称的表并执行INSERT INTO new_table SELECT * FROM old_table,截断旧表,然后重新插入新表.

Jac*_*mar 0

您缺少COUNT每个值的一个。可能有多行dealId = 11983

SELECT dealId, count(*)
FROM report_deal_plac
WHERE date = "2017-09-10"
group by dealId;
Run Code Online (Sandbox Code Playgroud)

您应该会看到您正在寻找的结果