mysql - 最近发布帖子的用户返回结果的速度比旧帖子的用户要快得多

标签 mysql sql

user 1 has post ids: 2,5,7,9

user 2 has post ids:600000, 600020, 600025, 600033

我的表格帖子有 600050 行。

这就是问题所在,如果我想选择用户 1 的帖子,则需要 15.+ 秒,如果我想选择用户 2 的帖子,则需要 0,0005 秒。

我注意到最近发布帖子的用户加载速度会更快。

用户1:

select c.nome from posts p 
join cadastro c on p.user=c.id 

where (p.user in (1)) and p.delete='0' 
//p.user in 1 - if I place to 2 it will get results much faster

group by p.id
order by p.id desc limit ?

我希望更快地从两者中获得结果。不仅仅是用户2。 有什么想法吗?

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user` int(11) UNSIGNED NOT NULL,
  `titulo` text,
  `data` datetime NOT NULL,
  `ip` varchar(20),
  `delete` tinyint(1) NOT NULL DEFAULT '0'
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 ;

我尝试为用户添加索引,但没有任何改变。

最佳答案

您没有 user 索引,因此您每次都必须进行表扫描才能查找帖子。这是 B 资本并且会削弱性能。修复它:

CREATE INDEX idx_user ON posts (user, `delete`);

然后您可以使用EXPLAIN进行检查,以确保其正确应用于您的查询。

请注意,尽量避免使用 MySQL reserved keywords例如 DELETE 作为列名称。类似“已删除”的操作也同样有效。

关于mysql - 最近发布帖子的用户返回结果的速度比旧帖子的用户要快得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277460/

相关文章:

php - 使用 php 和 mysql 的 GoogleCharts

sql - 找出 PL/SQL 脚本在等待什么

sql - 将多行的列列为行(postgresQL)

javascript - 无需刷新页面即可提交和获取数据

php - 表中的当年数据按月记录

sql - 初级SQL问题)

sql - 在postgresql中灵活游泳

mysql - MariaDB/PostgreSQL - 查询优化

mysql - 在 Hive 中使用 case 语句填充前面的值

mysql - 获取某个项目的最新条目? mysql