在包含1500000行的MySql表上運行下面的選擇大約需要5分30秒。MySql轉換問題:從UNIX_TIMESTAMP到INT(11)
SELECT * FROM my_table WHERE timestamp BETWEEN UNIX_TIMESTAMP('2008-04-23 01:37:02') AND UNIX_TIMESTAMP('2008-04-23 01:37:03')
[Executed: 25/01/10 5:32:47 EST PM ] [Execution: 231094/ms]
轉換和更換通過UNIX_TIMESTAMP功能在上面的查詢所返回的值將顯着地降低的持續時間:
SELECT UNIX_TIMESTAMP('2008-04-23 01:37:02'), UNIX_TIMESTAMP('2008-04-23 01:37:03')
UNIX_TIMESTAMP('2008-04-23 01:37:02') UNIX_TIMESTAMP('2008-04-23 01:37:03')
---------------------------------------- ----------------------------------------
1208911022 1208911023
SELECT * FROM my_table WHERE timestamp BETWEEN 1208911022 AND 1208911023
[Executed: 25/01/10 5:58:27 EST PM ] [Execution: 11875/ms]
的類型時間戳列是INT(11) 。
我們在這裏不討論索引 - 我不是數據庫的所有者,但我會要求索引該列。
我想問你爲什麼兩個查詢之間的時間差異很大?
似乎從時間戳列每INT(11)值轉換爲通過UNIX_TIMESTAMP返回值的類型!
更新1
MySQL版本:
SELECT VERSION()
5.1.23-rc-log
解釋結果:
EXPLAIN SELECT * FROM my_table WHERE timestamp BETWEEN UNIX_TIMESTAMP('2008-04-23 01:37:02') AND UNIX_TIMESTAMP('2008-04-23 01:37:03')
id select_type table type possible_keys key key_len ref rows Extra
----- -------------- ------------- ------- ---------------- ------ ---------- ------ -------- -----------
1 SIMPLE my_table ALL (null) (null) (null) (null) 15046061 Using where
EXPLAIN SELECT * FROM my_table WHERE timestamp BETWEEN 1208911022 AND 1208911023
id select_type table type possible_keys key key_len ref rows Extra
----- -------------- ------------- ------- ---------------- ------ ---------- ------ -------- -----------
1 SIMPLE my_table ALL (null) (null) (null) (null) 15046061 Using where
UPDATE 2
SELECT * FROM my_table WHERE timestamp >= UNIX_TIMESTAMP('2008-04-23 01:37:02') AND timestamp <= UNIX_TIMESTAMP('2008-04-23 01:37:03')
[Executed: 26/01/10 10:29:52 EST AM ] [Execution: 264172/ms]
EXPLAIN SELECT * FROM my_table WHERE timestamp >= UNIX_TIMESTAMP('2008-04-23 01:37:02') AND timestamp <= UNIX_TIMESTAMP('2008-04-23 01:37:03')
id select_type table type possible_keys key key_len ref rows Extra
----- -------------- ------------- ------- ---------------- ------ ---------- ------ -------- -----------
1 SIMPLE my_table ALL (null) (null) (null) (null) 15046061 Using where
似乎> =和< =未進行任何差別 - 運行時間超過5分鐘!
請將'EXPLAIN SELECT * FROM my_table WHERE時間戳BETWEEN UNIX_TIMESTAMP('2008-04-23 01:37:02')和UNIX_TIMESTAMP('2008-04-23 01:37:03')'的結果和'解釋SELECT * FROM my_table WHERE timestamp BETWEEN 1208911022 AND 1208911023'。這會告訴你正在使用哪些索引。 MySQL *很可能不會*在第一個查詢中使用索引,* * *在第二個查詢中使用索引。 – Asaph 2010-01-25 23:13:35
增加了EXPLAIN計劃和MySQL版本。 – 2010-01-26 15:47:40
感謝您發佈'EXPLAIN'語句的結果。現在我可以看到沒有索引被使用。你可以發佈'SHOW CREATE TABLE my_table'的結果嗎?'所以我們可以看到表上有什麼索引? – Asaph 2010-01-26 18:49:01