我有一张大约有4M行的表。每天晚上,大约有15个批处理作业在数据上运行,有几十万次插入和更新。问题是,当我运行一个简单的计数查询,如
select count(*) from items;
我必须等待大约15分钟才能返回。在研究了SO之后,我看到了
optimize table items;
似乎确实解决了问题,运行后,上面的查询立即返回。问题是,运行需要17个小时。有关寻找原因的建议,以及如何解决这个问题?
感谢您的帮助, 凯文
更新:
以下是我优化时会发生什么:
mysql> optimize table items;
+------------------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------------+----------+----------+-------------------------------------------------------------------+
| g_production.items | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| g_production.items | optimize | status | OK |
+------------------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (9 hours 20 min 48.36 sec)
另外,奇怪的是,select不使用主索引ID:
explain select count(id) from items;
+----+-------------+-------+-------+---------------+--------------------------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------------------- +---------+------+----------+-------------+
| 1 | SIMPLE | items | index | NULL | index_items_on_real_sale | 2 | NULL | 45152757 | Using index |
+----+-------------+-------+-------+---------------+--------------------------+---------+------+----------+-------------+
1 row in set (0.10 sec)
最后,这里是表格中的所有索引:
+-------+------------+---------------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| items | 0 | PRIMARY | 1 | id | A | 47144790 | NULL | NULL | | BTREE | | |
| items | 1 | index_items_on_affiliate_id | 1 | affiliate_id | A | 47144790 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_brand_id | 1 | brand_id | A | 1024886 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_real_sale | 1 | real_sale | A | 18 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_retailer_id_and_affiliate_id | 1 | retailer_id | A | 18 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_retailer_id_and_affiliate_id | 2 | affiliate_id | A | 47144790 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_retailer_id | 1 | retailer_id | A | 40021 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_shopzilla_id | 1 | shopzilla_id | A | 457716 | NULL | NULL | YES | BTREE | | |
| items | 1 | index_items_on_updated_at | 1 | updated_at | A | 6734970 | NULL | NULL | | BTREE | | |
注意EXPLAIN显示的索引的基数,我有4M行,但解释说它使用了index_items_on_real_sale,show indices命令显示的基数为18.这可能是问题吗? / p>
答案 0 :(得分:0)
这可能是一些事情,但我想知道它是否正确编入索引。另外,尝试使用explain运行查询,如下所示:
data = JSON.parse(localStorage.lastlayout);
win.moveTo(data.left, data.top);
win.resizeTo(data.width, data.height);
查看输出并查看它处理查询的行数和它们的索引类型等...
为了提供帮助,绝对需要更多信息,我只是根据您提供的有限信息进行猜测。