I created a table and index as described in this SE post, but when I query the table for a particular ID, no matches are found. When I disable the index, matches are found.
Commands ran:
CREATE TABLE mytable (id1 int, id2 int, score float) ENGINE=MyISAM;
LOAD DATA INFILE '50-billion-records.txt' INTO mytable (id1, id2, score);
ALTER TABLE mytable ADD INDEX id1_index (id1);
SELECT COUNT(*) FROM mytable; # returns 50 billion
SELECT COUNT(DISTINCT id1) FROM mytable; # returns 50K, should be about 50M
SELECT COUNT(*) FROM mytable WHERE id1 = 49302; # returns 0 results
ALTER TABLE mytable DISABLE KEYS;
SELECT * FROM mytable WHERE id1 = 49302 LIMIT 1; # returns 1 row
Is this a bug with MySQL, or does this behavior make sense for some reason?
Note: When I ran ALTER TABLE mytable ENABLE KEYS;
just now, the command is acting like it is building an index for the first time (it's still running after 30 minutes, and memory usage is at 80 GB, which matches my setting of myisam_sort_buffer_size=80G
. I'll reply when this command finishes running (the original ALTER .. ADD INDEX..
took 7.5 hours, so it may be a bit).
Update: Running SHOW PROCESSLIST
indicates "Repair with keycache" is taking place with my ENABLE KEYS
command.
Update 2: I killed the repair job on the original index because after several hours, the memory and IO seemed pretty constant, and I hoped if I started over, it may just work. So in second pass, I rebuilt the table and index, and after doing so, the exact same result occurs.
As requested, here is explain
for count queries with index enabled:
mysql> explain select * from mytable where id1 = 49302;
+----+-------------+-----------+------+---------------+-----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+-----------+---------+-------+------+-------------+
| 1 | SIMPLE | mytable | ref | id1_index | id1_index | 5 | const | 1 | Using where |
+----+-------------+-----------+------+---------------+-----------+---------+-------+------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT COUNT(DISTINCT id1) FROM mytable;
+----+-------------+-----------+-------+---------------+-----------+---------+------+-----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+-----------+---------+------+-----------+--------------------------+
| 1 | SIMPLE | mytable | range | NULL | id1_index | 5 | NULL | 170331743 | Using index for group-by |
+----+-------------+-----------+-------+---------------+-----------+---------+------+-----------+--------------------------+
1 row in set (0.01 sec)
Here is explains after disabling indexes (Note: 25 billion is correct number of records in table, not 50 billion as mentioned above):
mysql> explain select * from mytable where id1 = 66047071;
+----+-------------+-----------+------+---------------+------+---------+------+-------------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+-------------+-------------+
| 1 | SIMPLE | mytable | ALL | NULL | NULL | NULL | NULL | 25890424835 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+-------------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT COUNT(DISTINCT id1) FROM mytable;
+----+-------------+-----------+------+---------------+------+---------+------+-------------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+-------------+-------+
| 1 | SIMPLE | mytable | ALL | NULL | NULL | NULL | NULL | 25890424835 | |
+----+-------------+-----------+------+---------------+------+---------+------+-------------+-------+
1 row in set (0.00 sec)
Update 3: Still hoping to solve this oddity. Is there something I can do with myisamchk
that might fix this? Since I completely repopulated the table and rebuilt the index (i.e. starting from scratch) and got the same result, I assume this was not just some freak occurrence, and that it is due to some internal limit I'm unaware of. On a side note, I've tried switching to Postgres for this dataset, but running into some other unrelated issues (that I'll leave to a different question), so fixing this index is still a top priority for me. Thanks!!
Update 4: Running CHECK TABLE
currently. Will post back with updates as I have them
EXPLAIN
for those SELECT COUNT qrys in question, with both indexes enabled and also disabled?COUNT(DISTINCT..))
query iterates over 170mil rows. If only 170mil rows are in index, that'd explain some of these results.