LoginSignup
19
18

More than 5 years have passed since last update.

MySQLのSHOW TABLE STATUSに騙された

Posted at

rowsが違うんですが・・・

テストしてて気付きました。
rowsを早く知りたくて show table status を使っていたのですが、実は適当な値だったのです。
膝から崩れ落ちそうになりました。

コンソール

※ 固有名などはマスクしています

[root@hoge batch]# mysql -uroot -proot --database=my_work
mysql> show table status;
+-----------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| Name      | Engine | Version | Row_format | Rows    | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation       | Checksum | Create_options | Comment       |
+-----------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| hogehoge  | InnoDB |      10 | Compact    | 2502081 |          20164 | 50454331392 |               0 |   1566572544 |   7340032 |           NULL | 2014-06-02 03:17:08 | NULL        | NULL       | utf8_general_ci |     NULL |                |               |
+-----------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
mysql> select count(*) from hogehoge;
+----------+
| count(*) |
+----------+
|  4925548 |
+----------+
1 row in set (8.27 sec)

select count(*)の値が正しくて show table status; のrowsは正しくないです。
4925548 - 2502081 = 2423467 の差が出ています。倍近い差です。

ドキュメントを見ました

おじさん、ちょっとがっかりしちゃったな…

  • Rows

行数MyISAM のようないくつかのストレージ エンジンは、正確なカウントを格納します。InnoDB のような別のストレージ エンジンにとっては、この値はおおよその物であり、実際の値とは40から50%くらい異なります。そのような場合は、正確なカウントを得る為に SELECT COUNT(*) を利用してください。

MySQL :: MySQL 5.1 リファレンスマニュアル :: 12.5.4.27 SHOW TABLE STATUS 構文 http://dev.mysql.com/doc/refman/5.1/ja/show-table-status.html

気を付けます。。。

select count(xxx) のxxxはインデクスを張っているフィールドを指定するとちょっとは早いので頑張ります。
※ 正確にはNULLを許容しないフィールドを指定すると早いです

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18