[英]Mysql innodb query returning slow result
I have following query which is taking time. 我有以下查询需要时间。 Mytable type is innodb and have primary key on field tsh_id I have also added index on transaction_Id field
Mytable类型是innodb,并且在字段tsh_id上具有主键,我还在transaction_Id字段上添加了索引
following is implementation inside my database stored procedure. 以下是我的数据库存储过程中的实现。
DECLARE lv_timestamp DATETIME(3);
SET @lv_timestamp = NOW(3);
IF(mycondition) then
SET @lv_Duration :=( SELECT UNIX_TIMESTAMP (@lv_timestamp) - UNIX_TIMESTAMP ( `changedon` )
FROM `MyTable`
WHERE transaction_Id = _transaction_Id
ORDER BY tsh_id DESC
LIMIT 1)
End if;
Please suggest any sort of improvement 请提出任何改进建议
Edit: 编辑:
Explain to query says 解释查询说
"select_type":"SIMPLE",
"table":"MyTable",
"type":"ref",
"possible_keys":"IX_MyTable_Transaction",
"key":"IX_MyTable_Transaction",
"key_len":"98",
"ref":"const",
"rows":1,
"Extra":"Using where"
I am fairly certain your primary key is not a clustered key (its might be null or not unique or you changed it at one point), because that would explain this behaviour, at least if transaction_Id
isn't unique either (and otherwise you wouldn't need limit 1
). 我相当确定您的主键不是集群键(它可能为null或不是唯一的,或者您曾在某一时刻对其进行过更改),因为这将解释这种现象,至少在
transaction_Id
也不唯一的情况下(否则,不需要limit 1
)。
To improve this specific query, you can create the following index: 为了改进此特定查询,您可以创建以下索引:
create index ix_mytable_transaction_id_tsh_id on MyTable (transaction_id, tsh_id desc);
Use explain
again. 再次使用
explain
。
If it doesn't use the new key, force it: 如果它不使用新密钥,请强制执行以下操作:
SELECT ... FROM `MyTable` force index(ix_mytable_transaction_id_tsh_id) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.