gpt4 book ai didi

mysql - 如何提高选择案例的性能?

转载 作者:行者123 更新时间:2023-11-30 23:06:22 26 4
gpt4 key购买 nike

我不知道如何提高这些查询的性能?

我有这样的问题

SELECT some_field, another_field, (CASE  
WHEN HOUR(TIMEDIFF(now(), created_at)) <= 12
THEN (likes_count - (HOUR(TIMEDIFF(now(), created_at)) * 0.25))
WHEN HOUR(TIMEDIFF(now(), created_at)) > 12
AND HOUR(TIMEDIFF(now(), created_at)) <= 48
THEN (likes_count -(HOUR(TIMEDIFF(now(), created_at)) * 0.5))
ELSE (likes_count - (HOUR(TIMEDIFF(now(), created_at)) * 3))
END) as total
FROM some_table
WHERE likes_count >= '30'
ORDER BY total DESC, created_at DESC
LIMIT 10

我的生产服务器花了将近 6 秒,问题是这些查询位于我的主页(索引)
如果不使用 select case 的查询只需要 0.05 秒..
我试图通过使用 memcached 来提高性能,因此应用程序并不总是查询数据库,但是查询数据库时问题仍然存在,6 秒...

有人对如何改进这些查询有建议吗?

这是我的解释查询

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |<br/>
| 1 | SIMPLE | journals | ref | container,indx_journal_status,indx_journal_like_count | indx_journal_status | 32 | const | 22369 | Using where; Using filesort |



这是我的表结构

| Field | Type | Null | Key | Default | Extra |<br/>
+-----------------------------+-----------------------+------+-----+-------------------+----------------+<br/>
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |<br/>
| title | varchar(20) | NO | MUL | Status | |<br/>
| description | bigint(20) | YES | | NULL | |<br/>
| created_at | timestamp | YES | MUL | CURRENT_TIMESTAMP | |<br/>
| likes_count | int(11) | NO | MUL | 0 | |



最佳答案

最后,我能够通过重新启动我的后端应用程序服务器之一来减少查询负载。
看起来有一个查询/进程需要很长时间才能运行。
现在我的查询用了 1.2 秒,但我仍然会尝试提高我的应用/查询性能,以便它能够在 0.5 秒内运行。

关于mysql - 如何提高选择案例的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21698319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com