这是一个表Mysql表,在该表上,date ='Some date'的简单sql不起作用
已检查日志。 重新加载表几次并尝试。
select * from TRN_RP_CONSUMPTION_DAILY limit 1;
+------------+------------------------+----------------------+------------------------+----------------+-------------------+-----------------+-------------------+----------------------+--------------------+----------------------+-------------------+-----------------+---------------+--------------+
| TRCD_DATE | TRCD_SPREAD_START_DATE | TRCD_SPREAD_END_DATE | TRCD_SOURCE_CHENNEL_ID | TRCD_CIRCLE_ID | TRCD_CATALOUGE_ID | TRCD_CONTENT_ID | TRCD_SONG_CODE_ID | TRCD_YT_CP_POLICY_ID | TRCD_YT_CHANNEL_ID | TRCD_PRODUCT_NAME_ID | TRCD_TRXN_TYPE_ID | TRCD_TRXN_COUNT | TRCD_TOP_LINE | TRCD_REVENUE |
+------------+------------------------+----------------------+------------------------+----------------+-------------------+-----------------+-------------------+----------------------+--------------------+----------------------+-------------------+-----------------+---------------+--------------+
| 2018-01-01 | 2018-01-01 | 2018-01-04 | 5 | 1 | 1 | 945723 | 1 | 1 | 1 | 211 | 180 | 1.75 | 0 | 0 |
+------------+------------------------+----------------------+------------------------+----------------+-------------------+-----------------+-------------------+----------------------+--------------------+----------------------+-------------------+-----------------+---------------+--------------+
select * from TRN_RP_CONSUMPTION_DAILY limit 1;
CREATE TABLE `TRN_RP_CONSUMPTION_DAILY` (
`TRCD_DATE` date NOT NULL DEFAULT '0000-00-00',
`TRCD_SPREAD_START_DATE` date NOT NULL DEFAULT '0000-00-00',
`TRCD_SPREAD_END_DATE` date NOT NULL DEFAULT '0000-00-00',
`TRCD_SOURCE_CHENNEL_ID` smallint(5) unsigned NOT NULL DEFAULT '1',
`TRCD_CIRCLE_ID` smallint(5) unsigned NOT NULL DEFAULT '1',
`TRCD_CATALOUGE_ID` int(10) unsigned NOT NULL DEFAULT '1',
`TRCD_CONTENT_ID` int(10) unsigned NOT NULL DEFAULT '1',
`TRCD_SONG_CODE_ID` int(10) unsigned NOT NULL DEFAULT '1',
`TRCD_YT_CP_POLICY_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
`TRCD_YT_CHANNEL_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
`TRCD_PRODUCT_NAME_ID` smallint(5) unsigned NOT NULL DEFAULT '1',
`TRCD_TRXN_TYPE_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
`TRCD_TRXN_COUNT` double NOT NULL DEFAULT '0',
`TRCD_TOP_LINE` double NOT NULL DEFAULT '0',
`TRCD_REVENUE` double NOT NULL DEFAULT '0',
KEY `IDX_TRCD_DATE` (`TRCD_DATE`),
KEY `IDX_TRCD_SOURCE_CHENNEL_ID` (`TRCD_SOURCE_CHENNEL_ID`),
KEY `IDX_TRCD_CATALOUGE_ID` (`TRCD_CATALOUGE_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
select count(*) from TRN_RP_CONSUMPTION_DAILY where TRCD_DATE='2018-01-01';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
explain select count(*) from TRN_RP_CONSUMPTION_DAILY where TRCD_DATE='2018-01-01';
+----+-------------+--------------------------+------+---------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------------+------+---------------+---------------+---------+-------+------+-------------+
| 1 | SIMPLE | TRN_RP_CONSUMPTION_DAILY | ref | IDX_TRCD_DATE | IDX_TRCD_DATE | 3 | const | 1 | Using index |
+----+-------------+--------------------------+------+---------------+---------------+---------+-------+------+-------------+
select count(*) from TRN_RP_CONSUMPTION_DAILY FORCE INDEX(IDX_TRCD_DATE) where TRCD_DATE='2018-01-01';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
select count(*) from TRN_RP_CONSUMPTION_DAILY;
+------------+
| count(*) |
+------------+
| 2006275044 |
+------------+
103G = TRN_RP_CONSUMPTION_DAILY.MYD
52G = TRN_RP_CONSUMPTION_DAILY.MYI
select count(*) from TRN_RP_CONSUMPTION_DAILY where date(TRCD_DATE)='2018-01-01';
+----------+
| count(*) |
+----------+
| 1235523 |
+----------+
我知道这是可行的,因为在该索引列上使用函数时不会考虑索引。
服务器版本:5.5.60-38.12-log Percona Server(GPL),版本38.12, 修订版26ef816
clouse在其他列上的索引正常运行的地方。 有人可以帮忙为什么它在该日期列上不起作用? 我想在此表上添加更多索引,但这无法正常工作,所以我在这里停止。
答案 0 :(得分:0)
您是否在where子句中尝试过STR_TO_DATE MySQL函数?
在您的情况下:TRCD_DATE = STR_TO_DATE('2018-01-01','%Y,%m,%d')(或%m和%d取反)
有关更多信息:https://www.w3schools.com/sql/func_mysql_str_to_date.asp
我希望这能回答您的问题/问题。
答案 1 :(得分:0)
您以表格作为日期,但是它是日期还是日期/时间。可能在EXACT上失败了吗?
将where子句更改为
where TRCD_DATE >='2018-01-01' AND TRCD_DATE < '2018-01-02'
因此,从早上12:00(上午)一直到第二天的11:59:59 pm,您将获得任何东西。不要尝试转换数据列,这将阻止使用索引。
答案 2 :(得分:0)
从MyISAM移至InnoDB。
与此同时,其中之一应该起作用。 (在列表中向下移动,直到获得可用的表。大多数选项都很慢,因为它们涉及到复制表。)
CHECK TABLE TRN_RP_CONSUMPTION_DAILY;
报告错误,请执行REPAIR TABLE TRN_RP_CONSUMPTION_DAILY;
。
OPTIMIZE TABLE TRN_RP_CONSUMPTION_DAILY;
REPAIR TABLE TRN_RP_CONSUMPTION_DAILY USE_FRM;
DROP INDEX ...
(针对每个索引),然后是ADD INDEX ...
将表格复制到上方:
CREATE TABLE new LIKE real;
INSERT INTO new SELECT * FROM TRN_RP_CONSUMPTION_DAILY;
RENAME TABLE TRN_RP_CONSUMPTION_DAILY TO old,
new TO TRN_RP_CONSUMPTION_DAILY;
DROP TABLE old;
要从备份还原吗?
这些是MyISAM表有时需要的; InnoDB更强大。