Mysql 在 600 万行表上的性能

2021-12-21 00:00:00 indexing performance mysql

有一天我怀疑我必须学习 hadoop 并将所有这些数据传输到非结构化数据库,但我惊讶地发现性能在如此短的时间内显着下降.

我有一个不到 600 万行的 mysql 表.我正在对这个表进行一个非常简单的查询,并且相信我拥有所有正确的索引.

查询是

<前>从事件中选择日期、时间 WHERE venid='47975' AND date>='2009-07-11' ORDER BY date

解释返回

<前>id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE updateshows range date_idx date_idx 7 NULL 648997 使用 where

因此,据我所知,我使用了正确的索引,但此查询需要 11 秒才能运行.

数据库是 MyISAM,phpMyAdmin 说表是 1.0GiB.

这里有什么想法吗?

date_idx 是 date 和 venid 列的索引.那应该是两个单独的索引吗?

解决方案

您要确保查询将仅使用索引,因此请确保索引涵盖您选择的所有字段.此外,由于它涉及范围查询,因此您需要首先在索引中使用 venid,因为它是作为常量进行查询的.因此,我会像这样创建和索引:

ALTER TABLE events ADD INDEX indexNameHere (venid, date, time);

有了这个索引,完成查询所需的所有信息都在索引中.这意味着,希望存储引擎能够在不实际查找表本身的情况下获取信息.但是,MyISAM 可能无法执行此操作,因为它不会将数据存储在索引的叶子中,因此您可能无法获得所需的速度提升.如果是这种情况,请尝试创建表的副本,并在副本上使用 InnoDB 引擎.在那里重复相同的步骤,看看速度是否有显着提高.InnoDB 确实将字段值存储在索引叶中,并允许覆盖索引.

现在,希望您在解释查询时会看到以下内容:

mysql>EXPLAIN SELECT date, time FROM events WHERE venid='47975' AND date>='2009-07-11' ORDER BY date;id select_type table type possible_keys key [..] Extra1 SIMPLE 事件范围 date_idx, indexNameHere indexNameHere Using index, Using where

One day I suspect I'll have to learn hadoop and transfer all this data to a non-structured database, but I'm surprised to find the performance degrade so significantly in such a short period of time.

I have a mysql table with just under 6 million rows. I am doing a very simple query on this table, and believe I have all the correct indexes in place.

the query is

SELECT date, time FROM events WHERE venid='47975' AND date>='2009-07-11' ORDER BY date

the explain returns

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  updateshows     range   date_idx    date_idx    7   NULL    648997  Using where

so i am using the correct index as far as I can tell, but this query is taking 11 seconds to run.

The database is MyISAM, and phpMyAdmin says the table is 1.0GiB.

Any ideas here?

Edited: The date_idx is indexes both the date and venid columns. Should those be two seperate indexes?

解决方案

What you want to make sure is that the query will use ONLY the index, so make sure that the index covers all the fields you are selecting. Also, since it is a range query involved, You need to have the venid first in the index, since it is queried as a constant. I would therefore create and index like so:

ALTER TABLE events ADD INDEX indexNameHere (venid, date, time);

With this index, all the information that is needed to complete the query is in the index. This means that, hopefully, the storage engine is able to fetch the information without actually seeking inside the table itself. However, MyISAM might not be able to do this, since it doesn't store the data in the leaves of the indexes, so you might not get the speed increase you desire. If that's the case, try to create a copy of the table, and use the InnoDB engine on the copy. Repeat the same steps there and see if you get a significant speed increase. InnoDB does store the field values in the index leaves, and allow covering indexes.

Now, hopefully you'll see the following when you explain the query:

mysql> EXPLAIN SELECT date, time FROM events WHERE venid='47975' AND date>='2009-07-11' ORDER BY date;

id  select_type table  type  possible_keys        key       [..]  Extra
1   SIMPLE   events range date_idx, indexNameHere indexNameHere   Using index, Using where

相关文章