简体   繁体   English

MySQL上的SQL查询优化

[英]sql query optimisation on MySQL

I have the below SQL query and want to know if there is a way to optimise the query. 我有下面的SQL查询,想知道是否有一种优化查询的方法。

SELECT * FROM LogEntry WHERE UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(updateTime) > 10;

Will adding index on updateTime improve the performance on something more can be done to improve the performance 将在updateTime上添加索引可以提高性能,而可以做更多的事情来提高性能

Yes. 是。 You need to think of a way to remove the function on the column. 您需要考虑一种删除列上函数的方法。 So if I have the logic right: 因此,如果我有正确的逻辑:

SELECT le.*
FROM LogEntry le
WHERE le.updateTime < NOW() - interval 10 second;

This can then take advantage of an index on LogEntry(updateTime) . 然后,可以利用LogEntry(updateTime)上的索引。

There are good reasons to explicitly list the columns being returned. 有充分的理由明确列出要返回的列。 This only has a small effect on performance -- unless the row size is quite big. 这只会对性能产生很小的影响-除非行大小很大。

我要做的第一件事是删除您的select *并选择特定的列

In addition to the optimisation of the request, you may think about partitioning your table. 除了优化请求之外,您还可以考虑对表进行分区。 For instance, you could partition the table LogEntry on updateTime by day. 例如,您可以按日在updateTime上对表LogEntry进行分区。 It would speed your request drastically if you have a lot of data in your table. 如果表中有很多数据,它将大大加快您的请求速度。

Note: to partition your table on updateTime, updateTime must be part of the primary key. 注意:要在updateTime上对表进行分区,updateTime必须是主键的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM