English 中文(简体)
两个几乎完全相同的表格之间业绩差距
原标题:Performance disparities between two almost identical tables
  • 时间:2012-05-25 11:43:53
  •  标签:
  • mysql

我有两个相同的表格,只有一个表格有一个时间戳值列,另一个表格有一个日期时间值列。索引相同。数值相同。

但当我运行 SELECT 台站时, MAX( 时间戳) 将每个站点的最大时间标记从 Group 逐个站点放入 ; 如果台站是带有时间戳的台站, 它执行得非常快, 如果我用日期之一尝试它, 那么我还没有看到一个查询执行。 在这两种情况下, 都对 < code> 时间标记 栏进行了索引, 只有类型变化 。

还是约会时间不适合搜索和索引?

下面是 EXPLAIN 提供的 :

+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | Extra                    |
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
|  1 | SIMPLE      | stations | range | NULL          | stamp   | 33      | NULL | 1511 | Using index for group-by |
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+

+----+-------------+--------+-------+---------------+---------+---------+------+---------+-------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows    | Extra |
+----+-------------+--------+-------+---------------+---------+---------+------+---------+-------+
|  1 | SIMPLE      |stations2 | index | NULL          | station | 2       | NULL | 3025467 |       |
+----+-------------+--------+-------+---------------+---------+---------+------+---------+-------+

SHOW :

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stations | CREATE TABLE `stations` (
  `station` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `available` smallint(6) DEFAULT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `stamp` (`station`,`timestamp`),
  KEY `time` (`timestamp`),
  KEY `timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stations2 | CREATE TABLE `stations2` (
  `station` smallint(5) unsigned NOT NULL,
  `available` smallint(5) unsigned DEFAULT NULL,
  `timestamp` datetime DEFAULT NULL,
  KEY `station` (`station`),
  KEY `timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
最佳回答

您可以从解释中看到,没有用于选择的密钥( 可能的密钥为 NULL ) 。 您没有“ 出处” 条款, 因此这是有道理的 。

MySQL 可以用一个索引来确定 MAX, 也可以用一个索引来优化组合 。 但是, 要能够优化组合组合, 您需要同时将您 MAX () 函数中的列和您分组 条款中的列都纳入一个复合索引。 在第一个表格中, 您将这个复合索引作为唯一的密钥, 称为印章 。 EXPLIN 结果显示, MySQL 正在使用该索引 。

在第二个表格上,您没有这个复合索引, 所以 MySQL 需要做更多的工作。 它需要手动对结果进行分组, 并通过手动扫描每行来保持每个站点的 MAX 值。 如果您在第二个表格上添加相同的复合索引, 您将会看到两个站点之间的类似性能 。

然而,时代科技计划仍然会略微优于DATETIME,因为时代科技计划被视为一个单一的4字节整数值,其处理速度比8字节特殊DATETIME值要快。数据集越大,你可以看到的差别就越大。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...