- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在高负载应用程序中收到了请求:
SELECT posts.id as post_id, posts.uid, text, date, like_count,
dislike_count, comments_count, post_likes.liked, image, aspect,
u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number,
u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color,
u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map
FROM posts
LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = '478831'
LEFT OUTER JOIN users u ON posts.uid = u.id
WHERE posts.info = 0 AND
( posts.uid = 478831 OR EXISTS(SELECT friend_id
FROM friends
WHERE user_id = 478831
AND posts.uid = friend_id
AND confirmed = 2)
)
order by posts.id desc limit 0, 20;
执行时间在 6-7 秒之间。 - 绝对糟糕。
EXPLAIN EXTENDED output:
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: posts
type: ref
possible_keys: uid,info
key: info
key_len: 1
ref: const
rows: 471277
filtered: 100.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: post_likes
type: ref
possible_keys: post_id
key: post_id
key_len: 8
ref: anumbers.posts.id,const
rows: 1
filtered: 100.00
Extra:
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: u
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: anumbers.posts.uid
rows: 1
filtered: 100.00
Extra:
*************************** 4. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: friends
type: eq_ref
possible_keys: user_id_2,user_id,friend_id,confirmed
key: user_id_2
key_len: 9
ref: const,anumbers.posts.uid,const
rows: 1
filtered: 100.00
Extra: Using index
4 rows in set, 2 warnings (0.00 sec)
mysql> `show index from posts;`+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| posts | 0 | PRIMARY | 1 | id | A | 1351269 | NULL | NULL | | BTREE | | || posts | 1 | uid | 1 | uid | A | 122842 | NULL | NULL | | BTREE | | || posts | 1 | gps_x | 1 | gps_y | A | 1351269 | NULL | NULL | | BTREE | | || posts | 1 | city_id | 1 | city_id | A | 20 | NULL | NULL | | BTREE | | || posts | 1 | info | 1 | info | A | 20 | NULL | NULL | | BTREE | | || posts | 1 | group_id | 1 | group_id | A | 20 | NULL | NULL | | BTREE | | |+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+mysql> `show index from post_likes;`+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| post_likes | 0 | PRIMARY | 1 | id | A | 10276317 | NULL | NULL | | BTREE | | || post_likes | 1 | post_id | 1 | post_id | A | 3425439 | NULL | NULL | | BTREE | | || post_likes | 1 | post_id | 2 | uid | A | 10276317 | NULL | NULL | | BTREE | | |+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+mysql> `show index from users;`+-------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |+-------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| users | 0 | PRIMARY | 1 | id | A | 497046 | NULL | NULL | | BTREE | | || users | 0 | number | 1 | number | A | 497046 | NULL | NULL | | BTREE | | || users | 1 | name | 1 | name | A | 99409 | NULL | NULL | | BTREE | | || users | 1 | show_phone | 1 | show_phone | A | 8 | NULL | NULL | | BTREE | | || users | 1 | show_mail | 1 | show_mail | A | 12 | NULL | NULL | | BTREE | | || users | 1 | show_on_map | 1 | show_on_map | A | 18 | NULL | NULL | | BTREE | | || users | 1 | show_on_map | 2 | map_activity | A | 497046 | NULL | NULL | | BTREE | | |+-------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+mysql> `show index from friends;`+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| friends | 0 | PRIMARY | 1 | id | A | 1999813 | NULL | NULL | | BTREE | | || friends | 0 | user_id_2 | 1 | user_id | A | 666604 | NULL | NULL | | BTREE | | || friends | 0 | user_id_2 | 2 | friend_id | A | 1999813 | NULL | NULL | | BTREE | | || friends | 0 | user_id_2 | 3 | confirmed | A | 1999813 | NULL | NULL | | BTREE | | || friends | 1 | user_id | 1 | user_id | A | 499953 | NULL | NULL | | BTREE | | || friends | 1 | friend_id | 1 | friend_id | A | 499953 | NULL | NULL | | BTREE | | || friends | 1 | confirmed | 1 | confirmed | A | 17 | NULL | NULL | | BTREE | | |+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
What I've done (tried to do):Set FORCE KEY(uid)
for post table - works faster approximatelly on 2 secs.Of course I've tried rewrite this query and made somthing like this:
SELECT posts.id as post_id, posts.uid, text, date, like_count, dislike_count, comments_count, post_likes.liked, image, aspect,
u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number,
u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color,
u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map
FROM posts
LEFT JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = '478831'
LEFT JOIN users u ON posts.uid = u.id LEFT JOIN friends AS f ON f.friend_id=posts.uid
WHERE posts.info = 0 AND confirmed = 2
group by posts.id, **posts.uid**
order by posts.id desc limit 0, 20;
不幸的是,GROUP BY posts.uid 终止了这个查询的执行,因为返回的帖子中有很多行。
替代的简化查询(如果有帮助的话):
SELECT posts.id as post_id, posts.uid
FROM posts
WHERE posts.info = 0 AND
( posts.uid = 478831 OR EXISTS(SELECT friend_id
FROM friends
WHERE user_id = 478831
AND posts.uid = friend_id
AND confirmed = 2)
)
order by posts.id desc limit 0, 20;
我需要的 - 执行速度小于 1 秒并输出相同结果的查询。任何帮助将不胜感激。提前致谢。
最佳答案
类似于@StanislavL 的解决方案,您可以执行连接而不是子查询:-
SELECT posts.id as post_id, posts.uid, text, date, like_count,
dislike_count, comments_count, post_likes.liked, image, aspect,
u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number,
u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color,
u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map
FROM posts
LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = posts.uid
LEFT OUTER JOIN users u ON posts.uid = u.id
LEFT OUTER JOIN friends f ON f.user_id = 478831 AND posts.uid = f.friend_id AND confirmed = 2
WHERE posts.info = 0
AND (posts.uid = 478831
OR f.friend_id IS NOT NULL)
order by posts.id desc limit 0, 20;
然而,OR 可能会阻止它有效地使用索引。为避免这种情况,您可以联合执行 2 个查询,第一个忽略 friends 表,第二个对 friends 表执行 INNER JOIN:-
SELECT posts.id as post_id, posts.uid, text, date, like_count,
dislike_count, comments_count, post_likes.liked, image, aspect,
u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number,
u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color,
u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map
FROM posts
LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = posts.uid
LEFT OUTER JOIN users u ON posts.uid = u.id
WHERE posts.info = 0
AND posts.uid = 478831
UNION
SELECT posts.id as post_id, posts.uid, text, date, like_count,
dislike_count, comments_count, post_likes.liked, image, aspect,
u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number,
u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color,
u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map
FROM posts
LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = posts.uid
LEFT OUTER JOIN users u ON posts.uid = u.id
INNER JOIN friends f ON f.user_id = 478831 AND posts.uid = f.friend_id AND confirmed = 2
WHERE posts.info = 0
order by posts.id desc limit 0, 20;
未测试
关于MySQL慢查询请求修复,覆盖以提高速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22089147/
自己试试看: import pandas as pd s=pd.Series(xrange(5000000)) %timeit s.loc[[0]] # You need pandas 0.15.1
我最近开始使用 Delphi 中的 DataSnap 来生成 RESTful Web 服务。在遵循 Marco Cantu 本人和互联网上其他几个人的指导后,我成功地使整个“链条”正常工作。 但是有一
我一直在为操作系统类(class)编写以下代码,但结果有些奇怪。该代码创建x线程并同时运行它们,以便将两个平方矩阵相乘。每个线程将输入矩阵的Number_of_rows/Number_of_threa
我正在尝试确定何时使用 parallel包以加快运行某些分析所需的时间。我需要做的一件事是创建矩阵,比较具有不同行数的两个数据框中的变量。我在 StackOverflow 上问了一个关于有效方法的问题
我最近对我的代码进行了一些清理,并在此过程中更改了此内容(不完全是真实的代码): read = act readSTRef test1 term i var = do t v^!terms.
我正在计时查询和同一个查询的执行时间,分页。 foreach (var x in productSource.OrderBy(p => p.AdminDisplayName) .Wher
我正在开发一个项目 (WPF),我有一个 Datagrid 从数据库加载超过 5000 条记录,所以我使用 BackgroundWorker 来通知用户数据正在加载,但它太慢了,我需要等待将近 2分钟
我在查询中添加 ORDER BY 时遇到问题。没有 ORDER BY 查询大约需要 26ms,一旦我添加 ORDER BY,它大约需要 20s。 我尝试了几种不同的方法,但似乎可以减少时间。 尝试 F
我是 Android 开发新手,遇到了性能问题。当我的 GridView 有太多项目时,它会变得有点慢。有什么方法可以让它运行得更快一些吗? 这是我使用的代码: 适配器: public class C
这里的要点是: 1.设置query_cache_type = 0;重置查询缓存; 2.在 heidisql(或任何其他客户端 UI)中运行任何查询 --> 执行,例如 45 毫秒 3.使用以下代码运行
想象下表: CREATE TABLE drops( id BIGSERIAL PRIMARY KEY, loc VARCHAR(5) NOT NULL, tag INT NOT
我的表 test_table 中的示例数据: date symbol value created_time 2010-01-09 symbol1
首先,如果已经有人问过这个问题,我深表歉意,至少我找不到任何东西。 无论如何,我将每 5 分钟运行一次 cron 任务。该脚本加载 79 个外部页面,而每个页面包含大约 200 个我需要在数据库中检查
我有下面的 SQL 代码,它来自 MySQL 数据库。现在它给了我期望的结果,但是查询很慢,我想我应该在进一步之前加快这个查询的速度。 表agentstatusinformation有: PKEY(主
我需要获取一个对象在 Core Data 中数千个其他对象之间的排名。现在,这是我的代码: - (void)rankMethod { //Fetch all objects NSFet
我正在编写一个应用程序,我需要在其中读取用户的地址簿并显示他所有联系人的列表。我正在测试的 iPhone 有大约 100 个联系人,加载联系人确实需要很多时间。 ABAddressBookRef ad
我正在使用 javascript 将 160 行添加到包含 10 列的表格中。如果我这样做: var cellText = document.createTextNode(value); cell.a
我是 Swift 的新手,我已经设置了一个 tableView,它从 JSON 提要中提取数据并将其加载到表中。 表格加载正常,但是当表格中有超过 10 个单元格时,它会变得缓慢且有些滞后,特别是它到
我在 InitializeCulture 和 Page_PreInit 事件之间的 asp.net 页面中遇到性能问题。当我重写 DeterminePostBackMode() 时,我发现问题出在 b
我在 Hetzner 上有一个带有 256GB RAM 6 个 CPU(12 个线程) 的专用服务器,它位于德国。我有 CENTOS 7.5。 EA4。 我的问题是 SSL。每天大约 2 小时,我们在
我是一名优秀的程序员,十分优秀!