2017-03-13 9 views
1

mysqlサーバで予期しない結果が発生しました。mysqlの時間が実際の行と同じではない

より多くの行、クエリ時間less ??

私は1つのテーブルがあり、各フィルタの合計行について:

select count(*) from tcr where eid=648; 
+----------+ 
| count(*) | 
+----------+ 
| 11336 | 
select count(*) from tcr where eid=997; 
+----------+ 
| count(*) | 
+----------+ 
| 1262307 | 

が、クエリ時間各フィルタの合計行にoppisiteです:

select * from tcr where eid=648 order by start_time desc limit 0,10; 
[data display] 
10 rows in set (16.92 sec) 

select * from tcr where eid=997 order by start_time desc limit 0,10; 
[data display] 
10 rows in set (0.21 sec) 

「クエリキャッシュをリセット」すべてのクエリSQLの前に実行されています。その結果を説明する添付: テーブルTCRのインデックスはBTW

KEY `cridx_eid` (`eid`), 
KEY `cridx_start_time` (`start_time`) 

である。これは非常に奇妙ですが、それはより多くの我々が取るreusltのように見える(EID = 997は、EID = 648

未満のラインを持っています
explain select * from talk_call_record where eid=648 order by start_time desc limit 0,10; 
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+ 

| id | select_type | table | type | possible_keys | key | key_len | ref | rows |余分| + ---- + ------------- + --- --------------- + ------- + --------------- + ---------- -------- + --------- + ------ + ------ + ------------- + | 1 | SIMPLE | talk_call_record |索引| cridx_eid | cridx_start_time | 5 | NULL | 3672 | Usiどこに|

explain select * from talk_call_record where eid=997 order by start_time desc limit 0,10; 
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+ 

| id | select_type | table   | type | possible_keys | key    | key_len | ref | rows | Extra  | 
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+ 
| 1 | SIMPLE  | talk_call_record | index | cridx_eid  | cridx_start_time | 5  | NULL | 32 | Using where | 

答えて

0

まず、非常に大きなテーブルが必要です。

MySQLがクエリのstart_timeにインデックスを使用しています。起こっているのは、一度に1行ずつテーブルを「歩いている」ということです。それはeid=648よりもはるかに迅速にeid=997を見つけることが起こります。 10個のレコードしか見つけられないので、エンジンは10番目になると停止します。

あなたは何ができますか?クエリの最適なインデックスは、複合インデックス((eid, start_time))です。これは、必要な値に直接移動します。

関連する問題