MySQL SELECT查询的速度在两个相似的表中

时间:2016-12-10 17:50:31

标签: mysql

我有以下两个MySQL表:

CREATE TABLE `english_british` (
  `word_id` int(11) NOT NULL,
  `word` text,
  `word_transcription` text,
  `line_complete` text,
  `pos` text,
  `hunspell_rules` text,
  `frequency_coca` int(8) NOT NULL DEFAULT '0',
  `to_check` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

ALTER TABLE `english_british`
  ADD PRIMARY KEY (`word_id`);

ALTER TABLE `english_british`
  MODIFY `word_id` int(11) NOT NULL AUTO_INCREMENT;

此表有112,711条记录,根据phpMyAdmin的重量为11.7 MiB。

第二张桌几乎相同:

CREATE TABLE `english_american` (
  `word_id` int(11) NOT NULL,
  `word` text,
  `word_transcription_source` text,
  `word_transcription` text,
  `o_to_check` tinyint(4) NOT NULL DEFAULT '0',
  `pos` text,
  `frequency_coca` int(8) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

ALTER TABLE `english_american`
  ADD PRIMARY KEY (`word_id`);

ALTER TABLE `english_american`
  MODIFY `word_id` int(11) NOT NULL AUTO_INCREMENT;

此表有32,338条记录,重量为2.5 MiB。

现在我运行两个类似的查询:

SELECT word FROM english_british WHERE `word`='I\'m'
SELECT word FROM english_american WHERE `word`='I\'m'

第一个查询(对于较大的表)只需0.0446秒即可执行。第二个查询(对于较小的表)需要0.1197秒。确切的时间可能会有所改变,但仍然会慢2.5-3倍。

我知道你要告诉我应该为列“word”创建索引,首先我需要将其类型更改为varchar。我会这样做,但首先我只是想了解为什么在较小的表上运行相同的查询需要这么长时间?

编辑:两个查询只返回一行。

编辑2:两个查询的EXPLAIN EXTENDED完全相同(行数除外)。

编辑3:SHOW PROFILES的结果:

+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| Status               | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file   | Source_line |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| starting             | 0.000081 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | NULL            | NULL          |        NULL |
| checking permissions | 0.000008 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        6043 |
| Opening tables       | 0.000016 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_base.cc   |        4519 |
| After opening tables | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_base.cc   |        4757 |
| System lock          | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | lock.cc       |         308 |
| Table lock           | 0.000007 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | lock.cc       |         313 |
| init                 | 0.000019 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        3427 |
| optimizing           | 0.000015 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        1092 |
| statistics           | 0.000018 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        1373 |
| preparing            | 0.000017 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        1398 |
| executing            | 0.000005 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        2551 |
| Sending data         | 0.121468 | 0.046875 |   0.078125 |              NULL |                NULL |        62672 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        3223 |
| end                  | 0.000017 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        3462 |
| query end            | 0.000010 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        5679 |
| closing tables       | 0.000004 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_base.cc   |         930 |
| Unlocking tables     | 0.000017 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | lock.cc       |         395 |
| freeing items        | 0.000017 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        7331 |
| updating status      | 0.000065 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        1936 |
| cleaning up          | 0.000022 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        1955 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
19 rows in set (0.00 sec)


+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| Status               | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file   | Source_line |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| starting             | 0.000059 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | NULL            | NULL          |        NULL |
| checking permissions | 0.000007 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        6043 |
| Opening tables       | 0.000020 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_base.cc   |        4519 |
| After opening tables | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_base.cc   |        4757 |
| System lock          | 0.000007 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | lock.cc       |         308 |
| Table lock           | 0.000008 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | lock.cc       |         313 |
| init                 | 0.000020 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        3427 |
| optimizing           | 0.000012 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        1092 |
| statistics           | 0.000013 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        1373 |
| preparing            | 0.000014 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        1398 |
| executing            | 0.000004 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        2551 |
| Sending data         | 0.049592 | 0.062500 |   0.000000 |              NULL |                NULL |           43 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        3223 |
| end                  | 0.000017 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_select.cc |        3462 |
| query end            | 0.000005 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        5679 |
| closing tables       | 0.000004 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_base.cc   |         930 |
| Unlocking tables     | 0.000012 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | lock.cc       |         395 |
| freeing items        | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        7331 |
| updating status      | 0.000065 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        1936 |
| cleaning up          | 0.000025 | 0.000000 |   0.000000 |              NULL |                NULL |            0 |             0 |          NULL |              NULL |                 0 |              NULL |  NULL | <unknown>       | sql_parse.cc  |        1955 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
19 rows in set (0.00 sec)

编辑4: 以下查询

SELECT COUNT(DISTINCT word) from english_british
SELECT COUNT(DISTINCT word) from english_american

分别返回111996和32197.

编辑5: 这似乎与缓存的结果无关。即使query_cache_size设置为零,速度也是相同的。

2 个答案:

答案 0 :(得分:0)

你是对的。使用word列的群集索引并将其更改为varchar。这是因为存储text字段所需的空间不仅仅是存储varchar,因此需要更多磁盘 - &gt;记忆读。我不能告诉你一个关于为什么执行时间那么多的确切方法,但我可以建议你查看执行计划(EXPLAIN命令)来自己解决。

答案 1 :(得分:0)

尝试进行计数(*)并查看每个表返回的行数。也许返回的行数减慢了它。